Skip to content

Initialize call hangs forever if MCP server does not return a Content-Type: text/plain #2432

Closed

Description

@HenriChabert

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:

sync with self:
    ...

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.py

from http.server import BaseHTTPRequestHandler, HTTPServer

class PlainTextHandler(BaseHTTPRequestHandler):
    def do_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)

    def log_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.py
import asyncio
import mcp
from mcp.client.streamable_http import streamable_http_client

async def main():
    print("connecting…")
    async with streamable_http_client("http://localhost:9000/mcp") as (
        read_stream, write_stream, _,
    ):
        async with mcp.ClientSession(read_stream, write_stream) as session:
            await session.initialize()  # ← hangs here forever

    print("done (you should never see this)")

Python & MCP Python SDK

- Python 3.12.12
- MCP - 1.27.0

Activity

  1. vishal-vanam commented on Apr 13, 2026

    @vishal-vanam

    I'd like to work on this. Will submit a PR shortly.

  2. added a commit that references this issue on Apr 14, 2026
    9968f2c
  3. ghost added
    bugSomething isn't working
    on Apr 17, 2026
  4. ghost added
    ready for workEnough information for someone to start working on
    on Apr 17, 2026
  5. ghost added
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Apr 17, 2026
  6. ghost added
    fix proposedBot has a verified fix diff in the comment
    on Apr 17, 2026
  7. Christian-Sidak commented on Apr 19, 2026

    @Christian-Sidak

    Opened a fix in #2472 -- adds a regression test verifying that initialize() raises MCPError immediately when a misconfigured server returns an unexpected content type (e.g. text/plain) instead of text/event-stream, rather than hanging forever.

  8. Kludex commented on Oct 14, 2026

    @Kludex
    Member

    Fixed on main (2.x): a non-JSON/SSE content type now resolves the request with a JSON-RPC error instead of hanging. v1.x only takes critical and security fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featurebugSomething isn't workingfix proposedBot has a verified fix diff in the commentready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions