Skip to content

< all problems19 · Level 06, MCP

Serve a Tool Call over MCP

medium · implement · MCP

Implement call_tool(request) for MCP tools/call. The params look like {"name": "add", "arguments": {"a": 2, "b": 3}}; the tools live in HANDLERS.

  1. Run the tool and return its value, stringified, under result with the id echoed: {"content": [{"type": "text", "text": "5"}]}.
  2. Unknown tool name. A bad request, not a server fault: error with code -32602, "Invalid params".
  3. A tool that raises. No crash, no leaked traceback. Return a normal result with isError: true and the exception (type and message) in the text block, so the model sees the failure. A successful call has no isError key.
  4. Missing arguments. The key may be absent for a no-argument tool like ping.

The catch: a tool the server does not have is a protocol error; a tool that exists and fails is a successful exchange reporting an application error.