Skip to content

Commit b8418f9

Browse files
fix: update mcp troubleshooting clarifying that the agent must be used within a context manager (#81)
1 parent 49c70ba commit b8418f9

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

docs/user-guide/concepts/tools/mcp-tools.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) is an open p
44

55
MCP enables communication between agents and MCP servers that provide additional tools. Strands includes built-in support for connecting to MCP servers and using their tools.
66

7+
When working with MCP tools in Strands, all agent operations must be performed within the MCP client's context manager (using a with statement).
8+
This requirement ensures that the MCP session remains active and connected while the agent is using the tools.
9+
If you attempt to use an agent or its MCP tools outside of this context, you'll encounter errors because the MCP session will have closed.
10+
11+
712
## MCP Server Connection Options
813

914
Strands provides several ways to connect to MCP servers:
@@ -47,6 +52,7 @@ with stdio_mcp_client:
4752

4853
# Create an agent with these tools
4954
agent = Agent(tools=tools)
55+
agent("What is AWS Lambda?")
5056
```
5157

5258
### 2. Streamable HTTP
@@ -240,17 +246,28 @@ print(f"Calculation result: {result['content'][0]['text']}")
240246

241247
## Troubleshooting
242248

243-
### Common Issues
249+
### **MCPClientInitializationError**
250+
251+
AgentTools relying on an MCP connection must always be used within a context manager. When you create or use an agent outside a with statement, operations will fail because the MCP session is automatically closed once you exit the context manager block. The MCP connection must remain active throughout the agent's operations to maintain access to the tools and services it provides.
252+
253+
Correct usage:
254+
```python
255+
with mcp_client:
256+
agent = Agent(tools=mcp_client.list_tools_sync())
257+
response = agent("Your prompt") # Works
258+
```
244259

245-
1. **Connection Failures**:
246-
- Ensure the MCP server is running and accessible
247-
- Check network connectivity and firewall settings
248-
- Verify the URL or command is correct
260+
Incorrect usage:
261+
```python
262+
with mcp_client:
263+
agent = Agent(tools=mcp_client.list_tools_sync())
264+
response = agent("Your prompt") # Will fail with MCPClientInitializationError
265+
```
266+
### **Connection Failures**
267+
Connection failures occur when there are problems establishing a connection with the MCP server. To resolve these issues, first ensure that the MCP server is running and accessible from your network environment. You should also verify your network connectivity and check if any firewall settings are blocking the connection. Additionally, make sure that the URL or command you're using to connect to the server is correct and properly formatted.
249268

250-
2. **Tool Discovery Issues**:
251-
- Ensure the MCP server properly implements the `list_tools` method
252-
- Check that tools are correctly registered with the server
269+
### **Tool Discovery Issues**
270+
When encountering tool discovery problems, first confirm that the MCP server has properly implemented the list_tools method as this is essential for tool discovery to function. It's also important to verify that all tools have been correctly registered with the server.
253271

254-
3. **Tool Execution Errors**:
255-
- Verify that tool arguments match the expected schema
256-
- Check server logs for detailed error information
272+
### **Tool Execution Errors**
273+
Tool execution errors can arise during the actual operation of MCP tools. To resolve these errors, verify that all tool arguments being passed match the expected schema for that particular tool. When errors occur, consulting the server logs can provide detailed information about what went wrong during the execution process.

0 commit comments

Comments
 (0)