{"record":{"id":"0cdbc988d0ed1e09","repo":"spring-projects/spring-ai","slug":"server-does-not-recognize-session-invalidsession","errorCode":null,"errorMessage":"Server does not recognize session + invalidSession.sessionId() + . Invalidating.","messagePattern":"Server does not recognize session \\+ invalidSession\\.sessionId\\(\\) \\+ \\. Invalidating\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"mcp/transport/mcp-spring-webflux/src/main/java/org/springframework/ai/mcp/client/webflux/transport/WebClientStreamableHttpTransport.java","lineNumber":204,"sourceCode":"\t\t\t\t\t})\n\t\t\t\t\t.then();\n\t\treturn new DefaultMcpTransportSession(onClose);\n\t}\n\n\t@Override\n\tpublic void setExceptionHandler(Consumer<Throwable> handler) {\n\t\tlogger.debug(\"Exception handler registered\");\n\t\tthis.exceptionHandler.set(handler);\n\t}\n\n\tprivate void handleException(Throwable t) {\n\t\tif (logger.isDebugEnabled()) {\n\t\t\tlogger.debug(\"Handling exception for session \" + sessionIdOrPlaceholder(this.activeSession.get()), t);\n\t\t}\n\t\tif (t instanceof McpTransportSessionNotFoundException) {\n\t\t\tMcpTransportSession<?> invalidSession = this.activeSession.getAndSet(createTransportSession());\n\t\t\tif (logger.isWarnEnabled()) {\n\t\t\t\tlogger.warn(\"Server does not recognize session \" + invalidSession.sessionId() + \". Invalidating.\");\n\t\t\t}\n\t\t\tinvalidSession.close();\n\t\t}\n\t\tConsumer<Throwable> handler = this.exceptionHandler.get();\n\t\tif (handler != null) {\n\t\t\thandler.accept(t);\n\t\t}\n\t}\n\n\t@Override\n\tpublic Mono<Void> closeGracefully() {\n\t\treturn Mono.defer(() -> {\n\t\t\tlogger.debug(\"Graceful close triggered\");\n\t\t\tMcpTransportSession<Disposable> currentSession = this.activeSession\n\t\t\t\t.getAndSet(ClosedMcpTransportSession.INSTANCE);\n\t\t\tif (currentSession != null) {\n\t\t\t\treturn Mono.from(currentSession.closeGracefully());\n\t\t\t}","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/mcp/transport/mcp-spring-webflux/src/main/java/org/springframework/ai/mcp/client/webflux/transport/WebClientStreamableHttpTransport.java#L186-L222","documentation":"A warning logged in WebClientStreamableHttpTransport.handleException when the server responds with McpTransportSessionNotFoundException, meaning the HTTP session id this client is using is no longer valid on the server (expired, restarted server, or load-balanced to a node without session state). The transport invalidates the stale session and immediately creates a fresh one, then re-dispatches through the configured exception handler.","triggerScenarios":"MCP server restarted or evicted the session (mcp-session-id no longer recognized); sticky sessions not configured behind a load balancer; server session timeout shorter than client lifetime; a 404/session-not-found HTTP response mapped to McpTransportSessionNotFoundException during a stream/request.","commonSituations":"Rolling deployments of the MCP server; server-side session TTL expiry during long-running client connections; multi-instance deployments without shared session storage.","solutions":["Ensure the exception handler / retry logic re-issues the failed request after the session is recreated","Configure the MCP server with a longer session TTL or session persistence for long-lived clients","Enable sticky sessions or shared session state when running multiple server instances behind a load balancer","Treat this warning as expected recovery — if requests keep failing, check that reconnect logic resends the initialize handshake where required"],"exampleFix":"// before\ntransport.connect();\nclient.callTool(request); // fails if session was invalidated mid-flight\n// after\ntransport.connect();\nclient.callTool(request)\n    .retryWhen(Retry.backoff(3, Duration.ofMillis(500))\n        .filter(t -> t instanceof McpTransportSessionNotFoundException));","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"client.callTool(req)\n    .retryWhen(reactor.util.retry.Retry.backoff(3, Duration.ofMillis(500))\n        .filter(e -> e instanceof McpTransportSessionNotFoundException))\n    .block();","preventionTips":["Enable sticky sessions or shared session storage behind load balancers","Set server session TTL comfortably above client usage patterns","Re-initialize the client after session invalidation","Monitor for repeated warnings indicating server instability"],"tags":["mcp","session","http","recovery"],"backgroundTag":"session-not-found","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}