prometheus_query
Run Prometheus queries directly by inputting query expressions and optional time parameters. Integrate with the prometheus-mcp server to interact with monitoring metrics efficiently.
Instructions
Execute a Prometheus query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | prometheus query expression | |
| time | No | optional time parameter for the query, in RFC3339 format |
Implementation Reference
- src/server/tools.ts:49-55 (schema)Zod schema defining the input parameters for the prometheus_query tool: required 'query' string and optional 'time' string.const PrometheusQuerySchema = z.object({ query: z.string().describe("prometheus query expression"), time: z .string() .optional() .describe("optional time parameter for the query, in RFC3339 format"), });
- src/server/tools.ts:169-178 (handler)MCP tool handler definition for 'prometheus_query'. The handle function delegates to PrometheusClient.query with the provided query and optional time arguments.defineTool<typeof PrometheusQuerySchema, QueryResult>({ capability: "query", name: "prometheus_query", title: "Prometheus Query", description: "Execute a Prometheus query", inputSchema: PrometheusQuerySchema, type: "readonly", handle: async (client: PrometheusClient, args) => client.query(args.query, args.time), }),
- src/server/server.ts:58-62 (registration)Registration of prometheus_query (and other tools) to the MCP server. Filters tools by capability and registers each via _registerTool.const prometheusTools: ToolAny[] = tools.filter( (tool) => validatedConfig[capabilityMap[tool.capability]] ?? false, ); prometheusTools.forEach((tool) => this._registerTool(tool)); }
- src/prometheus/client.ts:112-119 (handler)Core implementation of the query logic in PrometheusClient: prepares parameters and makes HTTP request to Prometheus /api/v1/query endpoint.async query(query: string, time?: string): Promise<QueryResult> { const endpoint = "/api/v1/query"; const params: Record<string, string> = { query }; if (time) { params.time = time; } return this.request<QueryResult>(endpoint, params); }
- src/prometheus/client.ts:52-92 (helper)Private helper method for making authenticated HTTP requests to the Prometheus API, handles errors and logging.private async request<T>( endpoint: string, params?: Record<string, string>, ): Promise<T> { const url = new URL(endpoint, this.baseUrl); const queryParams = new URLSearchParams(params); if (queryParams) { url.search = queryParams.toString(); } logger.debug("making prometheus request", { endpoint, url }); try { const response = await fetch(url.toString(), { method: "GET", headers: this.headers, }); if (!response.ok) { const error = `http ${response.status}: ${response.statusText}`; logger.error(error, { endpoint, status: response.status }); throw new Error(error); } const result: Response<T> = await response.json(); if (result.status !== "success") { const errorMsg = result.error || "unknown error"; const error = `prometheus api error: ${errorMsg}`; logger.error(error, { endpoint, status: result.status }); throw new Error(error); } logger.debug("prometheus request successful", { endpoint }); return result.data; } catch (error) { logger.error("prometheus request failed", { endpoint, error: error instanceof Error ? error.message : String(error), }); throw error; }