Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions docs/examples/multi_mcp_execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ agent = Agent(
agent_name="Multi-MCP-Agent",
model_name="gpt-4o-mini",
max_loops=1,
mcp_urls=["http://localhost:8000/sse", "http://localhost:9001/sse"],
)

# Example JSON payloads produced by your model
response = json.dumps([
{"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, response)
# The agent will automatically dispatch requests to both MCP servers
agent.run("Call both MCP servers")

```
19 changes: 3 additions & 16 deletions docs/swarms/structs/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,24 +502,11 @@ agent = Agent(
agent_name="Multi-MCP-Agent",
model_name="gpt-4o-mini",
max_loops=1,
mcp_urls=["http://localhost:8000/sse", "http://localhost:9001/sse"],
)

# Example MCP payloads returned by your model
mcp_response = json.dumps([

{
"function_name": "get_price",
"server_url": "http://localhost:8000/sse",
"payload": {"symbol": "BTC"},
},
{
"function_name": "market_sentiment",
"server_url": "http://localhost:9001/sse",
"payload": {"symbol": "BTC"},
},
])

agent.handle_multiple_mcp_tools(agent.mcp_urls, mcp_response)
# Simply call run and the agent will reach out to both servers
agent.run("Call both MCP servers")

```

Expand Down
4 changes: 2 additions & 2 deletions docs/swarms/structs/agent_mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ payload in the following format:
]
```

Use `handle_multiple_mcp_tools` to execute each payload across the configured
servers.
When `mcp_urls` are provided, simply calling `agent.run()` will execute each
payload across the configured servers.

Example servers can be started with:

Expand Down
18 changes: 1 addition & 17 deletions examples/tools/mcp_examples/tests/test_multi_mcp_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,9 @@ def test_direct_tool_execution():
max_loops=1
)

# Create JSON payloads for multiple tools
payloads = [
{
"function_name": "get_weather",
"server_url": "http://localhost:8000/sse",
"payload": {"city": "Paris"}
},
{
"function_name": "get_news",
"server_url": "http://localhost:9001/sse",
"payload": {"topic": "science"}
}
]

# Execute the tools and capture output
print("Executing tools on multiple MCP servers...")
output = capture_output(
lambda: agent.handle_multiple_mcp_tools(agent.mcp_urls, json.dumps(payloads))
)
output = capture_output(lambda: agent.run("Call both MCP servers"))

# Extract and display results
print("\nResults from MCP tools:")
Expand Down
13 changes: 6 additions & 7 deletions tests/structs/test_multi_mcp.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import asyncio
import json
from swarms.structs.agent import Agent, extract_json_from_response

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)
def test_run_with_multiple_mcp_urls():
urls = ["http://server1", "http://server2"]
agent = Agent(agent_name="Test", llm=None, max_loops=1, mcp_urls=urls)

payloads = [
{
"function_name": "tool1",
Expand All @@ -23,16 +23,15 @@ def test_handle_multiple_mcp_tools():
]
called = []

async def fake_exec(
function_name, server_url, payload, *args, **kwargs
):
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, json.dumps(payloads))
agent.call_llm = lambda *args, **kwargs: json.dumps(payloads)
agent.run("test")

assert called == [
("tool1", "http://server1", {"a": 1}),
Expand Down
Loading