|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/mark3labs/mcp-go/mcp" |
| 9 | + "github.com/mark3labs/mcp-go/server" |
| 10 | + |
| 11 | + mcpgrafana "github.com/grafana/mcp-grafana" |
| 12 | +) |
| 13 | + |
| 14 | +type ListDatasourcesParams struct{} |
| 15 | + |
| 16 | +func listDatasources(ctx context.Context, args ListDatasourcesParams) (*mcp.CallToolResult, error) { |
| 17 | + c := mcpgrafana.GrafanaClientFromContext(ctx) |
| 18 | + datasources, err := c.Datasources.GetDataSources() |
| 19 | + if err != nil { |
| 20 | + return nil, fmt.Errorf("list datasources: %w", err) |
| 21 | + } |
| 22 | + b, err := json.Marshal(datasources.Payload) |
| 23 | + if err != nil { |
| 24 | + return nil, fmt.Errorf("marshal datasources: %w", err) |
| 25 | + } |
| 26 | + return mcp.NewToolResultText(string(b)), nil |
| 27 | +} |
| 28 | + |
| 29 | +var ListDatasourcesTool, ListDatasourcesHandler = mcpgrafana.MustTool( |
| 30 | + "list_datasources", |
| 31 | + "List datasources", |
| 32 | + listDatasources, |
| 33 | +) |
| 34 | + |
| 35 | +type GetDatasourceByUIDParams struct { |
| 36 | + UID string `json:"uid" jsonschema:"description=The uid of the datasource"` |
| 37 | +} |
| 38 | + |
| 39 | +func getDatasourceByUID(ctx context.Context, args GetDatasourceByUIDParams) (*mcp.CallToolResult, error) { |
| 40 | + c := mcpgrafana.GrafanaClientFromContext(ctx) |
| 41 | + datasource, err := c.Datasources.GetDataSourceByUID(args.UID) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("get datasource by uid %s: %w", args.UID, err) |
| 44 | + } |
| 45 | + b, err := json.Marshal(datasource.Payload) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("marshal datasource: %w", err) |
| 48 | + } |
| 49 | + return mcp.NewToolResultText(string(b)), nil |
| 50 | +} |
| 51 | + |
| 52 | +var GetDatasourceByUIDTool, GetDatasourceByUIDHandler = mcpgrafana.MustTool( |
| 53 | + "get_datasource_by_uid", |
| 54 | + "Get datasource by uid", |
| 55 | + getDatasourceByUID, |
| 56 | +) |
| 57 | + |
| 58 | +type GetDatasourceByNameParams struct { |
| 59 | + Name string `json:"name" jsonschema:"description=The name of the datasource"` |
| 60 | +} |
| 61 | + |
| 62 | +func getDatasourceByName(ctx context.Context, args GetDatasourceByNameParams) (*mcp.CallToolResult, error) { |
| 63 | + c := mcpgrafana.GrafanaClientFromContext(ctx) |
| 64 | + datasource, err := c.Datasources.GetDataSourceByName(args.Name) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("get datasource by name %s: %w", args.Name, err) |
| 67 | + } |
| 68 | + b, err := json.Marshal(datasource.Payload) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("marshal datasource: %w", err) |
| 71 | + } |
| 72 | + return mcp.NewToolResultText(string(b)), nil |
| 73 | +} |
| 74 | + |
| 75 | +var GetDatasourceByNameTool, GetDatasourceByNameHandler = mcpgrafana.MustTool( |
| 76 | + "get_datasource_by_name", |
| 77 | + "Get datasource by name", |
| 78 | + getDatasourceByName, |
| 79 | +) |
| 80 | + |
| 81 | +func AddDatasourceTools(mcp *server.MCPServer) { |
| 82 | + mcp.AddTool(ListDatasourcesTool, ListDatasourcesHandler) |
| 83 | + mcp.AddTool(GetDatasourceByUIDTool, GetDatasourceByUIDHandler) |
| 84 | + mcp.AddTool(GetDatasourceByNameTool, GetDatasourceByNameHandler) |
| 85 | +} |
0 commit comments