Streamable HTTP: a tool result that fails to marshal leaves the POST hanging (no JSON-RPC error)
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- go
- 領域
- api, networking
調査の方向性
Start in mcp/streamable.go at streamableServerConn.Write (around line 1824), where EncodeMessage fails before the response reaches the pending HTTP request. Then check how jsonrpc2 stores that error as writeErr. Run the reproduction with Stateless: true and confirm the third POST for id 2 hangs. Done means that POST returns a JSON-RPC error with code -32603 instead of timing out, and the encoding error is logged.
索引モデルが issue の本文から書いたものです。
説明
Describe the bug
If a tool handler returns a CallToolResult that json.Marshal cannot encode, the Streamable HTTP handler never answers the request. The client waits until its own timeout. No JSON-RPC error is sent, and the server keeps no trace the client can see.
This happens with Stateless: true and with Stateless: false. A normal tool on the same server answers right away. One example of an unencodable value is a float64 that is +Inf or NaN in StructuredContent. A cyclic value has the same effect.
To Reproduce
go-sdk v1.8.0:
package main
import (
"bytes"
"context"
"fmt"
"io"
"math"
"net/http"
"net/http/httptest"
"time"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func main() {
server := mcp.NewServer(&mcp.Implementation{Name: "repro", Version: "0.0.1"}, nil)
mcp.AddTool(server, &mcp.Tool{Name: "ok", Description: "returns a normal result"},
func(context.Context, *mcp.CallToolRequest, struct{}) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: "fine"}}}, nil, nil
})
mcp.AddTool(server, &mcp.Tool{Name: "bad", Description: "returns a result json.Marshal rejects"},
func(context.Context, *mcp.CallToolRequest, struct{}) (*mcp.CallToolResult, any, error) {
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "has +Inf"}},
StructuredContent: map[string]any{"value": math.Inf(1)}, // json: unsupported value: +Inf
}, nil, nil
})
ts := httptest.NewServer(mcp.NewStreamableHTTPHandler(
func(*http.Request) *mcp.Server { return server }, &mcp.StreamableHTTPOptions{Stateless: true}))
defer ts.Close()
client := &http.Client{Timeout: 5 * time.Second}
post := func(body string) {
req, _ := http.NewRequest(http.MethodPost, ts.URL, bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
req.Header.Set("Mcp-Protocol-Version", "2025-11-25")
start := time.Now()
resp, err := client.Do(req)
if err != nil {
fmt.Printf("no response after %s: %v\n", time.Since(start).Round(time.Millisecond), err)
return
}
defer resp.Body.Close()
out, _ := io.ReadAll(resp.Body)
fmt.Printf("HTTP %d in %s: %.120s\n", resp.StatusCode, time.Since(start).Round(time.Millisecond), bytes.TrimSpace(out))
}
post(`{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"repro","version":"0"}}}`)
post(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"ok","arguments":{}}}`)
post(`{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"bad","arguments":{}}}`)
}
Output:
HTTP 200 in 3ms: event: message data: {"jsonrpc":"2.0","id":0,"result":{...}}
HTTP 200 in 1ms: event: message data: {"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"fine"}]}}
no response after 5.004s: Post "http://127.0.0.1:41245": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
With Stateless: false (initialize, then notifications/initialized with the session ID), the result is the same.
Expected behavior
The request completes with a JSON-RPC error for id 2, for example -32603 "internal error: result could not be encoded". It must not hang. Ideally the server also logs the encoding error.
Where it seems to happen
streamableServerConn.Write encodes the message first (mcp/streamable.go, around line 1824 in v1.8.0) and returns the EncodeMessage error. At that point the response has not yet been routed to the pending HTTP request. On the jsonrpc2 side, the error is only kept as the connection's writeErr. The handler that waits for the response to this request ID is never released.
A possible fix: when a *jsonrpc.Response fails to encode, replace it with an error response for the same ID and write that instead. The other option is to encode the result in the request path before handing it to the writer, so the error can become a JSON-RPC error.
Workaround
On the server, marshal the StructuredContent (or the whole result) before returning it from the handler, and return a tool error when that fails.
Environment
- go-sdk v1.8.0 (latest at the time of writing)
- Go 1.27.1
- Linux
- 主要言語
- Go
- スター
- 5.2k
- フォーク
- 583
- 平均マージ
- 2日 1時間
- マージ済み PR(30日)
- 55
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
modelcontextprotocol/go-sdk のほかの issue
-
Session-lifecycle bookkeeping logs at info, spamming multiple log lines per request on stateless streamable HTTP対応中かも @anneheartrecord が 50 日前に担当しました。 オープンP3
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
modelcontextprotocol/go-sdk#1204 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Expose generic `SendNotification` on `ServerSession` for custom protocol extensions対応中かも @ajuijas が 216 日前に担当しました。 オープンneeds investigation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
modelcontextprotocol/go-sdk#745 · コメント 13 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
mcp: a property with an array "type" (any pointer field from jsonschema.For) disables x-mcp-header handling for the whole tool対応中かも @jay23606 が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 50/100
modelcontextprotocol/go-sdk#1406 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
jsonrpc: decimal and exponent integer IDs still lose precision after #1401対応中かも @skosovsky が 1 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
modelcontextprotocol/go-sdk#1404 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
mcp: rejected request IDs receive no JSON-RPC error envelope対応中かも @skosovsky が 5 日前に担当しました。 オープン
難易度 4/5 1〜2日 初心者へのやさしさ 68/100
modelcontextprotocol/go-sdk#1391 ·
メンテナーはふだん 1 日以内に返信
modelcontextprotocol/go-sdk の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 1 日以内に返信
-
clawsweeper:fix-shape-clear clawsweeper:queueable-fix clawsweeper:source-repro impact:session-state issue-rating: 🦞 diamond lobster no-stale P2
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
openclaw/wacli#503 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 81/100
apache/rocketmq-dashboard#6895 ·
メンテナーはふだん 3 日以内に返信
-
agent-butler-finding chore
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
jordansmall/spindrift#5466 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
open-telemetry/opentelemetry-go-compile-instrumentation#1507 ·
メンテナーはふだん 3 日以内に返信