Skip to content

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

fix(in-app-agent): refuse to delete user project keys during MCP cleanup (#16393)

Lysander Kieselad2710c3cd29
diff --git a/packages/shared/src/server/auth/apiKeys.ts b/packages/shared/src/server/auth/apiKeys.ts
index be2c341707..10eecd6a92 100644
--- a/packages/shared/src/server/auth/apiKeys.ts
+++ b/packages/shared/src/server/auth/apiKeys.ts
@@ -4,6 +4,7 @@ import { randomUUID } from "crypto";
import * as crypto from "crypto";
import type { Cluster, Redis } from "ioredis";
import { env } from "../../env";
+import { logger } from "../logger";
import { invalidateCachedApiKeys } from "./invalidateApiKeys";
export function getDisplaySecretKey(secretKey: string) {
@@ -103,6 +104,11 @@ export async function deleteApiKeyFromDb(p: {
entityId: string;
scope: ApiKeyScope;
redis?: Redis | Cluster | null;
+ /**
+ * When true, only delete keys minted for in-app agent MCP sessions.
+ * A matching project key that is not an agent key is left intact.
+ */
+ isInAppAgentKey?: boolean;
}) {
const entity =
p.scope === "PROJECT" ? { projectId: p.entityId } : { orgId: p.entityId };
@@ -115,6 +121,18 @@ export async function deleteApiKeyFromDb(p: {
},
});
+ if (p.isInAppAgentKey === true && apiKey.isInAppAgentKey !== true) {
+ logger.warn(
+ "Refusing to delete API key that is not an in-app agent MCP key",
+ {
+ apiKeyId: p.id,
+ entityId: p.entityId,
+ scope: p.scope,
+ },
+ );
+ return false;
+ }
+
await invalidateCachedApiKeys([apiKey], `key ${p.id}`, p.redis);
await p.prisma.apiKey.delete({
@@ -125,3 +143,20 @@ export async function deleteApiKeyFromDb(p: {
return true;
}
+
+/** Delete an in-app agent MCP session key. Skips user project keys. */
+export async function deleteInAppAgentMcpApiKeyFromDb(p: {
+ prisma: PrismaClient;
+ id: string;
+ projectId: string;
+ redis?: Redis | Cluster | null;
+}) {
+ return deleteApiKeyFromDb({
+ prisma: p.prisma,
+ id: p.id,
+ entityId: p.projectId,
+ scope: "PROJECT",
+ redis: p.redis,
+ isInAppAgentKey: true,
+ });
+}
diff --git a/web/src/features/in-app-agent/server/backgroundRunService.ts b/web/src/features/in-app-agent/server/backgroundRunService.ts
index 9289a0a887..75e1ce7190 100644
--- a/web/src/features/in-app-agent/server/backgroundRunService.ts
+++ b/web/src/features/in-app-agent/server/backgroundRunService.ts
@@ -9,7 +9,7 @@ import {
QueueJobs,
redis,
} from "@langfuse/shared/src/server";
-import { deleteApiKeyFromDb } from "@langfuse/shared/src/server/auth/apiKeys";
+import { deleteInAppAgentMcpApiKeyFromDb } from "@langfuse/shared/src/server/auth/apiKeys";
import {
InAppAgentRunErrorCode,
InAppAgentRunStatus,
@@ -65,11 +65,10 @@ export async function getBackgroundConversationSnapshot(params: {
projectId: params.projectId,
conversationId: params.conversationId,
deleteApiKey: async (apiKeyId) => {
- await deleteApiKeyFromDb({
+ await deleteInAppAgentMcpApiKeyFromDb({
prisma: params.prisma,
id: apiKeyId,
- entityId: params.projectId,
- scope: "PROJECT",
+ projectId: params.projectId,
redis,
});
},
diff --git a/worker/src/features/in-app-agent-integrity-runner/index.test.ts b/worker/src/features/in-app-agent-integrity-runner/index.test.ts
index e552a717ac..325f0de4f4 100644
--- a/worker/src/features/in-app-agent-integrity-runner/index.test.ts
+++ b/worker/src/features/in-app-agent-integrity-runner/index.test.ts
@@ -23,7 +23,7 @@ vi.mock("@langfuse/shared/src/server", async (importOriginal) => ({
}));
vi.mock("@langfuse/shared/src/server/auth/apiKeys", () => ({
- deleteApiKeyFromDb: vi.fn(),
+ deleteInAppAgentMcpApiKeyFromDb: vi.fn(),
}));
vi.mock("@langfuse/shared/src/db", () => ({
diff --git a/worker/src/features/in-app-agent-integrity-runner/index.ts b/worker/src/features/in-app-agent-integrity-runner/index.ts
index a384f2f632..064b204b98 100644
--- a/worker/src/features/in-app-agent-integrity-runner/index.ts
+++ b/worker/src/features/in-app-agent-integrity-runner/index.ts
@@ -1,6 +1,6 @@
import { prisma, type Prisma } from "@langfuse/shared/src/db";
import { logger, recordGauge, redis } from "@langfuse/shared/src/server";
-import { deleteApiKeyFromDb } from "@langfuse/shared/src/server/auth/apiKeys";
+import { deleteInAppAgentMcpApiKeyFromDb } from "@langfuse/shared/src/server/auth/apiKeys";
import {
IN_APP_AGENT_UNSETTLED_RUN_STATUSES,
InAppAgentRunErrorCode,
@@ -138,11 +138,10 @@ export class InAppAgentIntegrityRunner extends PeriodicExclusiveRunner {
projectId,
conversationId,
deleteApiKey: async (apiKeyId) => {
- await deleteApiKeyFromDb({
+ await deleteInAppAgentMcpApiKeyFromDb({
prisma,
id: apiKeyId,
- entityId: projectId,
- scope: "PROJECT",
+ projectId,
redis,
});
},
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
index 41c037cfcc..9f887c0c20 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
@@ -170,14 +170,14 @@ vi.mock("@langfuse/shared/src/server/auth/apiKeys", async (importOriginal) => {
return {
...actual,
- deleteApiKeyFromDb: async (
- ...args: Parameters<typeof actual.deleteApiKeyFromDb>
+ deleteInAppAgentMcpApiKeyFromDb: async (
+ ...args: Parameters<typeof actual.deleteInAppAgentMcpApiKeyFromDb>
) => {
scenarioRef.apiKeyDeleteCalls += 1;
if (scenarioRef.failApiKeyDelete) {
throw new Error("simulated api key delete failure");
}
- return actual.deleteApiKeyFromDb(...args);
+ return actual.deleteInAppAgentMcpApiKeyFromDb(...args);
},
};
});
@@ -914,6 +914,47 @@ describe("executeInAppAgentRun", () => {
).toHaveLength(0);
});
+ it("leaves a user project key intact when mcpApiKeyId points at it", async () => {
+ const { projectId, run, user } = await seedBackgroundRun({
+ status: "RUNNING",
+ });
+ const twoMinutesAgo = new Date(Date.now() - 2 * 60_000);
+ const userKey = await createAndAddApiKeysToDb({
+ prisma,
+ entityId: projectId,
+ scope: "PROJECT",
+ note: "user project key",
+ isInAppAgentKey: false,
+ createdByUserId: user.id,
+ });
+ await prisma.inAppAgentRun.update({
+ where: { id_projectId: { id: run.id, projectId } },
+ data: {
+ claimedAt: twoMinutesAgo,
+ heartbeatAt: twoMinutesAgo,
+ mcpApiKeyId: userKey.id,
+ },
+ });
+
+ scenarioRef.current = async () => {
+ throw new Error(
+ "agent loop must not start on stale unclaimable delivery",
+ );
+ };
+
+ await expect(
+ executeInAppAgentRun({ projectId, runId: run.id }),
+ ).resolves.toBeUndefined();
+
+ const failed = await getRun(projectId, run.id);
+ expect(failed.status).toBe("FAILED");
+ expect(failed.errorCode).toBe("worker_lost");
+ expect(failed.mcpApiKeyId).toBeNull();
+ expect(
+ await prisma.apiKey.findUnique({ where: { id: userKey.id } }),
+ ).not.toBeNull();
+ });
+
it("acknowledges delivery against an already-FAILED run without changing it", async () => {
const { projectId, run } = await seedBackgroundRun({ status: "FAILED" });
const finishedAt = new Date("2026-08-01T00:00:00.000Z");
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
index eb06c30730..d9a884de58 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
@@ -9,7 +9,7 @@ import {
} from "@langfuse/shared/src/server";
import {
createAndAddApiKeysToDb,
- deleteApiKeyFromDb,
+ deleteInAppAgentMcpApiKeyFromDb,
} from "@langfuse/shared/src/server/auth/apiKeys";
import {
InAppAgentRunErrorCode,
@@ -84,11 +84,10 @@ async function deleteInAppAgentMcpApiKey(params: {
apiKeyId: string;
}): Promise<void> {
try {
- await deleteApiKeyFromDb({
+ await deleteInAppAgentMcpApiKeyFromDb({
prisma,
id: params.apiKeyId,
- entityId: params.projectId,
- scope: "PROJECT",
+ projectId: params.projectId,
redis,
});
} catch (error) {