+31
-3
lines changedFilter options
+31
-3
lines changed Original file line number Diff line number Diff line change
@@ -3,14 +3,17 @@ package tools
3
3
import (
4
4
"context"
5
5
"fmt"
6
+
"strings"
6
7
7
8
"github.com/mark3labs/mcp-go/server"
8
9
9
10
"github.com/grafana/grafana-openapi-client-go/models"
10
11
mcpgrafana "github.com/grafana/mcp-grafana"
11
12
)
12
13
13
-
type ListDatasourcesParams struct{}
14
+
type ListDatasourcesParams struct {
15
+
Type string `json:"type,omitempty" jsonschema:"descripton=The type of datasources to search for. For example, 'prometheus', 'loki', 'tempo', etc..."`
16
+
}
14
17
15
18
type dataSourceSummary struct {
16
19
ID int64 `json:"id"`
@@ -22,11 +25,28 @@ type dataSourceSummary struct {
22
25
23
26
func listDatasources(ctx context.Context, args ListDatasourcesParams) ([]dataSourceSummary, error) {
24
27
c := mcpgrafana.GrafanaClientFromContext(ctx)
25
-
datasources, err := c.Datasources.GetDataSources()
28
+
resp, err := c.Datasources.GetDataSources()
26
29
if err != nil {
27
30
return nil, fmt.Errorf("list datasources: %w", err)
28
31
}
29
-
return summarizeDatasources(datasources.Payload), nil
32
+
datasources := filterDatasources(resp.Payload, args.Type)
33
+
return summarizeDatasources(datasources), nil
34
+
}
35
+
36
+
// filterDatasources returns only datasources of the specified type `t`. If `t`
37
+
// is an empty string no filtering is done.
38
+
func filterDatasources(datasources models.DataSourceList, t string) models.DataSourceList {
39
+
if t == "" {
40
+
return datasources
41
+
}
42
+
filtered := models.DataSourceList{}
43
+
t = strings.ToLower(t)
44
+
for _, ds := range datasources {
45
+
if strings.Contains(strings.ToLower(ds.Type), t) {
46
+
filtered = append(filtered, ds)
47
+
}
48
+
}
49
+
return filtered
30
50
}
31
51
32
52
func summarizeDatasources(dataSources models.DataSourceList) []dataSourceSummary {
Original file line number Diff line number Diff line change
@@ -57,6 +57,14 @@ func TestDatasourcesTools(t *testing.T) {
57
57
assert.Len(t, result, 3)
58
58
})
59
59
60
+
t.Run("list datasources for type", func(t *testing.T) {
61
+
ctx := newTestContext()
62
+
result, err := listDatasources(ctx, ListDatasourcesParams{Type: "Prometheus"})
63
+
require.NoError(t, err)
64
+
// Only two Prometheus datasources are provisioned in the test environment.
65
+
assert.Len(t, result, 2)
66
+
})
67
+
60
68
t.Run("get datasource by uid", func(t *testing.T) {
61
69
ctx := newTestContext()
62
70
result, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4