handleCallTool proxies a tool invocation to the appropriate MCP server based on the tool name prefix.
(rw http.ResponseWriter, r *http.Request)
| 65 | // handleCallTool proxies a tool invocation to the appropriate |
| 66 | // MCP server based on the tool name prefix. |
| 67 | func (api *API) handleCallTool(rw http.ResponseWriter, r *http.Request) { |
| 68 | ctx := r.Context() |
| 69 | |
| 70 | var req workspacesdk.CallMCPToolRequest |
| 71 | if !httpapi.Read(ctx, rw, r, &req) { |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | resp, err := api.manager.CallTool(ctx, req) |
| 76 | if err != nil { |
| 77 | status := http.StatusBadGateway |
| 78 | if errors.Is(err, ErrInvalidToolName) { |
| 79 | status = http.StatusBadRequest |
| 80 | } else if errors.Is(err, ErrUnknownServer) { |
| 81 | status = http.StatusNotFound |
| 82 | } |
| 83 | httpapi.Write(ctx, rw, status, codersdk.Response{ |
| 84 | Message: "MCP tool call failed.", |
| 85 | Detail: err.Error(), |
| 86 | }) |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | httpapi.Write(ctx, rw, http.StatusOK, resp) |
| 91 | } |