fix(agent): re-add mcp_servers parameter and fix NotRequired import for Python 3.10 compatibility

pull/819/head
DP37 3 months ago committed by ascender1729
parent 552f5cce2f
commit 242cfcd4b1

@ -0,0 +1,100 @@
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.
Loading…
Cancel
Save