prom_range_query
Execute PromQL queries over specified time ranges to retrieve and analyze time series data from Prometheus monitoring systems.
Instructions
Execute a PromQL query over a time range and return series data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | PromQL query expression | |
| start | No | Start time (ISO 8601, default: 1h ago) | |
| end | No | End time (ISO 8601, default: now) | |
| step | No | Query step (e.g., '60s', '5m', default: '60s') |
Implementation Reference
- src/tools/prometheus/query.ts:29-74 (handler)The rangeQuery function handles the execution of Prometheus range queries, fetches data from the Prometheus API, and formats the result into a readable string.
export async function rangeQuery(args: Record<string, unknown>): Promise<string> { const query = args.query as string; if (!query) throw new Error("PromQL query is required"); const end = (args.end as string) || new Date().toISOString(); const start = (args.start as string) || new Date(Date.now() - 3600000).toISOString(); const step = (args.step as string) || "60s"; const url = `${config.prometheusUrl}/api/v1/query_range?query=${encodeURIComponent(query)}&start=${encodeURIComponent(start)}&end=${encodeURIComponent(end)}&step=${encodeURIComponent(step)}`; const res = await fetch(url); if (!res.ok) throw new Error(`Prometheus range query failed: ${res.status}`); const data = (await res.json()) as { status: string; error?: string; data: { result: Array<{ metric: Record<string, string>; values: [number, string][] }> } }; if (data.status !== "success") throw new Error(`Query error: ${data.error || "unknown"}`); const results = data.data.result || []; if (results.length === 0) return `No results for range query: ${query}`; const lines: string[] = [`Range query: ${query}`, `Period: ${start} → ${end} (step: ${step})`, ""]; for (const r of results) { const labels = Object.entries(r.metric as Record<string, string>) .map(([k, v]) => `${k}="${v}"`) .join(", "); lines.push(`Metric: ${labels || "{}"}`); const values = (r.values as [number, string][]) || []; const first5 = values.slice(0, 5); const last5 = values.slice(-5); for (const [ts, val] of first5) { lines.push(` ${new Date(ts * 1000).toISOString()} → ${val}`); } if (values.length > 10) { lines.push(` ... (${values.length - 10} more data points) ...`); } if (values.length > 5) { for (const [ts, val] of last5) { lines.push(` ${new Date(ts * 1000).toISOString()} → ${val}`); } } lines.push(""); } return lines.join("\n"); } - src/tools/prometheus/index.ts:18-31 (registration)The MCP tool definition for 'prom_range_query', which defines its name, description, and input parameters.
{ name: "prom_range_query", description: "Execute a PromQL query over a time range and return series data", inputSchema: { type: "object" as const, properties: { query: { type: "string", description: "PromQL query expression" }, start: { type: "string", description: "Start time (ISO 8601, default: 1h ago)" }, end: { type: "string", description: "End time (ISO 8601, default: now)" }, step: { type: "string", description: "Query step (e.g., '60s', '5m', default: '60s')" }, }, required: ["query"], }, },