fix(mcp): include instructions from client-attached MCP servers

This commit is contained in:
Kit Langton 2026-11-03 16:13:52 -08:00
parent 3fcdcb35e8
commit 400d4451f7
2 changed files with 48 additions and 9 deletions

View file

@ -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<ServerName, string[]>()
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)

View file

@ -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<string>) =>
const clientServer = (label: string, tools: ReadonlyArray<string>, 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<unknown> = []
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<readonly [string, string]>) =>
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