mcp: a wrapped stdio server connection never learns the negotiated protocol version

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

  • #1556 来自 @tang-ontheway —— 已关闭,未合并

评估

难度
4/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go

调研方向

Read ServerSession.updateState and the unexported serverConnection interface, then compare how LoggingTransport forwards the negotiated protocol version with the client-side fix in #1107. Reproduce using the supplied IOTransport wrapper example and check the batch and cancelled-request cases described in the issue. Done when a wrapped stdio-style connection preserves the negotiated protocol behavior.

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

描述

Checklist
  • I searched the existing issues, and this has not been reported before.
  • This is a real problem that I ran into myself, using the latest release of the SDK.
  • I ran into it in a production system, not in a toy example, a benchmark, or an automated or AI-driven scan of the code.
  • I (a human) examined the relevant SDK code, and wrote or reviewed every line of this report. It is not unreviewed AI output.
  • This report is brief and concise, and sticks to the facts needed to understand the problem.
What happened?

ServerSession.updateState hands the negotiated protocol version to the connection only through the unexported serverConnection interface. LoggingTransport forwards it (#1328), but a Connection wrapper outside the package cannot implement an unexported method. So a stdio-style server connection (StdioTransport, IOTransport, InMemoryTransport) wrapped by user code behaves as if no version had been negotiated:

  • On v1.8.0 and main, it answers a JSON-RPC batch on a 2025-11-25 session. Without the wrapper, the server rejects the batch and closes the connection.
  • On main, it answers a request the client cancelled on a 2026-07-28 session. Without the wrapper, the response is dropped (#1267).

Expected: wrapping a Connection to observe or decorate messages (metrics, tracing, auditing) does not change protocol behavior. #1109 had the same cause on the client side and was fixed by deriving the version from the outgoing message (#1107) instead of the hook. Something similar could work here, or the hook could be exported so that wrappers can forward it. I can send a fix once you choose a direction.

AI assistance: prepared with Claude; I reviewed it.

How to reproduce
package main

import (
	"bufio"
	"context"
	"fmt"
	"io"

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

// wrapped stands in for a user-defined Connection decorator (metrics,
// tracing, auditing) that forwards the Connection methods.
type wrapped struct{ mcp.Transport }

func (w wrapped) Connect(ctx context.Context) (mcp.Connection, error) {
	c, err := w.Transport.Connect(ctx)
	return struct{ mcp.Connection }{c}, err
}

func run(wrap bool) {
	ctx := context.Background()
	clientR, serverW := io.Pipe()
	serverR, clientW := io.Pipe()
	var t mcp.Transport = &mcp.IOTransport{Reader: serverR, Writer: serverW}
	if wrap {
		t = wrapped{t}
	}
	server := mcp.NewServer(&mcp.Implementation{Name: "s", Version: "v"}, nil)
	ss, err := server.Connect(ctx, t, nil)
	if err != nil {
		panic(err)
	}
	defer ss.Close()

	lines := bufio.NewScanner(clientR)
	fmt.Fprintln(clientW, `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"c","version":"v"}}}`)
	lines.Scan()
	fmt.Fprintln(clientW, `{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}`)
	fmt.Fprintln(clientW, `[{"jsonrpc":"2.0","id":2,"method":"ping"}]`)
	if lines.Scan() {
		fmt.Printf("wrapped=%v: %s\n", wrap, lines.Text())
	} else {
		fmt.Printf("wrapped=%v: connection closed\n", wrap)
	}
}

func main() {
	run(false)
	run(true)
}

Output with v1.8.0 and with main at 0e849cb:

wrapped=false: connection closed
wrapped=true: [{"jsonrpc":"2.0","id":2,"result":{}}]
Where did you hit this?

Found while writing a Connection wrapper in Frame (the plugin framework under Flame), which wraps its stdio client connection to keep the original tools/call result JSON; that client-side wrapper is not affected today.

Go MCP SDK version

v1.8.0 (also main at 0e849cb)

Go version

go version go1.27.3 darwin/arm64

Logs

No response

主要语言
Go
星标
5.4k
派生
623
平均合并
22 小时 10 分钟
30 天内合并 PR
30

环境准备

在 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 摘要。