Skip to content

Commit 8186bf3

Browse files
committed
mcp: lock down Params and Result
Add unexported methods to the Params and Result interface, so that they're harder to implement outside the mcp package. It looks like these are the only two interfaces we need to lock down: others are either intentionally open (Transport, Connection), or already closed (Session). Fixes #263
1 parent e9e0da8 commit 8186bf3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

mcp/protocol.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ type CallToolResultFor[Out any] struct {
7878
IsError bool `json:"isError,omitempty"`
7979
}
8080

81+
func (*CallToolResultFor[Out]) mcpResult() {}
82+
8183
// UnmarshalJSON handles the unmarshalling of content into the Content
8284
// interface.
8385
func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error {
@@ -97,6 +99,7 @@ func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error {
9799
return nil
98100
}
99101

102+
func (x *CallToolParamsFor[Out]) mcpParams() {}
100103
func (x *CallToolParamsFor[Out]) GetProgressToken() any { return getProgressToken(x) }
101104
func (x *CallToolParamsFor[Out]) SetProgressToken(t any) { setProgressToken(x, t) }
102105

@@ -114,6 +117,7 @@ type CancelledParams struct {
114117
RequestID any `json:"requestId"`
115118
}
116119

120+
func (x *CancelledParams) mcpParams() {}
117121
func (x *CancelledParams) GetProgressToken() any { return getProgressToken(x) }
118122
func (x *CancelledParams) SetProgressToken(t any) { setProgressToken(x, t) }
119123

@@ -207,6 +211,8 @@ type CompleteParams struct {
207211
Ref *CompleteReference `json:"ref"`
208212
}
209213

214+
func (*CompleteParams) mcpParams() {}
215+
210216
type CompletionResultDetails struct {
211217
HasMore bool `json:"hasMore,omitempty"`
212218
Total int `json:"total,omitempty"`
@@ -221,6 +227,8 @@ type CompleteResult struct {
221227
Completion CompletionResultDetails `json:"completion"`
222228
}
223229

230+
func (*CompleteResult) mcpResult() {}
231+
224232
type CreateMessageParams struct {
225233
// This property is reserved by the protocol to allow clients and servers to
226234
// attach additional metadata to their responses.
@@ -245,6 +253,7 @@ type CreateMessageParams struct {
245253
Temperature float64 `json:"temperature,omitempty"`
246254
}
247255

256+
func (x *CreateMessageParams) mcpParams() {}
248257
func (x *CreateMessageParams) GetProgressToken() any { return getProgressToken(x) }
249258
func (x *CreateMessageParams) SetProgressToken(t any) { setProgressToken(x, t) }
250259

@@ -264,6 +273,7 @@ type CreateMessageResult struct {
264273
StopReason string `json:"stopReason,omitempty"`
265274
}
266275

276+
func (*CreateMessageResult) mcpResult() {}
267277
func (r *CreateMessageResult) UnmarshalJSON(data []byte) error {
268278
type result CreateMessageResult // avoid recursion
269279
var wire struct {
@@ -291,6 +301,7 @@ type GetPromptParams struct {
291301
Name string `json:"name"`
292302
}
293303

304+
func (x *GetPromptParams) mcpParams() {}
294305
func (x *GetPromptParams) GetProgressToken() any { return getProgressToken(x) }
295306
func (x *GetPromptParams) SetProgressToken(t any) { setProgressToken(x, t) }
296307

@@ -304,6 +315,8 @@ type GetPromptResult struct {
304315
Messages []*PromptMessage `json:"messages"`
305316
}
306317

318+
func (*GetPromptResult) mcpResult() {}
319+
307320
type InitializeParams struct {
308321
// This property is reserved by the protocol to allow clients and servers to
309322
// attach additional metadata to their responses.
@@ -315,6 +328,7 @@ type InitializeParams struct {
315328
ProtocolVersion string `json:"protocolVersion"`
316329
}
317330

331+
func (x *InitializeParams) mcpParams() {}
318332
func (x *InitializeParams) GetProgressToken() any { return getProgressToken(x) }
319333
func (x *InitializeParams) SetProgressToken(t any) { setProgressToken(x, t) }
320334

@@ -338,12 +352,15 @@ type InitializeResult struct {
338352
ServerInfo *Implementation `json:"serverInfo"`
339353
}
340354

355+
func (*InitializeResult) mcpResult() {}
356+
341357
type InitializedParams struct {
342358
// This property is reserved by the protocol to allow clients and servers to
343359
// attach additional metadata to their responses.
344360
Meta `json:"_meta,omitempty"`
345361
}
346362

363+
func (x *InitializedParams) mcpParams() {}
347364
func (x *InitializedParams) GetProgressToken() any { return getProgressToken(x) }
348365
func (x *InitializedParams) SetProgressToken(t any) { setProgressToken(x, t) }
349366

@@ -356,6 +373,7 @@ type ListPromptsParams struct {
356373
Cursor string `json:"cursor,omitempty"`
357374
}
358375

376+
func (x *ListPromptsParams) mcpParams() {}
359377
func (x *ListPromptsParams) GetProgressToken() any { return getProgressToken(x) }
360378
func (x *ListPromptsParams) SetProgressToken(t any) { setProgressToken(x, t) }
361379
func (x *ListPromptsParams) cursorPtr() *string { return &x.Cursor }
@@ -371,6 +389,7 @@ type ListPromptsResult struct {
371389
Prompts []*Prompt `json:"prompts"`
372390
}
373391

392+
func (x *ListPromptsResult) mcpResult() {}
374393
func (x *ListPromptsResult) nextCursorPtr() *string { return &x.NextCursor }
375394

376395
type ListResourceTemplatesParams struct {
@@ -382,6 +401,7 @@ type ListResourceTemplatesParams struct {
382401
Cursor string `json:"cursor,omitempty"`
383402
}
384403

404+
func (x *ListResourceTemplatesParams) mcpParams() {}
385405
func (x *ListResourceTemplatesParams) GetProgressToken() any { return getProgressToken(x) }
386406
func (x *ListResourceTemplatesParams) SetProgressToken(t any) { setProgressToken(x, t) }
387407
func (x *ListResourceTemplatesParams) cursorPtr() *string { return &x.Cursor }
@@ -397,6 +417,7 @@ type ListResourceTemplatesResult struct {
397417
ResourceTemplates []*ResourceTemplate `json:"resourceTemplates"`
398418
}
399419

420+
func (x *ListResourceTemplatesResult) mcpResult() {}
400421
func (x *ListResourceTemplatesResult) nextCursorPtr() *string { return &x.NextCursor }
401422

402423
type ListResourcesParams struct {
@@ -408,6 +429,7 @@ type ListResourcesParams struct {
408429
Cursor string `json:"cursor,omitempty"`
409430
}
410431

432+
func (x *ListResourcesParams) mcpParams() {}
411433
func (x *ListResourcesParams) GetProgressToken() any { return getProgressToken(x) }
412434
func (x *ListResourcesParams) SetProgressToken(t any) { setProgressToken(x, t) }
413435
func (x *ListResourcesParams) cursorPtr() *string { return &x.Cursor }
@@ -423,6 +445,7 @@ type ListResourcesResult struct {
423445
Resources []*Resource `json:"resources"`
424446
}
425447

448+
func (x *ListResourcesResult) mcpResult() {}
426449
func (x *ListResourcesResult) nextCursorPtr() *string { return &x.NextCursor }
427450

428451
type ListRootsParams struct {
@@ -431,6 +454,7 @@ type ListRootsParams struct {
431454
Meta `json:"_meta,omitempty"`
432455
}
433456

457+
func (x *ListRootsParams) mcpParams() {}
434458
func (x *ListRootsParams) GetProgressToken() any { return getProgressToken(x) }
435459
func (x *ListRootsParams) SetProgressToken(t any) { setProgressToken(x, t) }
436460

@@ -444,6 +468,8 @@ type ListRootsResult struct {
444468
Roots []*Root `json:"roots"`
445469
}
446470

471+
func (*ListRootsResult) mcpResult() {}
472+
447473
type ListToolsParams struct {
448474
// This property is reserved by the protocol to allow clients and servers to
449475
// attach additional metadata to their responses.
@@ -453,6 +479,7 @@ type ListToolsParams struct {
453479
Cursor string `json:"cursor,omitempty"`
454480
}
455481

482+
func (x *ListToolsParams) mcpParams() {}
456483
func (x *ListToolsParams) GetProgressToken() any { return getProgressToken(x) }
457484
func (x *ListToolsParams) SetProgressToken(t any) { setProgressToken(x, t) }
458485
func (x *ListToolsParams) cursorPtr() *string { return &x.Cursor }
@@ -468,6 +495,7 @@ type ListToolsResult struct {
468495
Tools []*Tool `json:"tools"`
469496
}
470497

498+
func (x *ListToolsResult) mcpResult() {}
471499
func (x *ListToolsResult) nextCursorPtr() *string { return &x.NextCursor }
472500

473501
// The severity of a log message.
@@ -489,6 +517,7 @@ type LoggingMessageParams struct {
489517
Logger string `json:"logger,omitempty"`
490518
}
491519

520+
func (x *LoggingMessageParams) mcpParams() {}
492521
func (x *LoggingMessageParams) GetProgressToken() any { return getProgressToken(x) }
493522
func (x *LoggingMessageParams) SetProgressToken(t any) { setProgressToken(x, t) }
494523

@@ -550,6 +579,7 @@ type PingParams struct {
550579
Meta `json:"_meta,omitempty"`
551580
}
552581

582+
func (x *PingParams) mcpParams() {}
553583
func (x *PingParams) GetProgressToken() any { return getProgressToken(x) }
554584
func (x *PingParams) SetProgressToken(t any) { setProgressToken(x, t) }
555585

@@ -569,6 +599,8 @@ type ProgressNotificationParams struct {
569599
Total float64 `json:"total,omitempty"`
570600
}
571601

602+
func (*ProgressNotificationParams) mcpParams() {}
603+
572604
// A prompt or prompt template that the server offers.
573605
type Prompt struct {
574606
// See [specification/2025-06-18/basic/index#general-fields] for notes on _meta
@@ -606,6 +638,7 @@ type PromptListChangedParams struct {
606638
Meta `json:"_meta,omitempty"`
607639
}
608640

641+
func (x *PromptListChangedParams) mcpParams() {}
609642
func (x *PromptListChangedParams) GetProgressToken() any { return getProgressToken(x) }
610643
func (x *PromptListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
611644

@@ -646,6 +679,7 @@ type ReadResourceParams struct {
646679
URI string `json:"uri"`
647680
}
648681

682+
func (x *ReadResourceParams) mcpParams() {}
649683
func (x *ReadResourceParams) GetProgressToken() any { return getProgressToken(x) }
650684
func (x *ReadResourceParams) SetProgressToken(t any) { setProgressToken(x, t) }
651685

@@ -657,6 +691,8 @@ type ReadResourceResult struct {
657691
Contents []*ResourceContents `json:"contents"`
658692
}
659693

694+
func (*ReadResourceResult) mcpResult() {}
695+
660696
// A known resource that the server is capable of reading.
661697
type Resource struct {
662698
// See [specification/2025-06-18/basic/index#general-fields] for notes on _meta
@@ -697,6 +733,7 @@ type ResourceListChangedParams struct {
697733
Meta `json:"_meta,omitempty"`
698734
}
699735

736+
func (x *ResourceListChangedParams) mcpParams() {}
700737
func (x *ResourceListChangedParams) GetProgressToken() any { return getProgressToken(x) }
701738
func (x *ResourceListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
702739

@@ -754,6 +791,7 @@ type RootsListChangedParams struct {
754791
Meta `json:"_meta,omitempty"`
755792
}
756793

794+
func (x *RootsListChangedParams) mcpParams() {}
757795
func (x *RootsListChangedParams) GetProgressToken() any { return getProgressToken(x) }
758796
func (x *RootsListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
759797

@@ -798,6 +836,7 @@ type SetLevelParams struct {
798836
Level LoggingLevel `json:"level"`
799837
}
800838

839+
func (x *SetLevelParams) mcpParams() {}
801840
func (x *SetLevelParams) GetProgressToken() any { return getProgressToken(x) }
802841
func (x *SetLevelParams) SetProgressToken(t any) { setProgressToken(x, t) }
803842

@@ -873,6 +912,7 @@ type ToolListChangedParams struct {
873912
Meta `json:"_meta,omitempty"`
874913
}
875914

915+
func (x *ToolListChangedParams) mcpParams() {}
876916
func (x *ToolListChangedParams) GetProgressToken() any { return getProgressToken(x) }
877917
func (x *ToolListChangedParams) SetProgressToken(t any) { setProgressToken(x, t) }
878918

@@ -886,6 +926,8 @@ type SubscribeParams struct {
886926
URI string `json:"uri"`
887927
}
888928

929+
func (*SubscribeParams) mcpParams() {}
930+
889931
// Sent from the client to request cancellation of resources/updated
890932
// notifications from the server. This should follow a previous
891933
// resources/subscribe request.
@@ -897,6 +939,8 @@ type UnsubscribeParams struct {
897939
URI string `json:"uri"`
898940
}
899941

942+
func (*UnsubscribeParams) mcpParams() {}
943+
900944
// A notification from the server to the client, informing it that a resource
901945
// has changed and may need to be read again. This should only be sent if the
902946
// client previously sent a resources/subscribe request.
@@ -908,6 +952,8 @@ type ResourceUpdatedNotificationParams struct {
908952
URI string `json:"uri"`
909953
}
910954

955+
func (*ResourceUpdatedNotificationParams) mcpParams() {}
956+
911957
// TODO(jba): add CompleteRequest and related types.
912958

913959
// TODO(jba): add ElicitRequest and related types.

mcp/shared.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ func setProgressToken(p Params, pt any) {
335335

336336
// Params is a parameter (input) type for an MCP call or notification.
337337
type Params interface {
338+
// mcpParams discourages implementation of Params outside of this package.
339+
mcpParams()
340+
338341
// GetMeta returns metadata from a value.
339342
GetMeta() map[string]any
340343
// SetMeta sets the metadata on a value.
@@ -356,6 +359,9 @@ type RequestParams interface {
356359

357360
// Result is a result of an MCP call.
358361
type Result interface {
362+
// mcpResult discourages implementation of Result outside of this package.
363+
mcpResult()
364+
359365
// GetMeta returns metadata from a value.
360366
GetMeta() map[string]any
361367
// SetMeta sets the metadata on a value.
@@ -366,6 +372,7 @@ type Result interface {
366372
// Those methods cannot return nil, because jsonrpc2 cannot handle nils.
367373
type emptyResult struct{}
368374

375+
func (*emptyResult) mcpResult() {}
369376
func (*emptyResult) GetMeta() map[string]any { panic("should never be called") }
370377
func (*emptyResult) SetMeta(map[string]any) { panic("should never be called") }
371378

0 commit comments

Comments
 (0)