Invalid UTF-8 in `MCP-Protocol-Version`, `Mcp-Method` or `Mcp-Name` gives 500 instead of 400

未关闭 适合新手
#663 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@atesgoral 已经在做这个了。

开始于 2026年11月6日。

  • #670 来自 @atesgoral —— 未关闭
  • #671 来自 @koic —— 未关闭

评估

难度
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 内容生成。

描述

bug

On the modern path, StreamableHTTPTransport puts raw header values into JSON error bodies:

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 模板
  • 阅读贡献指南

从这里开始

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

modelcontextprotocol/ruby-sdk 的其他 Issue

查看 modelcontextprotocol/ruby-sdk 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

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