Skip to content

Commit 67233a6

Browse files
committed
refactor: add isNil interface method to all param structs and update protocol version checking in server and tests
1 parent 69a549f commit 67233a6

7 files changed

Lines changed: 89 additions & 61 deletions

File tree

mcp/protocol.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,12 @@ func (x *CallToolResult) UnmarshalJSON(data []byte) error {
165165
}
166166

167167
func (x *CallToolParams) isParams() {}
168+
func (x *CallToolParams) isNil() bool { return x == nil }
168169
func (x *CallToolParams) GetProgressToken() any { return getProgressToken(x) }
169170
func (x *CallToolParams) SetProgressToken(t any) { setProgressToken(x, t) }
170171

171172
func (x *CallToolParamsRaw) isParams() {}
173+
func (x *CallToolParamsRaw) isNil() bool { return x == nil }
172174
func (x *CallToolParamsRaw) GetProgressToken() any { return getProgressToken(x) }
173175
func (x *CallToolParamsRaw) SetProgressToken(t any) { setProgressToken(x, t) }
174176

@@ -187,6 +189,7 @@ type CancelledParams struct {
187189
}
188190

189191
func (x *CancelledParams) isParams() {}
192+
func (x *CancelledParams) isNil() bool { return x == nil }
190193
func (x *CancelledParams) GetProgressToken() any { return getProgressToken(x) }
191194
func (x *CancelledParams) SetProgressToken(t any) { setProgressToken(x, t) }
192195

@@ -374,7 +377,8 @@ type CompleteParams struct {
374377
Ref *CompleteReference `json:"ref"`
375378
}
376379

377-
func (*CompleteParams) isParams() {}
380+
func (x *CompleteParams) isParams() {}
381+
func (x *CompleteParams) isNil() bool { return x == nil }
378382

