Title: ClientSession Error Handling · Issue #1401 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: ClientSession Error Handling · Issue #1401 · modelcontextprotocol/python-sdk
X Title: ClientSession Error Handling · Issue #1401 · modelcontextprotocol/python-sdk
Description: Question I work on the Strands SDK, and we have an integration with MCP where we invoke tools through an MCP client. Our design spins up a thread, creates an asyncio event loop, and schedules a task to create a session with an MCP server...
Open Graph Description: Question I work on the Strands SDK, and we have an integration with MCP where we invoke tools through an MCP client. Our design spins up a thread, creates an asyncio event loop, and schedules a tas...
X Description: Question I work on the Strands SDK, and we have an integration with MCP where we invoke tools through an MCP client. Our design spins up a thread, creates an asyncio event loop, and schedules a tas...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1401
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ClientSession Error Handling","articleBody":"### Question\n\nI work on the Strands SDK, and we have an integration with MCP where we invoke tools through an MCP client. \n\nOur design spins up a thread, creates an asyncio event loop, and schedules a task to create a session with an MCP server using the python-sdk mcp ClientSession: https://github.com/strands-agents/sdk-python/blob/main/src/strands/tools/mcp/mcp_client.py#L391\n\nThen, when an LLM decides to call a tool, or multiple tools, we schedule additional tasks on this thread's event loop to call the mcp server: https://github.com/strands-agents/sdk-python/blob/main/src/strands/tools/mcp/mcp_client.py#L324-L330\n\n\nWe ran into an issue where when the mcp `streamablehttp_client` sse_read_timeout was lower than the time it took for a tool to return, the tool invocation tasks would hang due to an exception that was not propagated out of the ClientSession. We see an exception message (included in the additional context section), but the stack trace never enters our code, it stops in the mcp code.\n\nAfter investigating further, I found that this exception is sent through the [MCP ClientSession `message_handler`](https://github.com/modelcontextprotocol/python-sdk/blob/71889d7387f070cd872cab7c9aa3d1ff1fa5a5d2/src/mcp/client/session.py#L119), and in the default implementation of this message handler, an exception is never raised: https://github.com/modelcontextprotocol/python-sdk/blob/71889d7387f070cd872cab7c9aa3d1ff1fa5a5d2/src/mcp/client/session.py#L57-L60\n\nTo work around this issue, I have introduced my own message_handler to raise an exception passed to it: https://github.com/strands-agents/sdk-python/pull/922\n\nI wanted to know why these exceptions are not raised in the MCP ClientSession? This silent failure took a long time to debug, and I want able to find any documentation on this behavior. As a user of the ClientSession, I would expect these exceptions to be raised so that my code can handle them.\n\n### Additional Context\n\nError stack trace:\n```\nError reading SSE stream:\nTraceback (most recent call last):\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_transports/default.py\", line 101, in map_httpcore_exceptions\n yield\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_transports/default.py\", line 271, in __aiter__\n async for part in self._httpcore_stream:\n yield part\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/connection_pool.py\", line 407, in __aiter__\n raise exc from None\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/connection_pool.py\", line 403, in __aiter__\n async for part in self._stream:\n yield part\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/http11.py\", line 342, in __aiter__\n raise exc\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/http11.py\", line 334, in __aiter__\n async for chunk in self._connection._receive_response_body(**kwargs):\n yield chunk\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/http11.py\", line 203, in _receive_response_body\n event = await self._receive_event(timeout=timeout)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_async/http11.py\", line 217, in _receive_event\n data = await self._network_stream.read(\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n self.READ_NUM_BYTES, timeout=timeout\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n )\n ^\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_backends/anyio.py\", line 32, in read\n with map_exceptions(exc_map):\n ~~~~~~~~~~~~~~^^^^^^^^^\n File \"/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py\", line 162, in __exit__\n self.gen.throw(value)\n ~~~~~~~~~~~~~~^^^^^^^\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpcore/_exceptions.py\", line 14, in map_exceptions\n raise to_exc(exc) from exc\nhttpcore.ReadTimeout\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/mcp/client/streamable_http.py\", line 326, in _handle_sse_response\n async for sse in event_source.aiter_sse():\n ...\u003c10 lines\u003e...\n break\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx_sse/_api.py\", line 42, in aiter_sse\n async for line in lines:\n ...\u003c3 lines\u003e...\n yield sse\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_models.py\", line 1031, in aiter_lines\n async for text in self.aiter_text():\n for line in decoder.decode(text):\n yield line\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_models.py\", line 1018, in aiter_text\n async for byte_content in self.aiter_bytes():\n ...\u003c2 lines\u003e...\n yield chunk\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_models.py\", line 997, in aiter_bytes\n async for raw_bytes in self.aiter_raw():\n ...\u003c2 lines\u003e...\n yield chunk\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_models.py\", line 1055, in aiter_raw\n async for raw_stream_bytes in self.stream:\n ...\u003c2 lines\u003e...\n yield chunk\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_client.py\", line 176, in __aiter__\n async for chunk in self._stream:\n yield chunk\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_transports/default.py\", line 270, in __aiter__\n with map_httpcore_exceptions():\n ~~~~~~~~~~~~~~~~~~~~~~~^^\n File \"/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/contextlib.py\", line 162, in __exit__\n self.gen.throw(value)\n ~~~~~~~~~~~~~~^^^^^^^\n File \"/Users/ncclegg/Library/Application Support/hatch/env/virtual/strands-agents/X7vsTQrp/strands-agents/lib/python3.13/site-packages/httpx/_transports/default.py\", line 118, in map_httpcore_exceptions\n raise mapped_exc(message) from exc\nhttpx.ReadTimeout\n```","author":{"url":"https://github.com/Unshure","@type":"Person","name":"Unshure"},"datePublished":"2025-09-25T14:14:46.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":12},"url":"https://github.com/1401/python-sdk/issues/1401"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9980350c-cc7d-f418-1b43-f5a356d71437 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D508:2F98EE:11687F:167A75:6A5BE64D |
| html-safe-nonce | dbbb11f60ae0a86f5ffb72669fc3d3602df917dda0627b43f5c6167ff1a95060 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTA4OjJGOThFRToxMTY4N0Y6MTY3QTc1OjZBNUJFNjREIiwidmlzaXRvcl9pZCI6IjgyNTY4MTUwNTY1ODA3MDYwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 6a8f9c29865473160b4c0a75d1b846f7bce0b5635a04c472ab629a2323da9f59 |
| hovercard-subject-tag | issue:3453847925 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1401/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c881723109921e092ec13612cfa00cde22d6da0468ce1629e9226f4c15af24c5/modelcontextprotocol/python-sdk/issues/1401 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c881723109921e092ec13612cfa00cde22d6da0468ce1629e9226f4c15af24c5/modelcontextprotocol/python-sdk/issues/1401 |
| og:image:alt | Question I work on the Strands SDK, and we have an integration with MCP where we invoke tools through an MCP client. Our design spins up a thread, creates an asyncio event loop, and schedules a tas... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Unshure |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5290d7e14309ad1e76106a9c4237bd1041517e83ea182c8ab756752cb0c6940b |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9c975978430e9ad293956f2bbdaf153b1bd84a99 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width