mcp: small changes to CallTool structs (#60)
- Use `any` for CallToolResult.StructuredContent. - Change the non-generic structs into aliases for the generic ones.
This commit is contained in:
parent
bc59932176
commit
beed2f0677
3 changed files with 10 additions and 61 deletions
|
|
@ -7,7 +7,6 @@ package mcp_test
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
@ -94,7 +93,6 @@ func TestContent(t *testing.T) {
|
||||||
t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff)
|
t.Errorf("json.Marshal(%v) mismatch (-want +got):\n%s", test.in, diff)
|
||||||
}
|
}
|
||||||
result := fmt.Sprintf(`{"content":[%s]}`, string(got))
|
result := fmt.Sprintf(`{"content":[%s]}`, string(got))
|
||||||
log.Println(result)
|
|
||||||
var out mcp.CallToolResult
|
var out mcp.CallToolResult
|
||||||
if err := json.Unmarshal([]byte(result), &out); err != nil {
|
if err := json.Unmarshal([]byte(result), &out); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
||||||
|
|
@ -39,70 +39,18 @@ type Annotations struct {
|
||||||
Priority float64 `json:"priority,omitempty"`
|
Priority float64 `json:"priority,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CallToolParams struct {
|
type CallToolParams = CallToolParamsFor[any]
|
||||||
// This property is reserved by the protocol to allow clients and servers to
|
|
||||||
// attach additional metadata to their responses.
|
|
||||||
Meta `json:"_meta,omitempty"`
|
|
||||||
Arguments any `json:"arguments,omitempty"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *CallToolParams) GetProgressToken() any { return getProgressToken(x) }
|
|
||||||
func (x *CallToolParams) SetProgressToken(t any) { setProgressToken(x, t) }
|
|
||||||
|
|
||||||
type CallToolParamsFor[In any] struct {
|
type CallToolParamsFor[In any] struct {
|
||||||
// This property is reserved by the protocol to allow clients and servers to
|
// This property is reserved by the protocol to allow clients and servers to
|
||||||
// attach additional metadata to their responses.
|
// attach additional metadata to their responses.
|
||||||
Meta `json:"_meta,omitempty"`
|
Meta `json:"_meta,omitempty"`
|
||||||
Arguments In `json:"arguments,omitempty"`
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Arguments In `json:"arguments,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// The server's response to a tool call.
|
// The server's response to a tool call.
|
||||||
type CallToolResult struct {
|
type CallToolResult = CallToolResultFor[any]
|
||||||
// This property is reserved by the protocol to allow clients and servers to
|
|
||||||
// attach additional metadata to their responses.
|
|
||||||
Meta `json:"_meta,omitempty"`
|
|
||||||
// A list of content objects that represent the unstructured result of the tool
|
|
||||||
// call.
|
|
||||||
Content []Content `json:"content"`
|
|
||||||
// Whether the tool call ended in an error.
|
|
||||||
//
|
|
||||||
// If not set, this is assumed to be false (the call was successful).
|
|
||||||
//
|
|
||||||
// Any errors that originate from the tool should be reported inside the result
|
|
||||||
// object, with isError set to true, not as an MCP protocol-level error
|
|
||||||
// response. Otherwise, the LLM would not be able to see that an error occurred
|
|
||||||
// and self-correct.
|
|
||||||
//
|
|
||||||
// However, any errors in finding the tool, an error indicating that the
|
|
||||||
// server does not support tool calls, or any other exceptional conditions,
|
|
||||||
// should be reported as an MCP error response.
|
|
||||||
IsError bool `json:"isError,omitempty"`
|
|
||||||
// An optional JSON object that represents the structured result of the tool
|
|
||||||
// call.
|
|
||||||
// TODO(jba,rfindley): should this be any?
|
|
||||||
StructuredContent map[string]any `json:"structuredContent,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalJSON handles the unmarshalling of content into the Content
|
|
||||||
// interface.
|
|
||||||
func (x *CallToolResult) UnmarshalJSON(data []byte) error {
|
|
||||||
type res CallToolResult // avoid recursion
|
|
||||||
var wire struct {
|
|
||||||
res
|
|
||||||
Content []*wireContent `json:"content"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(data, &wire); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
if wire.res.Content, err = contentsFromWire(wire.Content, nil); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*x = CallToolResult(wire.res)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type CallToolResultFor[Out any] struct {
|
type CallToolResultFor[Out any] struct {
|
||||||
// This property is reserved by the protocol to allow clients and servers to
|
// This property is reserved by the protocol to allow clients and servers to
|
||||||
|
|
@ -111,6 +59,9 @@ type CallToolResultFor[Out any] struct {
|
||||||
// A list of content objects that represent the unstructured result of the tool
|
// A list of content objects that represent the unstructured result of the tool
|
||||||
// call.
|
// call.
|
||||||
Content []Content `json:"content"`
|
Content []Content `json:"content"`
|
||||||
|
// An optional JSON object that represents the structured result of the tool
|
||||||
|
// call.
|
||||||
|
StructuredContent Out `json:"structuredContent,omitempty"`
|
||||||
// Whether the tool call ended in an error.
|
// Whether the tool call ended in an error.
|
||||||
//
|
//
|
||||||
// If not set, this is assumed to be false (the call was successful).
|
// If not set, this is assumed to be false (the call was successful).
|
||||||
|
|
@ -124,9 +75,6 @@ type CallToolResultFor[Out any] struct {
|
||||||
// server does not support tool calls, or any other exceptional conditions,
|
// server does not support tool calls, or any other exceptional conditions,
|
||||||
// should be reported as an MCP error response.
|
// should be reported as an MCP error response.
|
||||||
IsError bool `json:"isError,omitempty"`
|
IsError bool `json:"isError,omitempty"`
|
||||||
// An optional JSON object that represents the structured result of the tool
|
|
||||||
// call.
|
|
||||||
StructuredContent Out `json:"structuredContent,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON handles the unmarshalling of content into the Content
|
// UnmarshalJSON handles the unmarshalling of content into the Content
|
||||||
|
|
@ -148,6 +96,9 @@ func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *CallToolParamsFor[Out]) GetProgressToken() any { return getProgressToken(x) }
|
||||||
|
func (x *CallToolParamsFor[Out]) SetProgressToken(t any) { setProgressToken(x, t) }
|
||||||
|
|
||||||
type CancelledParams struct {
|
type CancelledParams struct {
|
||||||
// This property is reserved by the protocol to allow clients and servers to
|
// This property is reserved by the protocol to allow clients and servers to
|
||||||
// attach additional metadata to their responses.
|
// attach additional metadata to their responses.
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ import (
|
||||||
// A ToolHandler handles a call to tools/call.
|
// A ToolHandler handles a call to tools/call.
|
||||||
// [CallToolParams.Arguments] will contain a map[string]any that has been validated
|
// [CallToolParams.Arguments] will contain a map[string]any that has been validated
|
||||||
// against the input schema.
|
// against the input schema.
|
||||||
// Perhaps this should be an alias for ToolHandlerFor[map[string]any, map[string]any].
|
// TODO: Perhaps this should be an alias for ToolHandlerFor[map[string]any, map[string]any]?
|
||||||
type ToolHandler func(context.Context, *ServerSession, *CallToolParamsFor[map[string]any]) (*CallToolResult, error)
|
type ToolHandler func(context.Context, *ServerSession, *CallToolParamsFor[map[string]any]) (*CallToolResult, error)
|
||||||
|
|
||||||
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.
|
// A ToolHandlerFor handles a call to tools/call with typed arguments and results.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue