Skip to content

Conversation

Kulaxyz
Copy link

@Kulaxyz Kulaxyz commented Jun 23, 2025

Added exception handling in the _handle_post_request method to gracefully handle transport errors and return proper JSON-RPC error responses.

Motivation and Context

When an MCP server returns HTTP 5xx errors (e.g., when the server is down) after initial connection, these errors were not being propagated to the caller. This made it impossible for client applications to detect and handle server failures appropriately.

How Has This Been Tested?

  • Verified that HTTP exceptions are properly forwarded to read streams
  • Confirmed error responses are delivered immediately with correct JSON-RPC format
  • All existing transport tests continue to pass

Breaking Changes

no

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Strands SDK is an example of an implementation that is hanging when MCP Server is down.

@yangmianmian
Copy link

When will this commit merge to the master?

@felixweinberger felixweinberger self-requested a review September 22, 2025 19:42
Copy link
Contributor

@felixweinberger felixweinberger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Kulaxyz thank you for this contribution! And apologies for the time it took to get back to you on this.

The problem raised here looks valid, but we should be less broad with Exception handling.

It would also be great if we could add tests to demonstrate the functionality and prevent regressions in future.

If you have the bandwidth to work on this I'd be happy to come back to this quickly!

content_type,
ctx.read_stream_writer,
)
except Exception as exc:
Copy link
Contributor

@felixweinberger felixweinberger Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too broad for the specific case you mention - we shouldn't be doing this broad exception handling specifically for 5xx= failures as we might mask other issues this way.

message.root.id,
)
return
if response.status_code == 404:
Copy link
Contributor

@felixweinberger felixweinberger Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should instead expand this or add a case like

if response.status_code in (500, 502, 503, ...):

That does specific handling of only those specific errors you're encountering and that are expected that we want to handle. That would prevent us from masking other errors that might occur.

Other unexpected HTTP errors should be handled by raise_for_status() lower down.

@felixweinberger felixweinberger added needs more work Not ready to be merged yet, needs additional changes. bug Something isn't working labels Sep 22, 2025
@felixweinberger
Copy link
Contributor

Similar PR: #1194

@felixweinberger felixweinberger self-assigned this Sep 22, 2025
@felixweinberger felixweinberger mentioned this pull request Sep 23, 2025
6 tasks
@felixweinberger felixweinberger added needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention and removed needs more work Not ready to be merged yet, needs additional changes. labels Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants