Free · Daily brief + weekly playbookDaily 60-sec AI Voice Brief + Weekly Production AI Agent PlaybookGet the brief →
ReleaseCode / SDK · ActionableMachine Learning Mastery→ original

How to Write an MCP Client with Python SDK 2.0 and Let Claude Call Tools

MCP is the protocol that lets a model call external tools, and a Python client translates between Claude and the server. Machine Learning Mastery built such a client on SDK 2.0: it launches a local weather server over stdio and passes its tools to Claude. Older tutorials were written for the first version's API, and pip install mcp now installs 2.x. Check your SDK version before copying any code.

AI-processed from Machine Learning Mastery; edited by Hamidun News
How to Write an MCP Client with Python SDK 2.0 and Let Claude Call Tools
Source: Machine Learning Mastery. Collage: Hamidun News.
Listen to article
Article Fact Sheet & Key Figures
ParameterDetails
Primary sourceMachine Learning Mastery ↗
Editor-in-Chief
The example server exposes two toolsweather by city and temperature conversion
Key metric #2Any stray print() to stdout breaks a stdio server, so write logs to stderr
Key metric #3Tool errors are returned to the client in a form the model can read

Imagine you want Claude to check the weather or run calculations through your own service. The model cannot reach other programs directly; it needs an intermediary. In Anthropic's ecosystem, that intermediary is the MCP (Model Context Protocol), and the program that handles it is called an MCP client. Machine Learning Mastery has published a step-by-step guide on writing such a client in Python with the official SDK version 2.0, connecting it to a local server over stdio (message exchange through the standard input and output of a running process), and letting Claude call the server's tools. For practitioners, it is also a warning: many guides found online no longer match the current API.

Who does what in MCP

MCP has three roles. The host is the AI application itself. The server provides tools, resources, or prompts. The client lives inside the host and maintains one connection to one server. Its job is simple: connect, ask the server which tools exist, and call the right one when the model asks for it.

An important detail: the model never talks to the server directly. The client translates in both directions, and that translation is the code you write yourself.

You will need Python 3.10 or later, the uv package manager, an Anthropic API key, and MCP SDK version 2.0.0 or newer. The version matters. SDK 2.0.0 was released to support the specification revision dated July 28, 2026 (2026-07-28), in which the protocol became stateless, meaning it is not tied to session state. A plain pip install mcp now installs 2.x. Code from older tutorials that uses ClientSession and session.initialize() will not match the installed API. All the differences are listed in the migration guide.

The author advises storing the API key in a .env file and adding that file to .gitignore so it does not end up in the repository. python-dotenv loads it.

A test server and the first calls

The client needs something to talk to, so the author writes a server using the MCPServer class. SDK 2.0 calls this what version one named FastMCP. The server exposes two tools: get_weather returns the temperature and wind speed for a city through the free Open-Meteo service, which requires no key, and convert_temperature converts values between Celsius, Fahrenheit, and kelvin. Two tools make it possible to see how Claude chains calls between them.

The @mcp.tool() decorator turns a function's type annotations and docstring into a name, a description, and an input schema. The client reads this schema and presents it to the model so the model can decide how to call the tool. You do not need to install the httpx2 HTTP client separately; the SDK itself is built on it.

There is a trap here. Under the stdio transport, the server reads stdin and writes JSON-RPC to stdout, and anything else in that stream breaks it. A single forgotten print() is enough to crash the server. That is why logs go to stderr.

The client's first step is to check the connection without any model involved. The async with Client(stdio_client(params)) block launches the server and connects to it, and on exit it stops both. You do not need to close the connection manually. The author sets the launch parameters with sys.executable rather than the string "python", which guarantees the server runs in the same virtual environment as the client. The explore.py script prints both tools along with their JSON Schema definitions.

The second step is to call a tool directly, again without a model, using call_tool. To parse the result, the author adds a helper function that joins the text blocks of the response into a single string. If a tool raises an exception, for example when convert_temperature receives an unknown unit, the error is returned to the client as a tool error, which the model can read and understand.

What comes next: the bridge to Claude

In the final part, the author shows what JSON-RPC messages look like under the hood in the 2026-07-28 revision. He then converts MCP tool descriptions into Claude's tool format and writes a loop that passes Claude's tool calls to the server. The loop handles chains (first weather, then temperature conversion) and tool errors.

The practical takeaway for anyone building agents: check your SDK version before copying code from third-party guides, keep the stdout of a stdio server clean, and return tool errors to the model so it has a chance to correct itself. The example is deliberately educational, using weather and degrees, but the same pattern works for any of your internal services.

Story

Key Questions Answered

Who does what in MCP?

MCP has three roles. The host is the AI application itself. The server provides tools, resources, or prompts. The client lives inside the host and maintains one connection to one server. Its job is simple: connect, ask the server which tools exist, and call the right one when the model asks for it. An important detail: the model never talks to the server directly. The client translates in both directions, and that translation is the code you write yourself.

What are the key details on A test server and the first calls?

The client needs something to talk to, so the author writes a server using the MCPServer class. SDK 2.0 calls this what version one named FastMCP. The server exposes two tools: get_weather returns the temperature and wind speed for a city through the free Open-Meteo service, which requires no key, and convert_temperature converts values between Celsius, Fahrenheit, and kelvin. Two tools make it possible to see how Claude chains calls between them.

What comes next: the bridge to Claude?

In the final part, the author shows what JSON-RPC messages look like under the hood in the 2026-07-28 revision. He then converts MCP tool descriptions into Claude's tool format and writes a loop that passes Claude's tool calls to the server. The loop handles chains (first weather, then temperature conversion) and tool errors.

ZK
Zhemal Khamidun — Head of AI at Alpina Digital · MIPT (academic supervisor of the Tech Entrepreneurship department). Building AI products since 2019. Hamidun News is a pet project that grew into a production aggregator with 6,000+ readers.
✦ Free · Waitlist · Hermes + DeepSeek

Free 2-Day Vibecoding & AI Agents Intensive

Day 1: the autonomous Hermes + DeepSeek AI agent — vibecoding and development on par with Claude Code / Codex / Gemini. Led by Jemal Hamidun, Head of AI at Alpina Digital and academic supervisor at MIPT. Right after you register with our bot Nika: the recording + 11 materials from the previous intensive.

Join the waitlist →Nomad AI Workspace
Jemal Hamidun

Editor-in-chief · Cite this analysis

Jemal Hamidun

Head of AI at Alpina Digital · Academic Supervisor of the Tech Entrepreneurship Department at MIPT

Head of AI & CPO of AlpinaGPT at Alpina Digital, Academic Lead of Tech Entrepreneurship at MIPT. Founder of Hamidun News and Vibe Academy.

Ready-to-paste citation for journalists, blogs and Telegram channels

Source: Hamidun News — How to Write an MCP Client with Python SDK 2.0 and Let Claude Call Tools https://news.hamidun.com/en/news/18707/how-to-write-an-mcp-client-with-python-sdk-2-0-and-let-claud

What do you think?
Loading comments…