* Tools: x-mcp-header annotation tooling + -32602 unknown-tool rendering (#1632) Card 9/10 of the SDK-v2 + New Spec stack. Adds SEP-2243 `x-mcp-header` tooling to the Tools tab and adjusts result rendering for SDK v2's unknown-tool behavior, and bumps the SDK to 2.0.0-beta.5. - **`core/json/xMcpHeader.ts`**: a faithful port of the SDK's (non-public) `scanXMcpHeaderDeclarations` — validates every SEP-2243 constraint (primitive types incl. `number`, RFC 9110 token, case-insensitive uniqueness, statically-reachable `properties`-only chain) and derives the mirrored `Mcp-Param-{Name}` params for a kept tool. - **Excluded tools**: the SDK silently drops tools with invalid annotations from `tools/list`. InspectorClient now re-lists the RAW (unfiltered) list and scans it to recompute the excluded set with reasons — gated to the modern non-stdio era (matching the SDK's gate), recomputed on every aggregate refresh, surfaced via a new `excludedToolsChange` event + `getExcludedTools()` and `useInspectorClient().excludedTools`. The Tools sidebar lists them struck-through under an "Excluded (SEP-2243)" divider with the reason on hover. - **Mirrored args**: ToolDetailPanel shows a "Mirrored request headers" section listing each arg → `Mcp-Param-{Name}` (with the browser-omits note). - **-32602 rendering**: SDK v2 rejects an unknown-tool call with `-32602` instead of an `isError` result, which previously rendered nothing. New ToolCallErrorPanel renders a thrown tool-call error, with a targeted "Unknown Tool" hint for `-32602`. Adds unit + Storybook + integration coverage (a real modern server with an invalid-header tool fixture) for every path. `npm run ci`-equivalent checks green. Closes #1632 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * test-servers: x-mcp-header modern showcase config + preset (#1632) Register the `invalid_header_tool` fixture as a preset and add `xmcpheader-modern-http.json` — a modern HTTP showcase serving echo, a valid-header `get_weather`, and the invalid-header tool — so the Tools-tab x-mcp-header tooling (mirrored headers, excluded-tools surface, -32602 rendering) can be exercised manually. Documented in the README alongside the other per-SEP showcase configs. Smoke-tested end to end in the web client. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * docs: add #1632 proof screenshots to pr-screenshots End-to-end web-client verification of the x-mcp-header Tools tooling against the modern showcase server: excluded-tools surface + reason, mirrored-headers section, and the -32602 unknown-tool error panel. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * Respond to review: disambiguate -32602, doc/search nits (#1632) Addresses the review on PR #1735: 1. -32602 "Unknown Tool" mislabel: `-32602` is generic Invalid params, so a known tool called with bad arguments hit the same code and rendered under the definitive "Unknown Tool" heading. Replace `isUnknownToolError` with `classifyToolCallError(code, message)` → "unknown-tool" only when the message names it (not found / unknown tool / …), else "invalid-params" (new "Invalid Parameters" heading + schema hint), else "generic". 2. Double pagination walk: expand the `listAllTools` comment to document that the excluded-tools recompute is a deliberately un-cached second raw walk (doubles list round-trips on modern non-stdio, runs even on a cache-served aggregate) — accepted for a debugging tool, no-op on legacy/stdio. 3. Doc nit: `scanXMcpHeaderDeclarations` no longer claims to visit "$ref targets" — reworded to note `$ref` is never followed (refs resolve into the unreachable `$defs` body). 4. Search nit: the excluded-list filter now matches name AND title, matching the main tools filter. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * Tighten unknown-tool matcher to be tool-scoped (#1632 re-review nit) The re-review noted the -32602 disambiguation was symmetric: broad markers ("does not exist", "unrecognized") could inversely mislabel a genuine invalid-params message (e.g. "property 'region' does not exist") as "Unknown Tool". Scope the not-found/does-not-exist family to require the word "tool" in the same clause (matching the SDK's "Tool <name> not found"); keep "unknown tool" / "no such tool" as standalone unambiguous markers. Adds a tool-less-"does not exist" test asserting it classifies as invalid-params. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 * Add trigger_invalid_params showcase tool + Invalid Parameters screenshot (#1632) Adds a `trigger_invalid_params` spec-error trigger to the modern showcase server: the injector answers its `tools/call` with a real `-32602 Invalid params` JSON-RPC error whose message does NOT name an unknown tool. This lets the "Invalid Parameters" branch of the tool-call error panel (added in the review response) be exercised live, distinct from the unknown-tool `-32602`. - `SPEC_ERROR_TRIGGERS` gains the `trigger_invalid_params` entry (HTTP 200, in-band -32602) + preset registration. - Enable `injectSpecErrors` on `xmcpheader-modern-http.json` and add the tool. - README + pr-screenshots README document it; adds the live "Invalid Parameters" proof screenshot. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XNdjEPKLG637X8YmhDiEk5 --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
255 lines
9.2 KiB
TypeScript
255 lines
9.2 KiB
TypeScript
import { useState, useEffect, useCallback } from "react";
|
|
import type { InspectorClientProtocol } from "../mcp/inspectorClientProtocol.js";
|
|
import type { AppRendererClient } from "../mcp/inspectorClientProtocol.js";
|
|
import type { TypedEvent } from "../mcp/inspectorClientEventTarget.js";
|
|
import type { ConnectionStatus } from "../mcp/types.js";
|
|
import type {
|
|
ClientCapabilities,
|
|
ServerCapabilities,
|
|
Implementation,
|
|
ProtocolEra,
|
|
DiscoverResult,
|
|
} from "@modelcontextprotocol/client";
|
|
import type { ExcludedTool } from "../mcp/types.js";
|
|
|
|
// Module-scope frozen object so the `?? EMPTY_CLIENT_CAPABILITIES`
|
|
// fallback below doesn't return a fresh literal on every render —
|
|
// downstream `useMemo`/`useEffect` deps that key on `clientCapabilities`
|
|
// would otherwise invalidate every tick when no client is attached.
|
|
const EMPTY_CLIENT_CAPABILITIES: ClientCapabilities = Object.freeze({});
|
|
|
|
export interface UseInspectorClientResult {
|
|
status: ConnectionStatus;
|
|
capabilities?: ServerCapabilities;
|
|
clientCapabilities: ClientCapabilities;
|
|
serverInfo?: Implementation;
|
|
instructions?: string;
|
|
protocolVersion?: string;
|
|
/**
|
|
* Protocol era negotiated with the server (SEP §7.8): `"legacy"` for the
|
|
* 2025-11-25 initialize handshake, `"modern"` for the 2026-era sessionless
|
|
* model. Populated for every era once connected (a plain legacy connect
|
|
* reports `"legacy"`); undefined only when not connected. (#1626)
|
|
*/
|
|
protocolEra?: ProtocolEra;
|
|
/**
|
|
* The `server/discover` result on a probed/pinned connect — server identity,
|
|
* capabilities, and supported versions learned without an initialize
|
|
* handshake. Undefined on a legacy connect. (#1626)
|
|
*/
|
|
discoverResult?: DiscoverResult;
|
|
/**
|
|
* Tools the SDK excluded from `tools/list` for invalid `x-mcp-header`
|
|
* annotations (SEP-2243), each with its reason. Empty on legacy/stdio
|
|
* connections and before connect (#1632).
|
|
*/
|
|
excludedTools: ExcludedTool[];
|
|
/**
|
|
* Message from the most recent mid-session transport failure (the client's
|
|
* `error` event — stdio crash, SSE drop, HTTP 5xx). Stays set until the next
|
|
* connection attempt (`status` → `"connecting"`) clears it, so consumers can
|
|
* render it without subscribing to the event directly. Handshake failures do
|
|
* NOT populate this — they reject the `connect()` promise instead.
|
|
*/
|
|
lastError?: string;
|
|
appRendererClient: AppRendererClient | null;
|
|
connect: () => Promise<void>;
|
|
disconnect: () => Promise<void>;
|
|
}
|
|
|
|
/**
|
|
* React hook that subscribes to InspectorClient events and provides reactive
|
|
* connection state. Log lists (message / stderr / fetch) live in dedicated
|
|
* state managers consumed via useMessageLog / useStderrLog / useFetchRequestLog.
|
|
*
|
|
* Note: `appRendererClient` is read lazily from the client on every render
|
|
* and is NOT subscribed. It changes once at connect time and is not expected
|
|
* to change again during a session, so callers will see the current value
|
|
* on any rerender triggered by status / capabilities / serverInfo / instructions.
|
|
* If a future use case requires autonomous updates when the renderer attaches,
|
|
* add an `appRendererClientChange` event to `InspectorClientEventMap` and
|
|
* subscribe here.
|
|
*/
|
|
export function useInspectorClient(
|
|
inspectorClient: InspectorClientProtocol | null,
|
|
): UseInspectorClientResult {
|
|
const [status, setStatus] = useState<ConnectionStatus>(
|
|
inspectorClient?.getStatus() ?? "disconnected",
|
|
);
|
|
const [capabilities, setCapabilities] = useState<
|
|
ServerCapabilities | undefined
|
|
>(inspectorClient?.getCapabilities());
|
|
const [serverInfo, setServerInfo] = useState<Implementation | undefined>(
|
|
inspectorClient?.getServerInfo(),
|
|
);
|
|
const [instructions, setInstructions] = useState<string | undefined>(
|
|
inspectorClient?.getInstructions(),
|
|
);
|
|
const [protocolVersion, setProtocolVersion] = useState<string | undefined>(
|
|
inspectorClient?.getProtocolVersion(),
|
|
);
|
|
const [protocolEra, setProtocolEra] = useState<ProtocolEra | undefined>(
|
|
inspectorClient?.getProtocolEra(),
|
|
);
|
|
const [discoverResult, setDiscoverResult] = useState<
|
|
DiscoverResult | undefined
|
|
>(inspectorClient?.getDiscoverResult());
|
|
const [excludedTools, setExcludedTools] = useState<ExcludedTool[]>(
|
|
inspectorClient?.getExcludedTools() ?? [],
|
|
);
|
|
const [lastError, setLastError] = useState<string | undefined>(undefined);
|
|
|
|
useEffect(() => {
|
|
if (!inspectorClient) {
|
|
setStatus("disconnected");
|
|
setCapabilities(undefined);
|
|
setServerInfo(undefined);
|
|
setInstructions(undefined);
|
|
setProtocolVersion(undefined);
|
|
setProtocolEra(undefined);
|
|
setDiscoverResult(undefined);
|
|
setExcludedTools([]);
|
|
setLastError(undefined);
|
|
return;
|
|
}
|
|
|
|
setStatus(inspectorClient.getStatus());
|
|
setCapabilities(inspectorClient.getCapabilities());
|
|
setServerInfo(inspectorClient.getServerInfo());
|
|
setInstructions(inspectorClient.getInstructions());
|
|
setProtocolVersion(inspectorClient.getProtocolVersion());
|
|
setProtocolEra(inspectorClient.getProtocolEra());
|
|
setDiscoverResult(inspectorClient.getDiscoverResult());
|
|
setExcludedTools(inspectorClient.getExcludedTools());
|
|
setLastError(undefined);
|
|
|
|
const onStatusChange = (event: TypedEvent<"statusChange">) => {
|
|
setStatus(event.detail);
|
|
// A fresh connection attempt clears any stale error from the prior
|
|
// session so the UI doesn't keep showing why the last transport died.
|
|
if (event.detail === "connecting") {
|
|
setLastError(undefined);
|
|
}
|
|
};
|
|
const onError = (event: TypedEvent<"error">) => {
|
|
setLastError(event.detail.message);
|
|
};
|
|
const onCapabilitiesChange = (event: TypedEvent<"capabilitiesChange">) => {
|
|
setCapabilities(event.detail);
|
|
};
|
|
const onServerInfoChange = (event: TypedEvent<"serverInfoChange">) => {
|
|
setServerInfo(event.detail);
|
|
};
|
|
const onInstructionsChange = (event: TypedEvent<"instructionsChange">) => {
|
|
setInstructions(event.detail);
|
|
};
|
|
const onProtocolVersionChange = (
|
|
event: TypedEvent<"protocolVersionChange">,
|
|
) => {
|
|
setProtocolVersion(event.detail);
|
|
};
|
|
const onProtocolEraChange = (event: TypedEvent<"protocolEraChange">) => {
|
|
setProtocolEra(event.detail);
|
|
};
|
|
const onDiscoverResultChange = (
|
|
event: TypedEvent<"discoverResultChange">,
|
|
) => {
|
|
setDiscoverResult(event.detail);
|
|
};
|
|
const onExcludedToolsChange = (
|
|
event: TypedEvent<"excludedToolsChange">,
|
|
) => {
|
|
setExcludedTools(event.detail);
|
|
};
|
|
|
|
inspectorClient.addEventListener("statusChange", onStatusChange);
|
|
inspectorClient.addEventListener("error", onError);
|
|
inspectorClient.addEventListener(
|
|
"capabilitiesChange",
|
|
onCapabilitiesChange,
|
|
);
|
|
inspectorClient.addEventListener("serverInfoChange", onServerInfoChange);
|
|
inspectorClient.addEventListener(
|
|
"instructionsChange",
|
|
onInstructionsChange,
|
|
);
|
|
inspectorClient.addEventListener(
|
|
"protocolVersionChange",
|
|
onProtocolVersionChange,
|
|
);
|
|
inspectorClient.addEventListener("protocolEraChange", onProtocolEraChange);
|
|
inspectorClient.addEventListener(
|
|
"discoverResultChange",
|
|
onDiscoverResultChange,
|
|
);
|
|
inspectorClient.addEventListener(
|
|
"excludedToolsChange",
|
|
onExcludedToolsChange,
|
|
);
|
|
|
|
return () => {
|
|
inspectorClient.removeEventListener("statusChange", onStatusChange);
|
|
inspectorClient.removeEventListener("error", onError);
|
|
inspectorClient.removeEventListener(
|
|
"capabilitiesChange",
|
|
onCapabilitiesChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"serverInfoChange",
|
|
onServerInfoChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"instructionsChange",
|
|
onInstructionsChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"protocolVersionChange",
|
|
onProtocolVersionChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"protocolEraChange",
|
|
onProtocolEraChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"discoverResultChange",
|
|
onDiscoverResultChange,
|
|
);
|
|
inspectorClient.removeEventListener(
|
|
"excludedToolsChange",
|
|
onExcludedToolsChange,
|
|
);
|
|
};
|
|
}, [inspectorClient]);
|
|
|
|
const connect = useCallback(async () => {
|
|
if (!inspectorClient) return;
|
|
await inspectorClient.connect();
|
|
}, [inspectorClient]);
|
|
|
|
const disconnect = useCallback(async () => {
|
|
if (!inspectorClient) return;
|
|
await inspectorClient.disconnect();
|
|
}, [inspectorClient]);
|
|
|
|
return {
|
|
status,
|
|
capabilities,
|
|
// Read lazily on every render rather than subscribed: client capabilities
|
|
// are built once in InspectorClient's constructor (from `sample`, `elicit`,
|
|
// `roots`, `receiverTasks`) and never mutate during a session, so there's
|
|
// no event to subscribe to. The module-scope frozen empty object is the
|
|
// stable fallback when no client is attached.
|
|
clientCapabilities:
|
|
inspectorClient?.getClientCapabilities() ?? EMPTY_CLIENT_CAPABILITIES,
|
|
serverInfo,
|
|
instructions,
|
|
protocolVersion,
|
|
protocolEra,
|
|
discoverResult,
|
|
excludedTools,
|
|
lastError,
|
|
appRendererClient: inspectorClient?.getAppRendererClient() ?? null,
|
|
connect,
|
|
disconnect,
|
|
};
|
|
}
|