mcp: no reconnect when a legacy HTTP server answers an expired session with 400 "No valid session ID provided"

未关闭 适合新手
#66,073 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
typescript
领域
api

调研方向

从 packages/core/src/mcp/client.ts 中的 failure() 开始(大约第 497-512 行),它把 404 和带有 Bad Request: Server not initialized 的 400 映射为 SessionExpiredError;新情况是 body 中包含 No valid session ID 的 400,仍然以 transport 持有 session id 为条件。然后阅读 connection.ts:139-149,确认 recovering 就是触发重连的东西,这样你就能判断修复是否有效。在 packages/core/test 中添加或扩展一个 bun test,用报告中的 400 body 回应一个旧 session id,并断言该调用会重试并成功。注意报告者提出可以发送一个带测试的 PR,所以开始前先检查 thread。

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

描述

area/mcp platform/windows status/repro-simulated type/bug version/v2
Description

When a legacy Streamable HTTP server forgets the session (restart or idle expiry) and answers the old Mcp-Session-Id with HTTP 400 Bad Request: No valid session ID provided, opencode does not reconnect. Every later tool call on that server fails with that 400, and the server still shows connected until the user reconnects it by hand.

This was reported in #32809 (Atlassian MCP 1.12.4 after ~15-30 min idle). The bot closed it for inactivity and asked for a new issue if it is still relevant. It still happens on v2 dev.

The cause is failure() in packages/core/src/mcp/client.ts:497-512. It maps a 404 to SessionExpiredError, but a 400 only when the body contains Bad Request: Server not initialized. Older Python SDK session managers and the TypeScript SDK example servers answer an unknown session id with Bad Request: No valid session ID provided. FastMCP-based servers such as mcp-atlassian inherit this. That body falls through as a plain error, so recovering (connection.ts:139-149) never runs.

Fix idea: also treat a 400 whose body contains No valid session ID as an expired session. The check stays narrow: it only applies when the transport holds a session id, like the existing cases, so other 400s still pass through.

I can send a PR with a test if this is OK.

Plugins

No response

OpenCode version

dev adc958a7d7 (2.4.1)

Steps to reproduce
  1. Run a stateful legacy MCP server that answers an unknown session id with 400 {"jsonrpc":"2.0","error":{"code":-32000,"message":"Bad Request: No valid session ID provided"},"id":null}, and add it as a remote server.
  2. Call a tool once. It works.
  3. Restart the server (or let the session expire).
  4. Call the tool again. It fails with MCP tool "echo" on server "legacy" failed: Error POSTing to endpoint: {..."Bad Request: No valid session ID provided"...} (HTTP 400), and every later call fails the same way. With a 404 for the same case, opencode reconnects and the call succeeds.

I have this as a bun test in packages/core/test.

Screenshot

No response

Operating System

Windows 11

Terminal

No response

主要语言
TypeScript
星标
228k
派生
31k
平均合并
3 小时 22 分钟
30 天内合并 PR
476

环境准备

从这里开始

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

anomalyco/opencode 的其他 Issue

查看 anomalyco/opencode 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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