Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(mcp): coerce exact tags metrics filters (#15622)

Lysander Kiesel5b496a17b52e
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index 2c81d16f69..b7d1a8cfdf 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -1653,6 +1653,55 @@ describe("MCP Read Tools", () => {
expect(Number(rows[0].count_count)).toBe(3);
});
+ it("should coerce an exact string tags filter", async () => {
+ const { context, projectId } = await createMcpTestSetup();
+ const traceId = randomUUID();
+ const matchingTag = `mcp-metrics-tag-${nanoid()}`;
+
+ await createEventsCh([
+ createObservationEvent({
+ projectId,
+ traceId,
+ tags: [matchingTag],
+ startTime: new Date("2026-01-01T00:00:00.000Z"),
+ }),
+ createObservationEvent({
+ projectId,
+ traceId,
+ tags: [`mcp-metrics-tag-miss-${nanoid()}`],
+ startTime: new Date("2026-01-01T00:01:00.000Z"),
+ }),
+ ]);
+
+ const rows = getMetricRows(
+ await handleQueryMetrics(
+ {
+ view: "observations",
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [
+ {
+ type: "string",
+ column: "traceId",
+ operator: "=",
+ value: traceId,
+ },
+ {
+ type: "string",
+ column: "tags",
+ operator: "=",
+ value: matchingTag,
+ },
+ ],
+ ...metricsWindow,
+ } as unknown as Parameters<typeof handleQueryMetrics>[0],
+ context,
+ ),
+ );
+
+ expect(rows).toHaveLength(1);
+ expect(Number(rows[0].count_count)).toBe(1);
+ });
+
it("should accept raw metric names in orderBy", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();
diff --git a/web/src/features/mcp/features/metrics/tools/queryMetrics.ts b/web/src/features/mcp/features/metrics/tools/queryMetrics.ts
index 46802ac6e3..0a20917afd 100644
--- a/web/src/features/mcp/features/metrics/tools/queryMetrics.ts
+++ b/web/src/features/mcp/features/metrics/tools/queryMetrics.ts
@@ -4,6 +4,7 @@ import {
dimension,
metric,
validateQuery,
+ viewDeclarations,
viewsV2,
} from "@langfuse/shared/query";
import {
@@ -17,6 +18,45 @@ import { z } from "zod";
const DEFAULT_ROW_LIMIT = 100;
+const normalizeMetricFilters = (
+ input: z.infer<typeof MetricsQueryObjectV2>,
+): z.infer<typeof MetricsQueryObjectV2> => {
+ const tagsDimension = viewDeclarations.v2[input.view].dimensions.tags;
+ const tagsAreArray =
+ tagsDimension?.type === "string[]" || tagsDimension?.type === "arrayString";
+
+ if (!tagsAreArray) {
+ return input;
+ }
+
+ return {
+ ...input,
+ filters: input.filters.map((filter) => {
+ if (filter.column !== "tags") {
+ return filter;
+ }
+
+ if (filter.type === "string" && filter.operator === "=") {
+ return {
+ type: "arrayOptions",
+ column: filter.column,
+ operator: "any of",
+ value: [filter.value],
+ };
+ }
+
+ if (filter.type === "stringOptions") {
+ return {
+ ...filter,
+ type: "arrayOptions",
+ };
+ }
+
+ return filter;
+ }),
+ };
+};
+
const normalizeMetricOrderByFields = (
input: z.infer<typeof MetricsQueryObjectV2>,
) => {
@@ -110,7 +150,9 @@ export const [queryMetricsTool, handleQueryMetrics] = defineTool({
"mcp.metrics_view": input.view,
},
fn: async () => {
- const normalizedInput = normalizeMetricOrderByFields(input);
+ const normalizedInput = normalizeMetricOrderByFields(
+ normalizeMetricFilters(input),
+ );
const validation = validateQuery(normalizedInput, "v2");
if (!validation.valid) {