list_alert_policies
Retrieve all alert policies from your New Relic account to review and manage incident notification settings.
Instructions
List all alert policies in your New Relic account
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| target_account_id | No | Optional New Relic account ID |
Implementation Reference
- src/tools/alert.ts:65-103 (handler)The `listAlertPolicies` async method executes a NerdGraph GraphQL query to list all alert policies for a given account ID. It validates the account ID, builds a GraphQL query to fetch policies (id, name, incidentPreference, conditions), and returns the policies array or an empty array.
async listAlertPolicies(input: { target_account_id?: string; }): Promise<Array<Record<string, unknown>>> { const accountId = input.target_account_id; if (!accountId) { throw new Error('Account ID must be provided'); } if (!/^\d+$/.test(accountId)) { throw new Error('Invalid account ID format'); } const query = `{ actor { account(id: ${accountId}) { alerts { policiesSearch { policies { id name incidentPreference conditions { id name enabled } } } } } } }`; const response = await this.client.executeNerdGraphQuery<{ actor?: { account?: { alerts?: { policiesSearch?: { policies?: Array<Record<string, unknown>> } } }; }; }>(query); return response.data?.actor?.account?.alerts?.policiesSearch?.policies || []; } - src/tools/alert.ts:11-24 (schema)The `getPoliciesTool()` method defines the tool's name ('list_alert_policies'), description, and input schema. The schema accepts an optional `target_account_id` string parameter.
getPoliciesTool(): Tool { return { name: 'list_alert_policies', description: 'List all alert policies in your New Relic account', inputSchema: { type: 'object', properties: { target_account_id: { type: 'string', description: 'Optional New Relic account ID', }, }, }, }; - src/server.ts:57-105 (registration)The `registerTools()` method registers all tools into a Map. At line 74, `alertTool.getPoliciesTool()` is called to register the 'list_alert_policies' tool.
private registerTools(): void { const nrqlTool = new NrqlTool(this.client); const apmTool = new ApmTool(this.client); const entityTool = new EntityTool(this.client); const alertTool = new AlertTool(this.client); const syntheticsTool = new SyntheticsTool(this.client); const nerdGraphTool = new NerdGraphTool(this.client); const restDeployments = new RestDeploymentsTool(); const restApm = new RestApmTool(); const restMetrics = new RestMetricsTool(); // Register all tools const tools = [ nrqlTool.getToolDefinition(), apmTool.getListApplicationsTool(), entityTool.getSearchTool(), entityTool.getDetailsTool(), alertTool.getPoliciesTool(), alertTool.getIncidentsTool(), alertTool.getAcknowledgeTool(), syntheticsTool.getListMonitorsTool(), syntheticsTool.getCreateMonitorTool(), nerdGraphTool.getQueryTool(), // REST v2 tools restDeployments.getCreateTool(), restDeployments.getListTool(), restDeployments.getDeleteTool(), restApm.getListApplicationsTool(), restMetrics.getListMetricNamesTool(), restMetrics.getMetricDataTool(), restMetrics.getListApplicationHostsTool(), { name: 'get_account_details', description: 'Get New Relic account details', inputSchema: { type: 'object' as const, properties: { target_account_id: { type: 'string' as const, description: 'Optional account ID to get details for', }, }, }, }, ]; tools.forEach((tool) => { this.tools.set(tool.name, tool); }); - src/server.ts:205-209 (registration)In the `executeTool()` switch statement, the 'list_alert_policies' case dispatches to `AlertTool.listAlertPolicies()` with the resolved account ID.
case 'list_alert_policies': return await new AlertTool(this.client).listAlertPolicies({ ...args, target_account_id: accountId, }); - The `executeNerdGraphQuery` helper method is used by `listAlertPolicies` to make the actual HTTP POST request to the New Relic NerdGraph API. It handles API key validation, sends the GraphQL query, and returns the parsed JSON response.
async executeNerdGraphQuery<T = unknown>( query: string, variables?: Record<string, unknown> ): Promise<GraphQLResponse<T>> { // Check if API key is missing or empty if (!this.apiKey || this.apiKey === '' || this.apiKey.length === 0) { throw new Error('NEW_RELIC_API_KEY environment variable is not set'); } const response = await fetch(NERDGRAPH_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'API-Key': this.apiKey, }, body: JSON.stringify({ query, variables }), }); if (!response.ok) { if (response.status === 401) { throw new Error('Unauthorized: Invalid API key'); } throw new Error(`NerdGraph API error: ${response.status} ${response.statusText}`); } return (await response.json()) as GraphQLResponse<T>; }