🪢 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): exempt in-app agent from public API rate limits (#15880)
Lysander Kieseldfcc663b7f94
diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.tsindex be2c341707..f85a3f7ad7 100644--- a/packages/shared/src/server/auth/apiKeys.ts+++ b/packages/shared/src/server/auth/apiKeys.ts@@ -45,6 +45,7 @@ export async function createAndAddApiKeysToDb(p: {entityId: string;scope: ApiKeyScope;note?: string;+ // Internal-only API keys may set this because they bypass Public API rate limits.isInAppAgentKey?: boolean;/** User who created the key, e.g. via the UI. */createdByUserId?: string;diff --git a/web/src/__tests__/server/unit/mcp-route-in-app-agent-context.servertest.ts b/web/src/__tests__/server/unit/mcp-route-in-app-agent-context.servertest.tsindex 716c29f607..f667988472 100644--- a/web/src/__tests__/server/unit/mcp-route-in-app-agent-context.servertest.ts+++ b/web/src/__tests__/server/unit/mcp-route-in-app-agent-context.servertest.ts@@ -3,10 +3,27 @@ import {createInAppAgentMcpRunOverride,InAppAgentMcpRunOverrideSchema,} from "@langfuse/shared/in-app-agent/server/human-in-the-loop";-import { getInAppAgentContext } from "@/src/pages/api/public/mcp";+import {+ applyMcpPublicApiRateLimit,+ getInAppAgentContext,+} from "@/src/pages/api/public/mcp";+import { RateLimitService } from "@/src/features/public-api/server/RateLimitService";+import type { ApiAccessScope } from "@langfuse/shared/src/server";import type { NextApiRequest, NextApiResponse } from "next";import { createMocks } from "node-mocks-http";+const createApiAccessScope = (isInAppAgentKey: boolean): ApiAccessScope => ({+ projectId: "project-id",+ orgId: "org-id",+ apiKeyId: "api-key-id",+ publicKey: "pk-lf-test",+ accessLevel: "project",+ plan: "oss",+ rateLimitOverrides: [],+ isIngestionSuspended: false,+ isInAppAgentKey,+});+describe("MCP route in-app-agent context", () => {const createRequest = (overrideHeader?: string) => {const { req } = createMocks<NextApiRequest, NextApiResponse>({@@ -64,3 +81,51 @@ describe("MCP route in-app-agent context", () => {});});});++describe("MCP route public API rate limiting", () => {+ const rateLimitRequest = vi.fn();++ beforeEach(() => {+ vi.restoreAllMocks();+ rateLimitRequest.mockReset();+ vi.spyOn(RateLimitService, "getInstance").mockReturnValue({+ rateLimitRequest,+ } as unknown as RateLimitService);+ });++ it("exempts authenticated in-app-agent keys", async () => {+ const { res } = createMocks<NextApiRequest, NextApiResponse>();++ const responseSent = await applyMcpPublicApiRateLimit(+ createApiAccessScope(true),+ res,+ );++ expect(responseSent).toBe(false);+ expect(rateLimitRequest).not.toHaveBeenCalled();+ });++ it("rate limits normal keys based only on authenticated scope", async () => {+ const { res } = createMocks<NextApiRequest, NextApiResponse>();+ const sendRestResponseIfLimited = vi.fn((response: NextApiResponse) =>+ response.status(429).end(),+ );+ rateLimitRequest.mockResolvedValue({+ isRateLimited: () => true,+ sendRestResponseIfLimited,+ });++ const responseSent = await applyMcpPublicApiRateLimit(+ createApiAccessScope(false),+ res,+ );++ expect(responseSent).toBe(true);+ expect(rateLimitRequest).toHaveBeenCalledWith(+ createApiAccessScope(false),+ "public-api",+ );+ expect(sendRestResponseIfLimited).toHaveBeenCalledWith(res);+ expect(res.statusCode).toBe(429);+ });+});diff --git a/web/src/features/in-app-agent/README.md b/web/src/features/in-app-agent/README.mdindex 252153677d..48bc299471 100644--- a/web/src/features/in-app-agent/README.md+++ b/web/src/features/in-app-agent/README.md@@ -216,6 +216,8 @@ The supported worker path uses two run-scoped inputs when calling Langfuse MCP:The API key authenticates the request and scopes it to the project. Without an override, in-app-agent keys are restricted to MCP tools annotated with `readOnlyHint: true`. When the user approves a single Langfuse MCP tool call, the worker continuation creates a JSON override naming that one unprefixed MCP registry tool and passes it to the MCP route through the request header above. The retained POST adapter creates equivalent inputs inside `server/handler.ts`.+Foreground and background runs use the same HTTP MCP route because the background runtime executes in `worker` while the MCP registry and its domain handlers are owned by `web`. Requests authenticated with a temporary in-app-agent key are exempt from the route's customer-facing `public-api` rate-limit bucket; the assistant's run and concurrency limits are enforced before the MCP client starts. The exemption trusts only the authenticated API-key classification, never the user-agent or override header. Direct in-process execution is deferred because it would require moving the web-owned MCP handler dependencies into code shared with the worker.+MCP registry behavior:- Normal project API keys can call all enabled MCP tools.diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.tsindex 1ea3c8d826..d635357983 100644--- a/web/src/pages/api/public/mcp/index.ts+++ b/web/src/pages/api/public/mcp/index.ts@@ -33,7 +33,13 @@ import {} 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, redis } from "@langfuse/shared/src/server";+import {+ addTagsToCurrentSpan,+ addUserToSpan,+ logger,+ redis,+ type ApiAccessScope,+} from "@langfuse/shared/src/server";import { ApiAuthService } from "@/src/features/public-api/server/apiAuth";import { RateLimitService } from "@/src/features/public-api/server/RateLimitService";import { prisma } from "@langfuse/shared/src/db";@@ -120,15 +126,8 @@ export default async function handler();}- // Rate limit MCP requests- const rateLimitCheck =- await RateLimitService.getInstance().rateLimitRequest(- authCheck.scope,- "public-api",- );-- if (rateLimitCheck?.isRateLimited()) {- return rateLimitCheck.sendRestResponseIfLimited(res);+ if (await applyMcpPublicApiRateLimit(authCheck.scope, res)) {+ return;}// Build ServerContext from authenticated scope. In-app-agent keys need a@@ -190,6 +189,34 @@ export default async function handler(}}+export async function applyMcpPublicApiRateLimit(+ scope: ApiAccessScope,+ res: NextApiResponse,+): Promise<boolean> {+ const isInAppAgent = scope.isInAppAgentKey === true;++ addTagsToCurrentSpan({+ "mcp.is_in_app_agent": isInAppAgent,+ });++ // Assistant runs have separate limits; trust only the authenticated key flag for this exemption.+ if (isInAppAgent) {+ return false;+ }++ const rateLimitCheck = await RateLimitService.getInstance().rateLimitRequest(+ scope,+ "public-api",+ );++ if (!rateLimitCheck?.isRateLimited()) {+ return false;+ }++ rateLimitCheck.sendRestResponseIfLimited(res);+ return true;+}+export function getInAppAgentContext(req: NextApiRequest,isInAppAgentKey: boolean | undefined,