Skip to content

Commit beed2f0

Browse files
authored
mcp: small changes to CallTool structs (#60)
- Use `any` for CallToolResult.StructuredContent. - Change the non-generic structs into aliases for the generic ones.
1 parent bc59932 commit beed2f0

File tree

3 files changed

+10
-61
lines changed

3 files changed

+10
-61
lines changed

mcp/content_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package mcp_test
77
import (
88
"encoding/json"
99
"fmt"
10-
"log"
1110
"testing"
1211

1312
"github.com/google/go-cmp/cmp"
@@ -94,7 +93,6 @@ func TestContent(t *testing.T) {
9493
t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff)
9594
}
9695
result := fmt.Sprintf(`{"content":[%s]}`, string(got))
97-
log.Println(result)
9896
var out mcp.CallToolResult
9997
if err := json.Unmarshal([]byte(result), &out); err != nil {
10098
t.Fatal(err)

mcp/protocol.go

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,18 @@ type Annotations struct {
3939
Priority float64 `json:"priority,omitempty"`
4040
}
4141

42-
type CallToolParams struct {
43-
// This property is reserved by the protocol to allow clients and servers to
44-
// attach additional metadata to their responses.
45-
Meta `json:"_meta,omitempty"`
46-
Arguments any `json:"arguments,omitempty"`
47-
Name string `json:"name"`
48-
}
49-
50-
func (x *CallToolParams) GetProgressToken() any { return getProgressToken(x) }
51-
func (x *CallToolParams) SetProgressToken(t any) { setProgressToken(x, t) }
42+
type CallToolParams = CallToolParamsFor[any]
5243

5344
type CallToolParamsFor[In any] struct {
5445
// This property is reserved by the protocol to allow clients and servers to
5546
// attach additional metadata to their responses.
5647
Meta `json:"_meta,omitempty"`
57-
Arguments In `json:"arguments,omitempty"`
5848
Name string `json:"name"`
49+
Arguments In `json:"arguments,omitempty"`
5950
}
6051

6152
// The server's response to a tool call.
62-
type CallToolResult struct {
63-
// This property is reserved by the protocol to allow clients and servers to
64-
// attach additional metadata to their responses.
65-
Meta `json:"_meta,omitempty"`
66-
// A list of content objects that represent the unstructured result of the tool
67-
// call.
68-
Content []Content `json:"content"`
69-
// Whether the tool call ended in an error.
70-
//
71-
// If not set, this is assumed to be false (the call was successful).
72-
//
73-
// Any errors that originate from the tool should be reported inside the result
74-
// object, with isError set to true, not as an MCP protocol-level error
75-
// response. Otherwise, the LLM would not be able to see that an error occurred
76-
// and self-correct.
77-
//
78-
// However, any errors in finding the tool, an error indicating that the
79-
// server does not support tool calls, or any other exceptional conditions,
80-
// should be reported as an MCP error response.
81-
IsError bool `json:"isError,omitempty"`
82-
// An optional JSON object that represents the structured result of the tool
83-
// call.
84-
// TODO(jba,rfindley): should this be any?
85-
StructuredContent map[string]any `json:"structuredContent,omitempty"`
86-
}
87-
88-
// UnmarshalJSON handles the unmarshalling of content into the Content
89-
// interface.
90-
func (x *CallToolResult) UnmarshalJSON(data []byte) error {
91-
type res CallToolResult // avoid recursion
92-
var wire struct {
93-
res
94-
Content []*wireContent `json:"content"`
95-
}
96-
if err := json.Unmarshal(data, &wire); err != nil {
97-
return err
98-
}
99-
var err error
100-
if wire.res.Content, err = contentsFromWire(wire.Content, nil); err != nil {
101-
return err
102-
}
103-
*x = CallToolResult(wire.res)
104-
return nil
105-
}
53+
type CallToolResult = CallToolResultFor[any]
10654

10755
type CallToolResultFor[Out any] struct {
10856
// This property is reserved by the protocol to allow clients and servers to
@@ -111,6 +59,9 @@ type CallToolResultFor[Out any] struct {
11159
// A list of content objects that represent the unstructured result of the tool
11260
// call.
11361
Content []Content `json:"content"`
62+
// An optional JSON object that represents the structured result of the tool
63+
// call.
64+
StructuredContent Out `json:"structuredContent,omitempty"`
11465
// Whether the tool call ended in an error.
11566
//
11667
// If not set, this is assumed to be false (the call was successful).
@@ -124,9 +75,6 @@ type CallToolResultFor[Out any] struct {
12475
// server does not support tool calls, or any other exceptional conditions,
12576
// should be reported as an MCP error response.
12677
IsError bool `json:"isError,omitempty"`
127-
// An optional JSON object that represents the structured result of the tool
128-
// call.
129-
StructuredContent Out `json:"structuredContent,omitempty"`
13078
}
13179

13280
// UnmarshalJSON handles the unmarshalling of content into the Content
@@ -148,6 +96,9 @@ func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error {
14896
return nil
14997
}
15098

99+
func (x *CallToolParamsFor[Out]) GetProgressToken() any { return getProgressToken(x) }
100+
func (x *CallToolParamsFor[Out]) SetProgressToken(t any) { setProgressToken(x, t) }
101+
151102
type CancelledParams struct {
152103
// This property is reserved by the protocol to allow clients and servers to
153104
// attach additional metadata to their responses.

mcp/tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// A ToolHandler handles a call to tools/call.
1818
// [CallToolParams.Arguments] will contain a map[string]any that has been validated
1919
// against the input schema.
20-
// Perhaps this should be an alias for ToolHandlerFor[map[string]any, map[string]any].
20+
// TODO: Perhaps this should be an alias for ToolHandlerFor[map[string]any, map[string]any]?
2121
type ToolHandler func(context.Context, *ServerSession, *CallToolParamsFor[map[string]any]) (*CallToolResult, error)
2222

2323
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.

0 commit comments

Comments
 (0)