-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy path06_remote_agents.py
More file actions
70 lines (52 loc) · 1.93 KB
/
Copy path06_remote_agents.py
File metadata and controls
70 lines (52 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Example 6 — Drive agents running on a remote MCP server.
``agent_as_tool_server.py`` exposes a whole swarms ``Agent`` as a single MCP tool
called ``create_agent``. From the client side that is just another tool call, so
``MCPManager`` can spawn and run agents on another process or machine without any
of the raw protocol handling this used to require.
Run the server first:
python examples/mcp/servers/agent_as_tool_server.py
Then:
python examples/mcp/client/06_remote_agents.py
"""
import asyncio
from swarms.tools.mcp_manager import MCPManager
SERVER_URL = "http://localhost:8000/mcp"
def one_agent() -> None:
"""Create and run a single remote agent."""
manager = MCPManager(mcp_url=SERVER_URL)
result = manager.call_tool(
"create_agent",
{
"agent_name": "Research-Agent",
"system_prompt": "You are a concise research assistant.",
"model_name": "gpt-4o-mini",
"task": "Name the three largest moons of Jupiter.",
},
)
print(f"Research-Agent -> {result}")
async def several_agents() -> None:
"""Run several remote agents concurrently over the same connection."""
manager = MCPManager(mcp_url=SERVER_URL)
specs = [
{
"agent_name": "Finance-Agent",
"system_prompt": "You are a financial analyst. Be brief.",
"model_name": "gpt-4o-mini",
"task": "What is a P/E ratio?",
},
{
"agent_name": "Science-Agent",
"system_prompt": "You are a physicist. Be brief.",
"model_name": "gpt-4o-mini",
"task": "Why is the sky blue?",
},
]
results = await asyncio.gather(
*(manager.acall_tool("create_agent", spec) for spec in specs)
)
for spec, result in zip(specs, results):
print(f"{spec['agent_name']} -> {result}")
if __name__ == "__main__":
one_agent()
asyncio.run(several_agents())