diff --git a/internal/mcp/mcp_request.go b/internal/mcp/mcp_request.go index 4b41db1570..05b4552e49 100644 --- a/internal/mcp/mcp_request.go +++ b/internal/mcp/mcp_request.go @@ -111,6 +111,7 @@ func DecodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error) Result struct { Content []json.RawMessage `json:"content"` StructuredContent map[string]json.RawMessage `json:"structuredContent"` + IsError bool `json:"isError"` } `json:"result"` Error *struct { Code int `json:"code"` @@ -124,6 +125,19 @@ func DecodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error) return nil, errors.Newf("MCP tools/call failed: %d %s", jsonRPCResp.Error.Code, jsonRPCResp.Error.Message) } + if jsonRPCResp.Result.IsError { + if len(jsonRPCResp.Result.Content) > 0 { + var textContent struct { + Text string `json:"text"` + } + if err := json.Unmarshal(jsonRPCResp.Result.Content[0], &textContent); err == nil && textContent.Text != "" { + return nil, errors.Newf("MCP tool error: %s", textContent.Text) + } + return nil, errors.Newf("MCP tool error: %s", string(jsonRPCResp.Result.Content[0])) + } + return nil, errors.New("MCP tool returned an error") + } + return jsonRPCResp.Result.StructuredContent, nil } func readSSEResponseData(resp *http.Response) ([]byte, error) {