#28 AI agents and MCP 5.3 min
Build an MCP Client That Uses Many Servers in Python
I gave two MCP servers a tool called search each. Merge their tools by name, and one of them vanishes.
Count the unique tool names
Copy the code folder first. In a Python prompt build this list: shop__opening_hours, shop__service_price, shop__search, flights__search_flights, flights__book_flight, flights__search. Take the part of each name after the first double underscore, make a set of those parts and print its length. Predict the number first. We got 5 out of 6.
A hint
Use name.split('__', 1)[1] for each name.
Merge by name and lose a tool
Using the same six names, build a dictionary from the bare tool name to the server name, looping over the list in order. Print the length of the dictionary and which server the search entry belongs to. Predict both. We got 5 and flights. Write one sentence on which server's search tool silently disappeared.
A hint
Later keys overwrite earlier ones in a dictionary, so the first search was replaced.
Why split only at the first double underscore
In a Python prompt compare 'shop__my__tool'.split('__', 1) with 'shop__my__tool'.split('__'). Predict both results, then run them. We got ['shop', 'my__tool'] and ['shop', 'my', 'tool']. Explain which one hub.py needs in call and why.
A hint
The server name is the part before the first double underscore. The rest is the tool name.
Predict a seventh tool
In shop_server.py add a new tool called stock_level that takes an item name and returns a number, written like the other tools with a docstring. Predict what the first two lines printed by python compare.py will say about the number of tools and the unique names. Then run it with the packages and Ollama from the video and compare. This is a prediction task: we did not run the servers.
A hint
Think about whether search still appears twice. Do not expect the tool choice score to stay the same without checking.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
Two made up servers and six easy questions with tool names that explain themselves. Only the first tool the model chose was checked, not its arguments. The hub has no approval step, no timeouts and no call log.
Up next in AI agents and MCP
More in AI agents and MCP
- #01 Local AI Agent in Python: No API Key (Ollama)
- #12 Multi-Agent Patterns in Plain Python: Pipeline, Router, Loop
- #22 OpenAI Agents SDK vs LangGraph vs Plain Python
- #23 Agent Memory: What to Keep and What to Forget
- #24 Approval Gates for Risky Agent Actions
- #25 Tool Errors: Retries, Fallbacks and Made-Up Answers
