Skip to content

Commit 3bf811f

Browse files
committed
Update search tool; add test
1 parent a2a8dd3 commit 3bf811f

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

tools/search.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ package tools
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

8-
"github.com/mark3labs/mcp-go/mcp"
97
"github.com/mark3labs/mcp-go/server"
108

119
"github.com/grafana/grafana-openapi-client-go/client/search"
10+
"github.com/grafana/grafana-openapi-client-go/models"
1211
mcpgrafana "github.com/grafana/mcp-grafana"
1312
)
1413

1514
type SearchDashboardsParams struct {
1615
Query string `json:"query" jsonschema:"description=The query to search for"`
1716
}
1817

19-
func searchDashboards(ctx context.Context, args SearchDashboardsParams) (*mcp.CallToolResult, error) {
18+
func searchDashboards(ctx context.Context, args SearchDashboardsParams) (models.HitList, error) {
2019
c := mcpgrafana.GrafanaClientFromContext(ctx)
2120
params := search.NewSearchParamsWithContext(ctx)
2221
if args.Query != "" {
@@ -26,11 +25,7 @@ func searchDashboards(ctx context.Context, args SearchDashboardsParams) (*mcp.Ca
2625
if err != nil {
2726
return nil, fmt.Errorf("search dashboards for %+v: %w", c, err)
2827
}
29-
b, err := json.Marshal(search.Payload)
30-
if err != nil {
31-
return nil, fmt.Errorf("marshal search results: %w", err)
32-
}
33-
return mcp.NewToolResultText(string(b)), nil
28+
return search.Payload, nil
3429
}
3530

3631
var SearchDashboards = mcpgrafana.MustTool(

tools/search_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Requires a Grafana instance running on localhost:3000,
2+
// with a dashboard named "Demo" provisioned.
3+
// Run with `go test -tags integration`.
4+
//go:build integration
5+
6+
package tools
7+
8+
import (
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestSearchTools(t *testing.T) {
16+
t.Run("search dashboards", func(t *testing.T) {
17+
ctx := newTestContext()
18+
result, err := searchDashboards(ctx, SearchDashboardsParams{
19+
Query: "Demo",
20+
})
21+
require.NoError(t, err)
22+
assert.Len(t, result, 1)
23+
})
24+
}

0 commit comments

Comments
 (0)