run_nerdgraph_query
Execute custom NerdGraph GraphQL queries for querying data, managing incidents, or creating synthetics within New Relic MCP Server.
Instructions
Execute a custom NerdGraph GraphQL query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The GraphQL query to execute | |
| variables | No | Optional GraphQL variables to supply to the query |
Implementation Reference
- src/tools/nerdgraph.ts:33-44 (handler)The primary handler for the 'run_nerdgraph_query' tool. It validates the input parameters (query and optional variables) using Zod and delegates the execution to the NewRelicClient's executeNerdGraphQuery method.async execute(input: unknown): Promise<unknown> { // Validate input with Zod for consistency const schema = z.object({ query: z.string().min(1, 'Invalid or empty GraphQL query provided'), variables: z.record(z.any()).optional(), }); const { query, variables } = schema.parse(input); return await this.client.executeNerdGraphQuery(query, variables); } }
- src/tools/nerdgraph.ts:12-31 (schema)The schema definition for the tool, including name, description, and input schema specifying the required 'query' string and optional 'variables' object.getQueryTool(): Tool { return { name: 'run_nerdgraph_query', description: 'Execute a custom NerdGraph GraphQL query', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The GraphQL query to execute', }, variables: { type: 'object', description: 'Optional GraphQL variables to supply to the query', }, }, required: ['query'], }, }; }
- src/server.ts:79-80 (registration)Registration of the tool in the server's tools array during registerTools() method, where getQueryTool() is called to add the tool definition to the list of available tools.nerdGraphTool.getQueryTool(), // REST v2 tools
- src/server.ts:283-284 (handler)Dispatch handler in the server's executeTool switch statement that instantiates NerdGraphTool and calls its execute method with the tool arguments.case 'run_nerdgraph_query': return await new NerdGraphTool(this.client).execute(args);
- Supporting utility in NewRelicClient that performs the actual GraphQL query execution by making an HTTP POST request to New Relic's NerdGraph API endpoint.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>; }