Pydantic AI · Chapter 15 of 23
MCP
By the end of this chapter you can say what MCP is in one paragraph, write a small KhetBazaar MCP server with the mcp SDK, try its tools from Python, connect an agent to it with MCPToolset, give one agent the tools of two servers, and say when a tool belongs behind a server and when it stays a plain function. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Graphs
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.
Why you need this
The farmer assistant has tools now: prices, the forecast, inputs, claims. They live inside the assistant's own file. But the assistant is no longer KhetBazaar's only program that wants them. Ganesh's field app shows today's mandi price on his phone, Sneha's finance dashboard needs the same price to estimate payments, and next month the call centre gets its own agent. Copy the price tool into each and, the day the 2% fee changes, four programs give four different answers.
Kedar wants the tools written once, in one place, and used by every program that needs them, in any language, by any agent framework. That is what MCP is for.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
prices_server.py: KhetBazaar's prices as an MCP server
Write an MCP server with the mcp SDK: an MCPServer named khetbazaar-prices, with instructions (a sentence or two for the model about what the server is for), and two tools that call khetbazaar:
mandi_price(crop, mandi), today's price of a crop at a mandi;
price_trend(crop, mandi, days=7), the crop's modal price over the last few days.
Give each a docstring and type hints, and end the file with mcp.run() under if __name__ == "__main__":. khetbazaar raises ValueError for a crop or mandi it does not know: catch it and raise the SDK's ToolError with the same message. The check starts the server over stdio, like any client, and calls both tools, including mandi_price for cotton.
- Task 2
price_agent.py: an agent with the server's tools
Make an agent for openai-chat:lab-chat with the instructions You are KhetBazaar's farmer assistant. whose tools come from prices_server.py, over MCP: start it with a StdioTransport (sys.executable and the file's name) and give the agent an MCPToolset of it. Run it on the farmer's question exactly:
Pimpalgaon la draksha bhav kay ahe?
and print the run's output. Pass the server PYTHONPATH from os.environ in the transport's env. Do not import khetbazaar or write tools in this file.
- Task 3
two_servers.py: one agent, two servers
weather_server.py is Kedar's weather server (do not change it). Make ONE agent for openai-chat:lab-chat, with the instructions You are KhetBazaar's farmer assistant., and two MCP toolsets: prices_server.py and weather_server.py. Run it on two questions, in this order, and print each run's output on its own line:
garpit hoil ka Niphad madhe?
Nashik la tamatar bhav kay ahe?
- Task 4 · interview question
Interview: mcp_or_plain.py, MCP tools or plain tools
Make two agents for openai-chat:lab-chat, both with the instructions You are KhetBazaar's farmer assistant.: a plain one with one tool of its own, mandi_price(crop, mandi), written in this file with khetbazaar as in chapter 3, and no server; and an MCP one with the tools of prices_server.py. Ask both Lasalgaon la dalimb bhav kay ahe?, the plain agent first, and print two lines: plain: and its output, then mcp: and its output.
The interviewer will ask: the model saw the same tool both times, so when would you put KhetBazaar's tools behind an MCP server, and when keep them as plain functions?
What you will learn
- MCP in one paragraph: a server offers tools, a client calls them.
- A server with the mcp SDK: MCPServer, @mcp.tool(), ToolError.
- MCPToolset: a server's tools given to a Pydantic AI agent.
- The server's environment, and why it starts nearly empty.
- Several servers for one agent.
More in Graphs
Build "MCP" yourself
KhetBazaar, a Nashik agritech startup, answers farmers on WhatsApp from a bike. You build its farmer assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Pydantic AI are free. This one comes with the all labs plan.