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-seeing-n...

110 lines
3.2 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 seeing **`name 'batch_mcp_flow' is not defined`**
1. In **`swarms/structs/agent.py`** the method `mcp_execution_flow()` (or the block that calls MCP) still tries to invoke `batch_mcp_flow`.
2. At the very top of the file the import that *defines* that symbol was commented out when you switched to fastMCP:
```python
# from swarms.tools.mcp_integration import (
# MCPServerSseParams,
# batch_mcp_flow, # ← you lost this
# mcp_flow_get_tool_schema,
# )
```
3. When Python hits `batch_mcp_flow(…)` it isnt in the modules namespace, so the call blows up and your agent prints the MCPerror.
---
## Twostep fix
> Below assumes your `swarms/tools/mcp_integration.py` already contains a working (sync) wrapper named `batch_mcp_flow` that *returns a string*, **not** a coroutine object.
> If you kept it **async**, see the “If your wrapper is still async” note at the end.
---
### 1  · Restore the import in **`agent.py`**
Uncomment (or add) the line:
```python
from swarms.tools.mcp_integration import batch_mcp_flow
```
Keep the `MCPServerSseParams` import too if you reference it elsewhere.
---
### 2  · Make sure `batch_mcp_flow()` is **synchronous**
Inside **`swarms/tools/mcp_integration.py`** do something like:
```python
# ── swarms/tools/mcp_integration.py ─────────────────────
import asyncio
# … other imports …
async def _batch_mcp_flow_async(
params: list[MCPServerSseParams],
function_call: dict[str, Any],
) -> str:
# your existing async logic that talks to MCP
# and returns a JSONserialisable string/str
...
def batch_mcp_flow(
params: list[MCPServerSseParams],
function_call: dict[str, Any],
) -> str:
"""Blocking wrapper so callers don't have to await."""
return asyncio.run(
_batch_mcp_flow_async(params, function_call)
)
```
Now `agent.py` can call `batch_mcp_flow(...)` directly and
get **the tools real answer**, not a coroutine object.
---
### Quick checklist
- [ ] Reimport `batch_mcp_flow` in `swarms/structs/agent.py`.
- [ ] Ensure `batch_mcp_flow()` (the public one) **returns a value**, not a coroutine.
- [ ] Remove or comment out the temporary warning prints you added earlier.
---
### If your wrapper is still async
You can alternatively **await** it inside `mcp_execution_flow`:
```python
import asyncio
def mcp_execution_flow(self, response: str) -> str:
try:
resp_dict = json.loads(response)
return asyncio.run(
batch_mcp_flow(self.mcp_servers, resp_dict)
)
except Exception as e:
logger.error(f"MCP flow failed: {e}")
return f"[MCP-error] {e}"
```
Either approach works; the key is that the call you make inside the agent must not leave a bare coroutine object floating around.
Once those two lines are fixed, run the client again:
```bash
python examples/mcp_example/mcp_client.py
```
You should now see a clean answer:
```
Enter your query (or 'exit' to quit): add 3 and 33
Math Agent Response: 36
```
Happy calculating!