Context7 Platform -- Up-to-date docs for AI agents
fix(mcp): rewrite hallucinated `tools/call` argument names (#2598)
Fahreddin Özcan187287c690a4
diff --git a/.changeset/accept-hallucinated-tool-args.md b/.changeset/accept-hallucinated-tool-args.mdnew file mode 100644index 00000000..d0439284--- /dev/null+++ b/.changeset/accept-hallucinated-tool-args.md@@ -0,0 +1,5 @@+---+"@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.diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.tsindex cdf8dbb8..9dfd1f1f 100644--- a/packages/mcp/src/index.ts+++ b/packages/mcp/src/index.ts@@ -2,6 +2,7 @@import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";+import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";import { z } from "zod";import { searchLibraries, fetchLibraryContext } from "./lib/api.js";import { ClientContext } from "./lib/encryption.js";@@ -281,6 +282,29 @@ 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.+function installTransportArgAliasing(transport: Transport): void {+ transport.onmessage = (message) => {+ const msg = message as { method?: string; params?: { 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;+ }+ };+}+async function main() {const transportType = TRANSPORT_TYPE;@@ -410,6 +434,7 @@ async function main() {});await requestContext.run(context, async () => {+ installTransportArgAliasing(transport);await server.connect(transport);await transport.handleRequest(req, res, req.body);});@@ -544,6 +569,7 @@ async function main() {}};+ installTransportArgAliasing(transport);await server.connect(transport);console.error(`Context7 Documentation MCP Server v${SERVER_VERSION} running on stdio`);