diff --git a/src/server/EvidenceMcpServerCore.ts b/src/server/EvidenceMcpServerCore.ts index df756837d5..ec1e1b021a 100644 --- a/src/server/EvidenceMcpServerCore.ts +++ b/src/server/EvidenceMcpServerCore.ts @@ -317,8 +317,21 @@ export class EvidenceMcpServerCore extends RequestTrackingMcpServer { /** Install discovery after canonical registrations, before connecting. */ enableProgressiveCatalog(): void { if (this.progressiveCatalog !== undefined) return; + const { name, ...config } = DISCOVER_TOOLS; + // Transport discovery has no target or analysis lifecycle. Domain tools + // continue through registerTool and their original named handlers. Its + // advertisement still follows the selected input presentation. + const adapter = super.registerTool( + name, + { ...config, ...this.presentInput(name, config) }, + (input, context) => { + context.mcpReq.signal.throwIfAborted(); + return catalog.discover(input); + }, + ); const catalog = new ProgressiveToolCatalog( this.registrations, + adapter, this.catalogIdentity, (change) => { this.catalogChangeDepth++; @@ -335,18 +348,6 @@ export class EvidenceMcpServerCore extends RequestTrackingMcpServer { () => this.delivery, ); this.progressiveCatalog = catalog; - const { name, ...config } = DISCOVER_TOOLS; - // Transport discovery has no target or analysis lifecycle. Domain tools - // continue through registerTool and their original named handlers. Its - // advertisement still follows the selected input presentation. - super.registerTool( - name, - { ...config, ...this.presentInput(name, config) }, - (input, context) => { - context.mcpReq.signal.throwIfAborted(); - return catalog.discover(input); - }, - ); } override sendToolListChanged(): void { diff --git a/src/server/ProgressiveToolCatalog.test.ts b/src/server/ProgressiveToolCatalog.test.ts index 7982c05bc7..da66703d3c 100644 --- a/src/server/ProgressiveToolCatalog.test.ts +++ b/src/server/ProgressiveToolCatalog.test.ts @@ -195,6 +195,37 @@ it("loads exact names atomically and emits one notification for a batch", async expect(f.changed).toHaveBeenCalledTimes(1); }); +it("accepts its own advertised name like an already-loaded tool", async () => { + const f = await connect(); + const advertised = (await f.client.listTools()).tools.find( + ({ name }) => name === "discover_tools", + ); + const self = await f.discover({ names: ["discover_tools"] }); + expect(self.isError).not.toBe(true); + expect(self.structuredContent).toMatchObject({ + inventory_count: 4, + loaded_count: 2, + total_matches: 1, + tools: [{ name: "discover_tools", input_schema: advertised?.inputSchema }], + }); + expect(f.changed).not.toHaveBeenCalled(); + expect( + (await f.discover({ names: ["discover_tools", "missing"] })).isError, + ).toBe(true); + expect(f.changed).not.toHaveBeenCalled(); + const mixed = await f.discover({ names: ["alpha_sqlite", "discover_tools"] }); + expect(mixed.structuredContent).toMatchObject({ + loaded_count: 3, + tools: [{ name: "alpha_sqlite" }, { name: "discover_tools" }], + }); + expect((await f.client.listTools()).tools.map(({ name }) => name)).toEqual([ + "binary_session", + "alpha_sqlite", + "discover_tools", + ]); + expect(f.changed).toHaveBeenCalledTimes(1); +}); + it("rejects every malformed discovery without changing visibility", async () => { const f = await connect(); const initial = await f.client.listTools(); diff --git a/src/server/ProgressiveToolCatalog.ts b/src/server/ProgressiveToolCatalog.ts index ca62a35d34..8079826435 100644 --- a/src/server/ProgressiveToolCatalog.ts +++ b/src/server/ProgressiveToolCatalog.ts @@ -14,16 +14,26 @@ import { type CatalogIdentity, } from "../contracts/catalog/catalogDiscovery.js"; +type CatalogListing = { registered: RegisteredTool; description: string }; +const catalogListing = (registered: RegisteredTool): CatalogListing => ({ + registered, + description: (registered.description ?? "").slice(0, 240), +}); + /** Per-connection visibility; canonical SDK handles retain validation and authority. */ export class ProgressiveToolCatalog { readonly identity: CatalogIdentity; + /** In-scope domain tools; the adapter is never a search result or reset. */ private readonly tools: ReadonlyMap< string, - { registered: RegisteredTool; description: string; search: string } + CatalogListing & { search: string } >; + /** Exact names and counts also cover the always-advertised adapter. */ + private readonly inventory: ReadonlyMap; constructor( registrations: ReadonlyMap, + adapter: RegisteredTool, identity: CatalogIdentity, private readonly batchChanges: (change: () => void) => void, private readonly delivery: () => ToolResultDelivery, @@ -37,13 +47,16 @@ export class ProgressiveToolCatalog { .map(([name, registered]) => [ name, { - registered, - description: (registered.description ?? "").slice(0, 240), + ...catalogListing(registered), search: `${name} ${name.replaceAll("_", " ")} ${registered.description ?? ""}`.toLowerCase(), }, ]), ); + this.inventory = new Map([ + ...this.tools, + [DISCOVER_TOOLS.name, catalogListing(adapter)], + ]); this.reset(); } @@ -60,7 +73,7 @@ export class ProgressiveToolCatalog { discover(input: z.infer) { const names = input.names === undefined ? undefined : [...new Set(input.names)]; - const unknown = names?.filter((name) => !this.tools.has(name)) ?? []; + const unknown = names?.filter((name) => !this.inventory.has(name)) ?? []; if (unknown.length > 0) return this.delivery().toErrorToolResult( new AnalysisInputError( @@ -99,14 +112,17 @@ export class ProgressiveToolCatalog { }); const page = names ?? matches.slice(input.offset, input.offset + input.limit); - const selected = page.map((name) => ({ name, ...this.tools.get(name)! })); + const selected = page.map((name) => ({ + name, + ...this.inventory.get(name)!, + })); const result: z.infer = { profile: "hot-cold", - inventory_count: this.tools.size + 1, + inventory_count: this.inventory.size, loaded_count: - 1 + - [...this.tools.values()].filter(({ registered }) => registered.enabled) - .length + + [...this.inventory.values()].filter( + ({ registered }) => registered.enabled, + ).length + selected.filter(({ registered }) => !registered.enabled).length, total_matches: matches.length, next_offset: diff --git a/tests/boundary/mcp/deferredRuntime.test.ts b/tests/boundary/mcp/deferredRuntime.test.ts index d1be91adc2..f1d67ce0bd 100644 --- a/tests/boundary/mcp/deferredRuntime.test.ts +++ b/tests/boundary/mcp/deferredRuntime.test.ts @@ -182,12 +182,15 @@ it.each([ expect(invalid.isError).toBe(true); expect(await client.listTools()).toEqual(initial); const loaded = await call("discover_tools", { - names: ["list_unknowns", "list_unknowns"], + names: ["list_unknowns", "discover_tools", "list_unknowns"], }); expect(loaded.structuredContent).toMatchObject({ loaded_count: initial.tools.length + 1, - total_matches: 1, - tools: [{ name: "list_unknowns" }], + total_matches: 2, + tools: [ + { name: "list_unknowns" }, + { name: "discover_tools", input_schema: discovery.inputSchema }, + ], }); expect((await call("list_unknowns", {})).isError).not.toBe(true); const materialized = await client.listTools();