Lesson 07.05 · Phase 07 · Model Context Protocol

Build an MCP client

The client is where the protocol meets the agent loop. It has to find out what the server speaks, keep requests and responses straight, give up on calls that hang, and turn every outcome (success, tool error, protocol error, timeout) into something the model can read.

Learning objectives

  • Build a stdio client with request ids, a reader thread, per-request timeouts and cancellation.
  • Connect to current and older servers: probe with server/discover, choose a common version, and fall back to the legacy initialize handshake.
  • List tools across pages and translate them into a model's tool format.
  • Run an agent loop over MCP where tool errors, protocol errors and timeouts all come back to the model as observations.

Lesson 3 built a server that speaks exactly one revision. The hosts that connect to it don't get to choose: a user's config file can list a server written last week and one written two years ago, side by side. The client can't ask the user which protocol each one speaks. It has to find out.

Loading the full lesson…