🪢 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): accept exact observation id filters (#15625)
Lysander Kiesel0bf96c8b46ae
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.tsindex b7d1a8cfdf..332f55d310 100644--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts@@ -1351,6 +1351,44 @@ describe("MCP Read Tools", () => {).resolves.toMatchObject({ data: [] });});+ it("should treat an exact observation id filter as selective scope", async () => {+ const { context, projectId } = await createMcpTestSetup();+ const observation = createObservationEvent({+ projectId,+ input: "selective input",+ });++ await createEventsCh([observation]);++ const result = (await handleListObservations(+ {+ fields: ["id", "input"],+ filter: [+ {+ type: "string",+ column: "id",+ operator: "=",+ value: observation.id,+ },+ ],+ limit: 100,+ },+ context,+ )) as { data: Array<{ id: string; input: string }> };++ expect(result.data).toEqual([+ {+ id: observation.id,+ input: "selective input",+ url: buildObservationUrl({+ projectId,+ traceId: observation.trace_id,+ observationId: observation.id,+ }),+ },+ ]);+ });+it("should infer advanced filter type from the column", 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 c848df9893..64e6ce3012 100644--- a/web/src/features/mcp/features/observations/tools/listObservations.ts+++ b/web/src/features/mcp/features/observations/tools/listObservations.ts@@ -157,55 +157,81 @@ const ObservationMcpFilterBaseSchema = z`Advanced observation filter object. Example: ${OBSERVATION_MCP_FILTER_EXAMPLE_JSON}. The explicit form ${OBSERVATION_MCP_FILTER_EXAMPLE_WITH_TYPE_JSON} is also accepted.`,);-const ObservationMcpFilterSchema = z- .object({ column: z.string() })- .loose()- .superRefine((filter, ctx) => {- if (!OBSERVATION_MCP_FILTER_COLUMN_TYPES.has(filter.column)) {- ctx.addIssue({- code: "custom",- path: ["column"],- message: `Invalid observation filter column "${filter.column}". Call getObservationFilterSchema for accepted columns.`,- });- return;- }+const normalizeExactObservationIdFilter = (filter: unknown) => {+ if (!filter || typeof filter !== "object" || Array.isArray(filter)) {+ return filter;+ }- const type = OBSERVATION_MCP_FILTER_COLUMN_TYPES.get(filter.column);+ const candidate = filter as Record<string, unknown>;+ if (+ candidate.column !== "id" ||+ candidate.operator !== "=" ||+ typeof candidate.value !== "string" ||+ (candidate.type !== undefined && candidate.type !== "string")+ ) {+ return filter;+ }- if (!type || !isObservationMcpFilterType(type)) {- ctx.addIssue({- code: "custom",- path: ["type"],- message: `Invalid observation filter type "${String(type)}" for column "${filter.column}".`,- });- return;- }+ return {+ ...candidate,+ type: "stringOptions",+ operator: "any of",+ value: [candidate.value],+ };+};++const ObservationMcpFilterSchema = z.preprocess(+ normalizeExactObservationIdFilter,+ z+ .object({ column: z.string() })+ .loose()+ .superRefine((filter, ctx) => {+ if (!OBSERVATION_MCP_FILTER_COLUMN_TYPES.has(filter.column)) {+ ctx.addIssue({+ code: "custom",+ path: ["column"],+ message: `Invalid observation filter column "${filter.column}". Call getObservationFilterSchema for accepted columns.`,+ });+ return;+ }- const filterParseResult = OBSERVATION_MCP_FILTER_SCHEMA_BY_TYPE[type](- filter.column,- Boolean(filter.type),- ).safeParse(filter);+ const type = OBSERVATION_MCP_FILTER_COLUMN_TYPES.get(filter.column);- if (!filterParseResult.success) {- for (const issue of filterParseResult.error.issues) {+ if (!type || !isObservationMcpFilterType(type)) {ctx.addIssue({code: "custom",- path: issue.path,- message: issue.message,+ path: ["type"],+ message: `Invalid observation filter type "${String(type)}" for column "${filter.column}".`,});+ return;}- }- })- .transform((filter) => {- const type =- filter.type ?? OBSERVATION_MCP_FILTER_COLUMN_TYPES.get(filter.column);-- return singleFilter.parse(- filter.column === "tags"- ? { ...filter, type, column: "traceTags" }- : { ...filter, type },- );- });++ const filterParseResult = OBSERVATION_MCP_FILTER_SCHEMA_BY_TYPE[type](+ filter.column,+ Boolean(filter.type),+ ).safeParse(filter);++ if (!filterParseResult.success) {+ for (const issue of filterParseResult.error.issues) {+ ctx.addIssue({+ code: "custom",+ path: issue.path,+ message: issue.message,+ });+ }+ }+ })+ .transform((filter) => {+ const type =+ filter.type ?? OBSERVATION_MCP_FILTER_COLUMN_TYPES.get(filter.column);++ return singleFilter.parse(+ filter.column === "tags"+ ? { ...filter, type, column: "traceTags" }+ : { ...filter, type },+ );+ }),+);const ListObservationsBaseSchema = z.object({fields: ObservationFieldsSchema,