Skip to content

fix(mcp): reinitialize expired HTTP sessions - #2387

Open
lurenjia534 wants to merge 1 commit into
MoonshotAI:mainfrom
lurenjia534:fix/mcp-session-reinitialize-2380
Open

fix(mcp): reinitialize expired HTTP sessions#2387
lurenjia534 wants to merge 1 commit into
MoonshotAI:mainfrom
lurenjia534:fix/mcp-session-reinitialize-2380

Conversation

@lurenjia534

@lurenjia534 lurenjia534 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Related Issue

Resolve #2380

Problem

After a Streamable HTTP MCP server restarted, its old session id could remain attached to the client transport. A tool retry then sent the same expired id again instead of establishing a new session, so the server repeatedly returned Unknown session id.

What changed

  • Recognize a terminated session only when a sessionful Streamable HTTP transport receives the specification-defined HTTP 404 or the reported HTTP 400 / JSON-RPC -32600 response with an explicit invalid-session and reinitialize message.
  • Reinitialize the affected server and retry the interrupted tool call exactly once.
  • Make manual, RPC, OAuth, and automatic recovery callers join one reconnect attempt per server.
  • Resolve the current client before each call so existing tool wrappers remain valid after reconnect.
  • Never transparently replay a call that fails only with ConnectionClosed, because its server-side outcome is unknown and replaying could duplicate side effects.
  • Preserve the original session error alongside reconnect diagnostics, and allow an aborted caller to stop waiting without cancelling the shared reconnect.
  • Exercise real in-process Streamable HTTP sessions as well as concurrency, abort, failed-reconnect, and false-positive cases.

Validation

  • Before/after real Streamable HTTP E2E on the exact upstream base
    691ec4679ea19d6be8ac18f359088384ed3e446d and fixed
    e03fd73a947bf34668059ff694ece2566e539f8c: the added invalid-session
    scenario failed on upstream with the server's Unknown session id
    response, then passed unchanged after the fix by establishing a replacement
    session and retrying the interrupted call once.
  • pnpm --filter @moonshot-ai/agent-core exec vitest run test/mcp/client-http.test.ts test/mcp/connection-manager.test.ts test/mcp/tool-manager-mcp.test.ts — 93 passed
  • pnpm --filter @moonshot-ai/agent-core exec vitest run — 4,133 passed, 30 skipped, 3 expected failures, 1 todo
  • pnpm --filter @moonshot-ai/agent-core typecheck
  • pnpm --filter @moonshot-ai/agent-core build
  • pnpm --filter @moonshot-ai/kimi-code typecheck
  • pnpm --filter @moonshot-ai/kimi-code build
  • node apps/kimi-code/dist/main.mjs --version
  • pnpm exec oxlint --type-aware --quiet
  • git diff --check

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e03fd73

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lurenjia534
lurenjia534 marked this pull request as ready for review July 29, 2026 21:52
@pkg-pr-new

pkg-pr-new Bot commented Jul 29, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@e03fd73
npx https://pkg.pr.new/@moonshot-ai/kimi-code@e03fd73

commit: e03fd73

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e03fd73a94

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP client 收到 -32600 Unknown session id 后不自动 reinitialize,重试仍原样重发旧 session id(Streamable HTTP,server 重启后)

1 participant