opsgenie-mcp-server
Version:
A Model Context Protocol (MCP) server that provides comprehensive Opsgenie alert management capabilities
163 lines (162 loc) • 4.94 kB
JavaScript
const OPSGENIE_API_BASE = 'https://api.opsgenie.com';
export class OpsgenieAPIError extends Error {
status;
response;
constructor(message, status, response) {
super(message);
this.status = status;
this.response = response;
this.name = 'OpsgenieAPIError';
// Prevent unused parameter warnings
void status;
void response;
}
}
// Helper function for making Opsgenie API requests
async function makeOpsgenieRequest(endpoint, options) {
const { method = 'GET', body, params, apiKey } = options;
// Build URL with query parameters
const url = new URL(`${OPSGENIE_API_BASE}${endpoint}`);
if (params) {
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
if (Array.isArray(value)) {
url.searchParams.append(key, value.join(','));
}
else {
url.searchParams.append(key, String(value));
}
}
});
}
const headers = {
Authorization: `GenieKey ${apiKey}`,
'Content-Type': 'application/json',
};
const requestOptions = {
method,
headers,
};
if (body && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
requestOptions.body = JSON.stringify(body);
}
try {
const response = await fetch(url.toString(), requestOptions);
if (!response.ok) {
let errorMessage = `HTTP error! status: ${response.status}`;
let errorData = null;
try {
errorData = await response.json();
errorMessage = errorData.message || errorMessage;
}
catch {
// If JSON parsing fails, keep the original error message
}
throw new OpsgenieAPIError(errorMessage, response.status, errorData);
}
// Handle 202 responses (accepted) that might have different structure
if (response.status === 202) {
return (await response.json());
}
return (await response.json());
}
catch (error) {
if (error instanceof OpsgenieAPIError) {
throw error;
}
throw new OpsgenieAPIError(`Network error: ${error instanceof Error ? error.message : 'Unknown error'}`, 0);
}
}
// API Functions
/**
* List alerts
* GET /v2/alerts
*/
export async function listAlerts(apiKey, params) {
return makeOpsgenieRequest('/v2/alerts', {
method: 'GET',
params,
apiKey,
});
}
/**
* Create alert
* POST /v2/alerts
*/
export async function createAlert(apiKey, payload) {
return makeOpsgenieRequest('/v2/alerts', {
method: 'POST',
body: payload,
apiKey,
});
}
/**
* Acknowledge alert
* POST /v2/alerts/{identifier}/acknowledge
*/
export async function acknowledgeAlert(apiKey, identifier, payload, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/acknowledge`, {
method: 'POST',
body: payload,
params: { identifierType },
apiKey,
});
}
/**
* Close alert
* POST /v2/alerts/{identifier}/close
*/
export async function closeAlert(apiKey, identifier, payload, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/close`, {
method: 'POST',
body: payload,
params: { identifierType },
apiKey,
});
}
/**
* List alert notes
* GET /v2/alerts/{identifier}/notes
*/
export async function listAlertNotes(apiKey, identifier, params, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/notes`, {
method: 'GET',
params: { ...params, identifierType },
apiKey,
});
}
/**
* Add note to alert
* POST /v2/alerts/{identifier}/notes
*/
export async function addNoteToAlert(apiKey, identifier, payload, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/notes`, {
method: 'POST',
body: payload,
params: { identifierType },
apiKey,
});
}
/**
* List alert logs
* GET /v2/alerts/{identifier}/logs
*/
export async function listAlertLogs(apiKey, identifier, params, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/logs`, {
method: 'GET',
params: { ...params, identifierType },
apiKey,
});
}
/**
* Add details to alert
* POST /v2/alerts/{identifier}/details
*/
export async function addDetailsToAlert(apiKey, identifier, payload, identifierType = 'id') {
return makeOpsgenieRequest(`/v2/alerts/${encodeURIComponent(identifier)}/details`, {
method: 'POST',
body: payload,
params: { identifierType },
apiKey,
});
}