spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

MCPServer Redeployment causes this behavior "Error sending message: 404" in MCPClient

Open dilipsundarraj1 opened this issue 9 months ago • 2 comments

Bug Report

Bug description

I have a MCP Server and the MCP client built. First, I spin up the MCP Server and then the MCP client. My client app interacts with the MCP server and works fine.
However, when I restart or redeploy the MCP Server, the client app interacts with the MCP server, and I observe the following error:

2025-04-17T11:02:40.637-05:00 ERROR 14929 --- [onPool-worker-3] i.m.c.t.HttpClientSseClientTransport : Error sending message: 404 

Detailed Error

2025-04-17T11:02:39.088-05:00 INFO 14929 --- [nio-8086-exec-4] com.llm.controller.ChatsController : userInput message : UserInput(prompt=Find me all the phones ?) 2025-04-17T11:02:39.088-05:00 INFO 14929 --- [nio-8086-exec-4]  

com.llm.controller.ChatsController : requestSpec : org.springframework.ai.chat.client.DefaultChatClient$DefaultChatClientRequestSpec@2f15ab7e 2025-04-17T11:02:39.088-05:00 INFO 14929 --- [nio-8086-exec-4] com.llm.controller.ChatsController : responseSpec1 : org.springframework.ai.chat.client.DefaultChatClient$DefaultCallResponseSpec@21d250ec 2025-04-17T11:02:40.132-05:00 DEBUG 14929 --- [nio-8086-exec-4] o.s.a.m.tool.DefaultToolCallingManager : Executing tool call: spring_ai_mcp_client_person_mcp_server_getAllProducts 

2025-04-17T11:02:40.637-05:00 ERROR 14929 --- [onPool-worker-3] i.m.c.t.HttpClientSseClientTransport : Error sending message: 404 

2025-04-17T11:06:40.155-05:00 ERROR 14929 --- [nio-8086-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: reactor.core.Exceptions$ReactiveException: java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 240000ms in 'source(MonoCreate)' (and no fallback has been configured)] with root cause


Environment

  • Spring AI version: 1.0.0-M7
  • Java version: 21
  • Vector Store: [Provide details if used, or leave blank]

Steps to reproduce

  1. Spin up the MCP Server.
  2. Spin up the MCP client and interact with the MCP Server (this works fine).
  3. Restart or redeploy the MCP Server.
  4. Have the MCP client interact with the MCP server again.
  5. Observe the error.

Expected behavior

I expected the MCP client to interact with the MCP server successfully after the server restart, without any errors.

Minimal Complete Reproducible example

You can view the complete example in my GitHub repository.

dilipsundarraj1 avatar Apr 17 '25 16:04 dilipsundarraj1

After the server restarts, it loses all session states, but the client is still using its old session ID. This causes ‘session not found’ errors. Here is the relevant code snippet from the source:

private final ConcurrentHashMap<String, McpServerSession> sessions = new ConcurrentHashMap<>();
// ...
private ServerResponse handleMessage(ServerRequest request) {
    // ...
    String sessionId = request.param("sessionId").get();
    McpServerSession session = sessions.get(sessionId);

    if (session == null) {
        return ServerResponse.status(HttpStatus.NOT_FOUND).body(new McpError("Session not found: " + sessionId));
    }
    // ...
}

Source: WebMvcSseServerTransportProvider.java

yangtuooc avatar Apr 18 '25 09:04 yangtuooc

@yangtuooc Yep I also noticed the same thing. The reason for creating this issue is to bring it to everyone's attention. Ideally I would expect the server to enable the new connection using the passed in id or create a brand new connection .

dilipsundarraj1 avatar Apr 18 '25 19:04 dilipsundarraj1

According to https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#session-management

When a client receives HTTP 404 in response to a request containing an Mcp-Session-Id, it MUST start a new session by sending a new InitializeRequest without a session ID attached.

So the problem is related to MCP client, not the server.

jakubmalek avatar Aug 01 '25 08:08 jakubmalek

My MCP server is deployed in Kubernetes and has multiple pods. When the MCP client starts, it may GET /sse from podA to obtain a sessionId, but then POST to /mcp/message?sessionId=xx on podB, resulting in a 404 error... because podB didn't save this sessionId

lbhkobe avatar Aug 12 '25 06:08 lbhkobe

我的 MCP 服务器部署在 Kubernetes 中,有多个 Pod。MCP 客户端启动时,可能会从 PodA GET /sse 获取 sessionId,然后 POST 到 PodB 的 /mcp/message?sessionId=xx,导致 404 错误……因为 PodB 没有保存这个 sessionId。

这个问题可以解决吗,目前我们也遇到了同样问题

zhuchuanjie001 avatar Aug 13 '25 11:08 zhuchuanjie001

sessionId not found for multiple pods; This issue prevents the use of Spring AI.

734839030 avatar Aug 15 '25 15:08 734839030

我的 MCP 服务器部署在 Kubernetes 中,有多个 Pod。MCP 客户端启动时,可能会从 PodA GET /sse 获取 sessionId,然后 POST 到 PodB 的 /mcp/message?sessionId=xx,导致 404 错误……因为 PodB 没有保存这个 sessionId。

这个问题可以解决吗,目前我们也遇到了同样问题

重写spring ai mcp-server端的session保存和验证逻辑 or server端用单节点 or 弃用spring ai 尝试下langchain4j ,我们选择了方案二

lbhkobe avatar Aug 22 '25 09:08 lbhkobe