🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(mcp): Enable filtering observations by metadata (#14584)
Ben Bachem7fbe16551b02
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.tsindex 3a0a77b23a..7f632b7a58 100644--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts@@ -608,6 +608,12 @@ describe("MCP Read Tools", () => {expect(result.resource).toBe("observation");expect(result.columns.providedModelName.type).toBe("stringOptions");expect(result.columns.tags.type).toBe("arrayOptions");+ expect(result.columns.metadata).toEqual(+ expect.objectContaining({+ type: "stringObject",+ requiresKey: true,+ }),+ );expect(result.columns.traceTags).toBeUndefined();expect(result.columns.comments).toBeUndefined();expect(result.columns.scores).toBeUndefined();@@ -718,10 +724,65 @@ describe("MCP Read Tools", () => {operator: expect.objectContaining({ type: "string" }),value: expect.any(Object),type: expect.objectContaining({ type: "string" }),+ key: expect.objectContaining({ type: "string" }),}),);});+ it("should filter by metadata advanced filters", async () => {+ const { context, projectId } = await createMcpTestSetup();+ const traceId = randomUUID();+ const matchingObservation = createObservationEvent({+ projectId,+ traceId,+ name: `mcp-filter-metadata-match-${nanoid()}`,+ metadata: { region: "us-east", tenant: "acme" },+ });+ const nonMatchingObservation = createObservationEvent({+ projectId,+ traceId,+ name: `mcp-filter-metadata-miss-${nanoid()}`,+ metadata: { region: "eu-west", tenant: "acme" },+ });++ await createEventsCh([matchingObservation, nonMatchingObservation]);++ const result = (await handleListObservations(+ {+ filter: [+ {+ type: "stringOptions",+ column: "id",+ operator: "any of",+ value: [matchingObservation.id, nonMatchingObservation.id],+ },+ {+ type: "stringObject",+ column: "metadata",+ key: "region",+ operator: "contains",+ value: "us-",+ },+ ],+ fields: ["id", "name"],+ limit: 100,+ },+ context,+ )) as { data: Array<{ id: string; name: string; url: string }> };++ expect(result.data).toEqual([+ {+ id: matchingObservation.id,+ name: matchingObservation.name,+ url: buildObservationUrl({+ projectId,+ traceId,+ observationId: matchingObservation.id,+ }),+ },+ ]);+ });+it("should list observations with compact default projection", async () => {const { context, projectId } = await createMcpTestSetup();const traceId = randomUUID();diff --git a/web/src/features/mcp/features/observations/tools/listObservations.ts b/web/src/features/mcp/features/observations/tools/listObservations.tsindex 4b60f03ced..60892ac264 100644--- a/web/src/features/mcp/features/observations/tools/listObservations.ts+++ b/web/src/features/mcp/features/observations/tools/listObservations.ts@@ -144,6 +144,12 @@ const ObservationMcpFilterBaseSchema = zoperator: z.string(),value: z.any(),type: z.string().optional(),+ key: z+ .string()+ .optional()+ .describe(+ "Metadata/object key to filter on. Required when column is metadata.",+ ),}).describe(`Advanced observation filter object. Example: ${OBSERVATION_MCP_FILTER_EXAMPLE_JSON}. The explicit form ${OBSERVATION_MCP_FILTER_EXAMPLE_WITH_TYPE_JSON} is also accepted.`,@@ -226,7 +232,7 @@ const ListObservationsBaseSchema = z.object({.array(ObservationMcpFilterBaseSchema).optional().describe(- "Advanced filters. Each item must be an object with column, operator, value, and optional type. Type is inferred from getObservationFilterSchema columns when omitted.",+ "Advanced filters. Each item must be an object with column, operator, value, optional type, and optional key. Type is inferred from getObservationFilterSchema columns when omitted. Use key for metadata filters.",),});@@ -235,7 +241,7 @@ const ListObservationsInputSchema = ListObservationsBaseSchema.extend({.array(ObservationMcpFilterSchema).optional().describe(- "Advanced filters. Each item must be an object with column, operator, value, and optional type. Type is inferred from getObservationFilterSchema columns when omitted.",+ "Advanced filters. Each item must be an object with column, operator, value, optional type, and optional key. Type is inferred from getObservationFilterSchema columns when omitted. Use key for metadata filters.",),});@@ -300,6 +306,7 @@ export const [listObservationsTool, handleListObservations] = defineTool({description: ["Find and review observations in the current Langfuse project, such as generations, spans, events, agent steps, and tool calls.","Use filters to narrow results by trace, name, type, level, environment, time range, or advanced filter conditions. Results are paginated with an opaque cursor.",+ "For metadata filters, first call getObservationFilterMetadataKeys with observationIds, traceId, or a bounded time range, then set the relevant key field in the filter.","",'By default this returns compact summary fields. Use fields: ["*"] for the full observation, or pass specific field names to limit the response size.','Important: if you request metadata explicitly, for example fields: ["id", "metadata"], metadata values are truncated to 200 UTF-8 characters per key unless you also pass expandMetadataKeys with the keys that may need full values.',