modelcontextprotocol / modelcontextprotocol/python-sdk
Initialize call hangs forever if MCP server does not return a `Content-Type: text/plain`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 1 Std.
- Gemergte PRs (30 T.)
- 31
Beschreibung
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem in client.py gezeigten streamable_http_client-Einstiegspunkt und bilde das Verhalten anhand des PlainTextHandler-Beispiels in server.py nach. Verfolge die Verarbeitung der initialize-Antwort und füge Abdeckung für eine non-text/event-stream-Antwort hinzu; fertig ist die Aufgabe, wenn der Client umgehend mit einem verständlichen Fehler fehlschlägt, statt hängen zu bleiben.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- api, backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 58/100