Generic returns a type-erased version of a TypedTool where the arguments and return values are converted to/from json.RawMessage. This allows the tool to be referenced without knowing the concrete arguments or return values. The original TypedHandlerFunc is wrapped to handle type conversion.
()
| 206 | // or return values. The original TypedHandlerFunc is wrapped to handle type |
| 207 | // conversion. |
| 208 | func (t Tool[Arg, Ret]) Generic() GenericTool { |
| 209 | return GenericTool{ |
| 210 | Tool: t.Tool, |
| 211 | MCPAnnotations: t.MCPAnnotations, |
| 212 | UserClientOptional: t.UserClientOptional, |
| 213 | Handler: wrap(func(ctx context.Context, deps Deps, args json.RawMessage) (json.RawMessage, error) { |
| 214 | var typedArgs Arg |
| 215 | if err := json.Unmarshal(args, &typedArgs); err != nil { |
| 216 | return nil, xerrors.Errorf("failed to unmarshal args: %w", err) |
| 217 | } |
| 218 | ret, err := t.Handler(ctx, deps, typedArgs) |
| 219 | var buf bytes.Buffer |
| 220 | if err := json.NewEncoder(&buf).Encode(ret); err != nil { |
| 221 | return json.RawMessage{}, err |
| 222 | } |
| 223 | return buf.Bytes(), err |
| 224 | }, WithCleanContext, WithRecover), |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // GenericTool is a type-erased wrapper for GenericTool. |
| 229 | // This allows referencing the tool without knowing the concrete argument or |