|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/grafana/incident-go" |
| 9 | + mcpgrafana "github.com/grafana/mcp-grafana" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | + "github.com/mark3labs/mcp-go/server" |
| 12 | +) |
| 13 | + |
| 14 | +type ListIncidentsParams struct { |
| 15 | + Limit int `json:"limit" jsonschema:"description=The maximum number of incidents to return"` |
| 16 | + Drill bool `json:"drill" jsonschema:"description=Whether to include drill incidents"` |
| 17 | + Status string `json:"status" jsonschema:"description=The status of the incidents to include"` |
| 18 | +} |
| 19 | + |
| 20 | +func ListIncidents(ctx context.Context, args ListIncidentsParams) (*mcp.CallToolResult, error) { |
| 21 | + c := mcpgrafana.IncidentClientFromContext(ctx) |
| 22 | + is := incident.NewIncidentsService(c) |
| 23 | + query := "" |
| 24 | + if !args.Drill { |
| 25 | + query = "isdrill:false" |
| 26 | + } |
| 27 | + if args.Status != "" { |
| 28 | + query += fmt.Sprintf(" and status:%s", args.Status) |
| 29 | + } |
| 30 | + incidents, err := is.QueryIncidents(ctx, incident.QueryIncidentsRequest{ |
| 31 | + Query: incident.IncidentsQuery{ |
| 32 | + QueryString: query, |
| 33 | + OrderDirection: "DESC", |
| 34 | + Limit: args.Limit, |
| 35 | + }, |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("list incidents: %w", err) |
| 39 | + } |
| 40 | + b, err := json.Marshal(incidents.Incidents) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("marshal incidents: %w", err) |
| 43 | + } |
| 44 | + return mcp.NewToolResultText(string(b)), nil |
| 45 | +} |
| 46 | + |
| 47 | +var ListIncidentsTool, ListIncidentsHandler = mcpgrafana.MustTool( |
| 48 | + "list_incidents", |
| 49 | + "List incidents", |
| 50 | + ListIncidents, |
| 51 | +) |
| 52 | + |
| 53 | +type CreateIncidentParams struct { |
| 54 | + Title string `json:"title" jsonschema:"description=The title of the incident"` |
| 55 | + Severity string `json:"severity" jsonschema:"description=The severity of the incident"` |
| 56 | + RoomPrefix string `json:"roomPrefix" jsonschema:"description=The prefix of the room to create the incident in"` |
| 57 | + IsDrill bool `json:"isDrill" jsonschema:"description=Whether the incident is a drill incident"` |
| 58 | + Status string `json:"status" jsonschema:"description=The status of the incident"` |
| 59 | + AttachCaption string `json:"attachCaption" jsonschema:"description=The caption of the attachment"` |
| 60 | + AttachURL string `json:"attachUrl" jsonschema:"description=The URL of the attachment"` |
| 61 | + Labels []incident.IncidentLabel `json:"labels" jsonschema:"description=The labels to add to the incident"` |
| 62 | +} |
| 63 | + |
| 64 | +func CreateIncident(ctx context.Context, args CreateIncidentParams) (*mcp.CallToolResult, error) { |
| 65 | + c := mcpgrafana.IncidentClientFromContext(ctx) |
| 66 | + is := incident.NewIncidentsService(c) |
| 67 | + incident, err := is.CreateIncident(ctx, incident.CreateIncidentRequest{ |
| 68 | + Title: args.Title, |
| 69 | + Severity: args.Severity, |
| 70 | + RoomPrefix: args.RoomPrefix, |
| 71 | + IsDrill: args.IsDrill, |
| 72 | + Status: args.Status, |
| 73 | + AttachCaption: args.AttachCaption, |
| 74 | + AttachURL: args.AttachURL, |
| 75 | + Labels: args.Labels, |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("create incident: %w", err) |
| 79 | + } |
| 80 | + b, err := json.Marshal(incident) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("marshal incident: %w", err) |
| 83 | + } |
| 84 | + return mcp.NewToolResultText(string(b)), nil |
| 85 | +} |
| 86 | + |
| 87 | +var CreateIncidentTool, CreateIncidentHandler = mcpgrafana.MustTool( |
| 88 | + "create_incident", |
| 89 | + "Create an incident", |
| 90 | + CreateIncident, |
| 91 | +) |
| 92 | + |
| 93 | +type AddActivityToIncidentParams struct { |
| 94 | + IncidentID string `json:"incidentId" jsonschema:"description=The ID of the incident to add the activity to"` |
| 95 | + Body string `json:"body" jsonschema:"description=The body of the activity. URLs will be parsed and attached as context"` |
| 96 | + EventTime string `json:"eventTime" jsonschema:"description=The time that the activity occurred. If not provided, the current time will be used"` |
| 97 | +} |
| 98 | + |
| 99 | +func AddActivityToIncident(ctx context.Context, args AddActivityToIncidentParams) (*mcp.CallToolResult, error) { |
| 100 | + c := mcpgrafana.IncidentClientFromContext(ctx) |
| 101 | + as := incident.NewActivityService(c) |
| 102 | + activity, err := as.AddActivity(ctx, incident.AddActivityRequest{ |
| 103 | + IncidentID: args.IncidentID, |
| 104 | + ActivityKind: "userNote", |
| 105 | + Body: args.Body, |
| 106 | + EventTime: args.EventTime, |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + return nil, fmt.Errorf("add activity to incident: %w", err) |
| 110 | + } |
| 111 | + b, err := json.Marshal(activity) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("marshal incident: %w", err) |
| 114 | + } |
| 115 | + return mcp.NewToolResultText(string(b)), nil |
| 116 | +} |
| 117 | + |
| 118 | +var AddActivityToIncidentTool, AddActivityToIncidentHandler = mcpgrafana.MustTool( |
| 119 | + "add_activity_to_incident", |
| 120 | + "Add an activity to an incident", |
| 121 | + AddActivityToIncident, |
| 122 | +) |
| 123 | + |
| 124 | +func AddIncidentTools(mcp *server.MCPServer) { |
| 125 | + mcp.AddTool(ListIncidentsTool, ListIncidentsHandler) |
| 126 | + mcp.AddTool(CreateIncidentTool, CreateIncidentHandler) |
| 127 | + mcp.AddTool(AddActivityToIncidentTool, AddActivityToIncidentHandler) |
| 128 | +} |
0 commit comments