modelcontextprotocol / modelcontextprotocol/python-sdk

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

未关闭
#2,432 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug fix proposed P2 ready for work
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 client.py 中展示的 streamable_http_client 入口点开始,并使用 server.py 中的 PlainTextHandler 示例复现该行为。跟踪 initialize 响应的处理,并为 non-text/event-stream 响应添加覆盖;完成标准是客户端能够立即失败并给出易于理解的错误,而不是一直挂起。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
api, backend
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。