internal/mcp: use RawMessage for tool args
Change the type of CallToolParams.Arguments to json.RawMessage. Change-Id: Ief16ef261b9f41566a1a16efc60a9382a8d414eb Reviewed-on: https://go-review.googlesource.com/c/tools/+/672615 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
6394485a3d
commit
bbd253ee4c
4 changed files with 17 additions and 20 deletions
|
|
@ -181,18 +181,14 @@ func (c *Client) CallTool(ctx context.Context, name string, args map[string]any,
|
|||
err = fmt.Errorf("calling tool %q: %w", name, err)
|
||||
}
|
||||
}()
|
||||
argsJSON := make(map[string]json.RawMessage)
|
||||
for name, arg := range args {
|
||||
argJSON, err := json.Marshal(arg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling argument %s: %v", name, err)
|
||||
}
|
||||
argsJSON[name] = argJSON
|
||||
}
|
||||
|
||||
data, err := json.Marshal(args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshaling arguments: %w", err)
|
||||
}
|
||||
params := &CallToolParams{
|
||||
Name: name,
|
||||
Arguments: argsJSON,
|
||||
Arguments: json.RawMessage(data),
|
||||
}
|
||||
return standardCall[CallToolResult](ctx, c.conn, "tools/call", params)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,8 +53,15 @@ type config map[string]*typeConfig
|
|||
var declarations = config{
|
||||
"Annotations": {},
|
||||
"CallToolRequest": {
|
||||
Name: "-",
|
||||
Fields: config{"Params": {Name: "CallToolParams"}},
|
||||
Name: "-",
|
||||
Fields: config{
|
||||
"Params": {
|
||||
Name: "CallToolParams",
|
||||
Fields: config{
|
||||
"Arguments": {Substitute: "json.RawMessage"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"CallToolResult": {},
|
||||
"CancelledNotification": {
|
||||
|
|
|
|||
|
|
@ -29,8 +29,8 @@ type Annotations struct {
|
|||
}
|
||||
|
||||
type CallToolParams struct {
|
||||
Arguments map[string]json.RawMessage `json:"arguments,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Arguments json.RawMessage `json:"arguments,omitempty"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// The server's response to a tool call.
|
||||
|
|
|
|||
|
|
@ -40,14 +40,8 @@ func NewTool[TReq any](name, description string, handler func(context.Context, *
|
|||
panic(err)
|
||||
}
|
||||
wrapped := func(ctx context.Context, cc *ServerConnection, params *CallToolParams) (*CallToolResult, error) {
|
||||
// For simplicity, just marshal and unmarshal the arguments.
|
||||
// This could be avoided in the future.
|
||||
rawArgs, err := json.Marshal(params.Arguments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var v TReq
|
||||
if err := unmarshalSchema(rawArgs, schema, &v); err != nil {
|
||||
if err := unmarshalSchema(params.Arguments, schema, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content, err := handler(ctx, cc, v)
|
||||
|
|
|
|||
Loading…
Reference in a new issue