Skip to main content
Glama
cloudbring

New Relic MCP Server

by cloudbring

run_nrql_query

Execute NRQL queries to analyze New Relic metrics and events directly from your MCP client.

Instructions

Execute NRQL queries against New Relic data to analyze metrics and events

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nrqlYesThe NRQL query to execute
target_account_idNoOptional New Relic account ID to query

Implementation Reference

  • The execute method of NrqlTool implements the core handler logic for the 'run_nrql_query' tool, validating inputs and delegating to the NewRelicClient.
    async execute(input: { nrql?: string; target_account_id?: string }): Promise<NrqlQueryResult> { // Validate input if (!input.nrql || typeof input.nrql !== 'string' || input.nrql.trim() === '') { throw new Error('Invalid or empty NRQL query provided'); } if (!input.target_account_id) { throw new Error('Account ID must be provided'); } if (input.target_account_id && !/^\d+$/.test(input.target_account_id)) { throw new Error('Invalid account ID format'); } const result = await this.client.runNrqlQuery({ nrql: input.nrql, accountId: input.target_account_id, }); return result; }
  • Defines the input schema for the 'run_nrql_query' tool, provided via getToolDefinition() for MCP tool listing.
    getInputSchema() { return { type: 'object' as const, properties: { nrql: { type: 'string', description: 'The NRQL query to execute', }, target_account_id: { type: 'string', description: 'Optional New Relic account ID to query', }, }, required: ['nrql'], }; }
  • src/server.ts:165-169 (registration)
    In the server's executeTool switch statement, registers the handler by instantiating NrqlTool and calling execute for 'run_nrql_query'.
    case 'run_nrql_query': return await new NrqlTool(this.client).execute({ ...args, target_account_id: accountId, });
  • src/server.ts:58-105 (registration)
    Instantiates NrqlTool and registers its tool definition (name, description, schema) in the server's tools map for listTools requests.
    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); });
  • NewRelicClient.runNrqlQuery executes the GraphQL NerdGraph query against New Relic API to run the NRQL and returns the result.
    async runNrqlQuery(params: { nrql: string; accountId: string }): Promise<NrqlQueryResult> { if (!params.nrql || typeof params.nrql !== 'string') { throw new Error('Invalid or empty NRQL query provided'); } if (!params.accountId || !/^\d+$/.test(params.accountId)) { throw new Error('Invalid account ID format'); } const query = `{ actor { account(id: ${params.accountId}) { nrql(query: "${params.nrql.replace(/"/g, '\\"')}") { results metadata { eventTypes timeWindow { begin end } facets } } } } }`; try { type NrqlResponse = { actor?: { account?: { nrql?: { results?: Array<Record<string, unknown>>; metadata?: { eventTypes?: string[]; timeWindow?: { begin: number; end: number }; facets?: string[]; }; }; }; }; }; const response = (await this.executeNerdGraphQuery<NrqlResponse>( query )) as GraphQLResponse<NrqlResponse>; if (response.errors) { const errorMessage = response.errors[0]?.message || 'NRQL query failed'; throw new Error(errorMessage); } const nrqlResult = response.data?.actor?.account?.nrql; if (!nrqlResult) { throw new Error('No results returned from NRQL query'); } // Detect if it's a time series query const isTimeSeries = params.nrql.toLowerCase().includes('timeseries'); return { results: nrqlResult.results || [], metadata: { ...nrqlResult.metadata, timeSeries: isTimeSeries, }, }; } catch (error: unknown) { if (error instanceof Error && error.message.includes('Syntax error')) { throw new Error(`NRQL Syntax error: ${error.message}`); } throw error instanceof Error ? error : new Error(String(error)); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cloudbring/newrelic-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server