Skip to content

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

fix(mcp): Bring back rate limits for in-app agent (#15948)

Lysander Kiesel14ac3a06b53d
diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.ts
index f85a3f7ad7..be2c341707 100644
--- a/packages/shared/src/server/auth/apiKeys.ts
+++ b/packages/shared/src/server/auth/apiKeys.ts
@@ -45,7 +45,6 @@ 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.ts
index f667988472..716c29f607 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,27 +3,10 @@ import {
createInAppAgentMcpRunOverride,
InAppAgentMcpRunOverrideSchema,
} from "@langfuse/shared/in-app-agent/server/human-in-the-loop";
-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 { getInAppAgentContext } from "@/src/pages/api/public/mcp";
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>({
@@ -81,51 +64,3 @@ 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.md
index 48bc299471..252153677d 100644
--- a/web/src/features/in-app-agent/README.md
+++ b/web/src/features/in-app-agent/README.md
@@ -216,8 +216,6 @@ 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.ts
index d635357983..1ea3c8d826 100644
--- a/web/src/pages/api/public/mcp/index.ts
+++ b/web/src/pages/api/public/mcp/index.ts
@@ -33,13 +33,7 @@ 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 {
- addTagsToCurrentSpan,
- addUserToSpan,
- logger,
- redis,
- type ApiAccessScope,
-} from "@langfuse/shared/src/server";
+import { addUserToSpan, logger, redis } 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";
@@ -126,8 +120,15 @@ export default async function handler(
);
}
- if (await applyMcpPublicApiRateLimit(authCheck.scope, res)) {
- return;
+ // Rate limit MCP requests
+ const rateLimitCheck =
+ await RateLimitService.getInstance().rateLimitRequest(
+ authCheck.scope,
+ "public-api",
+ );
+
+ if (rateLimitCheck?.isRateLimited()) {
+ return rateLimitCheck.sendRestResponseIfLimited(res);
}
// Build ServerContext from authenticated scope. In-app-agent keys need a
@@ -189,34 +190,6 @@ 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,