Configure and manage multiple MCP servers for complex workflows
This guide shows you how to configure and use multiple MCP servers simultaneously with mcp_use, enabling complex workflows that span different domains.
import asynciofrom langchain_openai import ChatOpenAIfrom mcp_use import MCPAgent, MCPClientasync def main(): # Load multi-server configuration client = MCPClient.from_config_file("multi_server_config.json") # Create agent (all servers will be connected) llm = ChatOpenAI(model="gpt-4") agent = MCPAgent(llm=llm, client=client) # Agent has access to tools from all servers result = await agent.run( "Search for Python tutorials online, save the best ones to a file, " "then create a database table to track my learning progress" ) print(result)if __name__ == "__main__": asyncio.run(main())
Enable the server manager for more efficient resource usage:
Copy
Ask AI
import asynciofrom langchain_openai import ChatOpenAIfrom mcp_use import MCPAgent, MCPClientasync def main(): client = MCPClient.from_config_file("multi_server_config.json") # Enable server manager for dynamic server selection agent = MCPAgent( llm=llm, client=client, use_server_manager=True, # Only connects to servers as needed max_steps=30 ) # The agent will automatically choose appropriate servers result = await agent.run( "Research the latest AI papers, summarize them in a markdown file, " "and commit the file to my research repository on GitHub" ) print(result)if __name__ == "__main__": asyncio.run(main())
result = await agent.run( "Scrape product data from example-store.com, " "clean and analyze it with pandas, " "then save the results as CSV and Excel files")
result = await agent.run( "Create a new Python function to calculate fibonacci numbers, " "write unit tests for it, run the tests, " "and if they pass, commit the changes to the current git branch")
The server manager provides several performance benefits:
Copy
Ask AI
# Without server manager - all servers start immediatelyagent = MCPAgent(llm=llm, client=client, use_server_manager=False)# Result: All 5 servers start, consuming resources# With server manager - servers start only when neededagent = MCPAgent(llm=llm, client=client, use_server_manager=True)# Result: Only the required servers start for each task
Copy
Ask AI
# Without server manager - all servers start immediatelyagent = MCPAgent(llm=llm, client=client, use_server_manager=False)# Result: All 5 servers start, consuming resources# With server manager - servers start only when neededagent = MCPAgent(llm=llm, client=client, use_server_manager=True)# Result: Only the required servers start for each task
# If one server fails, others continue workingagent = MCPAgent( llm=llm, client=client, use_server_manager=True, ignore_server_errors=True # Continue on server failures)