modelcontextprotocol / modelcontextprotocol/python-sdk

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

Open
#2,432 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug fix proposed P2 ready for work
Dominant language
Python
Stars
24.3k
Forks
4k
Avg merge
1d 1h
Merged PRs (30d)
31

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the streamable_http_client entry point shown in client.py and reproduce the behavior using the PlainTextHandler example in server.py. Trace the initialize response handling and add coverage for a non-text/event-stream response; done means the client fails promptly with an understandable error instead of hanging.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.