@@ -2,18 +2,17 @@ package tools
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
"regexp"
8
7
"strings"
9
8
"time"
10
9
11
10
mcpgrafana "github.com/grafana/mcp-grafana"
12
- "github.com/mark3labs/mcp-go/mcp"
13
11
"github.com/mark3labs/mcp-go/server"
14
12
"github.com/prometheus/client_golang/api"
15
13
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
16
14
"github.com/prometheus/common/config"
15
+ "github.com/prometheus/common/model"
17
16
)
18
17
19
18
func promClientFromContext (ctx context.Context , uid string ) (promv1.API , error ) {
@@ -75,7 +74,7 @@ type QueryPrometheusParams struct {
75
74
QueryType string `json:"queryType,omitempty" jsonschema:"description=The type of query to use. Either 'range' or 'instant'"`
76
75
}
77
76
78
- func queryPrometheus (ctx context.Context , args QueryPrometheusParams ) (* mcp. CallToolResult , error ) {
77
+ func queryPrometheus (ctx context.Context , args QueryPrometheusParams ) (model. Value , error ) {
79
78
promClient , err := promClientFromContext (ctx , args .DatasourceUID )
80
79
if err != nil {
81
80
return nil , fmt .Errorf ("getting Prometheus client: %w" , err )
@@ -110,23 +109,13 @@ func queryPrometheus(ctx context.Context, args QueryPrometheusParams) (*mcp.Call
110
109
if err != nil {
111
110
return nil , fmt .Errorf ("querying Prometheus range: %w" , err )
112
111
}
113
-
114
- b , err := json .Marshal (result )
115
- if err != nil {
116
- return nil , fmt .Errorf ("marshalling Prometheus query result: %w" , err )
117
- }
118
- return mcp .NewToolResultText (string (b )), nil
112
+ return result , nil
119
113
} else if queryType == "instant" {
120
114
result , _ , err := promClient .Query (ctx , args .Expr , startTime )
121
115
if err != nil {
122
116
return nil , fmt .Errorf ("querying Prometheus instant: %w" , err )
123
117
}
124
-
125
- b , err := json .Marshal (result )
126
- if err != nil {
127
- return nil , fmt .Errorf ("marshalling Prometheus query result: %w" , err )
128
- }
129
- return mcp .NewToolResultText (string (b )), nil
118
+ return result , nil
130
119
}
131
120
132
121
return nil , fmt .Errorf ("invalid query type: %s" , queryType )
@@ -145,7 +134,7 @@ type ListPrometheusMetricNamesParams struct {
145
134
Page int `json:"page,omitempty" jsonschema:"description=The page number to return"`
146
135
}
147
136
148
- func listPrometheusMetricNames (ctx context.Context , args ListPrometheusMetricNamesParams ) (* mcp. CallToolResult , error ) {
137
+ func listPrometheusMetricNames (ctx context.Context , args ListPrometheusMetricNamesParams ) ([] string , error ) {
149
138
promClient , err := promClientFromContext (ctx , args .DatasourceUID )
150
139
if err != nil {
151
140
return nil , fmt .Errorf ("getting Prometheus client: %w" , err )
@@ -196,11 +185,7 @@ func listPrometheusMetricNames(ctx context.Context, args ListPrometheusMetricNam
196
185
matches = matches [start :end ]
197
186
}
198
187
199
- b , err := json .Marshal (matches )
200
- if err != nil {
201
- return nil , fmt .Errorf ("marshalling Prometheus metric names: %w" , err )
202
- }
203
- return mcp .NewToolResultText (string (b )), nil
188
+ return matches , nil
204
189
}
205
190
206
191
var ListPrometheusMetricNames = mcpgrafana .MustTool (
@@ -223,6 +208,9 @@ func (s Selector) String() string {
223
208
b := strings.Builder {}
224
209
b .WriteRune ('{' )
225
210
for i , f := range s .Filters {
211
+ if f .Type == "" {
212
+ f .Type = "="
213
+ }
226
214
b .WriteString (fmt .Sprintf (`%s%s'%s'` , f .Name , f .Type , f .Value ))
227
215
if i < len (s .Filters )- 1 {
228
216
b .WriteString (", " )
@@ -240,7 +228,7 @@ type ListPrometheusLabelNamesParams struct {
240
228
Limit int `json:"limit,omitempty" jsonschema:"description=Optionally, the maximum number of results to return"`
241
229
}
242
230
243
- func listPrometheusLabelNames (ctx context.Context , args ListPrometheusLabelNamesParams ) (* mcp. CallToolResult , error ) {
231
+ func listPrometheusLabelNames (ctx context.Context , args ListPrometheusLabelNamesParams ) ([] string , error ) {
244
232
promClient , err := promClientFromContext (ctx , args .DatasourceUID )
245
233
if err != nil {
246
234
return nil , fmt .Errorf ("getting Prometheus client: %w" , err )
@@ -278,11 +266,7 @@ func listPrometheusLabelNames(ctx context.Context, args ListPrometheusLabelNames
278
266
labelNames = labelNames [:limit ]
279
267
}
280
268
281
- b , err := json .Marshal (labelNames )
282
- if err != nil {
283
- return nil , fmt .Errorf ("marshalling Prometheus label names: %w" , err )
284
- }
285
- return mcp .NewToolResultText (string (b )), nil
269
+ return labelNames , nil
286
270
}
287
271
288
272
var ListPrometheusLabelNames = mcpgrafana .MustTool (
@@ -300,7 +284,7 @@ type ListPrometheusLabelValuesParams struct {
300
284
Limit int `json:"limit,omitempty" jsonschema:"description=Optionally, the maximum number of results to return"`
301
285
}
302
286
303
- func listPrometheusLabelValues (ctx context.Context , args ListPrometheusLabelValuesParams ) (* mcp. CallToolResult , error ) {
287
+ func listPrometheusLabelValues (ctx context.Context , args ListPrometheusLabelValuesParams ) (model. LabelValues , error ) {
304
288
promClient , err := promClientFromContext (ctx , args .DatasourceUID )
305
289
if err != nil {
306
290
return nil , fmt .Errorf ("getting Prometheus client: %w" , err )
@@ -338,11 +322,7 @@ func listPrometheusLabelValues(ctx context.Context, args ListPrometheusLabelValu
338
322
labelValues = labelValues [:limit ]
339
323
}
340
324
341
- b , err := json .Marshal (labelValues )
342
- if err != nil {
343
- return nil , fmt .Errorf ("marshalling Prometheus label values: %w" , err )
344
- }
345
- return mcp .NewToolResultText (string (b )), nil
325
+ return labelValues , nil
346
326
}
347
327
348
328
var ListPrometheusLabelValues = mcpgrafana .MustTool (
0 commit comments