fix(imports): correct fastmcp import to match library v2.0 changes

pull/819/head
DP37 3 months ago committed by ascender1729
parent 1a075caeb0
commit 8919dc6ba3

@ -0,0 +1,105 @@
### Why youre getting the `ImportError`
`fastmcp`  2.0 renamed **`FastClientSession`** → **`Client`** and moved it into the toplevel 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 smoketest
```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="MathMCPTester",
system_prompt=MATH_AGENT_PROMPT,
max_loops=1,
mcp_servers=[server], # < now recognised
output_type="final",
)
# 3. run a call that should trigger the servers “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 dont 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 smoketest above.
Thats all you need to get your Swarms agent talking to an existing MCP server without touching serverside code. 🚀
Loading…
Cancel
Save