Commit 7657384
authored
fix(mcp): guard against duplicate serve() on streamable-http MCP servers (#8435)
* fix(mcp): guard against duplicate serve() on streamable-http servers
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 #8411
* fix(mcp): log duplicate-start skips at debug level
Address review: the idempotency-guard skip messages are routine, not
noteworthy, so log them at debug instead of info.1 parent befb51c commit 7657384
3 files changed
Lines changed: 32 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
333 | 347 | | |
334 | 348 | | |
335 | 349 | | |
| |||
343 | 357 | | |
344 | 358 | | |
345 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
346 | 364 | | |
347 | 365 | | |
348 | 366 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
| |||
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
70 | 77 | | |
71 | 78 | | |
72 | 79 | | |
| |||
97 | 104 | | |
98 | 105 | | |
99 | 106 | | |
| 107 | + | |
100 | 108 | | |
101 | 109 | | |
102 | 110 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
| |||
272 | 275 | | |
273 | 276 | | |
274 | 277 | | |
| 278 | + | |
275 | 279 | | |
276 | 280 | | |
277 | 281 | | |
| |||
0 commit comments