parent
bc50b15ead
commit
5e91a4b08c
@ -0,0 +1,25 @@
|
|||||||
|
# Multi MCP Execution Example
|
||||||
|
|
||||||
|
This example demonstrates using a list of MCP servers with an `Agent`.
|
||||||
|
|
||||||
|
```python
|
||||||
|
import os
|
||||||
|
from swarms import Agent
|
||||||
|
|
||||||
|
# Configure multiple MCP URLs
|
||||||
|
os.environ["MCP_URLS"] = "http://localhost:8000/sse,http://localhost:9001/sse"
|
||||||
|
|
||||||
|
agent = Agent(
|
||||||
|
agent_name="Multi-MCP-Agent",
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
max_loops=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Example payloads produced by your model
|
||||||
|
payloads = [
|
||||||
|
{"function_name": "get_weather", "server_url": "http://localhost:8000/sse", "payload": {"city": "London"}},
|
||||||
|
{"function_name": "get_news", "server_url": "http://localhost:9001/sse", "payload": {"topic": "ai"}},
|
||||||
|
]
|
||||||
|
|
||||||
|
agent.handle_multiple_mcp_tools(agent.mcp_urls, payloads)
|
||||||
|
```
|
@ -0,0 +1,38 @@
|
|||||||
|
import asyncio
|
||||||
|
from swarms.structs.agent import Agent
|
||||||
|
from swarms.structs.agent import execute_mcp_call
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
|
||||||
|
def test_handle_multiple_mcp_tools():
|
||||||
|
agent = Agent(agent_name="Test", llm=None, max_loops=1)
|
||||||
|
urls = ["http://server1", "http://server2"]
|
||||||
|
payloads = [
|
||||||
|
{
|
||||||
|
"function_name": "tool1",
|
||||||
|
"server_url": "http://server1",
|
||||||
|
"payload": {"a": 1},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"function_name": "tool2",
|
||||||
|
"server_url": "http://server2",
|
||||||
|
"payload": {},
|
||||||
|
},
|
||||||
|
]
|
||||||
|
called = []
|
||||||
|
|
||||||
|
async def fake_exec(
|
||||||
|
function_name, server_url, payload, *args, **kwargs
|
||||||
|
):
|
||||||
|
called.append((function_name, server_url, payload))
|
||||||
|
return "ok"
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"swarms.structs.agent.execute_mcp_call", side_effect=fake_exec
|
||||||
|
):
|
||||||
|
agent.handle_multiple_mcp_tools(urls, payloads)
|
||||||
|
|
||||||
|
assert called == [
|
||||||
|
("tool1", "http://server1", {"a": 1}),
|
||||||
|
("tool2", "http://server2", {}),
|
||||||
|
]
|
Loading…
Reference in new issue