modelcontextprotocol / modelcontextprotocol/python-sdk

AssertionError: Unexpected message while using middlewares

オープン
#883 コメント 15 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug P1 ready for work
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
31

説明

Describe the bug
when I mount the mcp to starlette, and I use middleware to do some prechecks, I met this errors.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in call
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/app/.venv/lib/python3.12/site-packages/starlette/applications.py", line 113, in call
await self.middleware_stack(scope, receive, send)
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 187, in call
raise exc
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/errors.py", line 165, in call
await self.app(scope, receive, _send)
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/base.py", line 182, in call
with recv_stream, send_stream, collapse_excgroups():
^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/contextlib.py", line 158, in exit
self.gen.throw(value)
File "/app/.venv/lib/python3.12/site-packages/starlette/_utils.py", line 83, in collapse_excgroups
raise exc
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/base.py", line 185, in call
await response(scope, wrapped_receive, send)
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/base.py", line 223, in call
async for chunk in self.body_iterator:
File "/app/.venv/lib/python3.12/site-packages/starlette/middleware/base.py", line 169, in body_stream
assert message["type"] == "http.response.body", f"Unexpected message: {message}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Unexpected message: {'type': 'http.response.start', 'status': 200, 'headers': [(b'content-length', b'0')]}

To Reproduce
Steps to reproduce the behavior:

  1. mount mcp to starlette
  2. use middlewares
  3. call tools
class CustomHeaderMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        return await call_next(request)


middleware = [
    Middleware(CustomHeaderMiddleware)
]

app = Starlette(middleware=middleware)
app.mount("/", mcp_server.sse_app())

def main():
    uvicorn.run(
        app="atlantis_mcp_server:app",
        host="0.0.0.0",
        port=8000,
        workers=1,
        log_level="info"
    )

__all__ = ["main"]

Expected behavior
Should not raise type error

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Starlette アプリケーションの mcp_server.sse_app() のマウント、CustomHeaderMiddleware の例、および traceback に示されている middleware の呼び出し経路から始めます。マウントされた SSE アプリを通じてツールを呼び出して失敗を再現し、その後、予期しないメッセージの内容を理解できるまでレスポンスメッセージを追跡します。middleware でラップされたアプリが AssertionError なしでリクエストを完了すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api, backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
42/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。