Skip to content

Commit 825c6a6

Browse files
committed
feat: add Datadog downtimes tool handlers and schemas
This commit introduces comprehensive support for Datadog downtime management: - Create new downtimes tool module with schemas for listing, scheduling, and canceling downtimes - Add downtimes tool handlers to the main server configuration - Implement tool schemas with Zod for robust input validation - Support advanced downtime scheduling with recurrence options - Integrate downtimes tools into the existing Datadog tool ecosystem
1 parent 03a79c4 commit 825c6a6

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { TRACES_TOOLS, createTracesToolHandlers } from './tools/traces'
2626
import { HOSTS_TOOLS, createHostsToolHandlers } from './tools/hosts'
2727
import { ToolHandlers } from './utils/types'
2828
import { createDatadogConfig } from './utils/datadog'
29+
import { createDowntimesToolHandlers, DOWNTIMES_TOOLS } from './tools/downtimes'
2930

3031
const server = new Server(
3132
{
@@ -57,6 +58,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
5758
...DASHBOARDS_TOOLS,
5859
...TRACES_TOOLS,
5960
...HOSTS_TOOLS,
61+
...DOWNTIMES_TOOLS,
6062
],
6163
}
6264
})
@@ -79,6 +81,7 @@ const TOOL_HANDLERS: ToolHandlers = {
7981
...createDashboardsToolHandlers(datadogConfig),
8082
...createTracesToolHandlers(datadogConfig),
8183
...createHostsToolHandlers(datadogConfig),
84+
...createDowntimesToolHandlers(datadogConfig),
8285
}
8386
/**
8487
* Handler for invoking Datadog-related tools in the mcp-server-datadog.

src/tools/downtimes/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { DOWNTIMES_TOOLS, createDowntimesToolHandlers } from './tool'

src/tools/downtimes/schema.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { z } from 'zod'
2+
3+
export const ListDowntimesZodSchema = z.object({
4+
currentOnly: z.boolean().optional(),
5+
monitorId: z.number().optional(),
6+
})
7+
8+
export const ScheduleDowntimeZodSchema = z.object({
9+
scope: z.string().nonempty(), // example: 'host:my-host'
10+
start: z.number().optional(), // UNIX timestamp
11+
end: z.number().optional(), // UNIX timestamp
12+
message: z.string().optional(),
13+
timezone: z.string().optional(), // example: 'UTC', 'America/New_York'
14+
monitorId: z.number().optional(),
15+
monitorTags: z.array(z.string()).optional(),
16+
recurrence: z
17+
.object({
18+
type: z.enum(['days', 'weeks', 'months', 'years']),
19+
period: z.number().min(1),
20+
weekDays: z
21+
.array(z.enum(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']))
22+
.optional(),
23+
until: z.number().optional(), // UNIX timestamp
24+
})
25+
.optional(),
26+
})
27+
28+
export const CancelDowntimeZodSchema = z.object({
29+
downtimeId: z.number(),
30+
})

src/tools/downtimes/tool.ts

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { ExtendedTool, ToolHandlers } from '../../utils/types'
2+
import { client, v1 } from '@datadog/datadog-api-client'
3+
import { createToolSchema } from '../../utils/tool'
4+
import {
5+
ListDowntimesZodSchema,
6+
ScheduleDowntimeZodSchema,
7+
CancelDowntimeZodSchema,
8+
} from './schema'
9+
10+
type DowntimesToolName =
11+
| 'list_downtimes'
12+
| 'schedule_downtime'
13+
| 'cancel_downtime'
14+
type DowntimesTool = ExtendedTool<DowntimesToolName>
15+
16+
export const DOWNTIMES_TOOLS: DowntimesTool[] = [
17+
createToolSchema(
18+
ListDowntimesZodSchema,
19+
'list_downtimes',
20+
'List scheduled downtimes from Datadog',
21+
),
22+
createToolSchema(
23+
ScheduleDowntimeZodSchema,
24+
'schedule_downtime',
25+
'Schedule a downtime in Datadog',
26+
),
27+
createToolSchema(
28+
CancelDowntimeZodSchema,
29+
'cancel_downtime',
30+
'Cancel a scheduled downtime in Datadog',
31+
),
32+
] as const
33+
34+
type DowntimesToolHandlers = ToolHandlers<DowntimesToolName>
35+
36+
export const createDowntimesToolHandlers = (
37+
config: client.Configuration,
38+
): DowntimesToolHandlers => {
39+
const apiInstance = new v1.DowntimesApi(config)
40+
41+
return {
42+
list_downtimes: async (request) => {
43+
const { currentOnly, monitorId } = ListDowntimesZodSchema.parse(
44+
request.params.arguments,
45+
)
46+
47+
const res = await apiInstance.listDowntimes({
48+
currentOnly,
49+
monitorId,
50+
})
51+
52+
return {
53+
content: [
54+
{
55+
type: 'text',
56+
text: `Listed downtimes:\n${JSON.stringify(res, null, 2)}`,
57+
},
58+
],
59+
}
60+
},
61+
62+
schedule_downtime: async (request) => {
63+
const params = ScheduleDowntimeZodSchema.parse(request.params.arguments)
64+
65+
// Convert to the format expected by Datadog client
66+
const downtimeData: v1.Downtime = {
67+
scope: params.scope,
68+
start: params.start,
69+
end: params.end,
70+
message: params.message,
71+
timezone: params.timezone,
72+
monitorId: params.monitorId,
73+
monitor_tags: params.monitorTags,
74+
}
75+
76+
// Add recurrence configuration if provided
77+
if (params.recurrence) {
78+
downtimeData.recurrence = {
79+
type: params.recurrence.type,
80+
period: params.recurrence.period,
81+
week_days: params.recurrence.weekDays,
82+
until: params.recurrence.until,
83+
}
84+
}
85+
86+
const res = await apiInstance.createDowntime({
87+
body: downtimeData,
88+
})
89+
90+
return {
91+
content: [
92+
{
93+
type: 'text',
94+
text: `Scheduled downtime: ${JSON.stringify(res, null, 2)}`,
95+
},
96+
],
97+
}
98+
},
99+
100+
cancel_downtime: async (request) => {
101+
const { downtimeId } = CancelDowntimeZodSchema.parse(
102+
request.params.arguments,
103+
)
104+
105+
await apiInstance.cancelDowntime({
106+
downtimeId,
107+
})
108+
109+
return {
110+
content: [
111+
{
112+
type: 'text',
113+
text: `Cancelled downtime with ID: ${downtimeId}`,
114+
},
115+
],
116+
}
117+
},
118+
}
119+
}

0 commit comments

Comments
 (0)