| 58 | } |
| 59 | |
| 60 | func (t *WorkspaceMCPTool) Run( |
| 61 | ctx context.Context, |
| 62 | params fantasy.ToolCall, |
| 63 | ) (fantasy.ToolResponse, error) { |
| 64 | conn, err := t.getConn(ctx) |
| 65 | if err != nil { |
| 66 | return fantasy.NewTextErrorResponse( |
| 67 | "workspace connection failed: " + err.Error(), |
| 68 | ), nil |
| 69 | } |
| 70 | |
| 71 | var args map[string]any |
| 72 | if params.Input != "" { |
| 73 | if err := json.Unmarshal( |
| 74 | []byte(params.Input), &args, |
| 75 | ); err != nil { |
| 76 | return fantasy.NewTextErrorResponse( |
| 77 | "invalid JSON input: " + err.Error(), |
| 78 | ), nil |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | resp, err := conn.CallMCPTool(ctx, workspacesdk.CallMCPToolRequest{ |
| 83 | ToolName: t.info.Name, |
| 84 | Arguments: args, |
| 85 | }) |
| 86 | if err != nil { |
| 87 | // If the agent returns a 404 (ErrUnknownServer), the |
| 88 | // server was removed or renamed. Invalidate the chat's |
| 89 | // cached tool list so the next turn refetches. |
| 90 | var coderErr *codersdk.Error |
| 91 | if errors.As(err, &coderErr) && coderErr.StatusCode() == http.StatusNotFound { |
| 92 | if t.invalidateCache != nil { |
| 93 | t.invalidateCache() |
| 94 | } |
| 95 | } |
| 96 | return fantasy.NewTextErrorResponse(err.Error()), nil |
| 97 | } |
| 98 | |
| 99 | return convertMCPToolResponse(resp), nil |
| 100 | } |
| 101 | |
| 102 | func (t *WorkspaceMCPTool) ProviderOptions() fantasy.ProviderOptions { |
| 103 | return t.providerOpts |