MCP servers and clients · Chapter 8 of 16

A client in depth

By the end of this chapter you can write MCP clients that hold up in real use: start a server with exactly the environment and folder it needs, read everything it says about itself, tell a failed tool from a failed request, give up on a slow one, and use several servers at once. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Clients and production
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.

Why you need this

So far every client in this lab started a server from the same folder, asked one question and printed the answer. Rajat's assistant is not that simple. It talks to the stock server and the orders server at once, and the accounts team's sales server only starts when it is given a database path in an environment variable and the right working folder. Last week the assistant hung for a minute on a slow stock recount and then crashed, and nobody could tell whether the tool had failed or the request had.

Simran says the client is half of every MCP integration, and the half that hosts like Claude Desktop or an IDE quietly get right for you. In your own host, it is your code.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    inventory.py: everything a server offers

    In inventory.py, take a server file from the command line (python3 inventory.py stock_server.py), start it over stdio with the SDK's Client and print:

    Server: <name> (protocol <version>);

    Tools:, then one line per tool, two spaces and name(parameters), the parameters from its input schema in order with ? after one that is not required, like low_stock(store_id, limit?);

    Resources:, then each resource's URI; Templates:, then each template's URI template; Prompts:, then each prompt as name(arguments), ? after an optional one.

    Every item has two spaces in front, and a section with nothing in it has the one line none. It must work for stock_server.py and orders_server.py.

  2. Task 2

    safe_call.py: two kinds of failure

    In safe_call.py, write an exception class ToolFailed and async def call(client, name, args): it calls the tool and returns the result's structured content; when the result is an error it raises ToolFailed with the result's text (its text parts, joined by newlines); a protocol error (MCPError) goes through unchanged.

    When run, it connects to stock_server.py with a read timeout of 2 seconds and uses call() for stock_level with BB04 and P020, stock_level with BB99 and P020, and the slow stock_report with BB04, printing ok: <result>, tool failed: <message> or protocol error <code>: <message>, one line each.

  3. Task 3

    two_servers.py: route each call

    In two_servers.py, start stock_server.py and orders_server.py over stdio and keep both connected. Build an index from each tool's name to the client that lists it, then make the five calls in the starter, each through its own server, printing one line per call:

    <tool> (<server name>): <the result's structured content, as json.dumps writes it>;

    for a tool that no server has, <tool>: no server offers it.

  4. Task 4 · interview question

    Interview: fix_me.py, a server that will not start

    fix_me.py starts sales_server.py (given, do not change it) and should print the server's text for BB06, BB19 and BB33 on 2026-10-20, one line each, like BB06 Jagatpura sold ₹4,167.00 on 2026-10-20, GST ₹299.63.

    The server needs the environment variable BAZAAR_DB set to /workspace/data/bazaar.db and must run in the folder /workspace/data. Fix the client's server parameters and the way it reads the result, changing as little as you can.

What you will learn

  • StdioServerParameters: command, args, env and cwd, and what a server gets when you pass nothing.
  • What you learn on connecting: protocol version, server info, capabilities, instructions, and listing everything.
  • CallToolResult versus MCPError: a tool that failed versus a request that failed, and read_timeout_seconds.
  • Several servers at once, and calling the right one.

More in Clients and production

Build "A client in depth" yourself

Bazaar Bhai, a chain of 40 kirana stores in Jaipur, exposes its stock and orders once, to every AI tool. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of MCP servers and clients are free. This one comes with the all labs plan.