Welcome back! In the previous lesson, you learned how to make your agent more efficient by enabling tool caching when connecting to a single MCP server. This allowed your agent to quickly access the list of available tools, reducing latency and improving the user experience, especially during multi-turn conversations.
Now, let’s take the next step in building more capable and autonomous agents. In real-world scenarios, you often need your agent to handle tasks that go beyond the scope of a single MCP server. For example, imagine an agent that not only manages a shopping list but can also place orders for products from suppliers. To achieve this, you need to connect your agent to multiple MCP servers, each providing a different set of tools.
In this lesson, you will learn how to set up and coordinate multiple MCP servers, allowing your agent to solve more complex tasks by using tools from different services — all within a single conversation.
Before we dive into multi-server integration, let’s quickly revisit how an agent connects to a single MCP server. In the previous lesson, you saw that an MCP server is a process that exposes a set of tools — actions the agent can use to help answer user queries. The agent connects to the MCP server, fetches the list of available tools (optionally caching them), and then uses these tools as needed to fulfill user requests.
This single-server setup works well for focused tasks, such as managing a shopping list. However, as your agent’s responsibilities grow, you may want it to interact with several different services. For example, one MCP server might handle shopping list management, while another could handle product ordering. By connecting to multiple MCP servers, you can give your agent access to a much broader set of capabilities, making it more useful and autonomous.
To connect your agent to more than one MCP server, you can use the OpenAI Agents SDK’s MCPServerStdio
class for local subprocess servers, the MCPServerSse
class for remote HTTP-based servers, or even combine both types in a single agent. Each server instance should be configured individually, and you can assign a unique name
to each for easier identification during debugging or logging.
For example, suppose you have two local MCP servers: one for managing a shopping list (list/mcp_server.py
) and another for ordering products (products/mcp_server.py
). You can launch both servers like this:
Here, each MCPServerStdio
instance is configured with the command
to run the server, a descriptive name
, and the cache_tools_list
parameter set to True
for efficiency.
Once you have your MCP servers running, you can create an agent that uses tools from both servers. The key is to pass a list of MCP server instances to the agent’s mcp_servers
property. The agent will automatically aggregate the tools from all connected servers, making them available for use in any order or combination.
Here’s how you can set up the agent:
In this example, the agent receives both the list_server
and order_server
instances. The instructions
are crafted to guide the agent in using both sets of tools to manage the shopping list and place orders, all without needing to ask the user for confirmation. This setup allows the agent to coordinate actions across different services, making it much more powerful and autonomous.
To run the agent, you can use the Runner.run
method and provide an input prompt:
A typical output might look like this:
With this setup, the agent can check the shopping list using tools from one MCP server, then use tools from another MCP server to order each item from the correct suppliers. This allows the agent to coordinate actions across both servers in a single workflow, completing the entire process autonomously.
In this lesson, you learned how to connect an agent to multiple MCP servers, giving it access to a wider range of tools and capabilities. By configuring and launching each server separately and passing them to the agent, you enable your agent to coordinate actions across different services. This approach allows your agent to solve more complex, real-world tasks — such as managing a shopping list and ordering products — without needing user confirmation for every step.
You are now ready to practice these skills in hands-on exercises. Great job mastering this advanced integration technique!
