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-Both-stack-traces-co...

100 lines
2.8 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.

Both stacktraces come from very small compatibility issues in **your local copy** of the library.
Fixing them just needs two tiny edits.
---
## 1  `'Agent' object has no attribute 'mcp_servers'`
`Agent.__init__()` used to take `mcp_servers`, but in your checkout that
parameter line is commented out:
```python
# mcp_servers: List[MCPServerSseParams] = [],
```
Yet `handle_tool_init()` still refers to `self.mcp_servers`, so the
attribute is missing when the object is created.
### Patch
```diff
@@ class Agent:
- # mcp_servers: List[MCPServerSseParams] = [],
+ mcp_servers: Optional[list] = None, # list[MCPServerSseParams]
@@ def __init__(...):
- self.tools_list_dictionary = tools_list_dictionary
- # self.mcp_servers = mcp_servers
+ self.tools_list_dictionary = tools_list_dictionary
+ self.mcp_servers = mcp_servers
```
> *Nothing else has to change; the `exists(self.mcp_servers)` check will now
> work, and you can pass `mcp_servers=[server_one]` from your smoketest.*
---
## 2  `ImportError: cannot import name 'NotRequired' from typing`
`typing.NotRequired` only exists in **Python  3.11**.
On 3.10 you have to import it from **`typing_extensions`**.
The top of `swarms/tools/mcp_integration.py` currently has:
```python
from typing import Any, Dict, List, Optional, TypedDict, NotRequired
```
### Patch
```diff
-from typing import Any, Dict, List, Optional, TypedDict, NotRequired
+from typing import Any, Dict, List, Optional
+from typing_extensions import NotRequired, TypedDict
```
(There is already a `typing_extensions` import later, so you can simply reuse
it and remove `NotRequired` from the `typing` line.)
> Make sure the wheel `typingextensions` is installed:
> ```bash
> pip install -U typing_extensions
> ```
---
### After the fixes
Your smoketest script should run:
```python
from swarms.tools.mcp_integration import MCPServerSseParams
from swarms.prompts.agent_prompts import MATH_AGENT_PROMPT
from swarms import Agent
server_one = MCPServerSseParams(
url="http://127.0.0.1:6274",
headers={"Content-Type": "application/json"},
)
agent = Agent(
agent_name="MathTester",
agent_description="Does arithmetic via MCP add tool",
system_prompt=MATH_AGENT_PROMPT,
max_loops=1,
mcp_servers=[server_one],
output_type="final",
)
print(agent.run("Use the add tool to add 2 and 2"))
```
---
**Summary**
* add the `mcp_servers` parameter back into `Agent.__init__`
* import `NotRequired` from `typing_extensions`, not `typing`
After these two edits the “attribute missing” and “ImportError” messages
disappear and the agent can reach the MCP server for your smoketest.