Skip to content

Commit 97c3004

Browse files
authored
Handle MCP isError response flag (#1236)
1 parent ee83af1 commit 97c3004

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

internal/mcp/mcp_request.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func DecodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error)
111111
Result struct {
112112
Content []json.RawMessage `json:"content"`
113113
StructuredContent map[string]json.RawMessage `json:"structuredContent"`
114+
IsError bool `json:"isError"`
114115
} `json:"result"`
115116
Error *struct {
116117
Code int `json:"code"`
@@ -124,6 +125,19 @@ func DecodeToolResponse(resp *http.Response) (map[string]json.RawMessage, error)
124125
return nil, errors.Newf("MCP tools/call failed: %d %s", jsonRPCResp.Error.Code, jsonRPCResp.Error.Message)
125126
}
126127

128+
if jsonRPCResp.Result.IsError {
129+
if len(jsonRPCResp.Result.Content) > 0 {
130+
var textContent struct {
131+
Text string `json:"text"`
132+
}
133+
if err := json.Unmarshal(jsonRPCResp.Result.Content[0], &textContent); err == nil && textContent.Text != "" {
134+
return nil, errors.Newf("MCP tool error: %s", textContent.Text)
135+
}
136+
return nil, errors.Newf("MCP tool error: %s", string(jsonRPCResp.Result.Content[0]))
137+
}
138+
return nil, errors.New("MCP tool returned an error")
139+
}
140+
127141
return jsonRPCResp.Result.StructuredContent, nil
128142
}
129143
func readSSEResponseData(resp *http.Response) ([]byte, error) {

0 commit comments

Comments
 (0)