MCPcopy
hub / github.com/docker/docker-agent / invokeTool

Function invokeTool

pkg/tools/codemode/exec.go:139–178  ·  view source on GitHub ↗

invokeTool calls a single tool handler, filtering out nil optional arguments. It returns the output, the filtered arguments actually sent, and any error.

(ctx context.Context, tool tools.Tool, args map[string]any)

Source from the content-addressed store, hash-verified

137// invokeTool calls a single tool handler, filtering out nil optional arguments.
138// It returns the output, the filtered arguments actually sent, and any error.
139func invokeTool(ctx context.Context, tool tools.Tool, args map[string]any) (string, map[string]any, error) {
140 if tool.Handler == nil {
141 return "", args, fmt.Errorf("tool %q is not available in code mode", tool.Name)
142 }
143
144 var schema struct {
145 Required []string `json:"required"`
146 }
147 if err := tools.ConvertSchema(tool.Parameters, &schema); err != nil {
148 return "", args, err
149 }
150
151 // Strip nil optional arguments that goja passes for omitted parameters.
152 filtered := make(map[string]any)
153 for k, v := range args {
154 if slices.Contains(schema.Required, k) || v != nil {
155 filtered[k] = v
156 }
157 }
158
159 arguments, err := json.Marshal(filtered)
160 if err != nil {
161 return "", filtered, err
162 }
163
164 result, err := tool.Handler(ctx, tools.ToolCall{
165 Function: tools.FunctionCall{
166 Name: tool.Name,
167 Arguments: string(arguments),
168 },
169 })
170 if err != nil {
171 return "", filtered, err
172 }
173
174 if result == nil {
175 return "", filtered, nil
176 }
177 return result.Output, filtered, nil
178}

Callers 1

callToolFunction · 0.85

Calls 1

ConvertSchemaFunction · 0.92

Tested by

no test coverage detected