Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Streamable HTTP: a message with an `id` but no `method`, `result` or `error` is answered 202 with no body

未关闭
#1,476 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go
领域
api, backend

调研方向

Start in internal/jsonrpc2/messages.go at DecodeMessage, then trace how StreamableHTTPHandler handles decoded responses with no matching request. Add or locate tests for messages with an id but no method, result or error; check that a present result: null still counts as a response. Done means malformed input returns HTTP 400 with a JSON-RPC -32600 error carrying the id, while valid responses and notifications retain their behavior.

由索引模型根据 Issue 内容生成。

描述

What happened?

A JSON-RPC message that has an id but none of method, result or error is answered with HTTP 202 and an empty body by StreamableHTTPHandler. The sender gets no sign that the message was invalid, and nothing is processed.

The same malformed message without an id is already refused: HTTP 400, malformed payload: invalid request. So the SDK treats the missing method as invalid in one case and accepts it silently in the other.

Cause, as far as I can tell: DecodeMessage (internal/jsonrpc2/messages.go) treats any message without a method key as a response ("no method key, should be a response") and only rejects it when the id is invalid. It never checks that result or error is present. The message is then routed as a response to a request that does not exist, the transport takes the branch for "no calls in this POST", and writes http.StatusAccepted.

Expected: the message is neither a request, a notification nor a response, so it should be refused like the no-id case, with HTTP 400 and a JSON-RPC -32600 Invalid Request error that carries the id.

The MCP Streamable HTTP page (2026-07-28) says the POST body must be a single JSON-RPC request or notification, that an accepted notification gets 202 Accepted with no body, and that when the server cannot accept the input it returns an HTTP error status such as 400. A 202 for a message that is not a valid notification or response does not fit that.

How to reproduce
package main

import (
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"

	"github.com/modelcontextprotocol/go-sdk/mcp"
)

func main() {
	srv := mcp.NewServer(&mcp.Implementation{Name: "repro", Version: "0"}, nil)
	h := mcp.NewStreamableHTTPHandler(func(*http.Request) *mcp.Server { return srv },
		&mcp.StreamableHTTPOptions{Stateless: true})
	ts := httptest.NewServer(h)
	defer ts.Close()

	post := func(label, body string) {
		req, _ := http.NewRequest(http.MethodPost, ts.URL, strings.NewReader(body))
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("Accept", "application/json, text/event-stream")
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			panic(err)
		}
		b, _ := io.ReadAll(resp.Body)
		resp.Body.Close()
		fmt.Printf("%-24s -> HTTP %d, body %q\n", label, resp.StatusCode, string(b))
	}

	post("id, no method", `{"jsonrpc":"2.0","id":5,"params":{"name":"x","arguments":{}}}`)
	post("no id, no method", `{"jsonrpc":"2.0","params":{}}`)
}

Output on v1.7.0 and v1.8.0 (the code in main is the same):

id, no method            -> HTTP 202, body ""
no id, no method         -> HTTP 400, body "malformed payload: invalid request\n"

A possible fix: in DecodeMessage, when there is no method key, also require that a result or an error key is present (check the key, not the value, since "result": null is a valid response); otherwise return ErrInvalidRequest.

Where did you hit this?

An MCP server using StreamableHTTPHandler in stateless mode. QA found it by deleting "method": "tools/call" from a request by hand and getting a 202 with no body.

Go MCP SDK version

v1.7.0 and v1.8.0 (and main, by reading the code)

Go version

go1.27.1 darwin/arm64

Logs

None; the response is the log.

主要语言
Go
星标
5.3k
派生
606
平均合并
1 天 7 小时
30 天内合并 PR
77

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

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

modelcontextprotocol/go-sdk 的其他 Issue

查看 modelcontextprotocol/go-sdk 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

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