Skip to content

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

fix(mcp): Always require date range in get observations tool (#18485)

Ben Bachem0da6dea8774f
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index ad3a6acc11..e97b0a4f51 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -936,7 +936,7 @@ describe("MCP Read Tools", () => {
expect(result.fields.input.sensitive).toBe(true);
expect(result.fields.input.requiresScope).toBe(true);
expect(result.fields.input.scopeRequirement).toMatch(
- /traceId.*id filter.*fromStartTime.*toStartTime/i,
+ /requires both fromStartTime and toStartTime.*maximum range of 14 days.*even when scoped by traceId or observation id/i,
);
expect(result.fields.output.requiresScope).toBe(true);
expect(result.fields.metadata.expensive).toBe(true);
@@ -1147,15 +1147,18 @@ describe("MCP Read Tools", () => {
it("should filter by metadata advanced filters", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();
+ const startTime = new Date();
const matchingObservation = createObservationEvent({
projectId,
traceId,
+ startTime,
name: `mcp-filter-metadata-match-${nanoid()}`,
metadata: { region: "us-east", tenant: "acme" },
});
const nonMatchingObservation = createObservationEvent({
projectId,
traceId,
+ startTime,
name: `mcp-filter-metadata-miss-${nanoid()}`,
metadata: { region: "eu-west", tenant: "acme" },
});
@@ -1164,6 +1167,8 @@ describe("MCP Read Tools", () => {
const result = (await handleListObservations(
{
+ fromStartTime: new Date(startTime.getTime() - 1000).toISOString(),
+ toStartTime: new Date(startTime.getTime() + 1000).toISOString(),
filter: [
{
type: "stringOptions",
@@ -1306,9 +1311,11 @@ describe("MCP Read Tools", () => {
it("should include payload fields when requested with wildcard projection", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();
+ const startTime = new Date();
const observation = createObservationEvent({
projectId,
traceId,
+ startTime,
name: `mcp-list-wildcard-${nanoid()}`,
input: "visible input",
output: "visible output",
@@ -1318,7 +1325,13 @@ describe("MCP Read Tools", () => {
await createEventsCh([observation]);
const result = (await handleListObservations(
- { traceId, fields: ["*"], limit: 100 },
+ {
+ traceId,
+ fields: ["*"],
+ fromStartTime: new Date(startTime.getTime() - 1000).toISOString(),
+ toStartTime: new Date(startTime.getTime() + 1000).toISOString(),
+ limit: 50,
+ },
context,
)) as { data: Array<Record<string, unknown>> };
@@ -1444,10 +1457,12 @@ describe("MCP Read Tools", () => {
it("should match advanced input filters beyond the events_core truncation boundary", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();
+ const startTime = new Date();
const needle = `needle-${nanoid()}`;
const matchingObservation = createObservationEvent({
projectId,
traceId,
+ startTime,
input: `${"x".repeat(250)} ${needle}`,
});
@@ -1471,6 +1486,8 @@ describe("MCP Read Tools", () => {
value: needle,
},
],
+ fromStartTime: new Date(startTime.getTime() - 1000).toISOString(),
+ toStartTime: new Date(startTime.getTime() + 1000).toISOString(),
fields: ["id"],
limit: 100,
},
@@ -1489,14 +1506,12 @@ describe("MCP Read Tools", () => {
]);
});
- it("should require selective scope for full io and metadata access", async () => {
+ it("should require a time range for full io and metadata access", async () => {
const { context } = await createMcpTestSetup();
await expect(
handleListObservations({ fields: ["input"], limit: 100 }, context),
- ).rejects.toThrow(
- /requires traceId, an id filter, or both fromStartTime and toStartTime/i,
- );
+ ).rejects.toThrow(/requires both fromStartTime and toStartTime/i);
await expect(
handleListObservations(
@@ -1514,9 +1529,7 @@ describe("MCP Read Tools", () => {
},
context,
),
- ).rejects.toThrow(
- /requires traceId, an id filter, or both fromStartTime and toStartTime/i,
- );
+ ).rejects.toThrow(/requires both fromStartTime and toStartTime/i);
await expect(
handleListObservations(
@@ -1534,7 +1547,14 @@ describe("MCP Read Tools", () => {
},
context,
),
- ).resolves.toMatchObject({ data: [] });
+ ).rejects.toThrow(/requires both fromStartTime and toStartTime/i);
+
+ await expect(
+ handleListObservations(
+ { traceId: randomUUID(), fields: ["metadata"], limit: 50 },
+ context,
+ ),
+ ).rejects.toThrow(/requires both fromStartTime and toStartTime/i);
await expect(
handleListObservations(
@@ -1609,10 +1629,48 @@ describe("MCP Read Tools", () => {
).resolves.toMatchObject({ data: [] });
});
- it("should treat an exact observation id filter as selective scope", async () => {
+ it.each([
+ { traceId: randomUUID() },
+ {
+ filter: [
+ {
+ type: "string" as const,
+ column: "id",
+ operator: "=" as const,
+ value: randomUUID(),
+ },
+ ],
+ },
+ ])("should enforce the time range for scoped access: %j", async (scope) => {
+ const { context } = await createMcpTestSetup();
+
+ await expect(
+ handleListObservations(
+ { ...scope, fields: ["input"], limit: 50 },
+ context,
+ ),
+ ).rejects.toThrow(/requires both fromStartTime and toStartTime/i);
+
+ await expect(
+ handleListObservations(
+ {
+ ...scope,
+ fields: ["metadata"],
+ fromStartTime: "2026-01-01T00:00:00.000Z",
+ toStartTime: "2026-01-16T00:00:00.000Z",
+ limit: 50,
+ },
+ context,
+ ),
+ ).rejects.toThrow(/maximum range of 14 days/i);
+ });
+
+ it("should allow an exact observation id filter with a time range", async () => {
const { context, projectId } = await createMcpTestSetup();
+ const startTime = new Date();
const observation = createObservationEvent({
projectId,
+ startTime,
input: "selective input",
});
@@ -1621,6 +1679,8 @@ describe("MCP Read Tools", () => {
const result = (await handleListObservations(
{
fields: ["id", "input"],
+ fromStartTime: new Date(startTime.getTime() - 1000).toISOString(),
+ toStartTime: new Date(startTime.getTime() + 1000).toISOString(),
filter: [
{
type: "string",
@@ -1629,7 +1689,7 @@ describe("MCP Read Tools", () => {
value: observation.id,
},
],
- limit: 100,
+ limit: 50,
},
context,
)) as { data: Array<{ id: string; input: string }> };
diff --git a/web/src/features/mcp/server/observations/schema.ts b/web/src/features/mcp/server/observations/schema.ts
index 014ad86319..87d5d0b674 100644
--- a/web/src/features/mcp/server/observations/schema.ts
+++ b/web/src/features/mcp/server/observations/schema.ts
@@ -36,7 +36,7 @@ type ObservationMcpFieldType =
| "unknown";
const EXPENSIVE_FIELD_SCOPE_REQUIREMENT =
- "Requires traceId, an exact id filter, or both fromStartTime and toStartTime with a maximum range of 14 days.";
+ "Requires both fromStartTime and toStartTime with a maximum range of 14 days, even when scoped by traceId or observation id.";
const OBSERVATION_MCP_FIELDS = OBSERVATION_FIELD_GROUPS_PUBLIC_API.flatMap(
(group) => OBSERVATION_FIELD_GROUP_FIELD_NAMES[group],
diff --git a/web/src/features/mcp/server/observations/tools/listObservations.ts b/web/src/features/mcp/server/observations/tools/listObservations.ts
index 160d855bd5..f56e6e865e 100644
--- a/web/src/features/mcp/server/observations/tools/listObservations.ts
+++ b/web/src/features/mcp/server/observations/tools/listObservations.ts
@@ -314,15 +314,6 @@ const ListObservationsInputSchema = ListObservationsBaseSchema.extend({
type ListObservationsInput = z.infer<typeof ListObservationsInputSchema>;
-const hasObservationIdFilter = (filters: ListObservationsInput["filter"]) =>
- filters?.some(
- (filter) =>
- filter.column === "id" &&
- filter.type === "stringOptions" &&
- filter.operator === "any of" &&
- filter.value.length > 0,
- ) ?? false;
-
// Initially, we return io/metadata in full only if some limits are set
const assertAllowedExpensiveObservationAccess = (
input: ListObservationsInput,
@@ -344,15 +335,13 @@ const assertAllowedExpensiveObservationAccess = (
if (expensiveColumns.size === 0) return;
- if (input.traceId || hasObservationIdFilter(input.filter)) return;
-
if (!input.fromStartTime || !input.toStartTime) {
throw new InvalidRequestError(
`Accessing observation ${Array.from(expensiveColumns)
.sort()
.join(
", ",
- )} requires traceId, an id filter, or both fromStartTime and toStartTime with a maximum range of 14 days.`,
+ )} requires both fromStartTime and toStartTime with a maximum range of 14 days.`,
);
}
@@ -381,11 +370,11 @@ export const [listObservationsTool, handleListObservations] = defineTool({
"Find and review observations in the current Langfuse project, such as generations, spans, events, agent steps, and tool calls.",
"Traces consist of observations. Use this tool when the user asks to inspect traces: pass traceId to page through the observations for a specific trace; those observation records are the trace data returned by the API.",
"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 inspect metadata on selectively scoped observations by passing traceId, an exact id filter, or both fromStartTime and toStartTime with fields: ["id", "metadata"]. Then use a discovered key in a stringObject filter.',
+ 'For metadata filters, first inspect metadata by passing both fromStartTime and toStartTime with a maximum range of 14 days and fields: ["id", "metadata"]. Then use a discovered key in a stringObject 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.',
- "Requests that project or filter input, output, or metadata must include traceId, an id filter, or a date range of at most 14 days. Date-scoped input/output projections support a maximum limit of 50.",
+ "Requests that project or filter input, output, or metadata must include both fromStartTime and toStartTime with a date range of at most 14 days, even when scoped by traceId or observation id. Input/output projections support a maximum limit of 50.",
].join("\n"),
action: "traces:read",
baseSchema: ListObservationsBaseSchema,