promptdojo_

Writing a tiny MCP server — registries, dispatch, and the response shape — step 8 of 9

Write call_tool(name, args, registry) — the full dispatcher. It must:

  1. If name is not in registry, return the MCP error envelope with text unknown tool: <name>.
  2. Otherwise, loop over tool["inputSchema"]["required"] and return an error envelope with text missing required arg: <key> for the first missing required arg.
  3. If everything validates, call tool["handler"](args) and wrap the return value as the MCP success envelope.

All envelopes have the shape:

{"isError": bool, "content": [{"type": "text", "text": str}]}

Expected output:

ok hello alex
err missing required arg: name
err unknown tool: ghost

full-screen editor opens — close anytime to keep reading.