Skip to content

Commit 95e8252

Browse files
committed
mcp: move complete resultType into protocol types
1 parent a38032f commit 95e8252

5 files changed

Lines changed: 30 additions & 37 deletions

File tree

mcp/protocol.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ const (
2828
resultTypeInputRequired resultType = "input_required"
2929
)
3030

31+
type completeResultWithType struct {
32+
ResultType resultType `json:"resultType,omitempty"`
33+
}
34+
35+
func (r *completeResultWithType) setResultType(rt resultType) { r.ResultType = rt }
36+
func (*completeResultWithType) isCompleteResult() {}
37+
38+
type completeResultResponse interface {
39+
setResultType(resultType)
40+
isCompleteResult()
41+
}
42+
43+
func setCompleteResultType(res Result) {
44+
if r, ok := res.(completeResultResponse); ok {
45+
r.setResultType(resultTypeComplete)
46+
}
47+
}
48+
3149
// InputRequest is a type for parameters that a server can include in the response
3250
// to request input from client (SEP-2322). Implementations are [*ElicitParams],
3351
// [*CreateMessageParams], and [*ListRootsParams].
@@ -637,6 +655,7 @@ type CompletionResultDetails struct {
637655

638656
// The server's response to a completion/complete request
639657
type CompleteResult struct {
658+
completeResultWithType
640659
// This property is reserved by the protocol to allow clients and servers to
641660
// attach additional metadata to their responses.
642661
Meta `json:"_meta,omitempty"`
@@ -1114,6 +1133,7 @@ func (x *DiscoverParams) GetProgressToken() any { return getProgressToken(x) }
11141133
func (x *DiscoverParams) SetProgressToken(t any) { setProgressToken(x, t) }
11151134

11161135
type DiscoverResult struct {
1136+
completeResultWithType
11171137
Meta `json:"_meta,omitempty"`
11181138
Cacheable
11191139
// The versions of the Model Context Protocol that the server supports.
@@ -1177,6 +1197,7 @@ func (c *Cacheable) setDefaultCacheableValues() {
11771197

11781198
// The server's response to a prompts/list request from the client.
11791199
type ListPromptsResult struct {
1200+
completeResultWithType
11801201
// This property is reserved by the protocol to allow clients and servers to
11811202
// attach additional metadata to their responses.
11821203
Meta `json:"_meta,omitempty"`
@@ -1207,6 +1228,7 @@ func (x *ListResourceTemplatesParams) cursorPtr() *string { return &x.Cursor
12071228

12081229
// The server's response to a resources/templates/list request from the client.
12091230
type ListResourceTemplatesResult struct {
1231+
completeResultWithType
12101232
// This property is reserved by the protocol to allow clients and servers to
12111233
// attach additional metadata to their responses.
12121234
Meta `json:"_meta,omitempty"`
@@ -1237,6 +1259,7 @@ func (x *ListResourcesParams) cursorPtr() *string { return &x.Cursor }
12371259

12381260
// The server's response to a resources/list request from the client.
12391261
type ListResourcesResult struct {
1262+
completeResultWithType
12401263
// This property is reserved by the protocol to allow clients and servers to
12411264
// attach additional metadata to their responses.
12421265
Meta `json:"_meta,omitempty"`
@@ -1303,6 +1326,7 @@ func (x *ListToolsParams) cursorPtr() *string { return &x.Cursor }
13031326

13041327
// The server's response to a tools/list request from the client.
13051328
type ListToolsResult struct {
1329+
completeResultWithType
13061330
// This property is reserved by the protocol to allow clients and servers to
13071331
// attach additional metadata to their responses.
13081332
Meta `json:"_meta,omitempty"`

mcp/protocol_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/google/go-cmp/cmp"
13+
"github.com/google/go-cmp/cmp/cmpopts"
1314
)
1415

1516
func TestParamsMeta(t *testing.T) {
@@ -491,7 +492,7 @@ func TestCompleteResult(t *testing.T) {
491492
if err := json.Unmarshal([]byte(test.in), &got); err != nil {
492493
t.Fatalf("json.Unmarshal(CompleteResult) failed: %v", err)
493494
}
494-
if diff := cmp.Diff(test.want, got); diff != "" {
495+
if diff := cmp.Diff(test.want, got, cmpopts.IgnoreUnexported(CompleteResult{})); diff != "" {
495496
t.Errorf("CompleteResult unmarshal mismatch (-want +got):\n%s", diff)
496497
}
497498
})

mcp/server.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,43 +1906,11 @@ func (ss *ServerSession) handle(ctx context.Context, req *jsonrpc.Request) (any,
19061906
return nil, err
19071907
}
19081908
if validatedMeta.usesNewProtocol {
1909-
return withCompleteResultType(res), nil
1909+
setCompleteResultType(res)
19101910
}
19111911
return res, nil
19121912
}
19131913

1914-
func withCompleteResultType(res Result) any {
1915-
switch res.(type) {
1916-
case *CompleteResult, *DiscoverResult, *ListPromptsResult, *ListResourceTemplatesResult, *ListResourcesResult, *ListToolsResult:
1917-
return completeResult{Result: res}
1918-
default:
1919-
return res
1920-
}
1921-
}
1922-
1923-
type completeResult struct {
1924-
Result
1925-
}
1926-
1927-
func (r completeResult) MarshalJSON() ([]byte, error) {
1928-
data, err := json.Marshal(r.Result)
1929-
if err != nil {
1930-
return nil, err
1931-
}
1932-
data = bytes.TrimSpace(data)
1933-
if len(data) == 2 && data[0] == '{' && data[1] == '}' {
1934-
return []byte(`{"resultType":"complete"}`), nil
1935-
}
1936-
if len(data) < 2 || data[0] != '{' || data[len(data)-1] != '}' {
1937-
return data, nil
1938-
}
1939-
out := make([]byte, 0, len(data)+len(`,"resultType":"complete"`))
1940-
out = append(out, data[:len(data)-1]...)
1941-
out = append(out, `,"resultType":"complete"`...)
1942-
out = append(out, '}')
1943-
return out, nil
1944-
}
1945-
19461914
// InitializeParams returns the InitializeParams provided during the client's
19471915
// initial connection.
19481916
func (ss *ServerSession) InitializeParams() *InitializeParams {

mcp/testdata/conformance/server/discover.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ supports, its capabilities, and its identity.
2121
"jsonrpc": "2.0",
2222
"id": 1,
2323
"result": {
24+
"resultType": "complete",
2425
"ttlMs": 0,
2526
"cacheScope": "public",
2627
"supportedVersions": [
@@ -36,7 +37,6 @@ supports, its capabilities, and its identity.
3637
"serverInfo": {
3738
"name": "testServer",
3839
"version": "v1.0.0"
39-
},
40-
"resultType": "complete"
40+
}
4141
}
4242
}

mcp/transport_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ExampleLoggingTransport() {
4242
}
4343

4444
// Output:
45-
// read: {"jsonrpc":"2.0","id":1,"result":{"ttlMs":0,"cacheScope":"public","supportedVersions":["2026-07-28","2025-11-25","2025-06-18","2025-03-26","2024-11-05"],"capabilities":{"logging":{}},"serverInfo":{"name":"server","version":"v0.0.1"},"resultType":"complete"}}
45+
// read: {"jsonrpc":"2.0","id":1,"result":{"resultType":"complete","ttlMs":0,"cacheScope":"public","supportedVersions":["2026-07-28","2025-11-25","2025-06-18","2025-03-26","2024-11-05"],"capabilities":{"logging":{}},"serverInfo":{"name":"server","version":"v0.0.1"}}}
4646
// write: {"jsonrpc":"2.0","id":1,"method":"server/discover","params":{"_meta":{"io.modelcontextprotocol/clientCapabilities":{"roots":{"listChanged":true}},"io.modelcontextprotocol/clientInfo":{"name":"client","version":"v0.0.1"},"io.modelcontextprotocol/protocolVersion":"2026-07-28"}}}
4747
}
4848

0 commit comments

Comments
 (0)