modelcontextprotocol / modelcontextprotocol/python-sdk

Make JSON-RPC ID type coercion configurable

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

還沒有人認領這個 Issue。

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

描述

Background

PR #1720 added automatic type coercion for JSON-RPC response IDs, allowing string IDs like "0" to match integer request IDs like 0. This makes the client more tolerant of servers that echo back IDs in a different but semantically equivalent format.

Spec Language

Per the JSON-RPC 2.0 and MCP specifications, response IDs should be exact matches:

JSON-RPC 2.0: Response ID "MUST be the same as the value of the id member in the Request Object"

MCP Spec:

  • Result responses "MUST include the same ID as the request they correspond to"
  • Error responses "MUST include the same ID as the request they correspond to"

The language "same as the value" / "same ID" implies exact matching including type. The spec does not describe IDs as opaque and does not mandate type conversion or normalization. Servers should echo back the exact ID they received.

Current Behavior

PR #1720 makes the client more lenient by accepting type-coerced IDs (e.g., "0" matching 0). This is a workaround for non-compliant servers.

Proposal

Make this ID type coercion behavior configurable:

  • Strict mode: Require exact type matching (spec-compliant behavior)
  • Lenient mode: Allow type coercion between numeric strings and integers (current behavior after #1720)

This could be exposed as a session or client configuration option, defaulting to lenient for backwards compatibility.

Ideally strict mode is default, with an error message being clear as to why something fails. If we receive a response with an ID that's a string, even though the original request ID was an int we could first check if there are any IDs which would match after type coercion and then include that in the error message. "Request ID with same value of different type found, but ignored due to strict ID matching configuration".

Reasoning

By spec language, request ID 1 and request ID "1" are different IDs, but with this type coercion they are treated as the same. Given that many clients/servers are non-compliant with this anyway and do their own type conversions we should allow server/client users of the SDK to configure the behaviour for themselves.

Related

  • #1720 - Fix JSON-RPC error response ID matching

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先檢視 PR #1720 以及目前的 JSON-RPC 回應 ID 比對實作;該 issue 未指定特定的檔案或測試。判斷 session 或 client 設定應放置在哪裡,接著確認嚴格比對與寬鬆的數值強制轉換是否可選,並確保對不相符的回應 ID 有明確的處理,同時保留向後相容性。

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

評估

技術堆疊
python
領域
api, backend-api-design
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
45/100

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

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