Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(core): terminate legacy MCP sessions on close
  • Loading branch information
rekram1-node committed Oct 1, 2026
commit bfe1e69bb20a6d8f35bdf58e305faf70534edda6
15 changes: 14 additions & 1 deletion packages/core/src/mcp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { McpStdio } from "./stdio.js"
const DEFAULT_STARTUP_TIMEOUT = 30_000
const DEFAULT_CATALOG_TIMEOUT = 30_000
const DEFAULT_EXECUTION_TIMEOUT = 12 * 60 * 60 * 1_000 // 12 hours
const TERMINATE_TIMEOUT = 1_000
const toError = (error: unknown) => (error instanceof Error ? error : new Error(String(error)))

export type { GetPromptResult, Prompt, ReadResourceResult, Resource, Tool }
Expand Down Expand Up @@ -236,7 +237,19 @@ export const connect = Effect.fnUntraced(function* (
}).pipe(Effect.exit)
if (Exit.isSuccess(exit)) {
const client = exit.value
yield* Effect.addFinalizer(() => Effect.promise(() => client.close()).pipe(Effect.ignore))
yield* Effect.addFinalizer(() =>
Effect.gen(function* () {
// Close only aborts streams; the legacy session lives on until the server expires it unless
// terminated explicitly. Terminate first: close aborts the signal the DELETE shares.
const transport = session.transport
if (transport?.sessionId !== undefined && !session.reported)
yield* Effect.promise(() => transport.terminateSession()).pipe(
Effect.timeout(TERMINATE_TIMEOUT),
Effect.ignore,
)
yield* Effect.promise(() => client.close()).pipe(Effect.ignore)
}),
)
const catalog = { timeout: config.timeout?.catalog ?? DEFAULT_CATALOG_TIMEOUT }
const execution = config.timeout?.execution ?? DEFAULT_EXECUTION_TIMEOUT
const request = <A>(what: string, run: (signal: AbortSignal) => Promise<A>) =>
Expand Down
82 changes: 82 additions & 0 deletions packages/core/test/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,88 @@ test("reconnects and retries a tool call after the MCP session expires", async (
)
})

describe("MCP session termination", () => {
const terminations = (input: { hang?: boolean } = {}) => {
const deletes: Array<string | null> = []
return {
deletes,
respond: (request: Request) => {
if (request.method !== "DELETE") return undefined
deletes.push(request.headers.get("mcp-session-id"))
return input.hang ? new Promise<undefined>(() => {}) : undefined
},
}
}
const remote = (url: string) => new ConfigMCP.Remote({ type: "remote", url, oauth: false })

test("terminates the legacy session when the connection closes", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const recorded = terminations()
const server = yield* resourceServer({ respond: recorded.respond })
yield* Effect.scoped(connect("resources", remote(server.url), import.meta.dir))
expect(recorded.deletes).toEqual(server.state.sessions)
expect(recorded.deletes).toHaveLength(1)
}),
),
)
})

test("bounds an unanswered termination before closing", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const recorded = terminations({ hang: true })
const server = yield* resourceServer({ respond: recorded.respond })
const started = performance.now()
yield* Effect.scoped(connect("resources", remote(server.url), import.meta.dir))
const elapsed = performance.now() - started
expect(recorded.deletes).toHaveLength(1)
expect(elapsed).toBeGreaterThanOrEqual(900)
expect(elapsed).toBeLessThan(3_000)
}),
),
)
})

test("does not terminate a session the server already expired", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const recorded = terminations()
const server = yield* resourceServer({ respond: recorded.respond })
yield* Effect.gen(function* () {
const connection = yield* connect("resources", remote(server.url), import.meta.dir)
yield* Effect.promise(server.restart)
yield* connection.callTool({ name: "echo", args: {} }).pipe(Effect.flip)
}).pipe(Effect.scoped)
expect(recorded.deletes).toEqual([])
}),
),
)
})

test("does not terminate modern connections", async () => {
await Effect.runPromise(
Effect.scoped(
Effect.gen(function* () {
const recorded = terminations()
const server = yield* resourceServer({ modern: true, respond: recorded.respond })
yield* Effect.scoped(
connect(
"resources",
new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false, protocol: "2026-07-28" }),
import.meta.dir,
),
)
expect(recorded.deletes).toEqual([])
}),
),
)
})
})

describe.each([
["legacy", undefined],
["modern", "2026-07-28"],
Expand Down
Loading