Initial Checks
Description
We had some timeouts on our server while using MCP client from the SDK.
When users try to integrate our client with their MCP server, we do an initialize call to the server and we are waiting infinitely to the following:
After investigation, it seems the error happens when the MCP server is misconfigured, and returns something different than Content-Type: text/event-stream as a response from the initialize call.
Although it is on the server side, the client should be able to fail immediately with a understandable error to warn the user something went wrong with the server's initialize output.
Example Code
# server.pyfromhttp.serverimportBaseHTTPRequestHandler, HTTPServerclassPlainTextHandler(BaseHTTPRequestHandler):
defdo_POST(self):
length=int(self.headers.get("Content-Length", 0))
self.rfile.read(length)
body=b"this is not json"self.send_response(200)
self.send_header("Content-Type", "text/plain")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
deflog_message(self, format, *args):
print(f"[server] {format%args}")
server=HTTPServer(("localhost", 9000), PlainTextHandler)
print("[server] listening on http://localhost:9000")
server.serve_forever()
# client.pyimportasyncioimportmcpfrommcp.client.streamable_httpimportstreamable_http_clientasyncdefmain():
print("connecting…")
asyncwithstreamable_http_client("http://localhost:9000/mcp") as (
read_stream, write_stream, _,
):
asyncwithmcp.ClientSession(read_stream, write_stream) assession:
awaitsession.initialize() # ← hangs here foreverprint("done (you should never see this)")Python & MCP Python SDK
- Python 3.12.12
- MCP - 1.27.0
Initial Checks
Description
We had some timeouts on our server while using MCP client from the SDK.
When users try to integrate our client with their MCP server, we do an
initializecall to the server and we are waiting infinitely to the following:After investigation, it seems the error happens when the MCP server is misconfigured, and returns something different than
Content-Type: text/event-streamas a response from theinitializecall.Although it is on the server side, the client should be able to fail immediately with a understandable error to warn the user something went wrong with the server's
initializeoutput.Example Code
Python & MCP Python SDK