[BUG] Streamable HTTP MCP server stays unusable after session expiry (HTTP 404) until manual restart

Đang mở
#2,481 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
api, backend

Hướng nghiên cứu

Start in src/services/mcp/McpHub.ts, read callTool() and its existing UnauthorizedError recovery branch, then trace how deleteConnection() and connectToServer() rebuild a connection. Check the Streamable HTTP SDK error type and ensure recovery applies only to a request with an expired session, retries once, and preserves OAuth data. Done means the failed tool call succeeds after a session 404, while a repeated 404 surfaces as an error.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug
Problem (one or two sentences)

When a stateful Streamable HTTP MCP server restarts or drops its session, Zoo Code keeps sending the old Mcp-Session-Id. The server answers with HTTP 404, and Zoo Code does not start a new session. Every tool call fails until the user clicks Restart in Settings → MCP Servers.

Context (who is affected and when)

This bug affects users who connect Zoo Code to a stateful Streamable HTTP MCP server. It occurs when the server restarts, redeploys, or evicts an idle session. The tool stays in the tool list, so the model keeps calling it and keeps receiving an error. The user must restart the connection by hand after each server restart.

Reproduction steps
  1. Configure a streamable-http MCP server that returns an Mcp-Session-Id header during initialize.
  2. Connect Zoo Code to the server. Call one tool. The call succeeds.
  3. Restart the server process. The old session no longer exists on the server.
  4. Call the same tool again from a Zoo Code task.
  5. Observe the error. The MCP panel shows the server as disconnected. The tool is still advertised to the model.
  6. Open Settings → MCP Servers and click Restart for this server.
  7. Call the tool again. The call succeeds.

The same sequence occurs without a restart when the server evicts an idle session. Wait for the server's session idle timeout, then call a tool.

Expected result

Zoo Code detects the HTTP 404 for a request that carried an Mcp-Session-Id. Zoo Code starts a new session and retries the failed tool call once. The user does not need to restart the connection.

Actual result

The tool call fails with a Streamable HTTP 404 error. Zoo Code does not reconnect and does not retry. The connection stays unusable until the user clicks Restart in Settings → MCP Servers.

Variations tried (optional)
  • Restarting the VS Code window. This works, but it is slower than the Restart button.
  • Toggling the server off and on in Settings → MCP Servers. This works.
  • Editing the MCP settings file without a config change. This does nothing, because updateServerConnections() skips servers whose config is unchanged.
  • Using the sse transport instead of streamable-http. The SSE transport reconnects automatically through ReconnectingEventSource. The Streamable HTTP transport does not reconnect.
App Version

v3.98.0

API Provider (optional)

Not Applicable / Other

Model Used (optional)

No response

Zoo Code Task Links (optional)

No response

Relevant logs or errors (optional)


Root cause

The MCP specification requires the client to recover from this case. MCP 2025-06-18, 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.

Zoo Code does not implement this recovery. Three code paths are involved.

  1. The SDK keeps the stale session ID. Zoo Code pins @modelcontextprotocol/sdk 1.32.2. In StreamableHTTPClientTransport.send(), _sessionId is set only from a successful initialize response. The if (!response.ok) branch special-cases only 401 and 403. A 404 falls through to throw new StreamableHTTPError(404, ...). The transport does not clear _sessionId and does not re-initialize.

  2. McpHub.callTool() only recovers from 401. In src/services/mcp/McpHub.ts, callTool() has a branch for UnauthorizedError. That branch runs the OAuth flow, rebuilds the connection, and retries the call. There is no equivalent branch for a 404 or for a terminated session.

  3. transport.onclose does not reconnect. For streamable-http, transport.onclose sets connection.server.status = "disconnected" and notifies the webview. It does not rebuild the client or the transport. The SSE branch differs: it uses ReconnectingEventSource, so it reconnects on its own.

In addition, getServers() returns all enabled servers regardless of status. The tool therefore stays in the prompt after the session is gone. The model keeps calling a tool that cannot succeed.

Proposed fix

Add a session-expiry branch to callTool(), next to the existing UnauthorizedError branch:

  1. Catch the error from connection.client.request(...).
  2. Detect a 404 that belongs to a request with a session ID. With SDK 1.32.2, check error instanceof StreamableHTTPError && error.code === 404. Do not treat a 404 on initialize as session expiry.
  3. Call deleteConnection(name, source).
  4. Call connectToServer(name, config, source).
  5. Retry the original tool call once. Bound the retry to one attempt, so a repeated 404 still surfaces as an error.

A smaller alternative is to reconnect in transport.onclose with a bounded backoff. That approach also covers the case where no tool call follows the disconnect. The callTool() branch is still needed, because the close event can arrive after the next call starts.

Do not clear OAuth data in this path. restartConnection() currently deletes stored OAuth data for streamable-http servers. A recovery path must not force a new browser login.

Upstream status

The same gap exists in the MCP TypeScript SDK. No fix is merged at the time of this report.

Zoo Code uses the v1 SDK. The open v2 fix will not reach Zoo Code automatically. A Zoo Code-side fix is independent of the SDK migration and works with the current dependency.

Ngôn ngữ chính
TypeScript
Star
2k
Fork
301
Merge trung bình
3 ngày 14 giờ
Pull request đã merge (30 ngày)
103

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Zoo-Code-Org/Zoo-Code

Tất cả issue của Zoo-Code-Org/Zoo-Code

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.