|
|
### What happened?
|
|
|
|
|
|
The Replit auto‑merge put **two very different things in one file**:
|
|
|
|
|
|
1. **FastMCP server code** (`add / multiply / divide` tools)
|
|
|
2. **Client/Agent code** that is supposed to *call* those tools
|
|
|
|
|
|
That hybrid script can’t work correctly:
|
|
|
|
|
|
* The moment `mcp.run()` starts the SSE server loop, the code **after it never executes**, so the `Agent` you created would never run.
|
|
|
* Even if it did, you’d be pointing the client back to *itself* on the same port—nothing useful.
|
|
|
|
|
|
---
|
|
|
|
|
|
### Correct separation
|
|
|
|
|
|
| File | Role | Runs in its own process |
|
|
|
|------|------|-------------------------|
|
|
|
| **`math_server.py`** | Hosts the tools (`@mcp.tool()`), listens on :8000 | ✅ |
|
|
|
| **`math_agent.py`** | Swarms `Agent` that talks to that server | ✅ |
|
|
|
|
|
|
---
|
|
|
|
|
|
#### 1 . `math_server.py` – *server‑only*
|
|
|
|
|
|
```python
|
|
|
# math_server.py
|
|
|
from fastmcp import FastMCP
|
|
|
import time
|
|
|
|
|
|
mcp = FastMCP("Math‑Mock‑Server")
|
|
|
|
|
|
@mcp.tool()
|
|
|
def add(a: int, b: int) -> int:
|
|
|
time.sleep(0.1)
|
|
|
return a + b
|
|
|
|
|
|
@mcp.tool()
|
|
|
def multiply(a: int, b: int) -> int:
|
|
|
time.sleep(0.1)
|
|
|
return a * b
|
|
|
|
|
|
@mcp.tool()
|
|
|
def divide(a: int, b: int) -> float:
|
|
|
if b == 0:
|
|
|
raise ValueError("Cannot divide by zero")
|
|
|
time.sleep(0.1)
|
|
|
return a / b
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
print("🚀 Math MCP server on :8000")
|
|
|
mcp.run(transport="sse", host="0.0.0.0", port=8000)
|
|
|
```
|
|
|
|
|
|
(`server.py` never imports `Agent` or `MCPServerSseParams`.)
|
|
|
|
|
|
---
|
|
|
|
|
|
#### 2 . `math_agent.py` – *client/agent‑only*
|
|
|
|
|
|
```python
|
|
|
# math_agent.py
|
|
|
from swarms import Agent
|
|
|
from swarms.tools.mcp_integration import MCPServerSseParams
|
|
|
|
|
|
MATH_SERVER = MCPServerSseParams(
|
|
|
url="http://127.0.0.1:8000", # no “/mcp” path required
|
|
|
headers={"Content-Type": "application/json"},
|
|
|
)
|
|
|
|
|
|
agent = Agent(
|
|
|
agent_name="Math-Processing-Agent",
|
|
|
agent_description="Specialised agent for basic math ops",
|
|
|
system_prompt=(
|
|
|
"You can use the add, multiply and divide MCP tools "
|
|
|
"to answer any arithmetic question."
|
|
|
),
|
|
|
max_loops=1,
|
|
|
mcp_servers=[MATH_SERVER],
|
|
|
model_name="gpt-4o-mini",
|
|
|
output_type="final",
|
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
answer = agent.run("Use the add tool to add 2 and 2")
|
|
|
print("Agent replied ➜", answer)
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
### How to run (locally or on Replit)
|
|
|
|
|
|
```bash
|
|
|
# Terminal 1 (or first Replit tab)
|
|
|
python math_server.py
|
|
|
|
|
|
# Terminal 2 (second tab)
|
|
|
python math_agent.py
|
|
|
```
|
|
|
|
|
|
Expected console:
|
|
|
|
|
|
```
|
|
|
🚀 Math MCP server on :8000
|
|
|
Agent replied ➜ 4
|
|
|
```
|
|
|
|
|
|
---
|
|
|
|
|
|
### Key points to remember
|
|
|
|
|
|
1. **Never start an Agent in the same process that is serving MCP**—keep client and server separate.
|
|
|
2. `MCPServerSseParams.url` points to the **root** of the FastMCP server, not `/mcp`.
|
|
|
3. The Swarms base `Agent` already knows how to wrap the JSON‑RPC call; you only supply `mcp_servers=[…]`.
|
|
|
|
|
|
Follow that structure, and your manager’s “initialize the agent like this” requirement is fully satisfied. |