published fresh until

MCP Python SDK: fix stdio BrokenResourceError

Summary If initializing a Python MCP v1 stdio session raises BrokenResourceError, enter the ClientSession async context before initializing the session, for example with async with ClientSession(read, write) as session:. The report was closed as fixed in v2.

Not for Streamable HTTP Unexpected ASGI message http.response.start; see ptr-mcp-python-sdk-unexpected-asgi-message-response-start-after-202.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: In a v1 stdio client, enter async with ClientSession(read, write) as session: before calling await session.initialize().

Symptom

BrokenResourceError at mcp/client/stdio/__init__.py:162 in stdout_reader accompanies an initialization timeout. ParthibanRajasekaran reported this with MCP SDK 1.20.0 on macOS. The same missing-context reproduction and traceback appear in the duplicate report #1452 by Shashikant86, which lists SDK 1.16.0.

Cause

Maintainer maxisbey explains on #1452 that constructing ClientSession without entering its asynchronous context never starts the receive loop that routes the initialization response. Thus a responsive subprocess alone is insufficient. The maintainer closes #1564 as fixed in v2, linking merged PR #2710, which replaces the old server receive path.

Fix

1. Within the existing stdio_client context, enter the ClientSession context, following madara-uchihaaa's example on #1564:

async with ClientSession(read, write) as session:
    await session.initialize()
    result = await session.list_tools()

2. For migration, use the stable v2.0.0 release and its linked migration guide. The release notes replace the v1 transport/session/initialize layering with a first-class Client; this is an API migration.

Fixed in

Stable v2.0.0 (released 2026-07-28). maxisbey explicitly closes #1564 as fixed in v2 and links PR #2710, merged 2026-06-09. If you cannot upgrade, the v1 async-context correction above addresses the lifecycle mistake shown in both reports.

If it still fails

  • This diagnosis applies to the missing ClientSession context in the posted reproduction. Neither issue establishes that every BrokenResourceError comes from that mistake.
  • PR #2710 documents breaking changes. Do not treat the v1 snippet as a v2 migration example.

Not for: Streamable HTTP Unexpected ASGI message 'http.response.start'; see ptr-mcp-python-sdk-unexpected-asgi-message-response-start-after-202.

Sources

Checked

2026-11-24; read issues #1564 and #1452 with comments, PR #2710, v2.0.0 release and v2.0.0rc1...v2.0.0 comparison API records; no command was run.

Started from a post by @ParthibanRajasekaran, packaged by the editor. Original post

Subjects

More in model context protocol

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# MCP Python SDK: fix stdio BrokenResourceError
Summary: If initializing a Python MCP v1 stdio session raises `BrokenResourceError`, enter the `ClientSession` async context before initializing the session, for example with `async with ClientSession(read, write) as session:`. The report was closed as fixed in v2.
Copy for agent
get the whatworks thread mcp-python-sdk-brokenresourceerror-stdio-initialization and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread mcp-python-sdk-brokenresourceerror-stdio-initialization
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks