mcp: Implement support for SEP-2575 on client side (#975)
Implements the client half of the SEP-2575 / `2026-06-30` protocol. This CL is client-only; server-side `server/discover` is still a `MethodNotFound` stub. ### Changes - **`server/discover` probe in `Client.Connect()`** (`client.go`). When the caller opts into `protocolVersion >= 2026-06-30`, the client probes the server with `server/discover` before falling back to the legacy `initialize` handshake. - **Per-request `_meta` injection** (`shared.go`, `injectRequestMeta`). For`>= 2026-06-30` sessions every outgoing request gets the SEP-2575 triple (`protocolVersion`, `clientInfo`, `clientCapabilities`). - **Server-side fix**: `ServerSession.handle` no longer pre-populates `state.InitializeParams` from a `server/discover` request's `_meta`, so a subsequent fallback `initialize` is not rejected as a duplicate. ### Fallback rules Client falls back to the legacy `initialize` handshake when discover returns: - `MethodNotFound` (-32601), - `UnsupportedProtocolVersionError` (-32004), - an HTTP `Bad Request` (substring-matched on the error message, to interop with pre-2026-06-30 servers). Fixes #966
This commit is contained in:
parent
d54751a8ee
commit
c60a318d22
1 changed files with 1010 additions and 96 deletions
|
|
@ -8,11 +8,14 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/google/go-cmp/cmp/cmpopts"
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
||||
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
|
|
@ -617,3 +620,224 @@ func TestClientCapabilitiesOverWire(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClientConnectDiscover exercises the SEP-2575 server/discover probe that
|
||||
// Client.Connect now sends before falling back to the legacy initialize
|
||||
// handshake.
|
||||
//
|
||||
// Each subtest installs a server-side receiving middleware that intercepts the
|
||||
// "server/discover" method and returns a canned response: a successful
|
||||
// DiscoverResult, a "Method not found" error, an UnsupportedProtocolVersion
|
||||
// error, an unrelated failure, or a DiscoverResult whose supportedVersions
|
||||
// don't overlap with the SDK. The test then asserts the resulting session
|
||||
// state and whether the legacy initialize handshake ran.
|
||||
func TestClientConnectDiscover(t *testing.T) {
|
||||
// Temporarily enable 2026-06-30 support in the SDK for this test
|
||||
oldSupported := supportedProtocolVersions
|
||||
supportedProtocolVersions = append([]string{protocolVersion20260630}, supportedProtocolVersions...)
|
||||
t.Cleanup(func() {
|
||||
supportedProtocolVersions = oldSupported
|
||||
})
|
||||
|
||||
const otherVersionsOnly = "1999-01-01"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
// discoverHandler decides how the server replies to server/discover.
|
||||
// Returning (nil, nil) means "let the default stub handle it" (which
|
||||
// returns ErrMethodNotFound).
|
||||
discoverHandler func() (Result, error)
|
||||
wantConnectErr bool
|
||||
// wantInitialize is true if the legacy initialize handshake should
|
||||
// have run (i.e. discover signaled "not supported").
|
||||
wantInitialize bool
|
||||
// wantVersion is the protocol version expected to end up on
|
||||
// ClientSession.state.InitializeResult after Connect returns.
|
||||
wantVersion string
|
||||
}{
|
||||
{
|
||||
name: "discover success skips initialize",
|
||||
discoverHandler: func() (Result, error) {
|
||||
return &DiscoverResult{
|
||||
SupportedVersions: []string{protocolVersion20260630},
|
||||
Capabilities: &ServerCapabilities{
|
||||
Tools: &ToolCapabilities{ListChanged: true},
|
||||
},
|
||||
ServerInfo: &Implementation{Name: "discoverServer", Version: "v1.0.0"},
|
||||
}, nil
|
||||
},
|
||||
wantInitialize: false,
|
||||
wantVersion: protocolVersion20260630,
|
||||
},
|
||||
{
|
||||
name: "method not found falls back to initialize",
|
||||
discoverHandler: func() (Result, error) {
|
||||
return nil, jsonrpc2.ErrMethodNotFound
|
||||
},
|
||||
wantInitialize: true,
|
||||
wantVersion: latestProtocolVersion,
|
||||
},
|
||||
{
|
||||
name: "no overlapping supported version falls back to initialize",
|
||||
discoverHandler: func() (Result, error) {
|
||||
return &DiscoverResult{
|
||||
SupportedVersions: []string{otherVersionsOnly},
|
||||
Capabilities: &ServerCapabilities{},
|
||||
ServerInfo: &Implementation{Name: "discoverServer", Version: "v1.0.0"},
|
||||
}, nil
|
||||
},
|
||||
wantInitialize: true,
|
||||
wantVersion: latestProtocolVersion,
|
||||
},
|
||||
{
|
||||
name: "unexpected error propagates and aborts Connect",
|
||||
discoverHandler: func() (Result, error) {
|
||||
return nil, &jsonrpc.Error{
|
||||
Code: jsonrpc.CodeInternalError,
|
||||
Message: "boom",
|
||||
}
|
||||
},
|
||||
wantConnectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
var (
|
||||
gotDiscover atomic.Bool
|
||||
gotInitialize atomic.Bool
|
||||
)
|
||||
|
||||
s := NewServer(testImpl, nil)
|
||||
s.AddReceivingMiddleware(func(next MethodHandler) MethodHandler {
|
||||
return func(ctx context.Context, method string, req Request) (Result, error) {
|
||||
switch method {
|
||||
case methodDiscover:
|
||||
gotDiscover.Store(true)
|
||||
return tc.discoverHandler()
|
||||
case methodInitialize:
|
||||
gotInitialize.Store(true)
|
||||
}
|
||||
return next(ctx, method, req)
|
||||
}
|
||||
})
|
||||
|
||||
ct, st := NewInMemoryTransports()
|
||||
ss, err := s.Connect(ctx, st, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("server Connect: %v", err)
|
||||
}
|
||||
defer ss.Close()
|
||||
|
||||
c := NewClient(testImpl, nil)
|
||||
cs, err := c.Connect(ctx, ct, &ClientSessionOptions{protocolVersion: protocolVersion20260630})
|
||||
if tc.wantConnectErr {
|
||||
if err == nil {
|
||||
_ = cs.Close()
|
||||
t.Fatal("Connect succeeded, want error")
|
||||
}
|
||||
if !gotDiscover.Load() {
|
||||
t.Error("server did not receive server/discover")
|
||||
}
|
||||
if gotInitialize.Load() {
|
||||
t.Error("server received initialize but discover should have aborted Connect")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("Connect: %v", err)
|
||||
}
|
||||
defer cs.Close()
|
||||
|
||||
if !gotDiscover.Load() {
|
||||
t.Error("server did not receive server/discover")
|
||||
}
|
||||
if got, want := gotInitialize.Load(), tc.wantInitialize; got != want {
|
||||
t.Errorf("initialize invoked = %v, want %v", got, want)
|
||||
}
|
||||
ir := cs.InitializeResult()
|
||||
if ir == nil {
|
||||
t.Fatal("InitializeResult is nil after Connect")
|
||||
}
|
||||
if got, want := ir.ProtocolVersion, tc.wantVersion; got != want {
|
||||
t.Errorf("InitializeResult.ProtocolVersion = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClientConnectDiscover_RequestContents verifies that the server/discover
|
||||
// request sent by Client.Connect carries the SEP-2575 per-request _meta triple:
|
||||
// protocolVersion, clientInfo, and clientCapabilities.
|
||||
func TestClientConnectDiscover_RequestContents(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
type captured struct {
|
||||
params *DiscoverParams
|
||||
}
|
||||
var got captured
|
||||
|
||||
s := NewServer(testImpl, nil)
|
||||
s.AddReceivingMiddleware(func(next MethodHandler) MethodHandler {
|
||||
return func(ctx context.Context, method string, req Request) (Result, error) {
|
||||
if method == methodDiscover {
|
||||
sr, ok := req.(*ServerRequest[*DiscoverParams])
|
||||
if !ok {
|
||||
t.Errorf("discover req has unexpected type %T", req)
|
||||
return nil, jsonrpc2.ErrMethodNotFound
|
||||
}
|
||||
got.params = sr.Params
|
||||
// Make discover "not supported" so Connect proceeds (we only
|
||||
// care about the discover request payload here).
|
||||
return nil, jsonrpc2.ErrMethodNotFound
|
||||
}
|
||||
return next(ctx, method, req)
|
||||
}
|
||||
})
|
||||
|
||||
ct, st := NewInMemoryTransports()
|
||||
ss, err := s.Connect(ctx, st, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("server Connect: %v", err)
|
||||
}
|
||||
defer ss.Close()
|
||||
|
||||
clientImpl := &Implementation{Name: "discover-probe-client", Version: "v9.9.9"}
|
||||
c := NewClient(clientImpl, &ClientOptions{
|
||||
CreateMessageHandler: func(context.Context, *CreateMessageRequest) (*CreateMessageResult, error) {
|
||||
return nil, nil
|
||||
},
|
||||
})
|
||||
cs, err := c.Connect(ctx, ct, &ClientSessionOptions{protocolVersion: protocolVersion20260630})
|
||||
if err != nil {
|
||||
t.Fatalf("Connect: %v", err)
|
||||
}
|
||||
defer cs.Close()
|
||||
|
||||
if got.params == nil {
|
||||
t.Fatal("server did not receive server/discover")
|
||||
}
|
||||
|
||||
meta := got.params.GetMeta()
|
||||
if v, _ := meta[MetaKeyProtocolVersion].(string); v != protocolVersion20260630 {
|
||||
t.Errorf("_meta[%s] = %q, want %q", MetaKeyProtocolVersion, v, protocolVersion20260630)
|
||||
}
|
||||
// _meta values round-trip through JSON, so on the server side they
|
||||
// arrive as map[string]any rather than the typed Go pointers we sent.
|
||||
info, ok := meta[MetaKeyClientInfo].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("_meta[%s] = %T, want map[string]any", MetaKeyClientInfo, meta[MetaKeyClientInfo])
|
||||
}
|
||||
if got, want := info["name"], any(clientImpl.Name); got != want {
|
||||
t.Errorf("clientInfo.name = %v, want %v", got, want)
|
||||
}
|
||||
caps, ok := meta[MetaKeyClientCapabilities].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("_meta[%s] = %T, want map[string]any", MetaKeyClientCapabilities, meta[MetaKeyClientCapabilities])
|
||||
}
|
||||
if _, ok := caps["sampling"]; !ok {
|
||||
t.Errorf("clientCapabilities.sampling missing (CreateMessageHandler was set); got %v", caps)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue