You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/attached_assets/Pasted--Why-you-re-getting-...

105 lines
2.7 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 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. 🚀