Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashboard tools #62

Merged
merged 2 commits into from
Mar 26, 2025
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This provides access to your Grafana instance and the surrounding ecosystem.
## Features

- [x] Search for dashboards
- [x] Get dashboard by UID
- [x] List and fetch datasource information
- [ ] Query datasources
- [x] Prometheus
Expand Down Expand Up @@ -35,6 +36,7 @@ This is useful if you don't use certain functionality or if you don't want to ta
| Tool | Category | Description |
| --- | --- | --- |
| `search_dashboards` | Search | Search for dashboards |
| `get_dashboard_by_uid` | Dashboard | Get a dashboard by uid |
| `list_datasources` | Datasources | List datasources |
| `get_datasource_by_uid` | Datasources | Get a datasource by uid |
| `get_datasource_by_name` | Datasources | Get a datasource by name |
Expand Down
1 change: 1 addition & 0 deletions cmd/mcp-grafana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func newServer() *server.MCPServer {
tools.AddIncidentTools(s)
tools.AddPrometheusTools(s)
tools.AddLokiTools(s)
tools.AddDashboardTools(s)
return s
}

Expand Down
35 changes: 35 additions & 0 deletions tools/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tools

import (
"context"
"fmt"

"github.com/mark3labs/mcp-go/server"

"github.com/grafana/grafana-openapi-client-go/models"
mcpgrafana "github.com/grafana/mcp-grafana"
)

type GetDashboardByUIDParams struct {
UID string `json:"uid" jsonschema:"required,description=The UID of the dashboard"`
}

func getDashboardByUID(ctx context.Context, args GetDashboardByUIDParams) (*models.DashboardFullWithMeta, error) {
c := mcpgrafana.GrafanaClientFromContext(ctx)

dashboard, err := c.Dashboards.GetDashboardByUID(args.UID)
if err != nil {
return nil, fmt.Errorf("get dashboard by uid %s: %w", args.UID, err)
}
return dashboard.Payload, nil
}

var GetDashboardByUID = mcpgrafana.MustTool(
"get_dashboard_by_uid",
"Get dashboard by uid",
getDashboardByUID,
)

func AddDashboardTools(mcp *server.MCPServer) {
GetDashboardByUID.Register(mcp)
}
45 changes: 45 additions & 0 deletions tools/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Requires a Grafana instance running on localhost:3000,
// with a dashboard provisioned.
// Run with `go test -tags integration`.
//go:build integration

package tools

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDashboardTools(t *testing.T) {
t.Run("get dashboard by uid", func(t *testing.T) {
ctx := newTestContext()

// First, let's search for a dashboard to get its UID
searchResults, err := searchDashboards(ctx, SearchDashboardsParams{})
require.NoError(t, err)
require.Greater(t, len(searchResults), 0, "No dashboards found")

dashboardUID := searchResults[0].UID

// Now test the get dashboard by uid functionality
result, err := getDashboardByUID(ctx, GetDashboardByUIDParams{
UID: dashboardUID,
})
require.NoError(t, err)
dashboardMap, ok := result.Dashboard.(map[string]interface{})
require.True(t, ok, "Dashboard should be a map")
assert.Equal(t, dashboardUID, dashboardMap["uid"])
assert.NotNil(t, result.Meta)
})

t.Run("get dashboard by uid - invalid uid", func(t *testing.T) {
ctx := newTestContext()

_, err := getDashboardByUID(ctx, GetDashboardByUIDParams{
UID: "non-existent-uid",
})
require.Error(t, err)
})
}
Loading