diff --git a/packages/core/src/mcp/index.ts b/packages/core/src/mcp/index.ts index 43a4223c649..119a3214d68 100644 --- a/packages/core/src/mcp/index.ts +++ b/packages/core/src/mcp/index.ts @@ -769,13 +769,17 @@ export const layer = (options?: Options) => return yield* target.entry.connection.recovering(target.client, call).pipe(failed("server")) }), instructions: Effect.fn("MCP.instructions")(function* () { - return Array.from(entries) - .flatMap(([server, entry]) => { - const instructions = entry.connection.client?.instructions - if (!instructions) return [] - return [{ server, instructions }] - }) - .toSorted((a, b) => a.server.localeCompare(b.server)) + const byServer = new Map() + const add = (server: ServerName, instructions: string | undefined) => { + if (instructions) byServer.set(server, [...(byServer.get(server) ?? []), instructions]) + } + for (const [server, entry] of entries) add(server, entry.connection.client?.instructions) + // Like tools, an attached client's instructions come from the connection serving its name, and sit + // beside a configured server's of the same name since that server's other tools stay reachable. + for (const [server, stack] of attached) add(server, stack.at(-1)?.client.instructions) + return Array.from(byServer, ([server, parts]) => ({ server, instructions: parts.join("\n\n") })).toSorted( + (a, b) => a.server.localeCompare(b.server), + ) }), prompts: Effect.fn("MCP.prompts")(function* () { return Array.from(entries) diff --git a/packages/core/test/mcp-attach.test.ts b/packages/core/test/mcp-attach.test.ts index 44825d0147f..8f6acabd661 100644 --- a/packages/core/test/mcp-attach.test.ts +++ b/packages/core/test/mcp-attach.test.ts @@ -54,9 +54,9 @@ const it = testEffect( ) /** Starts an in-memory client MCP server whose tools answer with `label` and record the caller's session. */ -const clientServer = (label: string, tools: ReadonlyArray) => +const clientServer = (label: string, tools: ReadonlyArray, instructions?: string) => Effect.gen(function* () { - const server = new McpServer({ name: label, version: "1.0.0" }) + const server = new McpServer({ name: label, version: "1.0.0" }, instructions ? { instructions } : undefined) const sessions: Array = [] for (const tool of tools) server.registerTool(tool, { description: `${label} ${tool}` }, (ctx) => { @@ -149,6 +149,41 @@ it.live("attaches a client MCP server and routes its tools with the session", () }), ) +/** Waits until the listed server instructions match `expected`, as `[server, instructions]` pairs. */ +const instructionsSettle = (expected: ReadonlyArray) => + Effect.gen(function* () { + const mcp = yield* Mcp.Service + const listed = (yield* mcp.instructions()).map((item) => [item.server, item.instructions]) + if (JSON.stringify(listed) !== JSON.stringify(expected)) return yield* Effect.fail("pending") + }).pipe(Effect.retry({ times: 500, schedule: Schedule.spaced("2 millis") })) + +it.live("lists the instructions of the connection serving an attached name", () => + Effect.gen(function* () { + const mcp = yield* Mcp.Service + const first = yield* clientServer("first", ["shared"], "First instructions") + const second = yield* clientServer("second", ["shared"], "Second instructions") + const plain = yield* clientServer("plain", ["shared"]) + + yield* mcp.attach("tui", first.client) + expect(yield* mcp.instructions()).toEqual([ + { server: Mcp.ServerName.make("tui"), instructions: "First instructions" }, + ]) + + // The newest connection serves the name; a client without instructions adds no entry. + yield* mcp.attach("tui", second.client) + yield* mcp.attach("plain", plain.client) + expect(yield* mcp.instructions()).toEqual([ + { server: Mcp.ServerName.make("tui"), instructions: "Second instructions" }, + ]) + + yield* Effect.promise(() => second.server.close()) + yield* instructionsSettle([["tui", "First instructions"]]) + + yield* Effect.promise(() => first.server.close()) + yield* instructionsSettle([]) + }), +) + it.live("removes an attached entry when its scope closes", () => Effect.gen(function* () { const mcp = yield* Mcp.Service