Commit 36e3a67
committed
mcp: remove tool genericity
DO NOT SUBMIT
TESTS DO NOT PASS YET
API changes to remove genericity from the tool call path.
This makes it easier to write code that can deal with tools generally,
like wrappers around a ToolHandler.
Here is the go doc diff:
--- /tmp/old.doc 2025-08-14 09:03:30.772292329 -0400
+++ /tmp/new.doc 2025-08-14 08:58:37.113063370 -0400
@@ -73,7 +73,7 @@
FUNCTIONS
-func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out])
+func AddTool[In, Out any](s *Server, t *Tool, h TypedToolHandler[In, Out])
AddTool adds a Tool to the server, or replaces one with the same name.
If the tool's input schema is nil, it is set to the schema inferred from
the In type parameter, using jsonschema.For. If the tool's output schema is
@@ -81,6 +81,10 @@
schema is set to the schema inferred from Out. The Tool argument must not be
modified after this call.
+ The handler should return the result as the second return value. The first
+ return value, a *CallToolResult, may be nil, or its fields other than
+ StructuredContent may be populated.
+
func NewInMemoryTransports() (*InMemoryTransport, *InMemoryTransport)
NewInMemoryTransports returns two [InMemoryTransports] that connect to each
other.
@@ -125,24 +129,28 @@
func (c AudioContent) MarshalJSON() ([]byte, error)
-type CallToolParams = CallToolParamsFor[any]
-
-type CallToolParamsFor[In any] struct {
+type CallToolParams struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.
Meta `json:"_meta,omitempty"`
Name string `json:"name"`
- Arguments In `json:"arguments,omitempty"`
+ Arguments any `json:"arguments,omitempty"`
}
-func (x *CallToolParamsFor[Out]) GetProgressToken() any
+func (x *CallToolParams) GetProgressToken() any
-func (x *CallToolParamsFor[Out]) SetProgressToken(t any)
+func (x *CallToolParams) SetProgressToken(t any)
-type CallToolResult = CallToolResultFor[any]
- The server's response to a tool call.
+func (c *CallToolParams) UnmarshalJSON(data []byte) error
+ When unmarshalling CallToolParams on the server side, we need to delay
+ unmarshaling of the arguments.
-type CallToolResultFor[Out any] struct {
+type CallToolRequest struct {
+ Session *ServerSession
+ Params *CallToolParams
+}
+
+type CallToolResult struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.
Meta `json:"_meta,omitempty"`
@@ -151,7 +159,7 @@
Content []Content `json:"content"`
// An optional JSON object that represents the structured result of the tool
// call.
- StructuredContent Out `json:"structuredContent,omitempty"`
+ StructuredContent any `json:"structuredContent,omitempty"`
// Whether the tool call ended in an error.
//
// If not set, this is assumed to be false (the call was successful).
@@ -166,8 +174,9 @@
// should be reported as an MCP error response.
IsError bool `json:"isError,omitempty"`
}
+ The server's response to a tool call.
-func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error
+func (x *CallToolResult) UnmarshalJSON(data []byte) error
UnmarshalJSON handles the unmarshalling of content into the Content
interface.
@@ -283,7 +292,7 @@
Session *ClientSession
Params P
}
- A ClientRequest is a request to a client.
+ A ClientRequest[P] is a request to a client.
func (r *ClientRequest[P]) GetParams() Params
@@ -1532,9 +1541,7 @@
type ServerSession struct {
// Has unexported fields.
}
- A ServerSession is a logical connection from a single MCP client.
- Its methods can be used to send requests or notifications to the client.
- Create a session by calling Server.Connect.
+ a session by calling Server.Connect.
Call ServerSession.Close to close the connection, or await client
termination with ServerSession.Wait.
@@ -1786,6 +1793,8 @@
// If not provided, Annotations.Title should be used for display if present,
// otherwise Name.
Title string `json:"title,omitempty"`
+
+ // Has unexported fields.
}
Definition for a tool the client can call.
@@ -1826,13 +1835,10 @@
Clients should never make tool use decisions based on ToolAnnotations
received from untrusted servers.
-type ToolHandler = ToolHandlerFor[map[string]any, any]
- A ToolHandler handles a call to tools/call. [CallToolParams.Arguments] will
- contain a map[string]any that has been validated against the input schema.
-
-type ToolHandlerFor[In, Out any] func(context.Context, *ServerRequest[*CallToolParamsFor[In]]) (*CallToolResultFor[Out], error)
- A ToolHandlerFor handles a call to tools/call with typed arguments and
- results.
+type ToolHandler func(ctx context.Context, req *ServerRequest[*CallToolParams], args any) (*CallToolResult, error)
+ A ToolHandler handles a call to tools/call. req.Params.Arguments will
+ contain a json.RawMessage containing the arguments. args will contain a
+ value that has been validated against the input schema.
type ToolListChangedParams struct {
// This property is reserved by the protocol to allow clients and servers to
@@ -1856,6 +1862,10 @@
Transports should be used for at most one call to Server.Connect or
Client.Connect.
+type TypedToolHandler[In, Out any] func(context.Context, *ServerRequest[*CallToolParams], In) (*CallToolResult, Out, error)
+ A TypedToolHandler handles a call to tools/call with typed arguments and
+ results.
+
type UnsubscribeParams struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.1 parent a834f3c commit 36e3a67
File tree
14 files changed
+374
-368
lines changed- examples/server/sse
- mcp
14 files changed
+374
-368
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
28 | | - | |
| 27 | + | |
| 28 | + | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
93 | | - | |
94 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
95 | 96 | | |
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
99 | 100 | | |
100 | | - | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
| 21 | + | |
| 22 | + | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
647 | 647 | | |
648 | 648 | | |
649 | 649 | | |
650 | | - | |
| 650 | + | |
651 | 651 | | |
652 | 652 | | |
653 | 653 | | |
| |||
836 | 836 | | |
837 | 837 | | |
838 | 838 | | |
839 | | - | |
| 839 | + | |
840 | 840 | | |
841 | 841 | | |
842 | 842 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
44 | | - | |
45 | | - | |
| 43 | + | |
46 | 44 | | |
47 | 45 | | |
48 | 46 | | |
49 | 47 | | |
50 | | - | |
| 48 | + | |
51 | 49 | | |
52 | 50 | | |
53 | | - | |
54 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
55 | 66 | | |
56 | | - | |
| 67 | + | |
| 68 | + | |
57 | 69 | | |
58 | 70 | | |
59 | 71 | | |
| |||
62 | 74 | | |
63 | 75 | | |
64 | 76 | | |
65 | | - | |
| 77 | + | |
66 | 78 | | |
67 | 79 | | |
68 | 80 | | |
| |||
78 | 90 | | |
79 | 91 | | |
80 | 92 | | |
81 | | - | |
| 93 | + | |
82 | 94 | | |
83 | 95 | | |
84 | 96 | | |
85 | | - | |
86 | | - | |
| 97 | + | |
| 98 | + | |
87 | 99 | | |
88 | 100 | | |
89 | 101 | | |
| |||
95 | 107 | | |
96 | 108 | | |
97 | 109 | | |
98 | | - | |
| 110 | + | |
99 | 111 | | |
100 | 112 | | |
101 | 113 | | |
102 | | - | |
103 | | - | |
104 | | - | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
105 | 117 | | |
106 | 118 | | |
107 | 119 | | |
| |||
867 | 879 | | |
868 | 880 | | |
869 | 881 | | |
| 882 | + | |
| 883 | + | |
870 | 884 | | |
871 | 885 | | |
872 | 886 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| 211 | + | |
211 | 212 | | |
212 | 213 | | |
213 | 214 | | |
| |||
514 | 515 | | |
515 | 516 | | |
516 | 517 | | |
517 | | - | |
518 | | - | |
519 | | - | |
520 | | - | |
521 | | - | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
522 | 525 | | |
523 | | - | |
| 526 | + | |
524 | 527 | | |
525 | 528 | | |
526 | 529 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
| |||
145 | 144 | | |
146 | 145 | | |
147 | 146 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 147 | + | |
158 | 148 | | |
159 | 149 | | |
160 | 150 | | |
| |||
163 | 153 | | |
164 | 154 | | |
165 | 155 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
170 | 162 | | |
171 | 163 | | |
172 | | - | |
173 | | - | |
174 | | - | |
| 164 | + | |
175 | 165 | | |
176 | | - | |
| 166 | + | |
177 | 167 | | |
178 | 168 | | |
179 | 169 | | |
180 | 170 | | |
181 | 171 | | |
182 | 172 | | |
183 | 173 | | |
184 | | - | |
185 | 174 | | |
186 | 175 | | |
187 | 176 | | |
| |||
326 | 315 | | |
327 | 316 | | |
328 | 317 | | |
329 | | - | |
| 318 | + | |
330 | 319 | | |
331 | 320 | | |
332 | 321 | | |
| |||
612 | 601 | | |
613 | 602 | | |
614 | 603 | | |
615 | | - | |
| 604 | + | |
616 | 605 | | |
617 | 606 | | |
618 | 607 | | |
| |||
626 | 615 | | |
627 | 616 | | |
628 | 617 | | |
629 | | - | |
| 618 | + | |
630 | 619 | | |
631 | 620 | | |
632 | 621 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
| 19 | + | |
| 20 | + | |
21 | 21 | | |
22 | | - | |
| 22 | + | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
408 | 408 | | |
409 | 409 | | |
410 | 410 | | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | 411 | | |
416 | 412 | | |
417 | 413 | | |
| |||
0 commit comments