The idea
An MCP client starts or connects to servers, asks what tools and resources they offer, and passes those to the model. When the model calls a tool, the client forwards the call to the right server and returns the result.
Desktop chat apps and IDEs are MCP clients configured with a JSON file listing servers. In your own agent you use the SDK’s client session: initialise, list_tools, call_tool.
The client is also where you enforce safety: which servers are allowed, which tools need user approval, and how results are shown.
Writing a client lets your own agents use the whole ecosystem of MCP servers without custom integrations.
Words to know
- Client sessionA live connection to one MCP server.
Try it without code
Read a client config
- Open your desktop app’s MCP config file.
- Find the command, args and env for each server.
- Add your notes server from the previous lesson.
Use it in code
Connect to a stdio server, list its tools and call one.
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
tools = await session.list_tools()
print([t.name for t in tools.tools])
result = await session.call_tool("add_note", {"title": "idea", "body": "Try MCP"})
print(result.content[0].text)
asyncio.run(main())
# To use with a model: convert each tool's name, description and inputSchema
# into your provider's tool format, and route tool_use calls to session.call_tool.Make your agent loop load tools from an MCP server at startup instead of hard-coded tool definitions.
Common mistakes
- Auto-approving every tool from every server.
- Not closing sessions, leaving server processes running.