Skip to content

Commit 8e1f203

Browse files
authored
feat: add Datadog downtimes tool handlers and schemas (#13)
* 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 * docs: update README with new Datadog downtime management functions Add documentation for two new Datadog downtime management functions: - `list_downtimes`: Retrieve scheduled downtimes with optional filtering - `schedule_downtime`: Create downtimes with advanced configuration options - `cancel_downtime`: Cancel a specific scheduled downtime Expand README to provide comprehensive details about input parameters and return values for these new downtime management tools.
1 parent 03a79c4 commit 8e1f203

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,44 @@ MCP server for the Datadog API, enabling incident management and more.
107107
- **Returns**: Success status and confirmation message.
108108

109109
11. `unmute_host`
110+
110111
- Unmute a host in Datadog.
111112
- **Inputs**:
112113
- `hostname` (string): The name of the host to unmute.
113114
- **Returns**: Success status and confirmation message.
114115

116+
12. `list_downtimes`
117+
118+
- List scheduled downtimes from Datadog.
119+
- **Inputs**:
120+
- `currentOnly` (optional boolean): Return only currently active downtimes when true.
121+
- `monitorId` (optional number): Filter by monitor ID.
122+
- **Returns**: Array of scheduled downtimes with details including scope, monitor information, and schedule.
123+
124+
13. `schedule_downtime`
125+
126+
- Schedule a downtime in Datadog.
127+
- **Inputs**:
128+
- `scope` (string): Scope to apply downtime to (e.g. 'host:my-host').
129+
- `start` (optional number): UNIX timestamp for the start of the downtime.
130+
- `end` (optional number): UNIX timestamp for the end of the downtime.
131+
- `message` (optional string): A message to include with the downtime.
132+
- `timezone` (optional string): The timezone for the downtime (e.g. 'UTC', 'America/New_York').
133+
- `monitorId` (optional number): The ID of the monitor to mute.
134+
- `monitorTags` (optional array): A list of monitor tags for filtering.
135+
- `recurrence` (optional object): Recurrence settings for the downtime.
136+
- `type` (string): Recurrence type ('days', 'weeks', 'months', 'years').
137+
- `period` (number): How often to repeat (must be >= 1).
138+
- `weekDays` (optional array): Days of the week for weekly recurrence.
139+
- `until` (optional number): UNIX timestamp for when the recurrence ends.
140+
- **Returns**: Scheduled downtime details including ID and active status.
141+
142+
14. `cancel_downtime`
143+
- Cancel a scheduled downtime in Datadog.
144+
- **Inputs**:
145+
- `downtimeId` (number): The ID of the downtime to cancel.
146+
- **Returns**: Confirmation of downtime cancellation.
147+
115148
## Setup
116149

117150
### Datadog Credentials

src/index.ts

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { DOWNTIMES_TOOLS, createDowntimesToolHandlers } from './tool'

src/tools/downtimes/schema.ts

Lines changed: 30 additions & 0 deletions
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

Lines changed: 119 additions & 0 deletions
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)