inspector/core/mcp/state/managedToolsState.ts
Cliff Hall 9e494544e7
fix(state): gate list fetches on server capability (#1350) (#1395)
The four list-fetching state managers fired their list RPC against every
server on connect regardless of advertised capabilities, so a server that
doesn't implement a given primitive replied -32601 "Method not found",
surfacing in the console on every connect. Mirrors the `tasks`-capability
gate added for ManagedRequestorTasksState in #1349.

- managedToolsState        → gate on capabilities.tools
- managedPromptsState      → gate on capabilities.prompts
- managedResourcesState    → gate on capabilities.resources
- managedResourceTemplatesState → gate on capabilities.resources
  (the spec defines no separate resourceTemplates capability;
  resources/templates/list is part of the resources surface)

When the capability is absent each manager sets an empty list, dispatches
its change event, and returns — the right semantics for "this server
doesn't support X."

Tests: existing flow tests now construct their FakeInspectorClient with
the relevant capability so the live list path is still exercised; added a
"refresh skips listX when capability absent" and a "connect against an
X-less server doesn't fire listX" test per manager. Updated the
useManagedX peers and resourceSubscriptionsState (which drives a
ManagedResourcesState refresh) to advertise the capability too.

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 22:06:55 -04:00

104 lines
3.4 KiB
TypeScript

/**
* ManagedToolsState: holds full tool list, syncs on toolsListChanged.
*
* Ported from v1.5/main. v2 substitutes `InspectorClientProtocol` for the
* concrete `InspectorClient` since the runtime class is not yet ported.
*/
import type { InspectorClientProtocol } from "../inspectorClientProtocol.js";
import type { Tool } from "@modelcontextprotocol/sdk/types.js";
import { TypedEventTarget } from "../typedEventTarget.js";
const MAX_PAGES = 100;
export interface ManagedToolsStateEventMap {
toolsChange: Tool[];
}
/**
* State manager that keeps a full tool list in sync with the server.
* Subscribes to client's connect (initial load), toolsListChanged, and
* statusChange; fetches all pages on refresh.
*/
export class ManagedToolsState extends TypedEventTarget<ManagedToolsStateEventMap> {
private tools: Tool[] = [];
private client: InspectorClientProtocol | null = null;
private unsubscribe: (() => void) | null = null;
private _metadata: Record<string, string> | undefined = undefined;
constructor(client: InspectorClientProtocol) {
super();
this.client = client;
const onConnect = (): void => {
void this.refresh();
};
const onToolsListChanged = (): void => {
void this.refresh();
};
const onStatusChange = (): void => {
if (this.client?.getStatus() === "disconnected") {
this.tools = [];
this.dispatchTypedEvent("toolsChange", []);
}
};
this.client.addEventListener("connect", onConnect);
this.client.addEventListener("toolsListChanged", onToolsListChanged);
this.client.addEventListener("statusChange", onStatusChange);
this.unsubscribe = () => {
if (this.client) {
this.client.removeEventListener("connect", onConnect);
this.client.removeEventListener("toolsListChanged", onToolsListChanged);
this.client.removeEventListener("statusChange", onStatusChange);
}
this.client = null;
};
}
getTools(): Tool[] {
return [...this.tools];
}
setMetadata(metadata?: Record<string, string>): void {
this._metadata = metadata;
}
async refresh(metadata?: Record<string, string>): Promise<Tool[]> {
const client = this.client;
if (!client || client.getStatus() !== "connected") {
return this.getTools();
}
// Gate on the server's `tools` capability — calling tools/list against a
// server that doesn't advertise it returns -32601 "Method not found",
// which then surfaces in the console for every connect against a
// tools-less server. Empty list is the right semantics for "this server
// doesn't support tools."
if (!client.getCapabilities()?.tools) {
this.tools = [];
this.dispatchTypedEvent("toolsChange", this.tools);
return this.getTools();
}
const effectiveMetadata = metadata ?? this._metadata;
this.tools = [];
let cursor: string | undefined;
let pageCount = 0;
do {
const result = await client.listTools(cursor, effectiveMetadata);
this.tools = cursor ? [...this.tools, ...result.tools] : result.tools;
cursor = result.nextCursor;
pageCount++;
if (pageCount >= MAX_PAGES) {
throw new Error(
`Maximum pagination limit (${MAX_PAGES} pages) reached while listing tools`,
);
}
} while (cursor);
this.dispatchTypedEvent("toolsChange", this.tools);
return this.getTools();
}
destroy(): void {
this.unsubscribe?.();
this.unsubscribe = null;
this.tools = [];
}
}