Skip to content

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

feat(mcp): Add canonical MCP tool outcome telemetry to DD (#15620)

Lysander Kieselc1b83fc92d67
diff --git a/web/src/__tests__/server/unit/run-mcp-tool.servertest.ts b/web/src/__tests__/server/unit/run-mcp-tool.servertest.ts
new file mode 100644
index 0000000000..251c5047b1
--- /dev/null
+++ b/web/src/__tests__/server/unit/run-mcp-tool.servertest.ts
@@ -0,0 +1,84 @@
+import type { Span } from "@opentelemetry/api";
+import { InvalidRequestError } from "@langfuse/shared";
+import { z } from "zod";
+import type { ServerContext } from "@/src/features/mcp/types";
+
+const { addUserToSpanMock, fakeSpan, instrumentAsyncMock } = vi.hoisted(() => {
+ const fakeSpan = {
+ setAttribute: vi.fn(),
+ setAttributes: vi.fn(),
+ };
+
+ return {
+ addUserToSpanMock: vi.fn(),
+ fakeSpan,
+ instrumentAsyncMock: vi.fn(async (_options, callback) =>
+ callback(fakeSpan),
+ ),
+ };
+});
+
+vi.mock("@langfuse/shared/src/server", async (importOriginal) => ({
+ ...(await importOriginal()),
+ addUserToSpan: addUserToSpanMock,
+ instrumentAsync: instrumentAsyncMock,
+}));
+
+import { runMcpTool } from "@/src/features/mcp/core/run-mcp-tool";
+
+const context: ServerContext = {
+ projectId: "project-id",
+ orgId: "org-id",
+ apiKeyId: "api-key-id",
+ accessLevel: "project",
+ publicKey: "pk-lf-test",
+ plan: "oss",
+ rateLimitOverrides: [],
+};
+
+describe("runMcpTool outcome telemetry", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it.each([
+ {
+ name: "successful execution",
+ execute: async () => "ok",
+ expectedOutcome: "success",
+ },
+ {
+ name: "actionable request error",
+ execute: async () => {
+ throw new InvalidRequestError("invalid input");
+ },
+ expectedOutcome: "request_error",
+ },
+ {
+ name: "in-handler schema validation error",
+ execute: async () => z.string().parse(42),
+ expectedOutcome: "request_error",
+ },
+ {
+ name: "unexpected server error",
+ execute: async () => {
+ throw new Error("database unavailable");
+ },
+ expectedOutcome: "server_error",
+ },
+ ])(
+ "records a bounded outcome for $name",
+ async ({ execute, expectedOutcome }) => {
+ await runMcpTool({
+ spanName: "mcp.test",
+ context,
+ fn: execute as (span: Span) => Promise<string>,
+ }).catch(() => undefined);
+
+ expect(fakeSpan.setAttribute).toHaveBeenCalledWith(
+ "mcp.outcome",
+ expectedOutcome,
+ );
+ },
+ );
+});
diff --git a/web/src/features/mcp/core/run-mcp-tool.ts b/web/src/features/mcp/core/run-mcp-tool.ts
index f0403c19fb..d14d4d7c52 100644
--- a/web/src/features/mcp/core/run-mcp-tool.ts
+++ b/web/src/features/mcp/core/run-mcp-tool.ts
@@ -1,9 +1,11 @@
import { BaseError } from "@langfuse/shared";
import { addUserToSpan, instrumentAsync } from "@langfuse/shared/src/server";
import { SpanKind, type Span } from "@opentelemetry/api";
+import { ZodError } from "zod";
import { UnstablePublicApiError } from "@/src/features/public-api/server/unstable-public-api-error-contract";
+import { isUserInputError } from "./errors";
import type { ServerContext } from "../types";
type McpToolAttribute = string | number | boolean;
@@ -42,8 +44,23 @@ export const runMcpTool = async <TResult>({
});
try {
- return await fn(span);
+ const result = await fn(span);
+ span.setAttribute("mcp.outcome", "success");
+ return result;
} catch (error) {
+ // Keep telemetry aligned with formatErrorForUser, which reports Zod
+ // validation failures as InvalidParams. Response validators should
+ // wrap schema-drift failures in a server-side BaseError.
+ const isRequestError =
+ error instanceof ZodError ||
+ isUserInputError(error) ||
+ (error instanceof BaseError && error.httpCode < 500);
+
+ span.setAttribute(
+ "mcp.outcome",
+ isRequestError ? "request_error" : "server_error",
+ );
+
// Expose the error cause on the span so trace analyses can group by
// cause instead of a single error class name.
if (error instanceof BaseError) {