Skip to content

Commit dbb8346

Browse files
committed
fix(core): reconnect MCP sessions a server rejects with "No valid session ID provided"
Older Python SDK session managers and the TypeScript SDK example servers answer an unknown Mcp-Session-Id with 400 "Bad Request: No valid session ID provided" instead of 404. Treat that body as an expired session, like the existing "Server not initialized" case, so the connection reconnects and retries the request. Fixes anomalyco#66073
1 parent adc958a commit dbb8346

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎packages/core/src/mcp/client.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,10 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) {
488488
})
489489

490490
// Legacy era only: the transport holds the Mcp-Session-Id the server minted, and the server answering
491-
// it with 404, or with the 400 a freshly restarted single-session server emits, means it no longer
492-
// knows this connection. Modern connections never carry a session id, so a modern 404 for an unknown
493-
// method is not mistaken for expiry. Other 400s pass through untouched.
491+
// it with 404, with the 400 a freshly restarted single-session server emits, or with the 400 that older
492+
// Python SDK session managers and the TypeScript SDK examples send for an unknown session, means it no
493+
// longer knows this connection. Modern connections never carry a session id, so a modern 404 for an
494+
// unknown method is not mistaken for expiry. Other 400s pass through untouched.
494495
const session: { transport?: StreamableHTTPClientTransport; expired?: () => void; reported: boolean } = {
495496
reported: false,
496497
}
@@ -502,7 +503,8 @@ export const connect = Effect.fnUntraced(function* (options: ConnectOptions) {
502503
error.status === 404 ||
503504
(error.status === 400 &&
504505
typeof error.data.text === "string" &&
505-
error.data.text.includes("Bad Request: Server not initialized"))
506+
(error.data.text.includes("Bad Request: Server not initialized") ||
507+
error.data.text.includes("Bad Request: No valid session ID provided")))
506508
if (!expired) return new Error(explain(error))
507509
if (!session.reported) {
508510
session.reported = true

‎packages/core/test/mcp.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,63 @@ test("reconnects and retries a tool call after the MCP session expires", async (
12571257
)
12581258
})
12591259

1260+
// Servers that answer a session they no longer know with 400 instead of 404.
1261+
describe.each([
1262+
[
1263+
"a restarted single-session server",
1264+
() =>
1265+
Response.json(
1266+
{ jsonrpc: "2.0", error: { code: -32000, message: "Bad Request: Server not initialized" }, id: null },
1267+
{ status: 400 },
1268+
),
1269+
],
1270+
[
1271+
"a TypeScript SDK example server",
1272+
() =>
1273+
Response.json(
1274+
{ jsonrpc: "2.0", error: { code: -32000, message: "Bad Request: No valid session ID provided" }, id: null },
1275+
{ status: 400 },
1276+
),
1277+
],
1278+
[
1279+
"an older Python SDK session manager",
1280+
() => new Response("Bad Request: No valid session ID provided", { status: 400 }),
1281+
],
1282+
] as const)("reconnects after %s answers the expired session with 400", (_, expired) => {
1283+
test("and retries the tool call", async () => {
1284+
await Effect.runPromise(
1285+
Effect.scoped(
1286+
Effect.gen(function* () {
1287+
const stale: { session?: string } = {}
1288+
const server = yield* resourceServer({
1289+
respond: (request) =>
1290+
stale.session !== undefined && request.headers.get("mcp-session-id") === stale.session
1291+
? expired()
1292+
: undefined,
1293+
})
1294+
yield* Effect.gen(function* () {
1295+
const service = yield* Mcp.Service
1296+
yield* service.callTool({ server: "resources", name: "echo", args: { n: 1 } })
1297+
expect(server.state.sessions).toHaveLength(1)
1298+
1299+
// The restarted server does not know the client's session and answers it with 400.
1300+
stale.session = server.state.sessions[0]
1301+
yield* Effect.promise(server.restart)
1302+
const result = yield* service.callTool({ server: "resources", name: "echo", args: { n: 2 } })
1303+
1304+
expect(result.isError).toBe(false)
1305+
expect(server.state.toolCalls.map((call) => call.arguments)).toEqual([{ n: 1 }, { n: 2 }])
1306+
expect(server.state.initializations).toBe(2)
1307+
expect((yield* service.servers()).find((entry) => entry.name === "resources")?.status).toEqual({
1308+
status: "connected",
1309+
})
1310+
}).pipe(Effect.provide(resourceMcpLayer(server.url)))
1311+
}),
1312+
),
1313+
)
1314+
})
1315+
})
1316+
12601317
describe.each([
12611318
["legacy", undefined],
12621319
["modern", "2026-07-28"],

0 commit comments

Comments
 (0)