|
|
@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
### Why you’re getting the `ImportError`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`fastmcp` ≥ 2.0 renamed **`FastClientSession`** → **`Client`** and moved it into the top‑level package.
|
|
|
|
|
|
|
|
So
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
from fastmcp import FastClientSession # 🚫 no longer exists
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
now raises the exact error you see.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Quick fix ‑ patch the import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Open **`swarms/tools/mcp_integration.py`** and replace the first import block:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
# OLD
|
|
|
|
|
|
|
|
from fastmcp import FastClientSession as ClientSession
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
# NEW – works with fastmcp ≥ 2.0
|
|
|
|
|
|
|
|
from fastmcp import Client as ClientSession
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*(If you need to stay on fastmcp 1.x, install the old version instead:
|
|
|
|
|
|
|
|
`pip install "fastmcp<2"` and keep your original import.)*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Minimal working smoke‑test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
# examples/mcp_example/mcp_client.py
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from swarms import Agent
|
|
|
|
|
|
|
|
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
|
|
|
|
|
|
|
|
from swarms.tools.mcp_integration import MCPServerSseParams
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 1. describe the remote MCP server you want to hit
|
|
|
|
|
|
|
|
server = MCPServerSseParams(
|
|
|
|
|
|
|
|
url="http://localhost:6274", # or any public MCP SSE endpoint
|
|
|
|
|
|
|
|
headers={"Content-Type": "application/json"}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 2. create a Swarms agent and hand it the server list
|
|
|
|
|
|
|
|
math_agent = Agent(
|
|
|
|
|
|
|
|
agent_name="Math‑MCP‑Tester",
|
|
|
|
|
|
|
|
system_prompt=MATH_AGENT_PROMPT,
|
|
|
|
|
|
|
|
max_loops=1,
|
|
|
|
|
|
|
|
mcp_servers=[server], # <‑‑ now recognised
|
|
|
|
|
|
|
|
output_type="final",
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 3. run a call that should trigger the server’s “add” tool
|
|
|
|
|
|
|
|
result = math_agent.run("Use the add tool to add 2 and 2")
|
|
|
|
|
|
|
|
print("🟢 RESULT:", result)
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Extra housekeeping
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1. **Expose `mcp_servers`**
|
|
|
|
|
|
|
|
In your current `Agent.__init__` the assignment line is commented out:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
# self.mcp_servers = mcp_servers # <- uncomment
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Make sure the attribute exists before it is accessed in `handle_tool_init()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. **Python < 3.11 + `NotRequired`**
|
|
|
|
|
|
|
|
Keep
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
from typing_extensions import NotRequired
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
(You already changed this – good.)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. **Install / upgrade fastmcp**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
|
|
|
pip install -U fastmcp # latest 2.x
|
|
|
|
|
|
|
|
# or, for the old API:
|
|
|
|
|
|
|
|
# pip install "fastmcp<2"
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### What if you don’t have your own server yet?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Until you spin up the Browserbase example (or any other MCP server), you can test against the **FastMCP public demo**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
|
|
|
demo = MCPServerSseParams(url="https://demo.fastmcp.io/sse")
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
It exposes trivial arithmetic tools (`add`, `multiply`, …) that match the smoke‑test above.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
That’s all you need to get your Swarms agent talking to an existing MCP server without touching server‑side code. 🚀
|