modelcontextprotocol / modelcontextprotocol/python-sdk

Refactor handler context to be transport- and handler-type-aware

未關閉
#2,021 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

P1 v2
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
31

描述

The current RequestContext / ServerRequestContext design has a few rough edges that warrant a dedicated refactor:

  1. Request vs notification context — Request handlers have request_id and meta, notification handlers do not. Currently request_id is optional (RequestId | None) on a single shared type, which loses type safety. Ideally these would be distinct types so request handlers get a context where request_id is always present, and notification handlers get one where it does not exist.

  2. Transport-specific context — The request_context field on ServerMessageMetadata is typed as Any because the server layer is transport-agnostic (e.g. it is a Starlette Request for HTTP transports, None for stdio). A better design would let transports provide their own typed context that handlers can access without casting.

These two concerns are related — both point toward making the context type more precise depending on how and where a handler is invoked.

AI Disclaimer

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先沿著 handler 入口點追蹤 RequestContext 和 ServerRequestContext,然後檢查 ServerMessageMetadata.request_context,以及針對 Starlette Request 和 stdio 提到的傳輸情況。比較 request handler 和 notification handler 如何接收 context,以及如何存取傳輸特定的值。在不依賴 Any 或可選 request ID 的情況下定義預期的型別邊界和傳輸行為,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend-api-design
Issue 類型
重構
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。