diff --git a/cmd/docs/search.go b/cmd/docs/search.go index 81dcf27..eefa443 100644 --- a/cmd/docs/search.go +++ b/cmd/docs/search.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/duneanalytics/cli/dunemcp" "github.com/duneanalytics/cli/output" "github.com/modelcontextprotocol/go-sdk/mcp" "github.com/spf13/cobra" @@ -93,6 +94,9 @@ func runSearch(cmd *cobra.Command, _ []string) error { if err != nil { return fmt.Errorf("searching docs: %w", err) } + if err := dunemcp.ToolError("search_dune_docs", result); err != nil { + return fmt.Errorf("searching docs: %w", err) + } // Parse results from content blocks. results := parseResults(result) diff --git a/cmd/docs/search_test.go b/cmd/docs/search_test.go index a57a57b..bd4f098 100644 --- a/cmd/docs/search_test.go +++ b/cmd/docs/search_test.go @@ -24,6 +24,16 @@ type searchArgs struct { func startMCPServer(t *testing.T, results []string) *httptest.Server { t.Helper() + var content []mcp.Content + for _, r := range results { + content = append(content, &mcp.TextContent{Text: r}) + } + return startMCPServerResult(t, &mcp.CallToolResult{Content: content}) +} + +// startMCPServerResult starts a docs server whose search tool returns result. +func startMCPServerResult(t *testing.T, result *mcp.CallToolResult) *httptest.Server { + t.Helper() server := mcp.NewServer(&mcp.Implementation{ Name: "test-docs", @@ -34,11 +44,7 @@ func startMCPServer(t *testing.T, results []string) *httptest.Server { Name: "search_dune_docs", Description: "Search Dune docs", }, func(ctx context.Context, req *mcp.CallToolRequest, args searchArgs) (*mcp.CallToolResult, any, error) { - var content []mcp.Content - for _, r := range results { - content = append(content, &mcp.TextContent{Text: r}) - } - return &mcp.CallToolResult{Content: content}, nil, nil + return result, nil, nil }) handler := mcp.NewStreamableHTTPHandler(func(_ *http.Request) *mcp.Server { @@ -116,6 +122,19 @@ func TestSearchEmptyResults(t *testing.T) { assert.Contains(t, buf.String(), "No results found.") } +func TestSearchToolError(t *testing.T) { + ts := startMCPServerResult(t, &mcp.CallToolResult{ + IsError: true, + Content: []mcp.Content{&mcp.TextContent{Text: "Search index unavailable."}}, + }) + + root, buf := newTestRoot() + root.SetArgs([]string{"docs", "search", "--query", "decoded tables", "--mcp-endpoint", ts.URL}) + + assert.EqualError(t, root.Execute(), "searching docs: Search index unavailable") + assert.NotContains(t, buf.String(), "result(s)") +} + func TestSearchRequiresQuery(t *testing.T) { root, _ := newTestRoot() root.SetArgs([]string{"docs", "search"}) diff --git a/dunemcp/dunemcp.go b/dunemcp/dunemcp.go index 566ffb0..133c493 100644 --- a/dunemcp/dunemcp.go +++ b/dunemcp/dunemcp.go @@ -80,13 +80,8 @@ func (c *Client) connect(ctx context.Context) (*mcp.ClientSession, error) { // the tool reported. A result without structured content falls back to its // first text block, which Dune tools fill with the same JSON. func Result(tool string, res *mcp.CallToolResult) (json.RawMessage, error) { - if res.IsError { - // Go error strings end without punctuation; the CLI's error printer adds a period. - msg := strings.TrimSuffix(strings.TrimSpace(text(res)), ".") - if msg == "" { - msg = tool + " failed without a message" - } - return nil, errors.New(msg) + if err := ToolError(tool, res); err != nil { + return nil, err } if res.StructuredContent != nil { var buf bytes.Buffer @@ -103,6 +98,19 @@ func Result(tool string, res *mcp.CallToolResult) (json.RawMessage, error) { return nil, errors.New(tool + ": the tool returned no structured result") } +// ToolError returns the error a tool result reports, or nil when it reports none. +func ToolError(tool string, res *mcp.CallToolResult) error { + if !res.IsError { + return nil + } + // Go error strings end without punctuation; the CLI's error printer adds a period. + msg := strings.TrimSuffix(strings.TrimSpace(text(res)), ".") + if msg == "" { + msg = tool + " failed without a message" + } + return errors.New(msg) +} + func text(res *mcp.CallToolResult) string { var parts []string for _, c := range res.Content {