Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions packages/plugins/mcp/src/sdk/discover-close.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "@effect/vitest";
import { Effect } from "effect";

import type { McpConnection, McpConnector } from "./connection";
import { discoverTools } from "./discover";

const discoveryClient = (): McpConnection["client"] =>
Object.assign(Object.create(null) as McpConnection["client"], {
listTools: () => Promise.resolve({ tools: [] }),
getServerVersion: () => ({ name: "hanging-close", version: "1.0.0" }),
getInstructions: () => undefined,
setRequestHandler: () => undefined,
});

const hangingCloseConnector = (state: { closeStarted: boolean }): McpConnector =>
Effect.succeed({
client: discoveryClient(),
close: () => {
state.closeStarted = true;
return new Promise<void>(() => {});
},
});

describe("MCP discovery teardown", () => {
it.live("does not strand discovery when close never settles", () =>
Effect.gen(function* () {
const state = { closeStarted: false };
const manifest = yield* discoverTools(hangingCloseConnector(state));

expect(state.closeStarted).toBe(true);
expect(manifest.server).toEqual({
name: "hanging-close",
version: "1.0.0",
instructions: null,
});
expect(manifest.tools).toEqual([]);
}),
);
});
24 changes: 14 additions & 10 deletions packages/plugins/mcp/src/sdk/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ const MAX_LIST_TOOLS_PAGES = 100;
// shape probe's single unauth POST.
const DEFAULT_DISCOVER_TIMEOUT = Duration.seconds(15);

// Teardown is best-effort and paid for by the request that performed discovery.
// A remote transport may accept close and then never settle, so use the same
// bound as the invocation connection pool instead of stranding the caller in an
// uninterruptible finalizer after discovery itself has already completed.
const CLOSE_TIMEOUT = Duration.seconds(2);

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -243,13 +249,11 @@ export const discoverTools = (
const closeConnection = (connection: {
readonly close: () => Promise<void>;
}): Effect.Effect<void, never> =>
Effect.ignore(
Effect.tryPromise({
try: () => connection.close(),
catch: () =>
new McpToolDiscoveryError({
stage: "list_tools",
message: "Failed closing MCP connection",
}),
}),
);
Effect.tryPromise({
try: () => connection.close(),
catch: () =>
new McpToolDiscoveryError({
stage: "list_tools",
message: "Failed closing MCP connection",
}),
}).pipe(Effect.timeout(CLOSE_TIMEOUT), Effect.ignore);
Loading