Context7 Platform -- Up-to-date docs for AI agents
feat: add more renamed variables to mcp (#2599)
Enes Gulese8b5a6122fff
diff --git a/.changeset/accept-hallucinated-tool-args.md b/.changeset/accept-hallucinated-tool-args.mdindex d0439284..374476c9 100644--- a/.changeset/accept-hallucinated-tool-args.md+++ b/.changeset/accept-hallucinated-tool-args.md@@ -2,4 +2,4 @@"@upstash/context7-mcp": patch----Accept hallucinated argument names on `tools/call` requests by rewriting them to the canonical names before validation. `context7CompatibleLibraryID` is mapped to `libraryId` and `userQuery` is mapped to `query` on incoming MCP messages. Some LLM clients produce these alternative names — likely echoing phrasing from each tool's description — and previously triggered `Invalid input: expected string, received undefined` errors. Tool input schemas published via `tools/list` are unchanged: canonical names remain the documented required fields, the rewrite is purely a server-side compatibility shim that runs only on `tools/call` and only when the canonical key is absent.+Accept hallucinated argument names on `tools/call` requests by rewriting them to the canonical names before validation. `userQuery` and `question` are mapped to `query` on either tool; on `query-docs`, `context7CompatibleLibraryID`, `libraryID`, and `libraryName` are mapped to `libraryId`. Some LLM clients produce these alternative names — likely echoing phrasing from each tool's description — and previously triggered `Invalid input: expected string, received undefined` errors. `libraryName` is only rewritten on `query-docs` calls because it is the canonical arg for `resolve-library-id`. Tool input schemas published via `tools/list` are unchanged: canonical names remain the documented required fields, the rewrite is purely a server-side compatibility shim that runs only on `tools/call` and only when the canonical key is absent.diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.tsindex 9dfd1f1f..125be614 100644--- a/packages/mcp/src/index.ts+++ b/packages/mcp/src/index.ts@@ -282,25 +282,56 @@ Do not call this tool more than 3 times per question.`,return server;}-// Rewrite hallucinated argument names on incoming `tools/call` messages so-// they validate against the canonical tool schema. Install BEFORE-// `server.connect(transport)`: the SDK's `Protocol.connect()` captures the-// existing `onmessage` and chains its dispatch handler over it, so our hook-// runs first on every incoming JSON-RPC message.+// Map of canonical arg name -> hallucinated aliases that should be rewritten+// to it. LLM clients often echo phrasing from tool descriptions instead of+// the literal schema keys, which trips Zod validation before the tool runs.+type AliasMap = Record<string, readonly string[]>;++const GLOBAL_ALIASES: AliasMap = {+ query: ["userQuery", "question"],+};++// Tool-scoped aliases, for keys that are canonical on one tool but a+// hallucination on another (e.g. `libraryName` is canonical for+// `resolve-library-id`, so we only rewrite it on `query-docs` calls).+const TOOL_ALIASES: Record<string, AliasMap> = {+ "query-docs": {+ libraryId: ["context7CompatibleLibraryID", "libraryID", "libraryName"],+ },+};++function applyAliases(args: Record<string, unknown>, aliases: AliasMap): void {+ for (const [canonical, alternatives] of Object.entries(aliases)) {+ if (canonical in args) continue;+ for (const alt of alternatives) {+ if (alt in args) {+ args[canonical] = args[alt];+ delete args[alt];+ break;+ }+ }+ }+}++// Install BEFORE `server.connect(transport)`: the SDK's `Protocol.connect()`+// captures the existing `onmessage` and chains its dispatch handler over it,+// so our hook runs first on every incoming JSON-RPC message.function installTransportArgAliasing(transport: Transport): void {transport.onmessage = (message) => {- const msg = message as { method?: string; params?: { arguments?: unknown } };+ const msg = message as {+ method?: string;+ params?: { name?: string; arguments?: unknown };+ };if (msg.method !== "tools/call") return;const args = msg.params?.arguments;if (!args || typeof args !== "object") return;const argsRecord = args as Record<string, unknown>;- if ("context7CompatibleLibraryID" in argsRecord && !("libraryId" in argsRecord)) {- argsRecord.libraryId = argsRecord.context7CompatibleLibraryID;- delete argsRecord.context7CompatibleLibraryID;- }- if ("userQuery" in argsRecord && !("query" in argsRecord)) {- argsRecord.query = argsRecord.userQuery;- delete argsRecord.userQuery;++ applyAliases(argsRecord, GLOBAL_ALIASES);++ const toolName = msg.params?.name;+ if (toolName && toolName in TOOL_ALIASES) {+ applyAliases(argsRecord, TOOL_ALIASES[toolName]);}};}