-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathmain.go
More file actions
458 lines (405 loc) · 18.4 KB
/
main.go
File metadata and controls
458 lines (405 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"os/signal"
"slices"
"strings"
"syscall"
"time"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
mcpgrafana "github.com/grafana/mcp-grafana"
"github.com/grafana/mcp-grafana/observability"
"github.com/grafana/mcp-grafana/tools"
"go.opentelemetry.io/otel/semconv/v1.39.0/mcpconv"
)
func maybeAddTools(s *server.MCPServer, tf func(*server.MCPServer), enabledTools []string, disable bool, category string) {
if !slices.Contains(enabledTools, category) {
slog.Debug("Not enabling tools", "category", category)
return
}
if disable {
slog.Info("Disabling tools", "category", category)
return
}
slog.Debug("Enabling tools", "category", category)
tf(s)
}
// disabledTools indicates whether each category of tools should be disabled.
type disabledTools struct {
enabledTools string
search, datasource, incident,
prometheus, loki, elasticsearch, alerting,
dashboard, folder, oncall, asserts, sift, admin,
pyroscope, navigation, proxied, annotations, rendering, cloudwatch, write,
examples, clickhouse, searchlogs,
runpanelquery, influxdb bool
}
// Configuration for the Grafana client.
type grafanaConfig struct {
// Whether to enable debug mode for the Grafana transport.
debug bool
// TLS configuration
tlsCertFile string
tlsKeyFile string
tlsCAFile string
tlsSkipVerify bool
}
func (dt *disabledTools) addFlags() {
flag.StringVar(&dt.enabledTools, "enabled-tools", "search,datasource,incident,prometheus,loki,alerting,dashboard,folder,oncall,asserts,sift,pyroscope,navigation,proxied,annotations,rendering", "A comma separated list of tools enabled for this server. Can be overwritten entirely or by disabling specific components, e.g. --disable-search.")
flag.BoolVar(&dt.search, "disable-search", false, "Disable search tools")
flag.BoolVar(&dt.datasource, "disable-datasource", false, "Disable datasource tools")
flag.BoolVar(&dt.incident, "disable-incident", false, "Disable incident tools")
flag.BoolVar(&dt.prometheus, "disable-prometheus", false, "Disable prometheus tools")
flag.BoolVar(&dt.loki, "disable-loki", false, "Disable loki tools")
flag.BoolVar(&dt.elasticsearch, "disable-elasticsearch", false, "Disable elasticsearch tools")
flag.BoolVar(&dt.alerting, "disable-alerting", false, "Disable alerting tools")
flag.BoolVar(&dt.dashboard, "disable-dashboard", false, "Disable dashboard tools")
flag.BoolVar(&dt.folder, "disable-folder", false, "Disable folder tools")
flag.BoolVar(&dt.oncall, "disable-oncall", false, "Disable oncall tools")
flag.BoolVar(&dt.asserts, "disable-asserts", false, "Disable asserts tools")
flag.BoolVar(&dt.sift, "disable-sift", false, "Disable sift tools")
flag.BoolVar(&dt.admin, "disable-admin", false, "Disable admin tools")
flag.BoolVar(&dt.pyroscope, "disable-pyroscope", false, "Disable pyroscope tools")
flag.BoolVar(&dt.navigation, "disable-navigation", false, "Disable navigation tools")
flag.BoolVar(&dt.proxied, "disable-proxied", false, "Disable proxied tools (tools from external MCP servers)")
flag.BoolVar(&dt.write, "disable-write", false, "Disable write tools (create/update operations)")
flag.BoolVar(&dt.annotations, "disable-annotations", false, "Disable annotation tools")
flag.BoolVar(&dt.rendering, "disable-rendering", false, "Disable rendering tools (panel/dashboard image export)")
flag.BoolVar(&dt.cloudwatch, "disable-cloudwatch", false, "Disable CloudWatch tools")
flag.BoolVar(&dt.examples, "disable-examples", false, "Disable query examples tools")
flag.BoolVar(&dt.clickhouse, "disable-clickhouse", false, "Disable ClickHouse tools")
flag.BoolVar(&dt.searchlogs, "disable-searchlogs", false, "Disable search logs tools")
flag.BoolVar(&dt.runpanelquery, "disable-runpanelquery", false, "Disable run panel query tools")
flag.BoolVar(&dt.influxdb, "disable-influxdb", false, "Disable InfluxDb tools")
}
func (gc *grafanaConfig) addFlags() {
flag.BoolVar(&gc.debug, "debug", false, "Enable debug mode for the Grafana transport")
// TLS configuration flags
flag.StringVar(&gc.tlsCertFile, "tls-cert-file", "", "Path to TLS certificate file for client authentication")
flag.StringVar(&gc.tlsKeyFile, "tls-key-file", "", "Path to TLS private key file for client authentication")
flag.StringVar(&gc.tlsCAFile, "tls-ca-file", "", "Path to TLS CA certificate file for server verification")
flag.BoolVar(&gc.tlsSkipVerify, "tls-skip-verify", false, "Skip TLS certificate verification (insecure)")
}
func (dt *disabledTools) addTools(s *server.MCPServer) {
enabledTools := strings.Split(dt.enabledTools, ",")
enableWriteTools := !dt.write
maybeAddTools(s, tools.AddSearchTools, enabledTools, dt.search, "search")
maybeAddTools(s, tools.AddDatasourceTools, enabledTools, dt.datasource, "datasource")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddIncidentTools(mcp, enableWriteTools) }, enabledTools, dt.incident, "incident")
maybeAddTools(s, tools.AddPrometheusTools, enabledTools, dt.prometheus, "prometheus")
maybeAddTools(s, tools.AddLokiTools, enabledTools, dt.loki, "loki")
maybeAddTools(s, tools.AddElasticsearchTools, enabledTools, dt.elasticsearch, "elasticsearch")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddAlertingTools(mcp, enableWriteTools) }, enabledTools, dt.alerting, "alerting")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddDashboardTools(mcp, enableWriteTools) }, enabledTools, dt.dashboard, "dashboard")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddFolderTools(mcp, enableWriteTools) }, enabledTools, dt.folder, "folder")
maybeAddTools(s, tools.AddOnCallTools, enabledTools, dt.oncall, "oncall")
maybeAddTools(s, tools.AddAssertsTools, enabledTools, dt.asserts, "asserts")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddSiftTools(mcp, enableWriteTools) }, enabledTools, dt.sift, "sift")
maybeAddTools(s, tools.AddAdminTools, enabledTools, dt.admin, "admin")
maybeAddTools(s, tools.AddPyroscopeTools, enabledTools, dt.pyroscope, "pyroscope")
maybeAddTools(s, tools.AddNavigationTools, enabledTools, dt.navigation, "navigation")
maybeAddTools(s, func(mcp *server.MCPServer) { tools.AddAnnotationTools(mcp, enableWriteTools) }, enabledTools, dt.annotations, "annotations")
maybeAddTools(s, tools.AddRenderingTools, enabledTools, dt.rendering, "rendering")
maybeAddTools(s, tools.AddCloudWatchTools, enabledTools, dt.cloudwatch, "cloudwatch")
maybeAddTools(s, tools.AddExamplesTools, enabledTools, dt.examples, "examples")
maybeAddTools(s, tools.AddClickHouseTools, enabledTools, dt.clickhouse, "clickhouse")
maybeAddTools(s, tools.AddSearchLogsTools, enabledTools, dt.searchlogs, "searchlogs")
maybeAddTools(s, tools.AddRunPanelQueryTools, enabledTools, dt.runpanelquery, "runpanelquery")
maybeAddTools(s, tools.AddInfluxTools, enabledTools, dt.influxdb, "influxdb")
}
func newServer(transport string, dt disabledTools, obs *observability.Observability) (*server.MCPServer, *mcpgrafana.ToolManager) {
sm := mcpgrafana.NewSessionManager()
// Declare variable for ToolManager that will be initialized after server creation
var stm *mcpgrafana.ToolManager
// Create hooks
hooks := &server.Hooks{
OnRegisterSession: []server.OnRegisterSessionHookFunc{sm.CreateSession},
OnUnregisterSession: []server.OnUnregisterSessionHookFunc{sm.RemoveSession},
}
// Add proxied tools hooks if enabled and we're not running in stdio mode.
// (stdio mode is handled by InitializeAndRegisterServerTools; per-session tools
// are not supported).
if transport != "stdio" && !dt.proxied {
// OnBeforeListTools: Discover, connect, and register tools
hooks.OnBeforeListTools = []server.OnBeforeListToolsFunc{
func(ctx context.Context, id any, request *mcp.ListToolsRequest) {
if stm != nil {
if session := server.ClientSessionFromContext(ctx); session != nil {
stm.InitializeAndRegisterProxiedTools(ctx, session)
}
}
},
}
// OnBeforeCallTool: Fallback in case client calls tool without listing first
hooks.OnBeforeCallTool = []server.OnBeforeCallToolFunc{
func(ctx context.Context, id any, request *mcp.CallToolRequest) {
if stm != nil {
if session := server.ClientSessionFromContext(ctx); session != nil {
stm.InitializeAndRegisterProxiedTools(ctx, session)
}
}
},
}
}
// Merge observability hooks with existing hooks
hooks = observability.MergeHooks(hooks, obs.MCPHooks())
s := server.NewMCPServer("mcp-grafana", mcpgrafana.Version(),
server.WithInstructions(`
This server provides access to your Grafana instance and the surrounding ecosystem.
Available Capabilities:
- Dashboards: Search, retrieve, update, and create dashboards. Extract panel queries and datasource information.
- Datasources: List and fetch details for datasources.
- Prometheus & Loki: Run PromQL and LogQL queries, retrieve metric/log metadata, and explore label names/values.
- ClickHouse: Query ClickHouse datasources via Grafana with macro and variable substitution support.
- Elasticsearch: Query Elasticsearch datasources using Lucene syntax or Query DSL for logs and metrics.
- InfluxDB : Query InfluxDB datasourcs with SQL , InfluxQL , Flux languages
- Incidents: Search, create, update, and resolve incidents in Grafana Incident.
- Sift Investigations: Start and manage Sift investigations, analyze logs/traces, find error patterns, and detect slow requests.
- Alerting: List and fetch alert rules and notification contact points.
- OnCall: View and manage on-call schedules, shifts, teams, and users.
- Admin: List teams and perform administrative tasks.
- Pyroscope: Profile applications and fetch profiling data.
- Navigation: Generate deeplink URLs for Grafana resources like dashboards, panels, and Explore queries.
- Rendering: Export dashboard panels or full dashboards as PNG images (requires Grafana Image Renderer plugin).
- Proxied Tools: Access tools from external MCP servers (like Tempo) through dynamic discovery.
Note that some of these capabilities may be disabled. Do not try to use features that are not available via tools.
`),
server.WithHooks(hooks),
)
// Initialize ToolManager now that server is created
stm = mcpgrafana.NewToolManager(sm, s, mcpgrafana.WithProxiedTools(!dt.proxied))
dt.addTools(s)
return s, stm
}
type tlsConfig struct {
certFile, keyFile string
}
func (tc *tlsConfig) addFlags() {
flag.StringVar(&tc.certFile, "server.tls-cert-file", "", "Path to TLS certificate file for server HTTPS (required for TLS)")
flag.StringVar(&tc.keyFile, "server.tls-key-file", "", "Path to TLS private key file for server HTTPS (required for TLS)")
}
// httpServer represents a server with Start and Shutdown methods
type httpServer interface {
Start(addr string) error
Shutdown(ctx context.Context) error
}
// runHTTPServer handles the common logic for running HTTP-based servers
func runHTTPServer(ctx context.Context, srv httpServer, addr, transportName string) error {
// Start server in a goroutine
serverErr := make(chan error, 1)
go func() {
if err := srv.Start(addr); err != nil {
serverErr <- err
}
close(serverErr)
}()
// Wait for either server error or shutdown signal
select {
case err := <-serverErr:
return err
case <-ctx.Done():
slog.Info(fmt.Sprintf("%s server shutting down...", transportName))
// Create a timeout context for shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
if err := srv.Shutdown(shutdownCtx); err != nil {
return fmt.Errorf("shutdown error: %v", err)
}
slog.Debug("Shutdown called, waiting for connections to close...")
// Wait for server to finish
select {
case err := <-serverErr:
// http.ErrServerClosed is expected when shutting down
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("server error during shutdown: %v", err)
}
case <-shutdownCtx.Done():
slog.Warn(fmt.Sprintf("%s server did not stop gracefully within timeout", transportName))
}
}
return nil
}
func handleHealthz(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
}
// runMetricsServer starts a separate HTTP server for metrics.
func runMetricsServer(addr string, o *observability.Observability) {
mux := http.NewServeMux()
mux.Handle("/metrics", o.MetricsHandler())
slog.Info("Starting metrics server", "address", addr)
if err := http.ListenAndServe(addr, mux); err != nil {
slog.Error("metrics server error", "error", err)
}
}
func run(transport, addr, basePath, endpointPath string, logLevel slog.Level, dt disabledTools, gc mcpgrafana.GrafanaConfig, tls tlsConfig, obs observability.Config) error {
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: logLevel})))
// Set up observability (metrics and tracing)
o, err := observability.Setup(obs)
if err != nil {
return fmt.Errorf("failed to setup observability: %w", err)
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := o.Shutdown(shutdownCtx); err != nil {
slog.Error("failed to shutdown observability", "error", err)
}
}()
s, tm := newServer(transport, dt, o)
// Create a context that will be cancelled on shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up signal handling for graceful shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sigChan)
// Handle shutdown signals
go func() {
<-sigChan
slog.Info("Received shutdown signal")
cancel()
// For stdio, close stdin to unblock the Listen call
if transport == "stdio" {
_ = os.Stdin.Close()
}
}()
// Start the appropriate server based on transport
switch transport {
case "stdio":
srv := server.NewStdioServer(s)
cf := mcpgrafana.ComposedStdioContextFunc(gc)
srv.SetContextFunc(cf)
// For stdio (single-tenant), initialize proxied tools on the server directly
if !dt.proxied {
stdioCtx := cf(ctx)
if err := tm.InitializeAndRegisterServerTools(stdioCtx); err != nil {
slog.Error("failed to initialize proxied tools for stdio", "error", err)
}
}
slog.Info("Starting Grafana MCP server using stdio transport", "version", mcpgrafana.Version())
err := srv.Listen(ctx, os.Stdin, os.Stdout)
if err != nil && err != context.Canceled {
return fmt.Errorf("server error: %v", err)
}
return nil
case "sse":
httpSrv := &http.Server{Addr: addr}
srv := server.NewSSEServer(s,
server.WithSSEContextFunc(mcpgrafana.ComposedSSEContextFunc(gc)),
server.WithStaticBasePath(basePath),
server.WithHTTPServer(httpSrv),
)
mux := http.NewServeMux()
if basePath == "" {
basePath = "/"
}
mux.Handle(basePath, observability.WrapHandler(srv, basePath))
mux.HandleFunc("/healthz", handleHealthz)
if obs.MetricsEnabled {
if obs.MetricsAddress == "" {
mux.Handle("/metrics", o.MetricsHandler())
} else {
go runMetricsServer(obs.MetricsAddress, o)
}
}
httpSrv.Handler = mux
slog.Info("Starting Grafana MCP server using SSE transport",
"version", mcpgrafana.Version(), "address", addr, "basePath", basePath, "metrics", obs.MetricsEnabled)
return runHTTPServer(ctx, srv, addr, "SSE")
case "streamable-http":
httpSrv := &http.Server{Addr: addr}
opts := []server.StreamableHTTPOption{
server.WithHTTPContextFunc(mcpgrafana.ComposedHTTPContextFunc(gc)),
server.WithStateLess(dt.proxied), // Stateful when proxied tools enabled (requires sessions)
server.WithEndpointPath(endpointPath),
server.WithStreamableHTTPServer(httpSrv),
}
if tls.certFile != "" || tls.keyFile != "" {
opts = append(opts, server.WithTLSCert(tls.certFile, tls.keyFile))
}
srv := server.NewStreamableHTTPServer(s, opts...)
mux := http.NewServeMux()
mux.Handle(endpointPath, observability.WrapHandler(srv, endpointPath))
mux.HandleFunc("/healthz", handleHealthz)
if obs.MetricsEnabled {
if obs.MetricsAddress == "" {
mux.Handle("/metrics", o.MetricsHandler())
} else {
go runMetricsServer(obs.MetricsAddress, o)
}
}
httpSrv.Handler = mux
slog.Info("Starting Grafana MCP server using StreamableHTTP transport",
"version", mcpgrafana.Version(), "address", addr, "endpointPath", endpointPath, "metrics", obs.MetricsEnabled)
return runHTTPServer(ctx, srv, addr, "StreamableHTTP")
default:
return fmt.Errorf("invalid transport type: %s. Must be 'stdio', 'sse' or 'streamable-http'", transport)
}
}
func main() {
var transport string
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio, sse or streamable-http)")
flag.StringVar(
&transport,
"transport",
"stdio",
"Transport type (stdio, sse or streamable-http)",
)
addr := flag.String("address", "localhost:8000", "The host and port to start the sse server on")
basePath := flag.String("base-path", "", "Base path for the sse server")
endpointPath := flag.String("endpoint-path", "/mcp", "Endpoint path for the streamable-http server")
logLevel := flag.String("log-level", "info", "Log level (debug, info, warn, error)")
showVersion := flag.Bool("version", false, "Print the version and exit")
var dt disabledTools
dt.addFlags()
var gc grafanaConfig
gc.addFlags()
var tls tlsConfig
tls.addFlags()
var obs observability.Config
flag.BoolVar(&obs.MetricsEnabled, "metrics", false, "Enable Prometheus metrics endpoint")
flag.StringVar(&obs.MetricsAddress, "metrics-address", "", "Separate address for metrics server (e.g., :9090). If empty, metrics are served on the main server at /metrics")
flag.Parse()
if *showVersion {
fmt.Println(mcpgrafana.Version())
os.Exit(0)
}
// Convert local grafanaConfig to mcpgrafana.GrafanaConfig
grafanaConfig := mcpgrafana.GrafanaConfig{Debug: gc.debug}
if gc.tlsCertFile != "" || gc.tlsKeyFile != "" || gc.tlsCAFile != "" || gc.tlsSkipVerify {
grafanaConfig.TLSConfig = &mcpgrafana.TLSConfig{
CertFile: gc.tlsCertFile,
KeyFile: gc.tlsKeyFile,
CAFile: gc.tlsCAFile,
SkipVerify: gc.tlsSkipVerify,
}
}
// Set OTel resource identity
obs.ServerName = "mcp-grafana"
obs.ServerVersion = mcpgrafana.Version()
// Map transport flag to semconv network.transport values
switch transport {
case "stdio":
obs.NetworkTransport = mcpconv.NetworkTransportPipe
case "sse", "streamable-http":
obs.NetworkTransport = mcpconv.NetworkTransportTCP
}
if err := run(transport, *addr, *basePath, *endpointPath, parseLevel(*logLevel), dt, grafanaConfig, tls, obs); err != nil {
panic(err)
}
}
func parseLevel(level string) slog.Level {
var l slog.Level
if err := l.UnmarshalText([]byte(level)); err != nil {
return slog.LevelInfo
}
return l
}