Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions internal/mcpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ type toolCallResult struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
IsError bool `json:"isError,omitempty"`
IsError bool `json:"isError,omitempty"`
StructuredContent json.RawMessage `json:"structuredContent,omitempty"`
}

// CallTool invokes a named MCP tool with the given arguments and returns the
Expand Down Expand Up @@ -179,16 +180,23 @@ func (c *Client) CallToolContext(ctx context.Context, name string, args map[stri
if err := json.Unmarshal(rpcResp.Result, &result); err != nil {
return strings.TrimSpace(string(rpcResp.Result)), nil
}
if result.IsError && len(result.Content) > 0 {
return "", fmt.Errorf("mcp tool %s: %s", name, result.Content[0].Text)
}
var textBlocks []string
for _, block := range result.Content {
if block.Type == "text" && block.Text != "" {
textBlocks = append(textBlocks, block.Text)
}
}
return strings.Join(textBlocks, "\n"), nil
text := strings.Join(textBlocks, "\n")
if result.IsError {
if text == "" {
text = "tool reported an error without text"
}
return "", fmt.Errorf("mcp tool %s: %s", name, text)
}
if text == "" && len(result.StructuredContent) > 0 {
text = string(result.StructuredContent)
}
return text, nil
}

type Tool struct {
Expand Down
28 changes: 28 additions & 0 deletions internal/mcpclient/result_envelope_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package mcpclient

import (
"net/http"
"strings"
"testing"
)

func TestMCPToolResultEnvelope(t *testing.T) {
t.Run("error without text", func(t *testing.T) {
_, client := newFake(t, func(w http.ResponseWriter, id int64) {
writeJSON(w, `{"jsonrpc":"2.0","id":2,"result":{"isError":true,"content":[]}}`)
})
out, err := client.CallTool("fixture", nil)
if err == nil || !strings.Contains(err.Error(), "fixture") || out != "" {
t.Fatalf("expected a tool error, got %q, %v", out, err)
}
})
t.Run("structured result without text", func(t *testing.T) {
_, client := newFake(t, func(w http.ResponseWriter, id int64) {
writeJSON(w, `{"jsonrpc":"2.0","id":2,"result":{"structuredContent":{"count":3}}}`)
})
out, err := client.CallTool("fixture", nil)
if err != nil || out != `{"count":3}` {
t.Fatalf("expected structured JSON, got %q, %v", out, err)
}
})
}