Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ This function is invoked whenever the server requests sampling.
For protocol versions `2026-07-28` and later, sampling requests are
delivered via the
[Multi Round-Trip Requests](#multi-round-trip-requests)
pattern.
pattern. On those sessions the client rejects a standalone
`sampling/createMessage` request with `MethodNotFound` (`-32601`) without
calling `CreateMessageHandler`.

```go
func Example_sampling() {
Expand Down Expand Up @@ -257,7 +259,9 @@ you must declare that capability explicitly (see [Capabilities](#capabilities))
For protocol versions `2026-07-28` and later, elicitation requests are
delivered via the
[Multi Round-Trip Requests](#multi-round-trip-requests)
pattern.
pattern. On those sessions the client rejects a standalone
`elicitation/create` request with `MethodNotFound` (`-32601`) without
calling `ElicitationHandler`.

```go
func Example_elicitation() {
Expand Down
8 changes: 6 additions & 2 deletions internal/docs/client.src.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ This function is invoked whenever the server requests sampling.
For protocol versions `2026-07-28` and later, sampling requests are
delivered via the
[Multi Round-Trip Requests](#multi-round-trip-requests)
pattern.
pattern. On those sessions the client rejects a standalone
`sampling/createMessage` request with `MethodNotFound` (`-32601`) without
calling `CreateMessageHandler`.

%include ../../mcp/client_example_test.go sampling -

Expand All @@ -99,7 +101,9 @@ you must declare that capability explicitly (see [Capabilities](#capabilities))
For protocol versions `2026-07-28` and later, elicitation requests are
delivered via the
[Multi Round-Trip Requests](#multi-round-trip-requests)
pattern.
pattern. On those sessions the client rejects a standalone
`elicitation/create` request with `MethodNotFound` (`-32601`) without
calling `ElicitationHandler`.

%include ../../mcp/client_example_test.go elicitation -

Expand Down
13 changes: 9 additions & 4 deletions mcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,10 +1129,15 @@ func (cs *ClientSession) receivingMethodInfos() map[string]methodInfo {
}

func (cs *ClientSession) handle(ctx context.Context, req *jsonrpc.Request) (any, error) {
if (req.Method == methodPing || req.Method == methodListRoots) && req.IsCall() && cs.usesNewProtocol() {
return nil, &jsonrpc.Error{
Code: jsonrpc.CodeMethodNotFound,
Message: fmt.Sprintf("%q is not supported in the new protocol", req.Method),
if req.IsCall() && cs.usesNewProtocol() {
switch req.Method {
// Roots, sampling and elicitation reach a 2026-07-28 client only as
// multi round-trip input requests, which do not pass through handle.
case methodPing, methodListRoots, methodCreateMessage, methodElicit:
return nil, &jsonrpc.Error{
Code: jsonrpc.CodeMethodNotFound,
Message: fmt.Sprintf("%q is not supported in the new protocol", req.Method),
}
}
}
if req.IsCall() {
Expand Down
61 changes: 61 additions & 0 deletions mcp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,67 @@ func TestClientRejectsStandaloneListRootsOnNewProtocol(t *testing.T) {
}
}

func TestClientRejectsStandaloneElicitationAndSamplingOnNewProtocol(t *testing.T) {
ctx := context.Background()

s := NewServer(testImpl, nil)
ct, st := NewInMemoryTransports()
ss, err := s.Connect(ctx, st, nil)
if err != nil {
t.Fatalf("server Connect: %v", err)
}
defer ss.Close()

var elicited, sampled atomic.Bool
c := NewClient(testImpl, &ClientOptions{
ElicitationHandler: func(context.Context, *ElicitRequest) (*ElicitResult, error) {
elicited.Store(true)
return &ElicitResult{Action: "decline"}, nil
},
CreateMessageHandler: func(context.Context, *CreateMessageRequest) (*CreateMessageResult, error) {
sampled.Store(true)
return &CreateMessageResult{Model: "m", Role: "assistant", Content: &TextContent{Text: "x"}}, nil
},
})
cs, err := c.Connect(ctx, ct, nil)
if err != nil {
t.Fatalf("client Connect: %v", err)
}
defer cs.Close()

if ir := cs.InitializeResult(); ir == nil || ir.ProtocolVersion < protocolVersion20260728 {
t.Fatalf("InitializeResult = %+v, want a session negotiated at %s", ir, protocolVersion20260728)
}

// Send the requests directly, bypassing the send-side guards in
// ServerSession.Elicit and ServerSession.CreateMessage, to simulate a
// non-conforming server.
_, elicitErr := handleSend[*ElicitResult](ctx, methodElicit, newServerRequest(ss, &ElicitParams{
Message: "name?",
RequestedSchema: map[string]any{"type": "object", "properties": map[string]any{"name": map[string]any{"type": "string"}}},
}))
_, sampleErr := handleSend[*CreateMessageWithToolsResult](ctx, methodCreateMessage, newServerRequest(ss, &CreateMessageParams{
MaxTokens: 10,
Messages: []*SamplingMessage{{Role: "user", Content: &TextContent{Text: "hi"}}},
}))
for _, tc := range []struct {
method string
err error
ran bool
}{
{methodElicit, elicitErr, elicited.Load()},
{methodCreateMessage, sampleErr, sampled.Load()},
} {
var jsonrpcErr *jsonrpc.Error
if !errors.As(tc.err, &jsonrpcErr) || jsonrpcErr.Code != jsonrpc.CodeMethodNotFound {
t.Errorf("%s to a 2026-07-28 client = %v, want a *jsonrpc.Error with code %d", tc.method, tc.err, jsonrpc.CodeMethodNotFound)
}
if tc.ran {
t.Errorf("the client's handler ran for a standalone %s", tc.method)
}
}
}

// TestClientIncomingPingDuringInitialize verifies that a ping the server
// sends while the legacy initialize handshake is still in flight is answered,
// and that the client's ping handler reads the session state safely while
Expand Down
Loading