Query Langfuse metrics
getMetricsRun metrics queries on Langfuse data to obtain counts, latency, cost, and token usage with a JSON query string.
Instructions
Run a metrics query (counts, latency, cost, token usage). Pass a JSON query string per the Langfuse metrics API.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | JSON metrics query (view, dimensions, metrics, filters, fromTimestamp, toTimestamp). See Langfuse docs. |
Implementation Reference
- src/tools.ts:285-286 (handler)The handler function that executes the getMetrics tool: passes the 'query' parameter to the Langfuse /api/public/metrics endpoint via client.get() and wraps the result in asJson().
async ({ query }) => asJson(await client.get("/api/public/metrics", { query })), ); - src/tools.ts:276-283 (schema)Input schema for getMetrics: a single required 'query' string parameter containing a JSON metrics query.
inputSchema: { query: z .string() .min(1) .describe( "JSON metrics query (view, dimensions, metrics, filters, fromTimestamp, toTimestamp). See Langfuse docs.", ), }, - src/tools.ts:270-286 (registration)Registration of the 'getMetrics' tool via server.registerTool() inside the registerTools function.
server.registerTool( "getMetrics", { title: "Query Langfuse metrics", description: "Run a metrics query (counts, latency, cost, token usage). Pass a JSON query string per the Langfuse metrics API.", inputSchema: { query: z .string() .min(1) .describe( "JSON metrics query (view, dimensions, metrics, filters, fromTimestamp, toTimestamp). See Langfuse docs.", ), }, }, async ({ query }) => asJson(await client.get("/api/public/metrics", { query })), ); - src/tools.ts:6-8 (helper)The asJson helper function that wraps API response data into MCP text content format.
const asJson = (data: unknown) => ({ content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }], }); - src/client.ts:27-65 (helper)The LangfuseClient.get() method that performs HTTP GET requests with query params and auth headers.
async get(path: string, params: QueryParams = {}): Promise<unknown> { const url = new URL(`${this.baseUrl}${path}`); for (const [key, value] of Object.entries(params)) { if (value === undefined || value === null || value === "") continue; if (Array.isArray(value)) { for (const item of value) url.searchParams.append(key, String(item)); } else { url.searchParams.set(key, String(value)); } } const response = await fetch(url, { headers: { Authorization: this.authHeader, Accept: "application/json", }, }); const text = await response.text(); if (!response.ok) { throw new LangfuseError( `Langfuse API ${response.status} ${response.statusText}: ${text.slice(0, 500)}`, response.status, text, ); } try { return JSON.parse(text) as unknown; } catch { throw new LangfuseError( `Langfuse API returned non-JSON response from ${url.pathname}: ${text.slice(0, 200)}`, response.status, text, ); } }