Merge 7c5f265721
into d3a28edd77
commit
409543a1f5
@ -0,0 +1,34 @@
|
|||||||
|
# Multi MCP Execution Example
|
||||||
|
|
||||||
|
This example demonstrates using a list of MCP servers with an `Agent`.
|
||||||
|
|
||||||
|
Start the example servers in separate terminals:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python examples/tools/mcp_examples/servers/weather_server.py
|
||||||
|
python examples/tools/mcp_examples/servers/news_server.py
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
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 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)
|
||||||
|
|
||||||
|
```
|
@ -0,0 +1,12 @@
|
|||||||
|
from mcp.server.fastmcp import FastMCP
|
||||||
|
|
||||||
|
mcp = FastMCP("NewsServer")
|
||||||
|
|
||||||
|
mcp.settings.port = 9001
|
||||||
|
|
||||||
|
@mcp.tool(name="get_news", description="Return simple news headline")
|
||||||
|
def get_news(topic: str) -> str:
|
||||||
|
return f"Latest {topic} news headline"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
mcp.run(transport="sse")
|
@ -0,0 +1,12 @@
|
|||||||
|
from mcp.server.fastmcp import FastMCP
|
||||||
|
|
||||||
|
mcp = FastMCP("WeatherServer")
|
||||||
|
|
||||||
|
mcp.settings.port = 8000
|
||||||
|
|
||||||
|
@mcp.tool(name="get_weather", description="Return simple weather info")
|
||||||
|
def get_weather(city: str) -> str:
|
||||||
|
return f"Weather in {city}: Sunny 22°C"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
mcp.run(transport="sse")
|
@ -0,0 +1,73 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
from swarms import Agent
|
||||||
|
import io
|
||||||
|
import sys
|
||||||
|
from contextlib import redirect_stdout
|
||||||
|
|
||||||
|
print("\n=== Testing Multiple MCP Tool Execution ===\n")
|
||||||
|
|
||||||
|
# Configure multiple MCP URLs
|
||||||
|
os.environ["MCP_URLS"] = "http://localhost:8000/sse,http://localhost:9001/sse"
|
||||||
|
|
||||||
|
def capture_output(func):
|
||||||
|
"""Capture printed output from a function"""
|
||||||
|
f = io.StringIO()
|
||||||
|
with redirect_stdout(f):
|
||||||
|
func()
|
||||||
|
return f.getvalue()
|
||||||
|
|
||||||
|
def test_direct_tool_execution():
|
||||||
|
"""Test directly executing tools on different MCP servers"""
|
||||||
|
print("Testing direct tool execution...\n")
|
||||||
|
|
||||||
|
agent = Agent(
|
||||||
|
agent_name="Multi-MCP-Agent",
|
||||||
|
model_name="gpt-4o-mini",
|
||||||
|
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))
|
||||||
|
)
|
||||||
|
|
||||||
|
# Extract and display results
|
||||||
|
print("\nResults from MCP tools:")
|
||||||
|
print(output)
|
||||||
|
|
||||||
|
print("\nTest complete - Multiple MCP execution successful!")
|
||||||
|
|
||||||
|
def test_agent_configuration():
|
||||||
|
"""Test different ways to configure agents with multiple MCP URLs"""
|
||||||
|
print("\n=== Testing Agent MCP Configuration Methods ===\n")
|
||||||
|
|
||||||
|
# Method 1: Configure via environment variables (already set above)
|
||||||
|
agent1 = Agent(agent_name="Env-Config-Agent")
|
||||||
|
print(f"Agent1 MCP URLs (from env): {agent1.mcp_urls}")
|
||||||
|
|
||||||
|
# Method 2: Configure via direct parameter
|
||||||
|
agent2 = Agent(
|
||||||
|
agent_name="Direct-Config-Agent",
|
||||||
|
mcp_urls=["http://localhost:8000/sse", "http://localhost:9001/sse"]
|
||||||
|
)
|
||||||
|
print(f"Agent2 MCP URLs (from param): {agent2.mcp_urls}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_agent_configuration()
|
||||||
|
test_direct_tool_execution()
|
@ -0,0 +1,50 @@
|
|||||||
|
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)
|
||||||
|
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, json.dumps(payloads))
|
||||||
|
|
||||||
|
assert called == [
|
||||||
|
("tool1", "http://server1", {"a": 1}),
|
||||||
|
("tool2", "http://server2", {}),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_json_from_response():
|
||||||
|
payloads = [
|
||||||
|
{"function_name": "foo", "server_url": "http://x", "payload": {"x": 1}}
|
||||||
|
]
|
||||||
|
text = "Random text" + json.dumps(payloads) + " end"
|
||||||
|
result = extract_json_from_response(text)
|
||||||
|
assert result == payloads
|
||||||
|
|
Loading…
Reference in new issue