[BUG] MCP over HTTP can't read a JSON reply that starts with a byte order mark

Aperta Adatta ai principianti
#1,879 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@yorzi ci sta già lavorando.

Dal 29/11/2026.

  • #1880 di @yorzi — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
json, ruby

Direzione di ricerca

Inizia con RubyLLM::MCP::HTTP::Stream#feed, dove il primo carattere determina se la risposta viene analizzata come JSON o come flusso di eventi. Esegui il caso fornito bundle exec ruby -Ilib repro.rb e ispeziona i test vicini dei flussi HTTP MCP. La modifica è completa quando una risposta JSON con il prefisso BOM UTF-8 continua a essere analizzata correttamente, mentre la gestione del JSON semplice e dei flussi di eventi continua a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Basic checks
  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code
What's broken?

RubyLLM::MCP can't read a JSON reply that starts with a UTF-8 byte order mark. MCP::HTTP::Stream#feed tells JSON from an event stream by the first character. With the mark in front, that character is neither { nor [, so the reply goes to the event stream parser, which drops the mark and then ignores the JSON line as an unknown field. The request raises as if the server had not answered.

RFC 8259 (section 8.1) says a sender must not add the mark, and that a parser may ignore it. The event stream side already does. I found this by reading the code, and I don't know of a public server that sends one.

How to reproduce

From the repo root, bundle exec ruby -Ilib repro.rb:

require "ruby_llm"
require "webmock"
WebMock.enable!

BOM = "\xEF\xBB\xBF".b
RESULTS = {
  "server/discover" => { resultType: "complete", supportedVersions: ["2026-07-28"], capabilities: { tools: {} } },
  "tools/list" => { resultType: "complete", tools: [{ name: "search", inputSchema: { type: "object" } }] }
}

WebMock::API.stub_request(:post, "https://mcp.example.com/mcp").to_return do |request|
  call = JSON.parse(request.body)
  body = { jsonrpc: "2.0", id: call["id"], result: RESULTS.fetch(call["method"], {}) }.to_json
  { body: (ENV["PLAIN"] ? "" : BOM) + body, headers: { "Content-Type" => "application/json" } }
end

class Docs < RubyLLM::MCP
  url "https://mcp.example.com/mcp"
end

puts Docs.new.tools.map(&:name).inspect

With PLAIN=1, which leaves the mark out, it prints ["search"].

Expected behavior

["search"]

What actually happened
mcp.example.com did not answer initialize (RubyLLM::MCP::Error)
Environment
  • Ruby version: 4.0.9
  • RubyLLM version: 2.2.3, main at c0889f414
  • Provider: none, the MCP server is stubbed
  • OS: macOS arm64
Lingua principale
Ruby
Stelle
4.5k
Fork
543
Merge medio
11h 38m
PR unite (30g)
410

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di crmne/ruby_llm

Tutte le issue di crmne/ruby_llm

Issue simili

Altre issue su Ruby

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.