mcp: statically type the server-side tool params (#378)

Introduce CallToolParamsRaw on the server side, so that tool handlers
see in the type system that a tool's arguments are a json.RawMessage.

Fixes #377.
This commit is contained in:
Jonathan Amsterdam 2025-08-30 08:16:06 -04:00 • committed by GitHub
commit 7bfde44981
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,6 +40,7 @@ type Annotations struct {
Priority float64 `json:"priority,omitempty"`
}
// CallToolParams is used by clients to call a tool.
type CallToolParams struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.
@ -48,20 +49,13 @@ type CallToolParams struct {
Arguments any `json:"arguments,omitempty"`
}
// When unmarshalling CallToolParams on the server side, we need to delay unmarshaling of the arguments.
func (c *CallToolParams) UnmarshalJSON(data []byte) error {
var raw struct {
Meta `json:"_meta,omitempty"`
Name string `json:"name"`
RawArguments json.RawMessage `json:"arguments,omitempty"`
}
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
c.Meta = raw.Meta
c.Name = raw.Name
c.Arguments = raw.RawArguments
return nil
// CallToolParamsRaw is passed to tool handlers on the server.
type CallToolParamsRaw 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 json.RawMessage `json:"arguments,omitempty"`
}
// The server's response to a tool call.
@ -115,6 +109,10 @@ func (x *CallToolParams) isParams() {}
func (x *CallToolParams) GetProgressToken() any { return getProgressToken(x) }
func (x *CallToolParams) SetProgressToken(t any) { setProgressToken(x, t) }
func (x *CallToolParamsRaw) isParams() {}
func (x *CallToolParamsRaw) GetProgressToken() any { return getProgressToken(x) }
func (x *CallToolParamsRaw) SetProgressToken(t any) { setProgressToken(x, t) }
type CancelledParams struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.