Hmbown/CodeWhale · error
MCP session expired (transport=sse endpoint= status= )
Error message
MCP session expired (transport=sse endpoint={} status={}): {} What it means
The MCP SSE transport POSTed a message and the server returned a non-success status whose body looks like an expired-session response (detected by is_mcp_stale_session_body). The client reports this as a stale/expired MCP session so callers can mark the transport stale and reconnect with a fresh session instead of treating it as a hard failure.
Solutions
- Reconnect the SSE transport to obtain a fresh session (the stale-session detection exists so callers do this).
- Restart the TUI session or toggle the MCP server off/on to force a new endpoint event.
- Check the MCP server's session TTL and logs to confirm the session was dropped server-side.
- If behind a load balancer, enable session affinity (sticky routing).
Example fix
// before: keep sending over the old session
transport.send(msg).await?;
// after: on stale-session error, rebuild the transport
if let Err(e) = transport.send(msg).await {
if e.to_string().contains("session expired") {
transport = SseTransport::connect(&cfg).await?; // fresh session
} else { return Err(e.into()); }
} Defensive patterns
Strategy: retry
Try / catch
// detect stale session and rebuild transport
match transport.send(msg).await {
Err(e) if e.to_string().contains("session expired") => {
transport = SseTransport::connect(&cfg).await?; // fresh session, retry once
transport.send(msg).await?;
}
other => other?,
} Prevention
- Reconnect the SSE transport proactively on reconnect/backoff signals instead of reusing stale sessions.
- Enable load-balancer session affinity for MCP servers with sessions.
- Monitor for server restarts and invalidate client sessions on deploy.
When it happens
Trigger: Sending a JSON-RPC message over an SSE transport whose session id the server no longer recognizes — server replies 404/400/410 with an expired-session-style body after a server restart, session TTL expiry, or reconnect to a different backend.
Common situations: Long-running TUI session outliving the server's session TTL; MCP server restarted or redeployed while the client kept its session endpoint; load balancer routing POSTs to a node that doesn't know the session.
Related errors
- MCP SSE rejected (transport=http url= status= )
- ${compactRuntimeError(response.status, body)}
- ${compactRuntimeError(response.status, body)}
- ${compactRuntimeError(response.status, body)}
- ${compactRuntimeError(response.status, body)}
AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22).
Data as JSON: /api/errors/0bc57f2bc6db5b63.
Report an issue: GitHub.
Appendix: source
Thrown at crates/tui/src/mcp/sse.rs:297
let headers = self.auth.resolved_headers().await?;
let request = apply_safe_custom_headers(
with_default_mcp_http_headers(self.client.post(&endpoint), true),
&headers,
)
.body(msg);
let response = self.client.send(request).await.with_context(|| {
format!(
"MCP SSE POST send failed (transport=sse endpoint={})",
mask_url_secrets(&endpoint)
)
})?;
let status = response.status();
if !status.is_success() {
let body_excerpt = bounded_body_excerpt(response, ERROR_BODY_PREVIEW_BYTES).await;
let stale_session = is_mcp_stale_session_body(&body_excerpt);
let body_excerpt = self.auth.server_error_preview(&body_excerpt);
if stale_session {
anyhow::bail!(
"MCP session expired (transport=sse endpoint={} status={}): {}",
mask_url_secrets(&endpoint),
status,
body_excerpt
);
}
anyhow::bail!(
"MCP SSE POST rejected (transport=sse endpoint={} status={}): {}",
mask_url_secrets(&endpoint),
status,
body_excerpt
);
}
Ok(())
}
async fn recv(&mut self) -> Result<Vec<u8>> {
loop {View on GitHub (pinned to 73e0f67d83)