mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 15:05:51 +00:00
fix(mcp): accept discover_tools in exact-name discovery batches (#13288)
With REA_MCP_TOOL_CATALOG_PROFILE=hot-cold, discover_tools is listed in tools/list, yet an exact-name request that named it failed with "Unknown or excluded tool names. No tools were loaded." Exact names load atomically, so a batch such as ["inspect_sqlite_database", "discover_tools"] also left the valid cold tool unloaded. The catalog validated names against the domain registrations it was built from, and the server registered the adapter only after building it. The server now registers discover_tools first and passes that registration to ProgressiveToolCatalog. Exact-name lookup, the returned entries and both counts read one inventory that includes the adapter, which replaces the two "+ 1" adjustments. Search and reset still cover only domain tools, so a query never spends a page slot on the adapter and reset never hides it. Unknown or excluded names are still rejected before anything loads. Signed-off-by: Deepak Jain <deepujain@gmail.com>
This commit is contained in:
1 parent
6357f775a0
commit
fd40fd9ab2
4 files changed
+75
-24
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<string, CatalogListing>;
|
||||
|
||||
constructor(
|
||||
registrations: ReadonlyMap<string, RegisteredTool>,
|
||||
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<string, CatalogListing>([
|
||||
...this.tools,
|
||||
[DISCOVER_TOOLS.name, catalogListing(adapter)],
|
||||
]);
|
||||
this.reset();
|
||||
}
|
||||
|
||||
@@ -60,7 +73,7 @@ export class ProgressiveToolCatalog {
|
||||
discover(input: z.infer<typeof DISCOVER_TOOLS.inputSchema>) {
|
||||
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<typeof DISCOVER_TOOLS.outputSchema> = {
|
||||
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:
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user