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
@@ -24,10 +24,15 @@ type Registry struct {
|
||||
// NewRegistry builds a Registry that registers cfg's tools into reg. Options
|
||||
// apply to the underlying Client (logger, injected transport). It is named
|
||||
// NewRegistry rather than New because this package already exports New for
|
||||
// Client.
|
||||
// Client. A tool-list-changed handler is wired automatically so the registry
|
||||
// re-syncs when the server announces tools/list_changed.
|
||||
func NewRegistry(reg *registry.Registry, cfg MCPConfig, opts ...Option) *Registry {
|
||||
r := &Registry{reg: reg}
|
||||
opts = append(opts, WithToolListChangedHandler(r.refresh))
|
||||
c := New(cfg, opts...)
|
||||
return &Registry{reg: reg, mcp: c, log: c.log}
|
||||
r.mcp = c
|
||||
r.log = c.log
|
||||
return r
|
||||
}
|
||||
|
||||
// Register connects the MCP server, discovers its tools, and registers each one
|
||||
@@ -59,6 +64,69 @@ func (r *Registry) Register(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// refresh re-syncs the registered tool set with the server after a
|
||||
// notifications/tools/list_changed notification. It re-discovers the current
|
||||
// tools and applies only the diff: it removes names that are gone and registers
|
||||
// newly appeared ones, so tools registered by other providers (which collide by
|
||||
// name) are preserved. It is invoked (off the transport read path) by the
|
||||
// client's tool-list-changed handler and coalesced by the client.
|
||||
func (r *Registry) refresh() {
|
||||
if r.reg == nil {
|
||||
return
|
||||
}
|
||||
tools, err := r.mcp.DiscoverTools(context.Background())
|
||||
if err != nil {
|
||||
r.log.Warn("mcp tool refresh failed", "name", r.mcp.cfg.Name, "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
incoming := make(map[string]bool, len(tools))
|
||||
for _, t := range tools {
|
||||
incoming[t.Name()] = true
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
if r.closed {
|
||||
return
|
||||
}
|
||||
|
||||
// Drop names the server no longer exposes. Tools registered by other
|
||||
// providers were skipped at registration time and are not in r.names, so they
|
||||
// are never removed here.
|
||||
kept := r.names[:0]
|
||||
for _, name := range r.names {
|
||||
if !incoming[name] {
|
||||
if err := r.reg.Remove(name); err != nil && !errors.Is(err, registry.ErrNotFound) {
|
||||
r.log.Warn("mcp refresh remove failed", "name", name, "err", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
kept = append(kept, name)
|
||||
}
|
||||
r.names = kept
|
||||
|
||||
// Register newly appeared tools. Skips names already present (own or
|
||||
// foreign) so a name collision with another provider is not doubled.
|
||||
existing := make(map[string]bool, len(r.names))
|
||||
for _, name := range r.names {
|
||||
existing[name] = true
|
||||
}
|
||||
for _, t := range tools {
|
||||
if existing[t.Name()] {
|
||||
continue
|
||||
}
|
||||
if err := r.reg.RegisterTool(t); err != nil {
|
||||
if !errors.Is(err, registry.ErrDuplicate) {
|
||||
r.log.Warn("mcp refresh register failed", "name", t.Name(), "err", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
r.names = append(r.names, t.Name())
|
||||
}
|
||||
r.log.Info("mcp tools refreshed", "name", r.mcp.cfg.Name, "count", len(r.names))
|
||||
}
|
||||
|
||||
// Unregister removes every tool this registry previously registered and closes
|
||||
// the underlying client. It is safe to call multiple times.
|
||||
func (r *Registry) Unregister(ctx context.Context) error {
|
||||
|
||||
Reference in new issue
Block a user