Skip to content

< all problems51 · Level 06, MCP

Discover and Call Tools as an MCP Client

medium · implement · MCP

Write an MCP client: the piece that talks to both the model and the server.

Implement mcp_answer(llm, server, question). The server exposes one method, handle(request), taking a JSON-RPC dict and returning the response dict (or None for a notification). In order:

  1. initialize: the handshake.
  2. notifications/initialized: a notification, so no id, and no reply to wait for.
  3. tools/list: discover what the server offers.
  4. Give the model the discovered tools (names, descriptions, inputSchema) and ask which one to call with what arguments, as JSON {"name": ..., "arguments": {...}}.
  5. tools/call with what the model chose.
  6. Return the text of the first content block in the result.

If the model names a tool the server does not have, do not send it; return the model's raw reply so the failure is visible.

The server records every method in server.log, and the tests check the order: a client that calls tools before initializing is rejected by any real server. They also check the model is asked once.