feat(mcp): Top5#3 对齐 MCP Go SDK——协议版本协商 + list_changed 差量刷新 + keepalive + Streamable HTTP
- 协议版本协商:initialize 先发最高版本(2025-11-25)并声明能力集合,遇 UnsupportedProtocolVersionError(-32022)/服务器不支持版本 → negotiateMutuallySupportedVersion 求出双方最高共同版本降级重试(最多2次);无共同版本→ErrUnsupportedProtocolVersion。 协商结果记 negotiatedVersion,HTTP 传输回填 Mcp-Protocol-Version 头。 - tools/list_changed 订阅:capabilities 声明 tools.listChanged:true;传输读无 id 帧路由到 handleServerNotification;refreshPending 原子旗标合并突发 + 新 goroutine 调 onToolListChanged (避免 stdio lineRPC 重入自锁);Registry 真差量刷新(重新 DiscoverTools→移除消失/注册新增, 跳过同名冲突保外来工具)。 - keepalive:WithKeepAlive(interval,failures) 起 goroutine 定时 Call(ping),连续失败≥阈值关 session(默认3,可配);服务器不支持 ping(-32601)停 keepalive 不误关;ctx+Close 取消不泄漏。 - Streamable HTTP 传输:newTransport 支持 TransportType=http|streamable(保留 stdio/SSE 推断, 空值走旧逻辑不破坏);Call POST+Accept json/event-stream 响应可单 JSON 或 SSE 流找匹配 id; Mcp-Session-Id 会话 + 独立 GET SSE 流收 server 通知;config 读 MCP_TRANSPORT。 - schema 缓存判定不做:客户端透传 map[string]any 无重复编译,SDK SchemaCache 是服务端 按 Go 类型反射场景,客户端无对应。 go build/vet/test 全绿(count=2 无 flaky) + windows/darwin 交叉编译。
This commit is contained in:
1 parent
464ed2cad3
commit
b0bd93b15c
13 files changed
+1517
-66
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
||||
)
|
||||
@@ -107,6 +108,58 @@ func TestRegistry_Unregister_Idempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_ToolListChangedRefreshes(t *testing.T) {
|
||||
t.Parallel()
|
||||
ft := &fakeTransport{tools: sampleTools()}
|
||||
reg := registry.New()
|
||||
r := NewRegistry(reg, MCPConfig{Name: "calc", Command: "x"}, WithTransport(ft))
|
||||
if err := r.Register(context.Background()); err != nil {
|
||||
t.Fatalf("Register: %v", err)
|
||||
}
|
||||
// The server's tool list changes: sub is dropped, mul appears.
|
||||
ft.setTools([]ToolInfo{
|
||||
{Name: "add", Description: "Adds numbers", InputSchema: map[string]any{"type": "object"}},
|
||||
{Name: "mul", Description: "Multiplies numbers", InputSchema: map[string]any{"type": "object"}},
|
||||
})
|
||||
ft.notifyServer(notificationToolsListChanged)
|
||||
|
||||
waitFor(t, time.Second, func() bool {
|
||||
_, err := reg.Get("mul")
|
||||
return err == nil
|
||||
})
|
||||
if _, err := reg.Get("add"); err != nil {
|
||||
t.Fatalf("Get(add) after refresh = %v, want present", err)
|
||||
}
|
||||
if _, err := reg.Get("sub"); !errors.Is(err, registry.ErrNotFound) {
|
||||
t.Fatalf("Get(sub) after refresh = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistry_ToolListChangedPreservesForeignTools(t *testing.T) {
|
||||
t.Parallel()
|
||||
ft := &fakeTransport{tools: sampleTools()}
|
||||
reg := registry.New()
|
||||
_ = reg.RegisterTool(&testTool{name: "add"}) // a foreign 'add' before refresh
|
||||
r := NewRegistry(reg, MCPConfig{Name: "calc", Command: "x"}, WithTransport(ft))
|
||||
if err := r.Register(context.Background()); err != nil {
|
||||
// 'add' collides with the foreign tool; Register must surface the dup.
|
||||
if !errors.Is(err, registry.ErrDuplicate) {
|
||||
t.Fatalf("Register error = %v, want registry.ErrDuplicate", err)
|
||||
}
|
||||
}
|
||||
// After the collision, sub is registered, 'add' stays foreign.
|
||||
ft.setTools([]ToolInfo{{Name: "sub", Description: "Subtracts", InputSchema: map[string]any{}}})
|
||||
ft.notifyServer(notificationToolsListChanged)
|
||||
waitFor(t, time.Second, func() bool {
|
||||
_, err := reg.Get("sub")
|
||||
return err == nil
|
||||
})
|
||||
// The foreign 'add' must be preserved even though it is not in the server list.
|
||||
if _, err := reg.Get("add"); err != nil {
|
||||
t.Fatalf("Get(add) after refresh = %v, want foreign tool preserved", err)
|
||||
}
|
||||
}
|
||||
|
||||
// testTool is a minimal sdk.Tool used to pre-register a colliding name.
|
||||
type testTool struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user