fix(mcp): guard against duplicate serve() on streamable-http MCP servers - #8435
Merged
thinhlpg merged 2 commits intoJul 15, 2026
Merged
Conversation
Remote streamable-http MCP servers (type "http") intermittently failed to connect with `400 Bad Request ... when send initialize request`, or connected then dropped a few seconds later. Root cause: `start_mcp_server` had no idempotency guard, so a server could be serve()'d twice (e.g. a rename fires activate + syncServersAndRestart, and `restart_active_mcp_servers` restarts active servers without clearing them first). Each serve() spins up an independent rmcp client that sends its own `initialize` (both with request id 0); the second is rejected by the server as a duplicate on an already-initialized session, tearing down the connection. Fix: make `start_mcp_server` idempotent — skip if the server is already running or a start is already in flight (tracked via a new `mcp_starting` set on AppState), clearing the in-flight marker when the attempt completes. The reconnect path (`schedule_mcp_start_task`) is intentionally left unguarded so auto-reconnect can still re-serve. Fixes janhq#8411
thinhlpg
force-pushed
the
fix/mcp-http-duplicate-initialize-8411
branch
from
July 15, 2026 07:43
674b210 to
703fc74
Compare
qnixsynapse
reviewed
Jul 15, 2026
qnixsynapse
reviewed
Jul 15, 2026
Address review: the idempotency-guard skip messages are routine, not noteworthy, so log them at debug instead of info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #8411
Remote streamable-HTTP MCP servers (
type: "http") intermittently fail to connect with:or connect (UI turns green) then drop a few seconds later. Server-side logs show Jan sending the
initializeJSON-RPC request twice with the same request id0; the first succeeds, the second is rejected as a duplicate on an already-initialized session.Root cause
start_mcp_serverhad no idempotency guard, so a server could beserve()'d twice. rmcp'sserve()creates a fresh client that sendsinitializeexactly once (request id starts at 0 per client) — so twoinitializewith id 0 means twoserve()calls, not an rmcp retry.Two starts happen in practice because
restart_active_mcp_servers(helpers.rs) restarts every active server without clearing it from the running map first, and several frontend flows fireactivateand a restart-all for one action (e.g. renaming a server:activate+syncServersAndRestart). The twoserve()calls each POSTinitialize; the second gets400.Fix
Make
start_mcp_serveridempotent (3 files, +32/-2):mcp_starting: HashSet<String>onAppState; cleared when the attempt finishes (success or failure).The reconnect path (
schedule_mcp_start_task) is intentionally left unguarded so health-check auto-reconnect can still re-serve.Verification
Tested locally against a mock streamable-HTTP server that mimics the remote (first
initialize→ 200 + session id, duplicate → 400), with injected latency to widen the race window:initialize→200+400 DUPLICATE.MCP server <name> start already in progress; skipping duplicate start; the mock receives exactly oneinitialize, no400, connection stays up.initializeand remain connected.🤖 Generated with Claude Code