spring-projects/spring-ai · error

Session + sessionRepresentation + was not found on the MCP s

Error message

Session + sessionRepresentation + was not found on the MCP server

What it means

A warning logged by mcpSessionNotFoundError when the MCP server indicates the client's mcp-session-id is unknown. The transport converts this into a McpTransportSessionNotFoundException on the connection flux so subscribers learn the session died and can re-initialize. This is the client-side detection of server-side session loss.

Source

Thrown at mcp/transport/mcp-spring-webflux/src/main/java/org/springframework/ai/mcp/client/webflux/transport/WebClientStreamableHttpTransport.java:442

						}
					}
					// inform the caller of sendMessage
					sink.error(t);
					return true;
				}).doFinally(s -> {
					@Nullable Disposable ref = disposableRef.getAndSet(null);
					if (ref != null) {
						transportSession.removeConnection(ref);
					}
				}).contextWrite(sink.contextView()).subscribe();
			disposableRef.set(connection);
			transportSession.addConnection(connection);
		});
	}

	private static Flux<McpSchema.JSONRPCMessage> mcpSessionNotFoundError(String sessionRepresentation) {
		if (logger.isWarnEnabled()) {
			logger.warn("Session " + sessionRepresentation + " was not found on the MCP server");
		}
		// inform the stream/connection subscriber
		return Flux.error(new McpTransportSessionNotFoundException(sessionRepresentation));
	}

	private Flux<McpSchema.JSONRPCMessage> extractError(ClientResponse response, String sessionRepresentation) {
		return response.<McpSchema.JSONRPCMessage>createError().onErrorResume(e -> {
			WebClientResponseException responseException = (WebClientResponseException) e;
			byte[] body = responseException.getResponseBodyAsByteArray();
			McpSchema.JSONRPCResponse.JSONRPCError jsonRpcError = null;
			Exception toPropagate;
			try {
				McpSchema.JSONRPCResponse jsonRpcResponse = this.jsonMapper.readValue(body,
						McpSchema.JSONRPCResponse.class);
				jsonRpcError = jsonRpcResponse.error();
				toPropagate = jsonRpcError != null ? new McpError(jsonRpcError)
						: new McpTransportException("Can't parse the jsonResponse " + jsonRpcResponse);
			}

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Handle McpTransportSessionNotFoundException in the client by re-running initialize/connect to obtain a new session
  2. Persist session state server-side or enable sticky sessions for multi-instance deployments
  3. Increase server session timeout for long-running workloads
  4. Recreate the client/transport after receiving this error instead of retrying on the dead session

Example fix

// before
client.callTool(request).block(); // dies with McpTransportSessionNotFoundException
// after
client.callTool(request)
    .onErrorResume(McpTransportSessionNotFoundException.class, e ->
        reconnectAndInitialize(client).then(client.callTool(request)))
Defensive patterns

Strategy: retry

Try / catch

try {
    client.callTool(req).block();
} catch (McpTransportSessionNotFoundException e) {
    // session lost: re-initialize and retry once
    client.close();
    McpClient newClient = buildClientAndInitialize();
    newClient.callTool(req).block();
}

Prevention

When it happens

Trigger: Server restarted or restarted sessions storage; session TTL expired server-side; load balancer routed the request to an instance without the session; sending a stale mcp-session-id header after long idle.

Common situations: Kubernetes pod restarts; autoscaling removing the node holding the session; long-lived idle connections exceeding server session timeouts; caching a session id across client restarts.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/e647af51c1ec1e3c. Report an issue: GitHub.