Skip to content
Merged
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
4 changes: 4 additions & 0 deletions cmd/docs/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 24 additions & 5 deletions cmd/docs/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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"})
Expand Down
22 changes: 15 additions & 7 deletions dunemcp/dunemcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading