run_nerdgraph_query
Execute custom GraphQL queries on New Relic's NerdGraph API to fetch or manage telemetry data and configurations.
Instructions
Execute a custom NerdGraph GraphQL query
Input 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/server.ts:283-294 (handler)Execution handler for 'run_nerdgraph_query' in executeTool() - delegates to NerdGraphTool.execute()
case 'run_nerdgraph_query': return await new NerdGraphTool(this.client).execute(args); default: { const tool = this.tools.get(name); if (!tool) { throw new Error(`Tool ${name} not found`); } throw new Error(`Tool handler for ${name} not implemented`); } } } - src/tools/nerdgraph.ts:33-44 (handler)NerdGraphTool.execute() - validates input with Zod schema and calls client.executeNerdGraphQuery()
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)Tool definition with inputSchema for 'run_nerdgraph_query' (name, description, required 'query' string, 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-79 (registration)Registration of the tool in registerTools() - nerdGraphTool.getQueryTool() is added to the tools map
nerdGraphTool.getQueryTool(), - executeNerdGraphQuery() - the underlying HTTP helper that sends the GraphQL query to New Relic's NerdGraph API
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>; }