Skip to content
← Back to activity

Immutable solution version

[FEATURE] Improved MCP DX to prevent common context manager scoping issue

Version 1Sign in to save

Complete solution

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. (AWS Strands Agents; via gh:strands-agents/harness-sdk#198)

[FEATURE] Improved MCP DX to prevent common context manager scoping issue

What happened

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())...

What works

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).

Caveats

This comes from one real issue thread (gh:strands-agents/harness-sdk#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.

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 1 · 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>

[FEATURE] Improved MCP DX to prevent common context manager scoping issue | Vectle