379383
type CompletionResultDetails struct {
380384
HasMore bool `json:"hasMore,omitempty"`
@@ -422,6 +426,7 @@ type CreateMessageParams struct {
422426
}
423427

424428
func (x *CreateMessageParams) isParams() {}
429+
func (x *CreateMessageParams) isNil() bool { return x == nil }
425430
func (x *CreateMessageParams) GetProgressToken() any { return getProgressToken(x) }
426431
func (x *CreateMessageParams) SetProgressToken(t any) { setProgressToken(x, t) }
427432

@@ -448,6 +453,7 @@ type CreateMessageWithToolsParams struct {
448453
}
449454

450455
func (x *CreateMessageWithToolsParams) isParams() {}
456+
func (x *CreateMessageWithToolsParams) isNil() bool { return x == nil }
451457
func (x *CreateMessageWithToolsParams) GetProgressToken() any { return getProgressToken(x) }
452458
func (x *CreateMessageWithToolsParams) SetProgressToken(t any) { setProgressToken(x, t) }
453459

@@ -654,6 +660,7 @@ type GetPromptParams struct {
654660
}
655661

656662
func (x *GetPromptParams) isParams() {}
663+
func (x *GetPromptParams) isNil() bool { return x == nil }
657664
func (x *GetPromptParams) GetProgressToken() any { return getProgressToken(x) }
658665
func (x *GetPromptParams) SetProgressToken(t any) { setProgressToken(x, t) }
659666

@@ -706,6 +713,7 @@ func (p *initializeParamsV2) toV1() *InitializeParams {
706713
}
707714

708715
func (x *InitializeParams) isParams() {}
716+
func (x *InitializeParams) isNil() bool { return x == nil }
709717
func (x *InitializeParams) GetProgressToken() any { return getProgressToken(x) }
710718
func (x *InitializeParams) SetProgressToken(t any) { setProgressToken(x, t) }
711719

@@ -739,6 +747,7 @@ type InitializedParams struct {
739747
}
740748

741749
func (x *InitializedParams) isParams() {}
750+
func (x *InitializedParams) isNil() bool { return x == nil }
742751
func (x *InitializedParams) GetProgressToken() any { return getProgressToken(x) }
743752
func (x *InitializedParams) SetProgressToken(t any) { setProgressToken(x, t) }
744753

@@ -752,6 +761,7 @@ type ListPromptsParams struct {
752761
}
753762

754763
func (x *ListPromptsParams) isParams() {}
764+
func (x *ListPromptsParams) isNil() bool { return x == nil }
755765
func (x *ListPromptsParams) GetProgressToken() any { return getProgressToken(x) }
756766
func (x *ListPromptsParams) SetProgressToken(t any) { setProgressToken(x, t) }
757767
func (x *ListPromptsParams) cursorPtr() *string { return &x.Cursor }
@@ -780,6 +790,7 @@ type ListResourceTemplatesParams struct {
780790
}
781791

782792
func (x *ListResourceTemplatesParams) isParams() {}
793+
func (x *ListResourceTemplatesParams) isNil() bool { return x == nil }
783794
func (x *ListResourceTemplatesParams) GetProgressToken() any { return getProgressToken(x) }
784795
func (x *ListResourceTemplatesParams) SetProgressToken(t any) { setProgressToken(x, t) }
785796
func (x *ListResourceTemplatesParams) cursorPtr() *string { return &x.Cursor }
@@ -808,6 +819,7 @@ type ListResourcesParams struct {
808819
}
809820

810821
func (x *ListResourcesParams) isParams() {}
822+
func (x *ListResourcesParams) isNil() bool { return x == nil }
811823
func (x *ListResourcesParams) GetProgressToken() any { return getProgressToken(x) }
812824
func (x *ListResourcesParams) SetProgressToken(t any) { setProgressToken(x, t) }
813825
func (x *ListResourcesParams) cursorPtr() *string { return &x.Cursor }
@@ -833,6 +845,7 @@ type ListRootsParams struct {
833845
}
834846

835847
func (x *ListRootsParams) isParams() {}
848+
func (x *ListRootsParams) isNil() bool { return x == nil }
836849
func (x *ListRootsParams) GetProgressToken() any { return getProgressToken(x) }
837850
func (x *ListRootsParams) SetProgressToken(t any) { setProgressToken(x, t) }
838851

@@ -858,6 +871,7 @@ type ListToolsParams struct {
858871
}
859872

860873
func (x *ListToolsParams) isParams() {}
874+
func (x *ListToolsParams) isNil() bool { return x == nil }
861875
func (x *ListToolsParams) GetProgressToken() any { return getProgressToken(x) }
862876
func (x *ListToolsParams) SetProgressToken(t any) { setProgressToken(x, t) }
863877
func (x *ListToolsParams) cursorPtr() *string { return &x.Cursor }
@@ -896,6 +910,7 @@ type LoggingMessageParams struct {
896910
}
897911

898912
func (x *LoggingMessageParams) isParams() {}
913+
func (x *LoggingMessageParams) isNil() bool { return x == nil }
899914
func (x *LoggingMessageParams) GetProgressToken() any { return getProgressToken(x) }
900915
func (x *LoggingMessageParams) SetProgressToken(t any) { setProgressToken(x, t) }
901916

@@ -958,6 +973,7 @@ type PingParams struct {
958973
}
959974

960975
func (x *PingParams) isParams() {}
976+
func (x *PingParams) isNil() bool { return x == nil }
961977
func (x *PingParams) GetProgressToken() any { return getProgressToken(x) }
962978
func (x *PingParams) SetProgressToken(t any) { setProgressToken(x, t) }
963979

@@ -978,7 +994,8 @@ type ProgressNotificationParams struct {
978994
Total float64 `json:"total,omitempty"`
979995
}
980996

981-
func (*ProgressNotificationParams) isParams() {}
997+
func (x *ProgressNotificationParams) isParams() {}
998+
func (x *ProgressNotificationParams) isNil() bool { return x == nil }
982999

9831000
// IconTheme specifies the theme an icon is designed for.
9841001
type IconTheme string
@@ -1048,6 +1065,7 @@ type PromptListChangedParams struct {
10481065
}
10491066

10501067
func (x *PromptListChangedParams) isParams() {}
1068+
func (x *PromptListChangedParams) isNil() bool { return x == nil }
10511069
func (x *PromptListChangedParams) GetProgressToken() any { return getProgressToken(x) }
10521070
func (x *PromptListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
10531071

@@ -1089,6 +1107,7 @@ type ReadResourceParams struct {
10891107
}
10901108

10911109
func (x *ReadResourceParams) isParams() {}
1110+
func (x *ReadResourceParams) isNil() bool { return x == nil }
10921111
func (x *ReadResourceParams) GetProgressToken() any { return getProgressToken(x) }
10931112
func (x *ReadResourceParams) SetProgressToken(t any) { setProgressToken(x, t) }
10941113

@@ -1145,6 +1164,7 @@ type ResourceListChangedParams struct {
11451164
}
11461165

11471166
func (x *ResourceListChangedParams) isParams() {}
1167+
func (x *ResourceListChangedParams) isNil() bool { return x == nil }
11481168
func (x *ResourceListChangedParams) GetProgressToken() any { return getProgressToken(x) }
11491169
func (x *ResourceListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
11501170

@@ -1205,6 +1225,7 @@ type RootsListChangedParams struct {
12051225
}
12061226

12071227
func (x *RootsListChangedParams) isParams() {}
1228+
func (x *RootsListChangedParams) isNil() bool { return x == nil }
12081229
func (x *RootsListChangedParams) GetProgressToken() any { return getProgressToken(x) }
12091230
func (x *RootsListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
12101231

@@ -1288,6 +1309,7 @@ type SetLoggingLevelParams struct {
12881309
}
12891310

12901311
func (x *SetLoggingLevelParams) isParams() {}
1312+
func (x *SetLoggingLevelParams) isNil() bool { return x == nil }
12911313
func (x *SetLoggingLevelParams) GetProgressToken() any { return getProgressToken(x) }
12921314
func (x *SetLoggingLevelParams) SetProgressToken(t any) { setProgressToken(x, t) }
12931315

@@ -1390,6 +1412,7 @@ type ToolListChangedParams struct {
13901412
}
13911413

13921414
func (x *ToolListChangedParams) isParams() {}
1415+
func (x *ToolListChangedParams) isNil() bool { return x == nil }
13931416
func (x *ToolListChangedParams) GetProgressToken() any { return getProgressToken(x) }
13941417
func (x *ToolListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
13951418

@@ -1403,7 +1426,8 @@ type SubscribeParams struct {
14031426
URI string `json:"uri"`
14041427
}
14051428

1406-
func (*SubscribeParams) isParams() {}
1429+
func (x *SubscribeParams) isParams() {}
1430+
func (x *SubscribeParams) isNil() bool { return x == nil }
14071431

14081432
// Sent from the client to request cancellation of resources/updated
14091433
// notifications from the server. This should follow a previous
@@ -1416,7 +1440,8 @@ type UnsubscribeParams struct {
14161440
URI string `json:"uri"`
14171441
}
14181442

1419-
func (*UnsubscribeParams) isParams() {}
1443+
func (x *UnsubscribeParams) isParams() {}
1444+
func (x *UnsubscribeParams) isNil() bool { return x == nil }
14201445

14211446
// A notification from the server to the client, informing it that a resource
14221447
// has changed and may need to be read again. This should only be sent if the
@@ -1429,7 +1454,8 @@ type ResourceUpdatedNotificationParams struct {
14291454
URI string `json:"uri"`
14301455
}
14311456

1432-
func (*ResourceUpdatedNotificationParams) isParams() {}
1457+
func (x *ResourceUpdatedNotificationParams) isParams() {}
1458+
func (x *ResourceUpdatedNotificationParams) isNil() bool { return x == nil }
14331459

14341460
// TODO(jba): add CompleteRequest and related types.
14351461

@@ -1469,6 +1495,7 @@ type ElicitParams struct {
14691495
}
14701496

14711497
func (x *ElicitParams) isParams() {}
1498+
func (x *ElicitParams) isNil() bool { return x == nil }
14721499

14731500
func (x *ElicitParams) GetProgressToken() any { return getProgressToken(x) }
14741501
func (x *ElicitParams) SetProgressToken(t any) { setProgressToken(x, t) }
@@ -1500,7 +1527,8 @@ type ElicitationCompleteParams struct {
15001527
ElicitationID string `json:"elicitationId"`
15011528
}
15021529

1503-
func (*ElicitationCompleteParams) isParams() {}
1530+
func (x *ElicitationCompleteParams) isParams() {}
1531+
func (x *ElicitationCompleteParams) isNil() bool { return x == nil }
15041532

15051533
// An Implementation describes the name and version of an MCP implementation, with an optional
15061534
// title for UI representation.

mcp/server.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,15 +1461,15 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
14611461

14621462
switch req.Method {
14631463
case methodInitialize, methodPing, notificationInitialized:
1464-
if usesNewProtocol {
1464+
if usesNewProtocol.usesNewProtocol {
14651465
ss.server.opts.Logger.Error("method removed in the new protocol", "method", req.Method)
14661466
return nil, &jsonrpc.Error{
14671467
Code: jsonrpc.CodeMethodNotFound,
14681468
Message: fmt.Sprintf("%q is not supported in the new protocol", req.Method),
14691469
}
14701470
}
14711471
default:
1472-
if !initialized && !usesNewProtocol {
1472+
if !initialized && !usesNewProtocol.usesNewProtocol {
14731473
ss.server.opts.Logger.Error("method invalid during initialization", "method", req.Method)
14741474
return nil, fmt.Errorf("method %q is invalid during session initialization", req.Method)
14751475
}
@@ -1491,12 +1491,6 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
14911491

14921492
// InitializeParams returns the InitializeParams provided during the client's
14931493
// initial connection.
1494-
//
1495-
// Deprecated: with the >= 2026-06-30 protocol, sessions are sessionless and
1496-
// there is no `initialize` handshake. For new-protocol requests this method
1497-
// returns nil; use the per-request accessors [ServerRequest.ProtocolVersion],
1498-
// [ServerRequest.ClientInfo], and [ServerRequest.ClientCapabilities]
1499-
// instead.
15001494
func (ss *ServerSession) InitializeParams() *InitializeParams {
15011495
ss.mu.Lock()
15021496
defer ss.mu.Unlock()

mcp/server_test.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,19 +1010,10 @@ func TestServerCapabilitiesOverWire(t *testing.T) {
10101010
}
10111011
}
10121012

1013+
// SEP-2575 removes the initialization handshake. An `initialize` request
1014+
// that opts into the new protocol via `_meta.protocolVersion` must be
1015+
// rejected with `Method not found` (-32601).
10131016
func TestServerSessionHandle_RejectsInitializeOnNewProtocol(t *testing.T) {
1014-
// SEP-2575 removes the initialization handshake. An `initialize` request
1015-
// that opts into the new protocol via `_meta.protocolVersion` must be
1016-
// rejected with `Method not found` (-32601).
1017-
mustParams := func(t *testing.T, v any) json.RawMessage {
1018-
t.Helper()
1019-
data, err := json.Marshal(v)
1020-
if err != nil {
1021-
t.Fatal(err)
1022-
}
1023-
return data
1024-
}
1025-
10261017
tests := []struct {
10271018
name string
10281019
params any
@@ -1059,7 +1050,7 @@ func TestServerSessionHandle_RejectsInitializeOnNewProtocol(t *testing.T) {
10591050
req := &jsonrpc.Request{
10601051
ID: id,
10611052
Method: methodInitialize,
1062-
Params: mustParams(t, tc.params),
1053+
Params: mustMarshal(tc.params),
10631054
}
10641055
_, err = ss.handle(context.Background(), req)
10651056
if tc.wantReject {
@@ -1099,7 +1090,7 @@ func TestServerSessionHandle_RejectsInitializeOnNewProtocol(t *testing.T) {
10991090
req := &jsonrpc.Request{
11001091
ID: id,
11011092
Method: methodInitialize,
1102-
Params: mustParams(t, map[string]any{
1093+
Params: mustMarshal(map[string]any{
11031094
"_meta": map[string]any{
11041095
MetaKeyProtocolVersion: protocolVersion20260630,
11051096
MetaKeyClientInfo: map[string]any{"name": "c", "version": "1"},
@@ -1136,14 +1127,6 @@ func TestServerSessionHandle_RejectsInitializeOnNewProtocol(t *testing.T) {
11361127
// `ping`) all return Method not found when the request opts into the new
11371128
// protocol via `_meta.protocolVersion`.
11381129
func TestServerSessionHandle_RejectsRemovedMethodsOnNewProtocol(t *testing.T) {
1139-
mustParams := func(t *testing.T, v any) json.RawMessage {
1140-
t.Helper()
1141-
data, err := json.Marshal(v)
1142-
if err != nil {
1143-
t.Fatal(err)
1144-
}
1145-
return data
1146-
}
11471130
newProtoMeta := map[string]any{
11481131
"_meta": map[string]any{
11491132
MetaKeyProtocolVersion: protocolVersion20260630,
@@ -1171,7 +1154,7 @@ func TestServerSessionHandle_RejectsRemovedMethodsOnNewProtocol(t *testing.T) {
11711154
req := &jsonrpc.Request{
11721155
ID: id,
11731156
Method: tc.method,
1174-
Params: mustParams(t, newProtoMeta),
1157+
Params: mustMarshal(newProtoMeta),
11751158
}
11761159
_, err = ss.handle(context.Background(), req)
11771160
if err == nil {

0 commit comments

Comments
 (0)