MCPcopy Index your code
hub / github.com/coder/coder / Generic

Method Generic

codersdk/toolsdk/toolsdk.go:208–226  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

206// or return values. The original TypedHandlerFunc is wrapped to handle type
207// conversion.
208func (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

Callers 3

testToolFunction · 0.80
TestWorkspaceBashFunction · 0.80
toolsdk.goFile · 0.80

Calls 6

wrapFunction · 0.85
EncodeMethod · 0.80
UnmarshalMethod · 0.45
ErrorfMethod · 0.45
HandlerMethod · 0.45
BytesMethod · 0.45

Tested by 2

testToolFunction · 0.64
TestWorkspaceBashFunction · 0.64