Invalid UTF-8 in `MCP-Protocol-Version`, `Mcp-Method` or `Mcp-Name` gives 500 instead of 400
維護者通常 1 天內回覆
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 78/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- ruby
研究方向
從 lib/mcp/server/transports/streamable_http_transport.rb 開始,重點查看所提到的 header 處理,以及 decode_header_value 和 json_rpc_error_response 路徑。使用提供的 Rack 請求重現,並檢查附近的 transport 測試,找出無效的 header 值。完成標準是:這些 header 中的無效 UTF-8 會傳回預期的 400 回應,且不會引發 JSON 產生器例外或例外報告。
由索引模型根據 Issue 內容生成。
描述
On the modern path, StreamableHTTPTransport puts raw header values into JSON error bodies:
- the unsupported-version error's
data.requested(streamable_http_transport.rb:908) - the
Mcp-MethodandMcp-Namemismatch messages (:998-1026)
Rack gives header values as binary strings. Also, decode_header_value tags a decoded =?base64?...?= value as UTF-8 without checking it (:1586-1592), so an all-ASCII Mcp-Name: =?base64?/w==?= also produces an invalid string. json_rpc_error_response then calls to_json (:2064-2070), which raises JSON::GeneratorError. The rescue at :968-974 reports the exception and answers 500.
Related: #629 (a reply that cannot be generated gets Internal error) and #609 (another header that caused a 500). Open #654 adds a strict UTF-8 check when it decodes Mcp-Param-* values, but it does not change decode_header_value or these error bodies.
Repro
Run from the repository root with bundle exec ruby -I lib repro.rb:
require "mcp"
require "rack"
reported = []
MCP.configure { |config| config.exception_reporter = ->(error, _context) { reported << error.class } }
server = MCP::Server.new(name: "example")
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
env = Rack::MockRequest.env_for(
"http://localhost/",
method: "POST",
input: JSON.generate(jsonrpc: "2.0", id: 1, method: "tools/list"),
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json, text/event-stream",
"HTTP_MCP_PROTOCOL_VERSION" => "2026-07-28\xFF".b,
)
status, _headers, body = transport.call(env)
p [status, body.join, reported]
transport.close
Expected: 400 with "Unsupported protocol version", and no exception report. For the Mcp-Method and Mcp-Name cases, 400 with the header mismatch error (-32020).
Actual:
[500, "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32603,\"message\":\"Internal server error\"}}", [JSON::GeneratorError]]
The same 500 happens with invalid bytes in Mcp-Method or Mcp-Name on a modern tools/call, and with Mcp-Name: =?base64?/w==?=.
Suggested fix
Check header values with valid_encoding? (after tagging them as UTF-8) before using them. Leave invalid values out of error bodies, or scrub them. Make decode_header_value refuse a decoded value that is not valid UTF-8, the same way the codec in #654 does.
- 主要語言
- Ruby
- 星號
- 940
- 分支
- 143
- 平均合併
- 1 天 3 小時
- 30 天內合併 PR
- 47
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 沒有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
modelcontextprotocol/ruby-sdk 的其他 Issue
-
`Client::Stdio` passes the whole parent environment to the server process可能已有人在做 @atesgoral 今天認領。 未關閉bug
難度 4/5 3-5 天 新手友好度 55/100
modelcontextprotocol/ruby-sdk#662 ·
維護者通常 1 天內回覆
-
Elicitation results are not checked against `requested_schema`, but the docs say they match it可能已有人在做 @atesgoral 今天認領。 未關閉bug
難度 3/5 1-2 天 新手友好度 35/100
modelcontextprotocol/ruby-sdk#661 ·
維護者通常 1 天內回覆
-
HTTP client sends handler exception messages back to the server可能已有人在做 @atesgoral 今天認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 35/100
modelcontextprotocol/ruby-sdk#660 ·
維護者通常 1 天內回覆
-
`JWTVerifier` answers 500 instead of 401 for a PS256 token when it verifies with an EC key (jwt < 3.3.0)可能已有人在做 @atesgoral 今天認領。 未關閉bug
難度 3/5 1-2 天 新手友好度 35/100
modelcontextprotocol/ruby-sdk#659 ·
維護者通常 1 天內回覆
-
`logging/setLevel` from one session changes the log level of other sessions可能已有人在做 @atesgoral 今天認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 35/100
modelcontextprotocol/ruby-sdk#658 ·
維護者通常 1 天內回覆
查看 modelcontextprotocol/ruby-sdk 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
palladius/rails8-app-on-gcp#198 ·
-
bug
難度 1/5 1 小時以內 新手友好度 95/100
WikiEducationFoundation/WikiEduDashboard#7258 ·
維護者通常 1 天內回覆
-
UX/UI verified
難度 2/5 1-3 小時 新手友好度 73/100
維護者通常 5 天內回覆
-
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 1 天內回覆
-
enhancement
難度 2/5 1-3 小時 新手友好度 78/100
維護者通常 1 天內回覆