Skip to content

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

fix(mcp): support logical root observation filters (#15568)

Valery Meleshkin4dbb49d5cb21
diff --git a/packages/shared/src/eventsTable.ts b/packages/shared/src/eventsTable.ts
index 3a435cea59..3ad1e21b10 100644
--- a/packages/shared/src/eventsTable.ts
+++ b/packages/shared/src/eventsTable.ts
@@ -437,6 +437,7 @@ const OBSERVATION_MCP_ALLOWED_EVENTS_TABLE_FILTER_COLUMN_IDS = [
"output",
"metadata",
"traceTags",
+ "isRootObservation",
"hasParentObservation",
] as const satisfies readonly EventsTableColumnId[];
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index de79056c3c..d78d1c47cb 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -289,6 +289,7 @@ const createObservationEvent = (params: {
type?: "GENERATION" | "SPAN" | "EVENT";
startTime?: Date;
parentObservationId?: string | null;
+ isAppRoot?: boolean;
providedModelName?: string;
input?: string;
output?: string;
@@ -308,6 +309,7 @@ const createObservationEvent = (params: {
span_id: observationId,
trace_id: params.traceId ?? randomUUID(),
parent_span_id: params.parentObservationId ?? null,
+ is_app_root: params.isAppRoot ?? false,
project_id: params.projectId,
name: params.name ?? `mcp-observation-${nanoid()}`,
type: params.type ?? "GENERATION",
@@ -715,9 +717,10 @@ describe("MCP Read Tools", () => {
});
it("should return the observation projection field schema", async () => {
- const { context } = await createMcpTestSetup();
-
- const result = (await handleGetObservationFieldSchema({}, context)) as {
+ const result = (await handleGetObservationFieldSchema(
+ {},
+ mockServerContext(),
+ )) as {
resource: string;
defaultFields: string[];
fields: Record<
@@ -754,6 +757,7 @@ describe("MCP Read Tools", () => {
expect(result.fields.providedModelName.nullable).toBe(true);
expect(result.fields.startTime.type).toBe("datetime");
expect(result.fields.startTime.nullable).toBe(false);
+ expect(result.fields.isRootObservation.type).toBe("boolean");
expect(result.fields.costDetails.type).toBe("map<string, number>");
expect(result.fields.input.expensive).toBe(true);
expect(result.fields.input.sensitive).toBe(true);
@@ -821,6 +825,7 @@ describe("MCP Read Tools", () => {
["modelId", "stringOptions", true],
["providedModelName", "stringOptions", true],
["tags", "arrayOptions", false],
+ ["isRootObservation", "boolean", false],
["hasParentObservation", "boolean", false],
])(
"should expose the %s column used by observation filter values",
@@ -865,9 +870,13 @@ describe("MCP Read Tools", () => {
});
it("should expose object-shaped advanced filters in the tool schema", () => {
- const filterSchema = (
- listObservationsTool.inputSchema.properties as Record<string, unknown>
- ).filter as
+ const properties = listObservationsTool.inputSchema.properties as Record<
+ string,
+ unknown
+ >;
+ expect(properties.isRootObservation).toMatchObject({ type: "boolean" });
+
+ const filterSchema = properties.filter as
| {
type?: string;
items?: {
@@ -1015,6 +1024,7 @@ describe("MCP Read Tools", () => {
name: observation.name,
type: "GENERATION",
level: "DEFAULT",
+ isRootObservation: true,
providedModelName: "gpt-4o-mini",
url: buildObservationUrl({
projectId,
@@ -1162,6 +1172,68 @@ describe("MCP Read Tools", () => {
});
});
+ it("should expose semantic roots across list and filter values", async () => {
+ const { context, projectId } = await createMcpTestSetup();
+ const traceId = randomUUID();
+ const parentlessRoot = createObservationEvent({ projectId, traceId });
+ const appRoot = createObservationEvent({
+ projectId,
+ traceId,
+ parentObservationId: randomUUID(),
+ isAppRoot: true,
+ });
+ const child = createObservationEvent({
+ projectId,
+ traceId,
+ parentObservationId: appRoot.id,
+ });
+
+ await createEventsCh([parentlessRoot, appRoot, child]);
+
+ const rootResult = (await handleListObservations(
+ {
+ traceId,
+ isRootObservation: true,
+ fields: ["id", "parentObservationId", "isRootObservation"],
+ limit: 100,
+ },
+ context,
+ )) as { data: Array<Record<string, unknown> & { id: string }> };
+
+ const getRootValues = async (isRootObservation?: boolean) =>
+ (await handleGetObservationFilterValues(
+ {
+ column: "isRootObservation",
+ isRootObservation,
+ limit: 100,
+ },
+ context,
+ )) as { values: Array<{ value: boolean; count?: number }> };
+ const [allRootValues, scopedRootValues] = await Promise.all([
+ getRootValues(),
+ getRootValues(true),
+ ]);
+
+ expect(new Set(rootResult.data.map(({ id }) => id))).toEqual(
+ new Set([parentlessRoot.id, appRoot.id]),
+ );
+ expect(rootResult.data.find(({ id }) => id === appRoot.id)).toMatchObject(
+ {
+ parentObservationId: appRoot.parent_span_id,
+ isRootObservation: true,
+ },
+ );
+ expect(
+ allRootValues.values.map(({ value, count }) => [value, count]),
+ ).toEqual([
+ [false, 1],
+ [true, 2],
+ ]);
+ expect(
+ scopedRootValues.values.map(({ value, count }) => [value, count]),
+ ).toEqual([[true, 2]]);
+ });
+
it("should match advanced input filters beyond the events_core truncation boundary", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();
@@ -2003,6 +2075,16 @@ describe("MCP Read Tools", () => {
});
});
+ it("should expose semantic-root inputs", () => {
+ const properties = getObservationFilterValuesTool.inputSchema
+ .properties as Record<string, unknown>;
+
+ expect(properties).toMatchObject({
+ column: { enum: expect.arrayContaining(["isRootObservation"]) },
+ isRootObservation: { type: "boolean" },
+ });
+ });
+
it("should be available to in-app agent keys", async () => {
const context = mockServerContext({
inAppAgent: { permissions: "read" },
diff --git a/web/src/features/events/server/eventsService.ts b/web/src/features/events/server/eventsService.ts
index 81b9266a69..44b61a24f2 100644
--- a/web/src/features/events/server/eventsService.ts
+++ b/web/src/features/events/server/eventsService.ts
@@ -549,6 +549,7 @@ export async function getEventFilterValuePage(
params: GetObservationsFilterOptionsParams & {
column:
| "traceTags"
+ | "isRootObservation"
| "hasParentObservation"
| "providedModelName"
| "modelId"
diff --git a/web/src/features/mcp/features/observations/schema.ts b/web/src/features/mcp/features/observations/schema.ts
index 2b0000be34..861202cdae 100644
--- a/web/src/features/mcp/features/observations/schema.ts
+++ b/web/src/features/mcp/features/observations/schema.ts
@@ -20,10 +20,8 @@ type ObservationMcpFieldDefinition = ObservationMcpFieldMetadata & {
group: ObservationFieldGroupPublicApi;
};
-type ObservationMcpField = Exclude<
- (typeof OBSERVATION_FIELD_GROUP_FIELD_NAMES)[ObservationFieldGroupPublicApi][number],
- "isRootObservation"
->;
+type ObservationMcpField =
+ (typeof OBSERVATION_FIELD_GROUP_FIELD_NAMES)[ObservationFieldGroupPublicApi][number];
type ObservationMcpFieldType =
| "array"
@@ -37,8 +35,6 @@ type ObservationMcpFieldType =
const OBSERVATION_MCP_FIELDS = OBSERVATION_FIELD_GROUPS_PUBLIC_API.flatMap(
(group) => OBSERVATION_FIELD_GROUP_FIELD_NAMES[group],
-).filter(
- (field): field is ObservationMcpField => field !== "isRootObservation",
);
const OBSERVATION_MCP_FIELD_SET = new Set<string>(OBSERVATION_MCP_FIELDS);
@@ -53,6 +49,12 @@ const OBSERVATION_MCP_FIELD_METADATA: Record<
endTime: { type: "datetime", nullable: true, default: true },
projectId: { type: "string", sensitive: true },
parentObservationId: { type: "string", nullable: true, default: true },
+ isRootObservation: {
+ type: "boolean",
+ default: true,
+ description:
+ "Whether this observation is a logical root. App-root observations can be roots while retaining a non-null parentObservationId.",
+ },
type: { type: "string", default: true },
name: { type: "string", nullable: true, default: true },
level: { type: "string", default: true },
diff --git a/web/src/features/mcp/features/observations/tools/getObservationFilterValues.ts b/web/src/features/mcp/features/observations/tools/getObservationFilterValues.ts
index d378267cf4..f0852f9d84 100644
--- a/web/src/features/mcp/features/observations/tools/getObservationFilterValues.ts
+++ b/web/src/features/mcp/features/observations/tools/getObservationFilterValues.ts
@@ -34,6 +34,7 @@ const OBSERVATION_MCP_FILTER_VALUE_COLUMNS = [
"latency",
"timeToFirstToken",
"tags",
+ "isRootObservation",
"hasParentObservation",
] as const satisfies readonly ObservationMcpFilterColumn[];
@@ -46,6 +47,7 @@ const GetObservationFilterValuesBaseSchema = z.object({
fromStartTime: z.iso.datetime({ offset: true }).optional(),
toStartTime: z.iso.datetime({ offset: true }).optional(),
observationType: ObservationTypeDomain.optional(),
+ isRootObservation: z.boolean().optional(),
hasParentObservation: z.boolean().optional(),
limit: ObservationLimitSchema,
cursor: z.string().optional(),
@@ -89,8 +91,8 @@ const normalizeFilterOptions = (
): FilterOption[] => {
const normalizeValue = (value: unknown): string | boolean | null => {
if (typeof value === "string") {
- // Special handling for the "hasParentObservation" column to convert "true"/"false" strings to boolean values.
- if (column === "hasParentObservation") {
+ // ClickHouse returns boolean filter options as "true"/"false" strings.
+ if (column === "isRootObservation" || column === "hasParentObservation") {
if (value === "true") return true;
if (value === "false") return false;
}
@@ -185,6 +187,7 @@ export const [
column: eventsColumn,
projectId: context.projectId,
startTimeFilter,
+ isRootObservation: input.isRootObservation,
hasParentObservation: input.hasParentObservation,
observationType: input.observationType,
});
@@ -203,6 +206,7 @@ export const [
column: eventsColumn,
projectId: context.projectId,
startTimeFilter,
+ isRootObservation: input.isRootObservation,
hasParentObservation: input.hasParentObservation,
observationType: input.observationType,
limit: input.limit,
diff --git a/web/src/features/mcp/features/observations/tools/listObservations.ts b/web/src/features/mcp/features/observations/tools/listObservations.ts
index 7e795b4453..be7b70e4f5 100644
--- a/web/src/features/mcp/features/observations/tools/listObservations.ts
+++ b/web/src/features/mcp/features/observations/tools/listObservations.ts
@@ -218,7 +218,16 @@ const ListObservationsBaseSchema = z.object({
level: ObservationLevelDomain.optional(),
traceId: z.string().optional(),
version: z.string().optional(),
- parentObservationId: z.string().optional(),
+ parentObservationId: z
+ .string()
+ .optional()
+ .describe("Physical parent observation ID to match exactly."),
+ isRootObservation: z
+ .boolean()
+ .optional()
+ .describe(
+ "Filter by logical root status. App-root observations can match true while retaining a non-null parentObservationId.",
+ ),
// TODO: Re-enable string[] once the public observations API correctly
// applies allow-multiple environment filters instead of dropping arrays.
// see: https://linear.app/langfuse/issue/LFE-9852/bug-observations-api-accepts-multiple-environment-params-but-ignores
@@ -346,6 +355,7 @@ export const [listObservationsTool, handleListObservations] = defineTool({
type: input.type,
environment: input.environment,
parentObservationId: input.parentObservationId,
+ isRootObservation: input.isRootObservation,
fromStartTime: input.fromStartTime,
toStartTime: input.toStartTime,
version: input.version,