Skip to content

How to add multiple MCP servers in an Agent? #954

Open
@Mitty-ZH

Description

@Mitty-ZH

Please read this first

  • Have you read the docs?Agents SDK docs
  • Have you searched for related issues? Others may have had similar requests

Question

Hi, since I have a list of mcp_server urls, I wanna know how to add multi-MCPServerSse in an agent while the 'examples/mcp' just show single mcp_server.
Here's the code I handle for two servers, but for multi-servers, I don't think this is a elegant way.

async def run(mcp_servers: list[MCPServer]):
    agent = Agent(
        name="Assistant",
        instructions="Use the tools to answer the questions.",
        mcp_servers=mcp_servers,
        model_settings=ModelSettings(tool_choice="required"),
    )

async def main():
    server1 = MCPServerSse(
        name="xx",
        params={"url": "http://localhost:8000/sse"}
    )

    server2 = MCPServerSse(
        name="xx",
        params={"url": "http://localhost:8001/sse"},
        cache_tools_list=True
    )

    async with server1 as mcp_server_1, server2 as mcp_server_2:
        await run([mcp_server_1, mcp_server_2])

if __name__ == "__main__":
    asyncio.run(main())

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions