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-Here-s-why-the-lates...

107 lines
3.1 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.

Heres why the latest run blew up and what to change so the **Swarms Agent + MCP** combo finally works:
---
### 1  Fix the import thats blowing up
`mcp` exposes **ClientSession**, not **Client**.
Change the topoffile imports in **swarms/tools/mcp_integration.py**
```diff
- from mcp import Client as ClientSession
- from fastmcp.client.sse import sse_client # (this package doesnt exist)
+ from mcp import ClientSession # core SDK class
+ from mcp.client.sse import sse_client # SSE transport helper
```
(See MCP quickstart that shows `from mcp import ClientSession`citeturn3search8)
---
### 2  Use `typing_extensions.NotRequired` on Python  3.10
Python 3.11 added `typing.NotRequired`; on 3.10 you must pull it from
`typing_extensions`:
```python
from typing_extensions import NotRequired, TypedDict
```
You already have that line keep it.
---
### 3  Let your `Agent` actually hold the server list
The attributeerror you saw (`'Agent' object has no attribute 'mcp_servers'`)
happens because the constructor parameter was commented out.
Add the param and stash it:
```diff
-class Agent:
- def __init__(self,
+class Agent:
+ def __init__(self,
...
- # mcp_servers: List[MCPServerSseParams] = [],
+ mcp_servers: Optional[list[MCPServerSseParams]] = None,
*args, **kwargs):
...
- # self.mcp_servers = mcp_servers
+ self.mcp_servers = mcp_servers or []
```
Now `handle_tool_init()` can safely look at `self.mcp_servers`.
---
### 4  Example “smoke test” with the Browserbase MCP server
```python
# examples/mcp_example/mcp_client.py
from swarms.structs.agent import Agent
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
from swarms.tools.mcp_integration import MCPServerSseParams
browserbase = MCPServerSseParams(
url="http://127.0.0.1:6274", # running Browserbase MCP server
headers={"Content-Type": "application/json"},
)
math_bot = Agent(
agent_name="MathMCPBot",
agent_description="Performs arithmetic via MCP tools",
system_prompt=MATH_AGENT_PROMPT,
max_loops=1,
mcp_servers=[browserbase], # <-- now recognised
output_type="final",
)
print(math_bot.run("Use the add tool to add 2 and 2"))
```
Make sure youve installed the SDK:
```bash
pip install "mcp>=0.3.0" typing_extensions
```
and that the Browserbase server from the README snippet is running (or any
other MCP SSE server).
---
### 5  What to do *if* you really want “fastmcp”
There isnt a published `fastmcp` Python package today (hence the
`ModuleNotFoundError`). Stick with the official `mcp` SDK until/if the
fastvariant is released. When it lands the only changes youd make are the
two import lines in **mcp_integration.py**; the rest of the Swarms integration
stays identical.
---
Apply those three code edits, reinstall dependencies, and your smoke test
should run without import errors. If you hit the next snag, drop the new
traceback and well keep tightening the bolts.