-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix httpstreamable errorcode #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix httpstreamable errorcode #1194
Conversation
Ensures that any error code response from the MCP server is handled.
This ensures the error message is more specific and focused to a 400 bad request error only
It seems we are missing a try..except on a lower level that would send the JSON RPC message instead of crashing the server. |
Just a small correction to avoid confusion. The issue is entirely related with the MCP client. The MCP server does not crash in any way, only the client is affected. python-sdk/src/mcp/client/streamable_http.py Lines 253 to 299 in 3a45353
The main issue stems from line 285 above, since it raises a HTTPStatusError, which isn't caught. In the current main branch code, only an error 404 response is caught. All other responses would result in a crash because of line 285. I'm not sure if there is a lower level in the code than this before the response received from the server. Would it be best to put a try...except around line 285? Since an error 404 is already caught in the main branch, wouldn't it be best to catch any type of 4xx or 5xx error? Thank you for taking the time to review my pull request, I really appreciate it. |
Added additional
if
statements to catch a 400 bad request error messages within thestreamable_http.py
fileMotivation and Context
This change is needed to fix an issue with the current way that a client uses streamable HTTP to connect to a MCP server. Currently, an unavoidable exception is raised when creating a
SessionGroup
and adding a steamable HTTP connection to an MCP server that returns an error 4xx code when a primitive (prompts, resources and tools) is requested. As long as at least one of these primitives returns an error 4xx code (except a 404 error), it will cause the exception, which crashes the MCP client.In cases with independent sessions outside of
SessionGroup
, this error can be handled by the developer building the MCP client. However, when usingSessionGroup
, the developer cannot handle these errors, and so cannot prevent the program from crashing. This is because theSessionGroup
automatically sends web requests to prompts, resources and tools without checking if they are supported, which I think goes against the specification.The change is needed to ensure that an MCP client doesn't crash when an MCP server sends an error code for a bad request (which it can do as defined in the specifciation). The change was made to the
streamable_http.py
file, as this avoids a possible breaking change if theSessionGroup
was changed instead.How Has This Been Tested?
This has been tested with the AWS Knowledge MCP Server, which returns an error code 400 when interacting with a
prompt
orresource
primitive.Breaking Changes
No breaking changes. The only possible scenario of a breaking change is if a developer relies on the httpx status error exception currently raised to detect an error 400 code for an unsupported capability. In this case, the developer should be following the specification, and check if a capability is supported before it is requested.
Types of changes
Checklist
Additional context
I apologise in advance if I have missunderstood anything about the code regarding this issue or anything about the MCP specification. Please correct me on any errors I have made so that I can learn from them.