modelcontextprotocol / modelcontextprotocol/java-sdk

HttpClientStreamableHttpTransport: reconnect() called before status code check causes duplicate connections on 405

未关闭
#773 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

waiting for user
主要语言
Java
星标
3.7k
派生
1.1k
平均合并
1 天 15 小时
30 天内合并 PR
9

描述

Description

When connecting to an MCP server that doesn't support Streamable HTTP transport (returns 405 Method Not Allowed), HttpClientStreamableHttpTransport still calls markInitialized() and reconnect(), which triggers an unnecessary GET request.

This causes issues when using transport fallback (Streamable HTTP → SSE), as duplicate SSE sessions are created on the upstream server.

Steps to Reproduce

  1. Set up an MCP proxy/server that only supports SSE transport (not Streamable HTTP)
  2. Return 405 Method Not Allowed for POST requests to the Streamable HTTP endpoint
  3. Use a client that tries both transports in parallel (Streamable HTTP and SSE)
  4. Observe that the Streamable HTTP transport sends an additional GET request after receiving 405

Expected Behavior

When the server returns an error status (like 405), reconnect() should NOT be called. The transport should simply fail and let the fallback mechanism use SSE transport.

Actual Behavior

markInitialized() and reconnect() are called BEFORE checking the status code, so even error responses trigger a GET request:

https://github.com/modelcontextprotocol/java-sdk/blob/main/mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java#L468-L481

})).flatMap(responseEvent -> {
    if (transportSession.markInitialized(
            responseEvent.responseInfo().headers().firstValue("mcp-session-id").orElseGet(() -> null))) {
        // Once we have a session, we try to open an async stream for
        // the server to send notifications and requests out-of-band.

        reconnect(null).contextWrite(deliveredSink.contextView()).subscribe();
    }

    String sessionRepresentation = sessionIdOrPlaceholder(transportSession);

    int statusCode = responseEvent.responseInfo().statusCode();

    if (statusCode >= 200 && statusCode < 300) {

Also Affected: WebClientStreamableHttpTransport

The same bug exists in WebClientStreamableHttpTransport (lines 312-324):

https://github.com/modelcontextprotocol/java-sdk/blob/main/mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/client/transport/WebClientStreamableHttpTransport.java#L312-L324

.exchangeToFlux(response -> {
    if (transportSession
        .markInitialized(response.headers().asHttpHeaders().getFirst(HttpHeaders.MCP_SESSION_ID))) {
        // Once we have a session, we try to open an async stream for
        // the server to send notifications and requests out-of-band.
        reconnect(null).contextWrite(sink.contextView()).subscribe();
    }

    String sessionRepresentation = sessionIdOrPlaceholder(transportSession);

    // The spec mentions only ACCEPTED, but the existing SDKs can return
    // 200 OK for notifications
    if (response.statusCode().is2xxSuccessful()) {

Both implementations have the identical issue: markInitialized() and reconnect() are called before checking the HTTP status code.

Note: Why this is still a bug even though reconnect() handles 405

I noticed that reconnect() already has 405 handling logic:

} else if (statusCode == METHOD_NOT_ALLOWED) {
    logger.debug("The server does not support SSE streams, using request-response mode.");
    return Flux.empty();
}

However, this doesn't prevent the issue because:

  1. The GET request is already sent - By the time reconnect() handles the 405 response, the HTTP request has already been made
  2. Upstream creates a new session - When the GET request reaches the upstream server, it creates a new SSE session before returning any response
  3. Duplicate sessions exist - Now there are two sessions on upstream: one from SSE transport's GET, another from Streamable HTTP's reconnect() GET

The flow:

1. SSE transport: GET /sse → upstream creates session #1
2. Streamable HTTP: POST /mcp → 405
3. markInitialized(null) → returns true
4. reconnect() → GET /mcp sent → upstream creates session #2 (the damage is done!)
5. reconnect() receives response, handles 405 gracefully (but too late)
6. SSE transport: POST to session #1 → success
7. Result: session #2 is orphaned, potential timeout issues

The fix should prevent the GET request from being sent in the first place, not just handle the response gracefully.

Suggested Fix

Move markInitialized() and reconnect() inside the success status code check.

For HttpClientStreamableHttpTransport:

})).flatMap(responseEvent -> {
    String sessionRepresentation = sessionIdOrPlaceholder(transportSession);

    int statusCode = responseEvent.responseInfo().statusCode();

    if (statusCode >= 200 && statusCode < 300) {
        // Only initialize session and open async stream for successful responses
        if (transportSession.markInitialized(
                responseEvent.responseInfo().headers().firstValue("mcp-session-id").orElseGet(() -> null))) {
            // Once we have a session, we try to open an async stream for
            // the server to send notifications and requests out-of-band.
            reconnect(null).contextWrite(deliveredSink.contextView()).subscribe();
        }

For WebClientStreamableHttpTransport:

.exchangeToFlux(response -> {
    String sessionRepresentation = sessionIdOrPlaceholder(transportSession);

    if (response.statusCode().is2xxSuccessful()) {
        // Only initialize session and open async stream for successful responses
        if (transportSession
            .markInitialized(response.headers().asHttpHeaders().getFirst(HttpHeaders.MCP_SESSION_ID))) {
            reconnect(null).contextWrite(sink.contextView()).subscribe();
        }

Impact

This bug affects any scenario where:

  • A proxy or server doesn't support Streamable HTTP (returns 405)
  • Client implements transport fallback (try Streamable HTTP first, fall back to SSE)
  • Results in duplicate upstream sessions and potential timeout issues

Environment

  • MCP Java SDK version: latest (this code has been present since the initial Streamable HTTP implementation in commit c711f83)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java 和 mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/client/transport/WebClientStreamableHttpTransport.java 中的响应处理器开始。检查 markInitialized() 和 reconnect() 周围的状态码分支。完成标准是:非 2xx 响应(尤其是 405)不会触发 GET,同时成功响应会保留会话初始化,并且回退到 SSE 时不会产生重复会话。

由索引模型根据 Issue 内容生成。

评估

技术栈
java, spring
领域
backend-api-design, networking
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
描述清楚
新手友好度
75/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。