🪢 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): upgrade SDK to 1.32.0 and bound request validation (#19141)
Nimar2c80d4368ae7
diff --git a/packages/shared/src/constants.ts b/packages/shared/src/constants.tsindex 0260649914..59bd82cb34 100644--- a/packages/shared/src/constants.ts+++ b/packages/shared/src/constants.ts@@ -15,3 +15,6 @@ export enum ModelUsageUnit {* this long may be a prefix, so two of them being equal proves nothing.*/export const EXPERIMENT_IO_TRUNCATE_LENGTH = 1000;++/** Aggregate object members and array elements allowed in one MCP request body. */+export const MAX_MCP_REQUEST_ELEMENTS = 50_000;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex e11354b148..4143fb922d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -617,8 +617,8 @@ importers:specifier: ^1.2.3version: 1.2.3'@modelcontextprotocol/sdk':- specifier: 1.31.0+ specifier: 1.32.0'@next-auth/prisma-adapter':specifier: ^1.0.7version: 1.0.7(@prisma/[email protected]([email protected]([email protected])([email protected]))([email protected]))([email protected](patch_hash=298683aaa31566643f1899990a065b342ad777d8613f50a25ddc44dd46fb5b4d)([email protected](@babel/[email protected]([email protected]))(@opentelemetry/[email protected])(@playwright/[email protected])(@types/[email protected])([email protected]([email protected]))([email protected]))([email protected])([email protected]([email protected]))([email protected]))@@ -2955,8 +2955,8 @@ packages:react-dom:optional: true- '@modelcontextprotocol/[email protected]':- resolution: {integrity: sha512-UvTMgnNlnIBO/22ob2RcVGDlcvOslQs8T59+FTGdA0L27a39fdGF/EDETNtDVK4DZGpwomlsYpRdA8UXcVL/pw==}+ '@modelcontextprotocol/[email protected]':+ resolution: {integrity: sha512-8BviX/hK4Gd2eL1KdTwm0gfl4d3wdoErW0Jd00h6nomUAtk6IJk5vwQJc6kfWtJTzN5OEV/lgIiJKI8fiDGAlA==}engines: {node: '>=18'}peerDependencies:'@cfworker/json-schema': ^4.1.1@@ -14667,7 +14667,7 @@ snapshots:'@isaacs/ttlcache': 2.1.5'@lukeed/uuid': 2.0.1'@mastra/schema-compat': 1.2.10([email protected])- '@modelcontextprotocol/sdk': 1.31.0(@cfworker/[email protected])([email protected])([email protected])+ '@modelcontextprotocol/sdk': 1.32.0(@cfworker/[email protected])([email protected])([email protected])'@sindresorhus/slugify': 2.2.1'@standard-schema/spec': 1.1.0ajv: 8.20.0@@ -14716,7 +14716,7 @@ snapshots:'@isaacs/ttlcache': 2.1.5'@lukeed/uuid': 2.0.1'@mastra/schema-compat': 1.3.0([email protected])- '@modelcontextprotocol/sdk': 1.31.0(@cfworker/[email protected])([email protected])([email protected])+ '@modelcontextprotocol/sdk': 1.32.0(@cfworker/[email protected])([email protected])([email protected])'@sindresorhus/slugify': 2.2.1'@standard-schema/spec': 1.1.0ajv: 8.20.0@@ -14752,8 +14752,8 @@ snapshots:'@mastra/[email protected](@cfworker/[email protected])(@mastra/[email protected](@bufbuild/[email protected])(@cfworker/[email protected])(@grpc/[email protected])([email protected]([email protected]))([email protected]([email protected]))([email protected])([email protected])([email protected]))([email protected]([email protected]))([email protected])([email protected])([email protected])':dependencies:'@mastra/core': 1.46.0(@bufbuild/[email protected])(@cfworker/[email protected])(@grpc/[email protected])([email protected]([email protected]))([email protected]([email protected]))([email protected])([email protected])([email protected])- '@modelcontextprotocol/ext-apps': 1.7.2(@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected]))([email protected]([email protected]))([email protected])([email protected])- '@modelcontextprotocol/sdk': 1.31.0(@cfworker/[email protected])([email protected])([email protected])+ '@modelcontextprotocol/ext-apps': 1.7.2(@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected]))([email protected]([email protected]))([email protected])([email protected])+ '@modelcontextprotocol/sdk': 1.32.0(@cfworker/[email protected])([email protected])([email protected])exit-hook: 5.1.0fast-deep-equal: 3.1.3transitivePeerDependencies:@@ -14789,16 +14789,16 @@ snapshots:dependencies:'@chevrotain/types': 11.1.2- '@modelcontextprotocol/[email protected](@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected]))([email protected]([email protected]))([email protected])([email protected])':+ '@modelcontextprotocol/[email protected](@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected]))([email protected]([email protected]))([email protected])([email protected])':dependencies:- '@modelcontextprotocol/sdk': 1.31.0(@cfworker/[email protected])([email protected])([email protected])+ '@modelcontextprotocol/sdk': 1.32.0(@cfworker/[email protected])([email protected])([email protected])'@standard-schema/spec': 1.1.0zod: 4.6.6optionalDependencies:react: 19.2.4react-dom: 19.2.4([email protected])- '@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected])':+ '@modelcontextprotocol/[email protected](@cfworker/[email protected])([email protected])([email protected])':dependencies:'@hono/node-server': 1.19.17([email protected])ajv: 8.20.0diff --git a/web/package.json b/web/package.jsonindex 39047f6854..768a48c094 100644--- a/web/package.json+++ b/web/package.json@@ -61,7 +61,7 @@"@langfuse/icons": "workspace:*","@langfuse/shared": "workspace:*","@lezer/highlight": "^1.2.3",- "@modelcontextprotocol/sdk": "1.31.0",+ "@modelcontextprotocol/sdk": "1.32.0","@next-auth/prisma-adapter": "^1.0.7","@opentelemetry/api": "^1.9.1","@opentelemetry/exporter-trace-otlp-proto": "^0.220.0",diff --git a/web/src/__tests__/server/unit/mcp-security.servertest.ts b/web/src/__tests__/server/unit/mcp-security.servertest.tsindex 8bf7646839..d145cf2f47 100644--- a/web/src/__tests__/server/unit/mcp-security.servertest.ts+++ b/web/src/__tests__/server/unit/mcp-security.servertest.ts@@ -8,14 +8,19 @@ const mockEnv = vi.hoisted(() => ({vi.mock("@/src/env.mjs", () => mockEnv);-import type { NextApiRequest } from "next";+import type { NextApiRequest, NextApiResponse } from "next";+import { PayloadTooLargeError } from "@langfuse/shared";import { validateMcpRequestSecurity } from "@/src/features/mcp/server/security";-const mockRequest = (headers: NextApiRequest["headers"]): NextApiRequest =>- ({ headers }) as NextApiRequest;+const mockRequest = (+ headers: NextApiRequest["headers"],+ body?: unknown,+): NextApiRequest => ({ headers, body }) as NextApiRequest;+const mockResponse = { setHeader: vi.fn() } as unknown as NextApiResponse;describe("MCP request security", () => {beforeEach(() => {+ vi.clearAllMocks();mockEnv.env.NEXTAUTH_URL = "https://langfuse.example.com";mockEnv.env.NODE_ENV = "production";mockEnv.env.LANGFUSE_MCP_ALLOWED_HOSTS = [];@@ -30,6 +35,7 @@ describe("MCP request security", () => {host: "internal-langfuse.example.com",origin: "https://internal-langfuse.example.com",}),+ mockResponse,),).toBe("https://internal-langfuse.example.com");});@@ -41,7 +47,10 @@ describe("MCP request security", () => {];expect(() =>- validateMcpRequestSecurity(mockRequest({ host: "evil.example.com" })),+ validateMcpRequestSecurity(+ mockRequest({ host: "evil.example.com" }),+ mockResponse,+ ),).toThrow("Invalid Host header: evil.example.com");});@@ -54,7 +63,138 @@ describe("MCP request security", () => {host: "any-host.example.com",origin: "https://any-origin.example.com",}),+ mockResponse,),).toBe("https://any-origin.example.com");});++ it.each([+ [+ "unknown object members",+ () =>+ Object.fromEntries(Array.from({ length: 50_001 }, (_, i) => [i, null])),+ ],+ [+ "nested array elements",+ () => ({ params: { arguments: [Array(50_000).fill(null)] } }),+ ],+ [+ "metadata aggregated across a batch",+ () => [+ {+ jsonrpc: "2.0",+ id: 1,+ method: "tools/list",+ params: { _meta: Array(25_000).fill(null) },+ },+ {+ jsonrpc: "2.0",+ id: 2,+ method: "tools/list",+ params: { _meta: Array(25_000).fill(null) },+ },+ ],+ ],+ [+ "deeply nested input",+ () => {+ let body: unknown = null;+ for (let i = 0; i < 50_001; i++) body = [body];+ return body;+ },+ ],+ ])("rejects excessive %s even with wildcard hosts", (_, createBody) => {+ mockEnv.env.LANGFUSE_MCP_ALLOWED_HOSTS = ["*"];++ expect(() =>+ validateMcpRequestSecurity(+ mockRequest({ host: "langfuse.example.com" }, createBody()),+ mockResponse,+ ),+ ).toThrow(PayloadTooLargeError);+ });++ it("allows the element budget boundary and large prompt strings", () => {+ const body = {+ jsonrpc: "2.0",+ id: 1,+ method: "tools/call",+ params: {+ name: "prompts.create",+ arguments: { prompt: "x".repeat(4 * 1024 * 1024) },+ },+ };++ expect(+ validateMcpRequestSecurity(+ mockRequest({ host: "langfuse.example.com" }, body),+ mockResponse,+ ),+ ).toBeNull();+ expect(+ validateMcpRequestSecurity(+ mockRequest({ host: "langfuse.example.com" }, Array(50_000).fill(null)),+ mockResponse,+ ),+ ).toBeNull();+ expect(() =>+ validateMcpRequestSecurity(+ mockRequest({ host: "langfuse.example.com" }, Array(50_001).fill(null)),+ mockResponse,+ ),+ ).toThrow(PayloadTooLargeError);+ });++ it("allows a full dataset batch with structured chat inputs", () => {+ const body = {+ jsonrpc: "2.0",+ id: 1,+ method: "tools/call",+ params: {+ name: "batchUpsertDatasetItems",+ arguments: {+ datasetId: "test-dataset",+ items: Array.from({ length: 100 }, () => ({+ input: Array.from({ length: 100 }, () => ({+ role: "user",+ content: "test message",+ })),+ expectedOutput: { role: "assistant", content: "test answer" },+ metadata: { source: "chat", tags: ["test"] },+ })),+ },+ },+ };++ expect(+ validateMcpRequestSecurity(+ mockRequest({ host: "langfuse.example.com" }, body),+ mockResponse,+ ),+ ).toBeNull();+ });++ it("keeps CORS headers and recovery guidance on body-limit errors", () => {+ expect(() =>+ validateMcpRequestSecurity(+ mockRequest(+ {+ host: "langfuse.example.com",+ origin: "https://langfuse.example.com",+ },+ Array(50_001).fill(null),+ ),+ mockResponse,+ ),+ ).toThrow(+ expect.objectContaining({+ httpCode: 422,+ message: expect.stringContaining("Split batches into smaller requests"),+ }) as Error,+ );+ expect(mockResponse.setHeader).toHaveBeenCalledWith(+ "Access-Control-Allow-Origin",+ "https://langfuse.example.com",+ );+ });});diff --git a/web/src/features/mcp/README.md b/web/src/features/mcp/README.mdindex 53b80de3a5..02b4c3a612 100644--- a/web/src/features/mcp/README.md+++ b/web/src/features/mcp/README.md@@ -51,6 +51,8 @@ A complete list of tools can be seen under [mcp.reference.langfuse.com](https://## Architecture+MCP requests are limited to 4.5 MB and 50,000 object members/array elements combined.+### Stateless DesignThe Langfuse MCP server uses a **stateless per-request architecture**:diff --git a/web/src/features/mcp/server/datasets/tools/batchUpsertDatasetItems.ts b/web/src/features/mcp/server/datasets/tools/batchUpsertDatasetItems.tsindex a876d24e02..c6ee312bfc 100644--- a/web/src/features/mcp/server/datasets/tools/batchUpsertDatasetItems.ts+++ b/web/src/features/mcp/server/datasets/tools/batchUpsertDatasetItems.ts@@ -9,7 +9,7 @@ export const [batchUpsertDatasetItemsTool, handleBatchUpsertDatasetItems] =defineTool({name: "batchUpsertDatasetItems",description:- "Upsert multiple dataset items in one dataset. Items are processed in order. The operation stops on the first failure and is not atomic.",+ "Upsert up to 100 dataset items in one dataset. Items are processed in order. The operation stops on the first failure and is not atomic.",action: "datasets:CUD",baseSchema: BatchUpsertDatasetItemsMcpInput,inputSchema: BatchUpsertDatasetItemsMcpInput,diff --git a/web/src/features/mcp/server/security.ts b/web/src/features/mcp/server/security.tsindex 05852e7ff5..26d307043c 100644--- a/web/src/features/mcp/server/security.ts+++ b/web/src/features/mcp/server/security.ts@@ -1,10 +1,25 @@import { env } from "@/src/env.mjs";import { getBaseUrl } from "@/src/utils/base-url";-import { ForbiddenError } from "@langfuse/shared";+import {+ ForbiddenError,+ MAX_MCP_REQUEST_ELEMENTS,+ PayloadTooLargeError,+} from "@langfuse/shared";import { type NextApiRequest, type NextApiResponse } from "next";const LOCALHOST_HOSTNAMES = ["localhost", "127.0.0.1", "[::1]"] as const;+/** Validates MCP requests and preserves CORS headers on allowed-origin body errors. */+export function validateMcpRequestSecurity(+ req: NextApiRequest,+ res: NextApiResponse,+): string | null {+ const allowedOrigin = validateMcpRequestOrigin(req);+ applyMcpCorsHeaders(res, allowedOrigin);+ validateMcpRequestBody(req.body);+ return allowedOrigin;+}+function parseAllowedMcpHostEntry(entry: string,fallbackProtocol: string,@@ -67,7 +82,7 @@ function getAllowedMcpOriginsAndHostnames() {return { allowedHostnames, allowedOrigins };}-export function validateMcpRequestSecurity(req: NextApiRequest): string | null {+function validateMcpRequestOrigin(req: NextApiRequest): string | null {const originHeader = Array.isArray(req.headers.origin)? req.headers.origin[0]: req.headers.origin;@@ -115,7 +130,7 @@ export function validateMcpRequestSecurity(req: NextApiRequest): string | null {return origin;}-export function applyMcpCorsHeaders(+function applyMcpCorsHeaders(res: NextApiResponse,allowedOrigin: string | null,): void {@@ -131,3 +146,28 @@ export function applyMcpCorsHeaders();res.setHeader("Access-Control-Expose-Headers", "Mcp-Session-Id");}++/** Bounds structure before SDK schema validation; the route separately caps bytes. */+function validateMcpRequestBody(body: unknown): void {+ const pending: unknown[] = [body];+ let elements = 0;++ while (pending.length > 0) {+ const value = pending.pop();+ if (value === null || typeof value !== "object") continue;++ for (const key in value) {+ if (!Object.hasOwn(value, key)) continue;+ if (++elements > MAX_MCP_REQUEST_ELEMENTS) {+ throw new PayloadTooLargeError(+ `MCP request exceeds ${MAX_MCP_REQUEST_ELEMENTS} object members and array elements across the entire request. ` ++ "No tools in this request were executed. Split batches into smaller requests before retrying. " ++ "If a single item cannot fit, ask the user how to proceed. Do not truncate data or retry the same request unchanged.",+ );+ }++ const child = (value as Record<string, unknown>)[key];+ if (child !== null && typeof child === "object") pending.push(child);+ }+ }+}diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.tsindex 8c5b7248b1..56c7a4815f 100644--- a/web/src/pages/api/public/mcp/index.ts+++ b/web/src/pages/api/public/mcp/index.ts@@ -27,10 +27,7 @@import { type NextApiRequest, type NextApiResponse } from "next";import { createMcpServer } from "@/src/features/mcp/server/mcpServer";import { handleMcpRequest } from "@/src/features/mcp/server/transport";-import {- applyMcpCorsHeaders,- validateMcpRequestSecurity,-} from "@/src/features/mcp/server/security";+import { validateMcpRequestSecurity } from "@/src/features/mcp/server/security";import { formatErrorForUser } from "@/src/features/mcp/core/error-formatting";import { type ServerContext } from "@/src/features/mcp/types";import { addUserToSpan, logger } from "@langfuse/shared/src/server";@@ -71,8 +68,7 @@ export default async function handler(res: NextApiResponse,) {try {- const allowedOrigin = validateMcpRequestSecurity(req);- applyMcpCorsHeaders(res, allowedOrigin);+ validateMcpRequestSecurity(req, res);// Handle preflight OPTIONS request after request validationif (req.method === "OPTIONS") {