modelcontextprotocol / modelcontextprotocol/java-sdk

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

Abierto
#773 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

waiting for user
Lenguaje dominante
Java
Estrellas
3.7k
Forks
1.1k
Merge medio
1 d 15 h
PR fusionados (30 d)
9

Descripción

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)

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con los manejadores de respuestas en mcp-core/src/main/java/io/modelcontextprotocol/client/transport/HttpClientStreamableHttpTransport.java y mcp-spring/mcp-spring-webflux/src/main/java/io/modelcontextprotocol/client/transport/WebClientStreamableHttpTransport.java. Revisa las ramas de código de estado alrededor de markInitialized() y reconnect(). Se considera terminado cuando las respuestas que no son 2xx, especialmente 405, no activan un GET, mientras que las respuestas exitosas conservan la inicialización de la sesión y el fallback a SSE evita sesiones duplicadas.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
java, spring
Área
backend-api-design, networking
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Tranquilo
Claridad
Bien especificado
Aptitud para principiantes
75/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.