Immutable solution version
[FEATURE] Improved MCP DX to prevent common context manager scoping issue
Complete solution
Shows how to fix [FEATURE] Improved MCP DX to prevent common context manager scoping issue. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Upgrade to >= v1.14.0 to get the new method (experimental at release; a follow-up task tracks moving it to stable). Check your version of AWS Strands Agents first , maintainers often fix the underlying bug in a later release, and the thread may predate it.
Steps
- The common MCP footgun: listing tools inside 'with mcp_client:' then using the agent after the block exits, when the connection is closed, causes MCPClientInitializationError. This is addressed in strands-agents sdk-python v1.14.0, which shipped an experimental improved MCP developer experience. Upgrade to >= v1.14.0 to get the new method (experimental at release; a follow-up task tracks moving it to stable).
- This comes from one real issue thread (https://github.com/strands-agents/harness-sdk/issues/198); it is the verified fix or approach from that thread, not a general manual. Check your version of AWS Strands Agents first , maintainers often fix the underlying bug in a later release, and the thread may predate it.
When to use
You are seeing this: Issue strands-agents/harness-sdk#198 (closed, 14 comments): ### Problem Statement Currently, many users are encountering a common issue with MCP, where the user needs to manage the lifecycle of the connection using a context manager. Use this skill when you run into "[FEATURE] Improved MCP DX to prevent common context manager scoping issue".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
Versions mentioned in the source: sdk-python v1.14.0. If you are on something much newer or older, the details may have shifted.
Why this happens
Agent("do something") However, in both #96 and #184 we have seen customers encounter MCPClientInitializationError because they do something like the following which fails because the mcp connection is closed when we exit the with statement ` with mcp_client: agent =
Sources
- https://github.com/strands-agents/harness-sdk/issues/198
Evidence
Issue gh:strands-agents/harness-sdk#198 asked: Issue strands-agents/harness-sdk#198 (closed, 14 comments): ### Problem Statement Currently, many users are encountering a common issue with MCP, where the user needs to manage the lifecycle of the connection using a context manager. The intended syntax is the following `` with mcp_client: agent = Agent(tools=mcp_client.list_tools_sync()) agent("do something") However, in both #96 and #184 we have seen customers encounter MCPClientInitializationError because they do something like the following which fails because the mcp connection is closed when we exit the with statement `` with mcpclient: agent = Agent(tools=mcpclient.listtoolssync())... The verified answer from the thread: The common MCP footgun: listing tools inside 'with mcp_client:' then using the agent after the block exits, when the connection is closed, causes MCPClientInitializationError. This is addressed in strands-agents sdk-python v1.14.0, which shipped an experimental improved MCP developer experience. Upgrade to >= v1.14.0 to get the new method (experimental at release; a follow-up task tracks moving it to stable). Source: https://github.com/strands-agents/harness-sdk/issues/198
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_BddbYDs-tE5lfJKs1vJmBw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">[FEATURE] Improved MCP DX to prevent common context manager scoping issue <span>— Vectle</span></a>