Skip to content

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

feat(in-app-agent): approve a tool once for a whole conversation (#15849)

Lysander Kiesel2ee51d948175
diff --git a/packages/shared/prisma/migrations/20260806120000_add_in_app_agent_conversation_always_allowed_tools/migration.sql b/packages/shared/prisma/migrations/20260806120000_add_in_app_agent_conversation_always_allowed_tools/migration.sql
new file mode 100644
index 0000000000..5d97637aa3
--- /dev/null
+++ b/packages/shared/prisma/migrations/20260806120000_add_in_app_agent_conversation_always_allowed_tools/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "in_app_agent_conversations" ADD COLUMN "always_allowed_tools" TEXT[] NOT NULL DEFAULT ARRAY[]::TEXT[];
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index 6c4e7105da..a94174b834 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -239,6 +239,8 @@ model InAppAgentConversation {
renamedByUserAt DateTime? @map("renamed_by_user_at")
visibilityScope InAppAgentConversationVisibilityScope @default(PERSONAL) @map("visibility_scope")
providerSessionId String? @map("provider_session_id")
+ // Prefixed tool grants are revalidated against the registry and owner's role each run.
+ alwaysAllowedTools String[] @default([]) @map("always_allowed_tools")
deletedAt DateTime? @map("deleted_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
diff --git a/packages/shared/src/in-app-agent/backgroundWatch.ts b/packages/shared/src/in-app-agent/backgroundWatch.ts
index f1c7180fd8..c18e4023b2 100644
--- a/packages/shared/src/in-app-agent/backgroundWatch.ts
+++ b/packages/shared/src/in-app-agent/backgroundWatch.ts
@@ -17,6 +17,8 @@ export const InAppAgentApprovalDecisionSchema = z.object({
toolCallId: z.string().min(1),
approved: z.boolean(),
decidedByUserId: z.string().min(1),
+ /** Optional render-only scope; durable policy comes from conversation grants. */
+ scope: z.enum(["once", "conversation"]).optional(),
});
export type InAppAgentApprovalDecision = z.infer<
diff --git a/packages/shared/src/in-app-agent/server/agent.ts b/packages/shared/src/in-app-agent/server/agent.ts
index 5e14f4da93..06d0b3f087 100644
--- a/packages/shared/src/in-app-agent/server/agent.ts
+++ b/packages/shared/src/in-app-agent/server/agent.ts
@@ -13,7 +13,10 @@ import {
type InAppAgentToolApprovalRequest,
type ResumeForwardedProps,
} from "../schema";
-import { createManualToolApprovalRunInput } from "./human-in-the-loop";
+import {
+ createInAppAgentMcpRunOverride,
+ createManualToolApprovalRunInput,
+} from "./human-in-the-loop";
import type {
InAppAgentPromptMetadata,
InAppAgentTracingConfig,
@@ -23,8 +26,10 @@ import {
createSandboxTools,
createRedirectActionTool,
filterInAppAgentAvailableLangfuseMcpTools,
+ getInAppAgentMcpAllowedToolNames,
+ getInAppAgentRegistryToolName,
type CompletedInAppAgentMcpToolCall,
- type InAppAgentUserAccess,
+ type InAppAgentToolPolicy,
withOptionalSilentMcpOutput,
withInAppAgentToolApproval,
} from "./tools";
@@ -171,7 +176,7 @@ type CreateAgUiStreamOptions = {
url: string;
publicKey: string;
secretKey: string;
- userAccess: InAppAgentUserAccess;
+ toolPolicy: InAppAgentToolPolicy;
runOverride?: string;
};
redirectAction: {
@@ -498,14 +503,24 @@ export async function createAgUiStream(params: {
const pendingSyntheticEvents = [...runInput.syntheticEvents];
currentAdapter.setDeveloperGuidance(runInput.developerGuidance);
+ // Drop a one-off override after its call; standing grants remain in the policy.
+ const oneOffApprovedToolName =
+ forwardedProps?.command?.resume?.approved === true
+ ? getInAppAgentRegistryToolName(
+ forwardedProps.command.resume.approvalRequest?.toolName,
+ )
+ : undefined;
+
if (
- forwardedProps?.command?.resume?.approved === true &&
- params.options.langfuseMcp.runOverride
+ oneOffApprovedToolName &&
+ params.options.langfuseMcp.runOverride &&
+ !params.options.langfuseMcp.toolPolicy.autoApproved.has(
+ oneOffApprovedToolName,
+ )
) {
- // The override is intentionally single-use: execute the approved
- // mutating MCP tool with the first client, then rebuild the MCP
- // client without the override so the continuation returns to the
- // normal read-only in-app-agent policy.
+ const standingAllowedToolNames = getInAppAgentMcpAllowedToolNames(
+ params.options.langfuseMcp.toolPolicy,
+ );
await currentAdapter.cleanup();
@@ -517,7 +532,12 @@ export async function createAgUiStream(params: {
...params.options,
langfuseMcp: {
...params.options.langfuseMcp,
- runOverride: undefined,
+ runOverride:
+ standingAllowedToolNames.length > 0
+ ? await createInAppAgentMcpRunOverride({
+ toolNames: standingAllowedToolNames,
+ })
+ : undefined,
},
},
awsProfile,
@@ -782,31 +802,34 @@ async function createMastraAdapter(params: {
// agent.stream(..., { toolsets }) call. Keep Mastra's per-request MCP
// discovery, then prefix tool names for constructor-based tools so the
// model sees the same names that later appear in AG-UI tool-call events.
- const tools = withInAppAgentToolApproval({
- ...withOptionalSilentMcpOutput({
- tools: prefixToolsetTools(
- "langfuse",
- filterInAppAgentAvailableLangfuseMcpTools({
- tools: toolsets.langfuse,
- userAccess: params.options.langfuseMcp.userAccess,
- }),
- ),
- sandbox: params.options.sandbox,
- onToolCallCompleted: params.options.onMcpToolCallCompleted,
- }),
- ...withOptionalSilentMcpOutput({
- tools: prefixToolsetTools("langfuseDocs", toolsets.langfuseDocs),
- sandbox: params.options.sandbox,
- onToolCallCompleted: params.options.onMcpToolCallCompleted,
- }),
- [IN_APP_AGENT_REDIRECT_TOOL_NAME]: createRedirectActionTool({
- projectId: params.options.redirectAction.projectId,
- isV4Enabled: params.options.redirectAction.isV4Enabled,
- }),
- ...(params.options.sandbox
- ? createSandboxTools(params.options.sandbox)
- : {}),
- });
+ const tools = withInAppAgentToolApproval(
+ {
+ ...withOptionalSilentMcpOutput({
+ tools: prefixToolsetTools(
+ "langfuse",
+ filterInAppAgentAvailableLangfuseMcpTools({
+ tools: toolsets.langfuse,
+ policy: params.options.langfuseMcp.toolPolicy,
+ }),
+ ),
+ sandbox: params.options.sandbox,
+ onToolCallCompleted: params.options.onMcpToolCallCompleted,
+ }),
+ ...withOptionalSilentMcpOutput({
+ tools: prefixToolsetTools("langfuseDocs", toolsets.langfuseDocs),
+ sandbox: params.options.sandbox,
+ onToolCallCompleted: params.options.onMcpToolCallCompleted,
+ }),
+ [IN_APP_AGENT_REDIRECT_TOOL_NAME]: createRedirectActionTool({
+ projectId: params.options.redirectAction.projectId,
+ isV4Enabled: params.options.redirectAction.isV4Enabled,
+ }),
+ ...(params.options.sandbox
+ ? createSandboxTools(params.options.sandbox)
+ : {}),
+ },
+ params.options.langfuseMcp.toolPolicy,
+ );
params.onToolsAvailable?.(tools);
const reasoningProviderOptions = getBedrockReasoningProviderOptions(
diff --git a/packages/shared/src/in-app-agent/server/human-in-the-loop.ts b/packages/shared/src/in-app-agent/server/human-in-the-loop.ts
index bdc7004743..43f7d23f15 100644
--- a/packages/shared/src/in-app-agent/server/human-in-the-loop.ts
+++ b/packages/shared/src/in-app-agent/server/human-in-the-loop.ts
@@ -34,9 +34,16 @@ const InAppAgentLangfuseMcpToolNameSchema =
{ message: "Invalid MCP tool name" },
);
-export const InAppAgentMcpRunOverrideSchema = z.object({
- toolName: InAppAgentLangfuseMcpToolNameSchema,
-});
+/** Accept the legacy single-tool shape while older continuations may still be queued. */
+export const InAppAgentMcpRunOverrideSchema = z
+ .union([
+ z.object({ toolNames: z.array(InAppAgentLangfuseMcpToolNameSchema) }),
+ z.object({ toolName: InAppAgentLangfuseMcpToolNameSchema }),
+ ])
+ .transform((override) => ({
+ toolNames:
+ "toolNames" in override ? override.toolNames : [override.toolName],
+ }));
const MastraSuspendEventSchema = z.object({
type: z.literal("mastra_suspend"),
@@ -47,10 +54,12 @@ const MastraSuspendEventSchema = z.object({
});
export async function createInAppAgentMcpRunOverride(params: {
- toolName: InAppAgentLangfuseMcpToolName;
+ toolNames: InAppAgentLangfuseMcpToolName[];
}) {
return JSON.stringify({
- toolName: params.toolName,
+ // Older web pods read the singular field during rolling deploys.
+ toolName: params.toolNames[0],
+ toolNames: params.toolNames,
});
}
diff --git a/packages/shared/src/in-app-agent/server/runLifecycle.ts b/packages/shared/src/in-app-agent/server/runLifecycle.ts
index 8e9011ade0..0a8ed04f2e 100644
--- a/packages/shared/src/in-app-agent/server/runLifecycle.ts
+++ b/packages/shared/src/in-app-agent/server/runLifecycle.ts
@@ -9,6 +9,7 @@ import type { InAppAgentRun, PrismaClient } from "../../db";
import { logger } from "../../server";
import { buildInAppAgentApprovalDecisionEvent } from "../backgroundWatch";
import type { AgUiEvent } from "../schema";
+import type { InAppAgentPrefixedLangfuseMcpToolName } from "./tools";
import {
InAppAgentRunRequestSchema,
type InAppAgentRunRequest,
@@ -202,6 +203,8 @@ export async function decideToolApproval(params: {
toolCallId: string;
approved: boolean;
decidedByUserId: string;
+ /** Prefixed tool resolved from the persisted interrupt, never client input. */
+ alwaysAllowToolName?: InAppAgentPrefixedLangfuseMcpToolName;
model?: string;
}): Promise<InAppAgentRun> {
const outcome = await params.prisma.$transaction(async (tx) => {
@@ -266,6 +269,33 @@ export async function decideToolApproval(params: {
parentRun.request,
);
+ // Persist the grant in the same transaction as the exactly-once decision CAS.
+ if (params.alwaysAllowToolName) {
+ const conversation = await tx.inAppAgentConversation.findUnique({
+ where: {
+ id_projectId: {
+ id: params.conversationId,
+ projectId: params.projectId,
+ },
+ },
+ select: { alwaysAllowedTools: true },
+ });
+
+ if (
+ !conversation?.alwaysAllowedTools.includes(params.alwaysAllowToolName)
+ ) {
+ await tx.inAppAgentConversation.update({
+ where: {
+ id_projectId: {
+ id: params.conversationId,
+ projectId: params.projectId,
+ },
+ },
+ data: { alwaysAllowedTools: { push: params.alwaysAllowToolName } },
+ });
+ }
+ }
+
await appendConversationEventInTransaction({
tx,
projectId: params.projectId,
@@ -275,6 +305,9 @@ export async function decideToolApproval(params: {
toolCallId: params.toolCallId,
approved: params.approved,
decidedByUserId: params.decidedByUserId,
+ ...(params.alwaysAllowToolName
+ ? { scope: "conversation" as const }
+ : {}),
}),
});
diff --git a/packages/shared/src/in-app-agent/server/tools.test.ts b/packages/shared/src/in-app-agent/server/tools.test.ts
new file mode 100644
index 0000000000..652d06133a
--- /dev/null
+++ b/packages/shared/src/in-app-agent/server/tools.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from "vitest";
+
+import { createInAppAgentToolPolicy } from "./tools";
+
+describe("createInAppAgentToolPolicy", () => {
+ it("drops a stored grant the user's role no longer covers", () => {
+ const grants = ["langfuse_createModel"];
+
+ const asOwner = createInAppAgentToolPolicy({
+ userAccess: { projectRole: "OWNER", isAdmin: false },
+ alwaysAllowedTools: grants,
+ });
+
+ expect(asOwner.available.has("createModel")).toBe(true);
+ expect(asOwner.autoApproved.has("createModel")).toBe(true);
+
+ const asMember = createInAppAgentToolPolicy({
+ userAccess: { projectRole: "MEMBER", isAdmin: false },
+ alwaysAllowedTools: grants,
+ });
+
+ expect(asMember.available.has("createModel")).toBe(false);
+ expect(asMember.autoApproved.has("createModel")).toBe(false);
+ });
+});
diff --git a/packages/shared/src/in-app-agent/server/tools.ts b/packages/shared/src/in-app-agent/server/tools.ts
index 81c3d7ad95..43c420a6a9 100644
--- a/packages/shared/src/in-app-agent/server/tools.ts
+++ b/packages/shared/src/in-app-agent/server/tools.ts
@@ -397,13 +397,8 @@ export const IN_APP_AGENT_SANDBOX_TOOL_NAMES = new Set([
"bash",
]);
-// Tools in this set can run without a human-in-the-loop approval prompt. Every
-// other MCP tool is still exposed to the model, but Mastra suspends execution
-// until the user explicitly approves the exact call.
-export const IN_APP_AGENT_AUTO_APPROVED_TOOL_NAMES = new Set([
- ...Object.entries(IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES)
- .filter(([, policy]) => policy.approval === "auto")
- .map(([toolName]) => `langfuse_${toolName}`),
+// Runtime-owned tools never suspend; documentation tools are approved by prefix.
+const IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES = new Set<string>([
...IN_APP_AGENT_AUTO_APPROVED_EXTERNAL_TOOL_NAMES,
...IN_APP_AGENT_SANDBOX_TOOL_NAMES,
]);
@@ -416,6 +411,30 @@ export function isMcpToolName(
);
}
+/** Durable grants retain the MCP surface prefix to avoid cross-surface collisions. */
+export type InAppAgentPrefixedLangfuseMcpToolName =
+ `langfuse_${InAppAgentLangfuseMcpToolName}`;
+
+export function getInAppAgentRegistryToolName(
+ toolName: string | undefined,
+): InAppAgentLangfuseMcpToolName | undefined {
+ if (!toolName?.startsWith("langfuse_")) {
+ return undefined;
+ }
+
+ const registryToolName = toolName.slice("langfuse_".length);
+
+ return isMcpToolName(registryToolName) ? registryToolName : undefined;
+}
+
+export function getInAppAgentPrefixedToolName(
+ toolName: string | undefined,
+): InAppAgentPrefixedLangfuseMcpToolName | undefined {
+ const registryToolName = getInAppAgentRegistryToolName(toolName);
+
+ return registryToolName ? `langfuse_${registryToolName}` : undefined;
+}
+
export function isInAppAgentLangfuseMcpToolAvailable(params: {
toolName: InAppAgentLangfuseMcpToolName;
userAccess?: InAppAgentUserAccess;
@@ -437,22 +456,75 @@ export function isInAppAgentLangfuseMcpToolAvailable(params: {
});
}
+export type InAppAgentToolPolicy = {
+ readonly available: ReadonlySet<InAppAgentLangfuseMcpToolName>;
+ readonly autoApproved: ReadonlySet<InAppAgentLangfuseMcpToolName>;
+};
+
+export function createInAppAgentToolPolicy(params: {
+ userAccess?: InAppAgentUserAccess;
+ alwaysAllowedTools?: Iterable<string>;
+}): InAppAgentToolPolicy {
+ const available = new Set<InAppAgentLangfuseMcpToolName>();
+ const autoApproved = new Set<InAppAgentLangfuseMcpToolName>();
+
+ for (const toolName of IN_APP_AGENT_LANGFUSE_MCP_TOOL_NAMES) {
+ if (
+ !isInAppAgentLangfuseMcpToolAvailable({
+ toolName,
+ userAccess: params.userAccess,
+ })
+ ) {
+ continue;
+ }
+
+ available.add(toolName);
+
+ if (IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "auto") {
+ autoApproved.add(toolName);
+ }
+ }
+
+ for (const prefixedToolName of params.alwaysAllowedTools ?? []) {
+ const toolName = getInAppAgentRegistryToolName(prefixedToolName);
+
+ if (toolName && available.has(toolName)) {
+ autoApproved.add(toolName);
+ }
+ }
+
+ return { available, autoApproved };
+}
+
+export function getInAppAgentMcpAllowedToolNames(
+ policy: InAppAgentToolPolicy,
+ oneOffToolName?: InAppAgentLangfuseMcpToolName,
+): InAppAgentLangfuseMcpToolName[] {
+ const allowed = new Set<InAppAgentLangfuseMcpToolName>();
+
+ // Keep the current approval first for legacy web pods that accept one tool.
+ if (oneOffToolName && policy.available.has(oneOffToolName)) {
+ allowed.add(oneOffToolName);
+ }
+
+ for (const toolName of policy.autoApproved) {
+ if (
+ IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES[toolName].approval === "approval"
+ ) {
+ allowed.add(toolName);
+ }
+ }
+
+ return [...allowed];
+}
+
export function filterInAppAgentAvailableLangfuseMcpTools<TTool>(params: {
tools: Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> | undefined;
- userAccess?: InAppAgentUserAccess;
+ policy: InAppAgentToolPolicy;
}): Partial<Record<InAppAgentLangfuseMcpToolName, TTool>> {
return Object.fromEntries(
Object.entries(params.tools ?? {}).flatMap(([toolName, tool]) => {
- if (!isMcpToolName(toolName)) {
- return [];
- }
-
- if (
- !isInAppAgentLangfuseMcpToolAvailable({
- toolName,
- userAccess: params.userAccess,
- })
- ) {
+ if (!isMcpToolName(toolName) || !params.policy.available.has(toolName)) {
return [];
}
@@ -465,21 +537,33 @@ type InAppAgentTool = object;
export function withInAppAgentToolApproval<TTool extends InAppAgentTool>(
tools: Record<string, TTool>,
+ policy: InAppAgentToolPolicy,
): Record<string, TTool | (TTool & { requireApproval: true })> {
return Object.fromEntries(
Object.entries(tools).map(([toolName, tool]) => [
toolName,
- isInAppAgentAutoApprovedToolName(toolName)
+ isInAppAgentAutoApprovedToolName(toolName, policy)
? tool
: { ...tool, requireApproval: true },
]),
) as Record<string, TTool | (TTool & { requireApproval: true })>;
}
-function isInAppAgentAutoApprovedToolName(toolName: string): boolean {
- return (
+function isInAppAgentAutoApprovedToolName(
+ toolName: string,
+ policy: InAppAgentToolPolicy,
+): boolean {
+ if (
toolName.startsWith("langfuseDocs_") ||
- IN_APP_AGENT_AUTO_APPROVED_TOOL_NAMES.has(toolName)
+ IN_APP_AGENT_LOCAL_AUTO_APPROVED_TOOL_NAMES.has(toolName)
+ ) {
+ return true;
+ }
+
+ const registryToolName = getInAppAgentRegistryToolName(toolName);
+
+ return (
+ registryToolName !== undefined && policy.autoApproved.has(registryToolName)
);
}
diff --git a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
index 435a8a87c4..250ba78eb3 100644
--- a/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-api-route-auth.servertest.ts
@@ -6,7 +6,10 @@ import { z } from "zod";
import { env } from "@/src/env.mjs";
import { createAuthedProjectAPIRoute } from "@/src/features/public-api/server/createAuthedProjectAPIRoute";
-import { filterInAppAgentAvailableLangfuseMcpTools } from "@langfuse/shared/in-app-agent/server/tools";
+import {
+ createInAppAgentToolPolicy,
+ filterInAppAgentAvailableLangfuseMcpTools,
+} from "@langfuse/shared/in-app-agent/server/tools";
import { prisma } from "@langfuse/shared/src/db";
import {
createAndAddApiKeysToDb,
@@ -108,10 +111,9 @@ describe("in-app agent public API route auth", () => {
expect(
filterInAppAgentAvailableLangfuseMcpTools({
tools,
- userAccess: {
- projectRole: "MEMBER",
- isAdmin: false,
- },
+ policy: createInAppAgentToolPolicy({
+ userAccess: { projectRole: "MEMBER", isAdmin: false },
+ }),
}),
).toEqual({
listDatasets: { id: "listDatasets" },
@@ -121,10 +123,9 @@ describe("in-app agent public API route auth", () => {
expect(
filterInAppAgentAvailableLangfuseMcpTools({
tools,
- userAccess: {
- projectRole: "OWNER",
- isAdmin: false,
- },
+ policy: createInAppAgentToolPolicy({
+ userAccess: { projectRole: "OWNER", isAdmin: false },
+ }),
}),
).toEqual(tools);
});
diff --git a/web/src/__tests__/server/in-app-agent-background-run.servertest.ts b/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
index d0d6428da2..4e886fb6e7 100644
--- a/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-background-run.servertest.ts
@@ -510,6 +510,70 @@ describe("in-app agent background runs", () => {
expect(runId).not.toBe(parkedRunId);
});
+ it("grants a tool for the conversation only when the scope asks for it", async () => {
+ const { caller, projectId, userId } = await createCaller();
+ const onceConversation = await createConversation({ projectId, userId });
+ const grantConversation = await createConversation({ projectId, userId });
+
+ const onceRunId = await parkRunForApproval({
+ projectId,
+ conversationId: onceConversation.id,
+ userId,
+ toolCallId: "tool-call-once",
+ });
+
+ await caller.decideToolApproval({
+ projectId,
+ conversationId: onceConversation.id,
+ runId: onceRunId,
+ toolCallId: "tool-call-once",
+ approved: true,
+ });
+
+ await expect(
+ prisma.inAppAgentConversation.findFirstOrThrow({
+ where: { id: onceConversation.id, projectId },
+ select: { alwaysAllowedTools: true },
+ }),
+ ).resolves.toEqual({ alwaysAllowedTools: [] });
+
+ const grantRunId = await parkRunForApproval({
+ projectId,
+ conversationId: grantConversation.id,
+ userId,
+ toolCallId: "tool-call-grant",
+ });
+
+ await caller.decideToolApproval({
+ projectId,
+ conversationId: grantConversation.id,
+ runId: grantRunId,
+ toolCallId: "tool-call-grant",
+ approved: true,
+ approvalScope: "conversation",
+ });
+
+ await expect(
+ prisma.inAppAgentConversation.findFirstOrThrow({
+ where: { id: grantConversation.id, projectId },
+ select: { alwaysAllowedTools: true },
+ }),
+ ).resolves.toEqual({ alwaysAllowedTools: ["langfuse_createTextPrompt"] });
+
+ const decisionEvent = await prisma.inAppAgentEvent.findFirstOrThrow({
+ where: {
+ projectId,
+ conversationId: grantConversation.id,
+ runId: grantRunId,
+ },
+ orderBy: { sequenceNumber: "desc" },
+ });
+ expect(decisionEvent.event).toMatchObject({
+ name: IN_APP_AGENT_APPROVAL_DECISION_EVENT_NAME,
+ value: { toolCallId: "tool-call-grant", scope: "conversation" },
+ });
+ });
+
it("decides an approval exactly once and reads the tool args server-side", async () => {
const { caller, projectId, userId } = await createCaller();
const conversation = await createConversation({ projectId, userId });
diff --git a/web/src/__tests__/server/in-app-agent-stream.servertest.ts b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
index f68d8c8a6c..4ac436d39f 100644
--- a/web/src/__tests__/server/in-app-agent-stream.servertest.ts
+++ b/web/src/__tests__/server/in-app-agent-stream.servertest.ts
@@ -20,6 +20,7 @@ import {
type SandboxSession,
} from "@langfuse/shared/in-app-agent/server/sandbox";
import {
+ createInAppAgentToolPolicy,
IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES,
type InAppAgentLangfuseMcpToolName,
} from "@langfuse/shared/in-app-agent/server/tools";
@@ -107,6 +108,10 @@ const defaultInAppAgentUserAccess = {
isAdmin: false,
};
+const defaultInAppAgentToolPolicy = createInAppAgentToolPolicy({
+ userAccess: defaultInAppAgentUserAccess,
+});
+
async function createTestSandbox() {
let sandboxState: {
providerSessionId: string | null;
@@ -584,7 +589,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -883,7 +888,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -905,6 +910,75 @@ describe("createAgUiStream", () => {
expect(agentConfig?.defaultOptions).not.toHaveProperty("providerOptions");
});
+ it("stops gating a tool the user approved for the conversation", async () => {
+ const { createAgUiStream } =
+ await import("@langfuse/shared/in-app-agent/server/agent");
+ const input = {
+ threadId: "conversation-1",
+ runId: "run-1",
+ messages: [
+ { id: "user-message-1", role: "user" as const, content: "hello" },
+ ],
+ tools: [],
+ context: [],
+ state: {
+ type: "existingConversation" as const,
+ projectId: "project-1",
+ conversationId: "conversation-1",
+ },
+ forwardedProps: {},
+ };
+ const langfuseClient = {
+ getPrompt: promptMocks.getPrompt,
+ } as unknown as Langfuse;
+
+ adapterEvents.items = [
+ {
+ type: EventType.RUN_STARTED,
+ threadId: input.threadId,
+ runId: input.runId,
+ },
+ {
+ type: EventType.RUN_FINISHED,
+ threadId: input.threadId,
+ runId: input.runId,
+ },
+ ];
+
+ const grantedPolicy = createInAppAgentToolPolicy({
+ userAccess: defaultInAppAgentUserAccess,
+ alwaysAllowedTools: ["langfuse_upsertDataset"],
+ });
+
+ const stream = await createAgUiStream({
+ input,
+ signal: new AbortController().signal,
+ options: {
+ awsBedrock: { modelId: "eu.anthropic.claude-opus-4-8" },
+ langfuseMcp: {
+ url: "https://example.com/api/public/mcp",
+ publicKey: "pk",
+ secretKey: "sk",
+ toolPolicy: grantedPolicy,
+ runOverride: "conversation-grant",
+ },
+ redirectAction: { projectId: "project-1", isV4Enabled: false },
+ langfuseClient,
+ useLocalPrompt: false,
+ },
+ });
+
+ await readStream(stream);
+
+ const { Agent } = await import("@mastra/core/agent");
+ const agentTools = getAgentTools(vi.mocked(Agent).mock.calls[0]?.[0]);
+
+ expect(agentTools?.langfuse_upsertDataset).not.toHaveProperty(
+ "requireApproval",
+ );
+ expect(agentTools?.langfuse_createScoreConfig?.requireApproval).toBe(true);
+ });
+
it("executes approved tools manually and continues with tool result history", async () => {
const { createAgUiStream } =
await import("@langfuse/shared/in-app-agent/server/agent");
@@ -934,7 +1008,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -1140,7 +1214,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -1218,7 +1292,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -1364,7 +1438,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -1446,7 +1520,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
},
redirectAction: {
projectId: "project-1",
@@ -1596,7 +1670,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
runOverride: "run-override",
},
redirectAction: {
@@ -1685,7 +1759,7 @@ describe("createAgUiStream", () => {
url: "https://example.com/api/public/mcp",
publicKey: "pk",
secretKey: "sk",
- userAccess: defaultInAppAgentUserAccess,
+ toolPolicy: defaultInAppAgentToolPolicy,
},
redirectAction: {
projectId: "project-1",
diff --git a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
index 9a264825f5..c8646d52ef 100644
--- a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
+++ b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
@@ -201,11 +201,11 @@ describe("MCP public API tools", () => {
).resolves.toBeUndefined();
});
- it("resolves only the overridden mutating tool for in-app agent keys", async () => {
+ it("resolves allowlisted mutating tools plus reads for in-app agent keys", async () => {
const context = mockServerContext({
inAppAgent: {
- permissions: "single-tool-override",
- allowedToolName: "upsertDataset",
+ permissions: "tool-allowlist",
+ allowedToolNames: ["upsertDataset"],
},
});
@@ -217,14 +217,14 @@ describe("MCP public API tools", () => {
).resolves.toBeUndefined();
await expect(
toolRegistry.getEnabledTool("listDatasets", context),
- ).resolves.toBeUndefined();
+ ).resolves.toBeTruthy();
});
it("resolves the dashboard widget creation override for in-app agent keys", async () => {
const context = mockServerContext({
inAppAgent: {
- permissions: "single-tool-override",
- allowedToolName: "createDashboardWidget",
+ permissions: "tool-allowlist",
+ allowedToolNames: ["createDashboardWidget"],
},
});
diff --git a/web/src/__tests__/server/unit/in-app-agent-mcp-run-override.servertest.ts b/web/src/__tests__/server/unit/in-app-agent-mcp-run-override.servertest.ts
deleted file mode 100644
index c18b616142..0000000000
--- a/web/src/__tests__/server/unit/in-app-agent-mcp-run-override.servertest.ts
+++ /dev/null
@@ -1,49 +0,0 @@
-import {
- createInAppAgentMcpRunOverride,
- InAppAgentMcpRunOverrideSchema,
-} from "@langfuse/shared/in-app-agent/server/human-in-the-loop";
-
-describe("in-app agent MCP run override", () => {
- it("serializes the override as plain JSON", async () => {
- const token = await createInAppAgentMcpRunOverride({
- toolName: "upsertDataset",
- });
-
- expect(JSON.parse(token)).toEqual({
- toolName: "upsertDataset",
- });
- });
-
- it("accepts a matching plain JSON override", async () => {
- const token = await createInAppAgentMcpRunOverride({
- toolName: "upsertDataset",
- });
-
- expect(InAppAgentMcpRunOverrideSchema.safeParse(JSON.parse(token))).toEqual(
- {
- success: true,
- data: {
- toolName: "upsertDataset",
- },
- },
- );
- });
-
- it("rejects malformed or mismatched overrides", () => {
- expect(InAppAgentMcpRunOverrideSchema.safeParse("not-json").success).toBe(
- false,
- );
-
- expect(
- InAppAgentMcpRunOverrideSchema.safeParse({
- toolName: "notAMcpTool",
- }).success,
- ).toBe(false);
-
- expect(
- InAppAgentMcpRunOverrideSchema.safeParse({
- other: "value",
- }).success,
- ).toBe(false);
- });
-});
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 716c29f607..2a155a8bb3 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
@@ -1,8 +1,5 @@
import { IN_APP_AGENT_MCP_TOOL_OVERRIDE_HEADER } from "@langfuse/shared/in-app-agent";
-import {
- createInAppAgentMcpRunOverride,
- InAppAgentMcpRunOverrideSchema,
-} from "@langfuse/shared/in-app-agent/server/human-in-the-loop";
+import { createInAppAgentMcpRunOverride } from "@langfuse/shared/in-app-agent/server/human-in-the-loop";
import { getInAppAgentContext } from "@/src/pages/api/public/mcp";
import type { NextApiRequest, NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
@@ -47,20 +44,25 @@ describe("MCP route in-app-agent context", () => {
});
});
- it("returns a single-tool override for valid override headers", async () => {
+ it("returns the tool allowlist for valid override headers", async () => {
const overrideHeader = await createInAppAgentMcpRunOverride({
- toolName: "upsertDataset",
+ toolNames: ["upsertDataset", "createDashboardWidget"],
});
const req = createRequest(overrideHeader);
- expect(
- InAppAgentMcpRunOverrideSchema.parse(JSON.parse(overrideHeader)),
- ).toEqual({
- toolName: "upsertDataset",
+ expect(getInAppAgentContext(req, true)).toEqual({
+ permissions: "tool-allowlist",
+ allowedToolNames: ["upsertDataset", "createDashboardWidget"],
});
+ });
+
+ // Accept continuations queued by workers that still emit one tool.
+ it("still resolves the pre-allowlist single-tool header", () => {
+ const req = createRequest('{"toolName":"upsertDataset"}');
+
expect(getInAppAgentContext(req, true)).toEqual({
- permissions: "single-tool-override",
- allowedToolName: "upsertDataset",
+ permissions: "tool-allowlist",
+ allowedToolNames: ["upsertDataset"],
});
});
});
diff --git a/web/src/features/in-app-agent/README.md b/web/src/features/in-app-agent/README.md
index 7ec8a0a553..c9f2c840c1 100644
--- a/web/src/features/in-app-agent/README.md
+++ b/web/src/features/in-app-agent/README.md
@@ -198,19 +198,21 @@ The in-app agent worker uses two run-scoped inputs when calling Langfuse MCP:
- A temporary project-scoped API key marked as an in-app-agent key.
- An optional server-generated tool override sent with `x-langfuse-in-app-agent-tool-override`.
-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 creates a JSON override naming that one unprefixed MCP registry tool and passes it to the MCP route through the request header above.
+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`. With an override, the key may additionally call the named mutating tools. The worker builds the override from the current one-off approval and the conversation's stored grants. The parser also accepts the earlier singular `{"toolName":…}` shape so already-enqueued continuations remain executable during rollout.
MCP registry behavior:
- Normal project API keys can call all enabled MCP tools.
- In-app-agent keys can call read-only tools directly when the tool has `readOnlyHint: true`.
-- In-app-agent keys need a valid tool override to call one non-read-only Langfuse MCP tool.
+- In-app-agent keys need a valid tool override to call named non-read-only Langfuse MCP tools.
RBAC is the first gate for Langfuse MCP tools. Before a tool is exposed to the model, `server/tools.ts` checks the signed-in user's `projectRole` and `isAdmin` against the tool's required `ProjectScope` with `hasProjectAccess()`. That means the assistant never sees tools the user could not use manually in the product UI or APIs. Human approval is a second gate on top of RBAC for tools classified as `"approval"`: approval can allow one execution of a tool the user already has access to, but it does not widen the user's project permissions.
Human approval is separate from the MCP tool override. Shared `server/tools.ts` classifies every Langfuse MCP tool in `IN_APP_AGENT_LANGFUSE_MCP_TOOL_POLICIES`, using unprefixed MCP registry names and either `"auto"` or `"approval"`. The web in-app-agent server test imports the MCP registry's `McpToolName` contract and verifies both type equality and runtime registry equality, so adding a Langfuse MCP tool requires an explicit in-app agent approval classification without making MCP bootstrap depend on the in-app agent.
-`IN_APP_AGENT_AUTO_APPROVED_TOOL_NAMES` is generated from that map by prefixing Langfuse MCP tools with `langfuse_` and adding local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME`; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`. Mastra emits an interrupt, the browser asks the user, and the router records the decision for a durable worker continuation. `server/human-in-the-loop.ts` adapts Mastra's runtime interrupt payload into the Langfuse-owned `tool_approval_request` contract from `schema.ts`; the browser stores and forwards only that runtime-neutral shape.
+`server/tools.ts` turns that map into an `InAppAgentToolPolicy` with an `available` set for tools the model may see and an `autoApproved` set for tools that run without a prompt. `createInAppAgentToolPolicy()` intersects stored grants with current availability, so an unknown, deleted, reclassified, or now-out-of-role tool silently drops out. Local tools such as `IN_APP_AGENT_REDIRECT_TOOL_NAME` and the sandbox tools are always auto-approved; docs MCP tools are auto-approved by the `langfuseDocs_` prefix. `server/agent.ts` marks every other tool with Mastra `requireApproval: true`.
+
+Conversation-scoped grants live in `InAppAgentConversation.alwaysAllowedTools`, holding server-prefixed names such as `langfuse_createDashboardWidget`. The prefix is part of the identity, so a stored grant cannot be mistaken for a same-named tool on another MCP surface; unprefixed entries are rejected when the policy is rebuilt. Grants are written inside the same locked transaction as the approval decision in `server/runLifecycle.ts`. A one-off `Approve` writes nothing there; only `Always approve` does. The worker rebuilds the policy from the column on every run rather than trusting enqueue-time authorization. Mastra emits an interrupt, the browser asks the user, and the router records the decision for a durable worker continuation. `server/human-in-the-loop.ts` adapts Mastra's runtime interrupt payload into the Langfuse-owned `tool_approval_request` contract from `schema.ts`; the browser stores and forwards only that runtime-neutral shape.
The `InAppAgentPendingToolApproval` table is not used by background execution.
It remains temporarily so existing rows and Prisma relations stay valid.
diff --git a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
index 8fa20a937a..0be9a49ae8 100644
--- a/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/ControlledInAppAgentWindow.tsx
@@ -81,6 +81,7 @@ export function ControlledInAppAgentWindow(
messages,
pendingToolApprovals,
approveToolCall,
+ alwaysAllowToolCall,
rejectToolCall,
selectConversation,
selectedConversationId,
@@ -199,6 +200,7 @@ export function ControlledInAppAgentWindow(
onSubmit={submit}
executionUi={windowExecutionUi}
onApproveToolCall={approveToolCall}
+ onAlwaysAllowToolCall={alwaysAllowToolCall}
onRejectToolCall={rejectToolCall}
onSubmitFeedback={submitFeedback}
{...closeButtonProps}
diff --git a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
index 6538ccef26..d589d3479e 100644
--- a/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentExecution.clienttest.tsx
@@ -208,8 +208,8 @@ describe("in-app agent execution", () => {
window.sessionStorage.clear();
});
- it("renders persisted background messages and approval actions in the drawer", async () => {
- providerMocks.conversationQuery.data = {
+ it("always allows a persisted background tool for the conversation", async () => {
+ const approvedSnapshot = {
conversation: {
id: "conversation-1",
isWriteLocked: false,
@@ -224,9 +224,37 @@ describe("in-app agent execution", () => {
id: "persisted-assistant",
role: "assistant",
content: "I need approval.",
+ toolCalls: [
+ {
+ id: "tool-call-1",
+ type: "function",
+ function: {
+ name: "langfuse_createTextPrompt",
+ arguments: "{}",
+ },
+ },
+ {
+ id: "tool-call-2",
+ type: "function",
+ function: {
+ name: "langfuse_createDashboardWidget",
+ arguments: "{}",
+ },
+ },
+ ],
},
],
eventCursor: 12,
+ latestRun: {
+ id: "run-2",
+ status: InAppAgentRunStatus.RUNNING,
+ errorCode: null,
+ cancelRequested: false,
+ },
+ pendingToolApprovals: [],
+ } satisfies NonNullable<typeof providerMocks.conversationQuery.data>;
+ providerMocks.conversationQuery.data = {
+ ...approvedSnapshot,
latestRun: {
id: "run-1",
status: InAppAgentRunStatus.AWAITING_APPROVAL,
@@ -245,6 +273,10 @@ describe("in-app agent execution", () => {
},
],
};
+ providerMocks.decideToolApproval.mockResolvedValueOnce({});
+ providerMocks.utils.inAppAgent.getConversation.fetch.mockResolvedValueOnce(
+ approvedSnapshot,
+ );
window.sessionStorage.setItem(
"langfuse:in-app-ai-agent-selected-conversation:project-1",
JSON.stringify("conversation-1"),
@@ -254,16 +286,37 @@ describe("in-app agent execution", () => {
expect(await screen.findByText("Create the prompt")).toBeInTheDocument();
expect(screen.getByText("I need approval.")).toBeInTheDocument();
- expect(
- screen.getByRole("button", {
- name: "Confirm",
- }),
- ).toBeInTheDocument();
- expect(
+ fireEvent.click(
screen.getByRole("button", {
- name: "Reject",
+ name: "Always approve for this conversation",
}),
- ).toBeInTheDocument();
+ );
+
+ await waitFor(() => {
+ expect(providerMocks.decideToolApproval).toHaveBeenCalledOnce();
+ expect(providerMocks.decideToolApproval).toHaveBeenCalledWith({
+ projectId: "project-1",
+ conversationId: "conversation-1",
+ runId: "run-1",
+ toolCallId: "tool-call-1",
+ approved: true,
+ approvalScope: "conversation",
+ });
+ expect(providerMocks.capture).toHaveBeenCalledOnce();
+ expect(providerMocks.capture).toHaveBeenCalledWith(
+ "in_app_agent:tool_approval_decided",
+ {
+ isApproved: true,
+ toolName: "langfuse_createTextPrompt",
+ approvalScope: "conversation",
+ },
+ );
+ });
+ await waitFor(() => {
+ expect(
+ screen.queryByLabelText(/^createDashboardWidget:/),
+ ).not.toBeInTheDocument();
+ });
});
it("replays prompt and dashboard invalidations after a detached run completes", async () => {
diff --git a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
index 896a490e0b..b7e642badc 100644
--- a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.stories.tsx
@@ -60,6 +60,7 @@ export const ApprovalRequired = meta.story({
},
},
onApproveToolCall: fn(),
+ onAlwaysAllowToolCall: fn(),
onRejectToolCall: fn(),
},
});
@@ -85,6 +86,7 @@ export const ApprovalSubmitting = meta.story({
},
},
onApproveToolCall: fn(),
+ onAlwaysAllowToolCall: fn(),
onRejectToolCall: fn(),
},
});
@@ -111,14 +113,22 @@ export const ApprovalDisabled = meta.story({
},
},
onApproveToolCall: fn(),
+ onAlwaysAllowToolCall: fn(),
onRejectToolCall: fn(),
},
play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
const canvas = within(canvasElement);
await expect(
- canvas.getByRole("button", { name: "Confirm" }),
+ canvas.getByRole("button", { name: "Approve" }),
+ ).toBeDisabled();
+ await expect(
+ canvas.getByRole("button", {
+ name: "Always approve for this conversation",
+ }),
+ ).toBeDisabled();
+ await expect(
+ canvas.getByRole("button", { name: "Decline" }),
).toBeDisabled();
- await expect(canvas.getByRole("button", { name: "Reject" })).toBeDisabled();
},
});
diff --git a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.tsx b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.tsx
index b2b7e9b294..cf815c8a4c 100644
--- a/web/src/features/in-app-agent/components/InAppAgentToolCallCard.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentToolCallCard.tsx
@@ -1,5 +1,6 @@
"use client";
+import { useState } from "react";
import { Check, Loader2, Wrench } from "lucide-react";
import { Button } from "@/src/components/ui/button";
import { cn } from "@/src/utils/tailwind";
@@ -15,21 +16,43 @@ export function InAppAgentToolCallCard({
isCompact = false,
isDisabled = false,
onApproveToolCall,
+ onAlwaysAllowToolCall,
onRejectToolCall,
}: {
tool: InAppAgentToolCallContent;
isCompact?: boolean;
isDisabled?: boolean;
onApproveToolCall?: (approvalId: string) => Promise<void>;
+ onAlwaysAllowToolCall?: (approvalId: string) => Promise<void>;
onRejectToolCall?: (approvalId: string) => Promise<void>;
}) {
+ const [activeDecision, setActiveDecision] = useState<
+ "once" | "conversation" | "reject" | null
+ >(null);
const approval = tool.approval;
const isApprovalPending = approval?.status === "pending";
const isApprovalSubmitting = approval?.status === "submitting";
+ const isDecisionSubmitting = isApprovalSubmitting || activeDecision !== null;
const displayName = getInAppAgentToolDisplayName(tool.name);
const approveLabel = `Approve ${displayName}?`;
const usedLabel = `Used ${displayName}`;
+ const decide = async (
+ decision: NonNullable<typeof activeDecision>,
+ handler: ((approvalId: string) => Promise<void>) | undefined,
+ ) => {
+ if (!approval || !isApprovalPending || isDecisionSubmitting || !handler) {
+ return;
+ }
+
+ setActiveDecision(decision);
+ try {
+ await handler(approval.id);
+ } finally {
+ setActiveDecision(null);
+ }
+ };
+
return (
<div
className={cn(
@@ -63,38 +86,66 @@ export function InAppAgentToolCallCard({
variant="outline-success"
className="h-7"
disabled={
- isDisabled || isApprovalSubmitting || !onApproveToolCall
+ isDisabled || isDecisionSubmitting || !onApproveToolCall
}
+ aria-busy={activeDecision === "once"}
onClick={() => {
- if (isApprovalPending) {
- onApproveToolCall?.(approval.id).catch(() => undefined);
- }
+ decide("once", onApproveToolCall).catch(() => undefined);
}}
>
- {isApprovalSubmitting ? (
+ {activeDecision === "once" ? (
<Loader2 className="mr-1 size-3 animate-spin" />
) : (
<Check className="mr-1 size-3" />
)}
- Confirm
+ Approve
</Button>
+ {onAlwaysAllowToolCall ? (
+ <Button
+ type="button"
+ size="sm"
+ variant="outline"
+ className="h-7"
+ title={`Always approve ${displayName} for this conversation`}
+ aria-label="Always approve for this conversation"
+ disabled={isDisabled || isDecisionSubmitting}
+ aria-busy={activeDecision === "conversation"}
+ onClick={() => {
+ decide("conversation", onAlwaysAllowToolCall).catch(
+ () => undefined,
+ );
+ }}
+ >
+ {activeDecision === "conversation" ? (
+ <Loader2 className="mr-1 size-3 animate-spin" />
+ ) : null}
+ Always approve*
+ </Button>
+ ) : null}
<Button
type="button"
size="sm"
variant="outline"
className="h-7"
disabled={
- isDisabled || isApprovalSubmitting || !onRejectToolCall
+ isDisabled || isDecisionSubmitting || !onRejectToolCall
}
+ aria-busy={activeDecision === "reject"}
onClick={() => {
- if (isApprovalPending) {
- onRejectToolCall?.(approval.id).catch(() => undefined);
- }
+ decide("reject", onRejectToolCall).catch(() => undefined);
}}
>
- Reject
+ {activeDecision === "reject" ? (
+ <Loader2 className="mr-1 size-3 animate-spin" />
+ ) : null}
+ Decline
</Button>
</div>
+ {onAlwaysAllowToolCall ? (
+ <p className="text-muted-foreground text-xs">
+ * Approves every use of this tool in this conversation.
+ </p>
+ ) : null}
</div>
</div>
) : (
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
index f530241f54..3f82ab7a58 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.stories.tsx
@@ -1,16 +1,22 @@
import preview from "../../../../.storybook/preview";
import { type ReactNode, useEffect, useRef, useState } from "react";
import { expect, fn, userEvent, waitFor, within } from "storybook/test";
+import type { AgUiMessage } from "@langfuse/shared/in-app-agent";
import {
InAppAgentWindow,
type InAppAgentWindowMessage,
type InAppAgentWindowProps,
} from "./InAppAgentWindow";
import { getInAppAgentQuickActionContext } from "@/src/features/in-app-agent/quickActions";
+import {
+ createInAppAgentDisplayState,
+ projectInAppAgentMessagesForDisplay,
+} from "@/src/features/in-app-agent/lib/display";
import {
InAppAgentWindowShell,
useInAppAgentWindowShellPanelControl,
} from "./InAppAgentWindowShell";
+import { getDrawerMessages } from "./utils/utils";
function InAppAgentWindowStoryShell({
children,
@@ -601,6 +607,7 @@ const meta = preview.meta({
onOpenConversationHistory: fn(),
onNewConversation: fn(),
onApproveToolCall: fn(),
+ onAlwaysAllowToolCall: fn(),
onRejectToolCall: fn(),
onSelectConversation: fn(),
onClose: fn(),
@@ -1153,9 +1160,16 @@ export const RateLimited = meta.story({
canvas.getByRole("textbox", { name: "Message the assistant" }),
).toBeEnabled();
await expect(
- canvas.getByRole("button", { name: "Confirm" }),
+ canvas.getByRole("button", { name: "Approve" }),
+ ).toBeDisabled();
+ await expect(
+ canvas.getByRole("button", {
+ name: "Always approve for this conversation",
+ }),
+ ).toBeDisabled();
+ await expect(
+ canvas.getByRole("button", { name: "Decline" }),
).toBeDisabled();
- await expect(canvas.getByRole("button", { name: "Reject" })).toBeDisabled();
await expect(
canvas.getByRole("button", { name: "Start new conversation" }),
).toBeDisabled();
@@ -1375,3 +1389,142 @@ export const ProjectedMessageSubmitsFeedbackToSource = meta.story({
});
},
});
+
+export const AlwaysApprovesWithHiddenParallelCall = meta.story({
+ name: "(Test) Always Approves With Hidden Parallel Call",
+ args: {
+ selectedConversationId: "conversation-1",
+ isAssistantTurnInProgress: true,
+ onAlwaysAllowToolCall: fn(() => new Promise<void>(() => undefined)),
+ messages: getDrawerMessages({
+ error: null,
+ isRunning: false,
+ messages: [
+ {
+ id: "assistant-approval",
+ role: "assistant",
+ content: "I need approval before creating these resources.",
+ toolCalls: [
+ {
+ id: "approval-1",
+ type: "function",
+ function: {
+ name: "langfuse_createTextPrompt",
+ arguments: '{"name":"approved-prompt"}',
+ },
+ },
+ {
+ id: "deferred-sibling",
+ type: "function",
+ function: {
+ name: "langfuse_createDashboardWidget",
+ arguments: '{"name":"deferred-widget"}',
+ },
+ },
+ ],
+ },
+ ],
+ pendingToolApprovals: [
+ {
+ id: "approval-1",
+ status: "pending",
+ runId: "run-1",
+ approvalRequest: {
+ type: "tool_approval_request",
+ toolCallId: "approval-1",
+ toolName: "langfuse_createTextPrompt",
+ runId: "run-1",
+ },
+ },
+ ],
+ }),
+ },
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const approve = canvas.getByRole("button", { name: "Approve" });
+ const alwaysApprove = canvas.getByRole("button", {
+ name: "Always approve for this conversation",
+ });
+ const decline = canvas.getByRole("button", { name: "Decline" });
+
+ await expect(
+ canvas.queryByLabelText(/^createDashboardWidget:/),
+ ).not.toBeInTheDocument();
+ await userEvent.click(alwaysApprove);
+
+ await expect(args.onAlwaysAllowToolCall).toHaveBeenCalledOnce();
+ await expect(args.onAlwaysAllowToolCall).toHaveBeenCalledWith("approval-1");
+ await expect(alwaysApprove).toHaveAttribute("aria-busy", "true");
+ await expect(approve).toBeDisabled();
+ await expect(alwaysApprove).toBeDisabled();
+ await expect(decline).toBeDisabled();
+ },
+});
+
+export const ContinuedToolResultRendersOnce = meta.story({
+ name: "(Test) Continued Tool Result Renders Once",
+ args: {
+ selectedConversationId: "conversation-1",
+ isAssistantTurnInProgress: false,
+ messages: getDrawerMessages({
+ error: null,
+ isRunning: false,
+ messages: projectInAppAgentMessagesForDisplay(
+ [
+ {
+ id: "assistant-proposal",
+ role: "assistant",
+ content: "",
+ toolCalls: [
+ {
+ id: "tool-call-1",
+ type: "function",
+ function: {
+ name: "langfuse_createDashboardWidget",
+ arguments: '{"name":"Cost over time"}',
+ },
+ },
+ ],
+ },
+ {
+ id: "tool-call-1-approval-tool-call",
+ role: "assistant",
+ content: "",
+ toolCalls: [
+ {
+ id: "tool-call-1",
+ type: "function",
+ function: {
+ name: "langfuse_createDashboardWidget",
+ arguments: '{"name":"Changed by continuation"}',
+ },
+ },
+ ],
+ },
+ {
+ id: "tool-call-1-approval-tool-result",
+ role: "tool",
+ toolCallId: "tool-call-1",
+ content: '{"id":"widget-1"}',
+ },
+ ] satisfies AgUiMessage[],
+ createInAppAgentDisplayState(),
+ ),
+ }),
+ },
+ play: async ({ canvasElement }: { canvasElement: HTMLElement }) => {
+ const canvas = within(canvasElement);
+
+ await expect(canvas.queryByText("Called 2 tools")).not.toBeInTheDocument();
+ await expect(
+ canvas.getAllByLabelText("createDashboardWidget: succeeded"),
+ ).toHaveLength(1);
+ await userEvent.click(
+ canvas.getByLabelText("createDashboardWidget: succeeded"),
+ );
+ await expect(canvas.getByText(/Cost over time/)).toBeInTheDocument();
+ await expect(
+ canvas.queryByText(/Changed by continuation/),
+ ).not.toBeInTheDocument();
+ },
+});
diff --git a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
index 4e4b3c6005..585f16e686 100644
--- a/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
+++ b/web/src/features/in-app-agent/components/InAppAgentWindow.tsx
@@ -281,6 +281,7 @@ export type InAppAgentWindowProps = {
onLoadMoreConversations: () => void;
onNewConversation: () => void;
onApproveToolCall: (approvalId: string) => Promise<void>;
+ onAlwaysAllowToolCall?: (approvalId: string) => Promise<void>;
onRejectToolCall: (approvalId: string) => Promise<void>;
onOpenConversationHistory: () => void;
onSelectConversation: (conversationId: string) => void;
@@ -383,6 +384,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
onLoadMoreConversations,
onNewConversation,
onApproveToolCall,
+ onAlwaysAllowToolCall,
onRejectToolCall,
onOpenConversationHistory,
onSelectConversation,
@@ -853,6 +855,7 @@ export function InAppAgentWindow(props: InAppAgentWindowProps) {
isRateLimited || disablePendingToolApprovalActions
}
onApproveToolCall={onApproveToolCall}
+ onAlwaysAllowToolCall={onAlwaysAllowToolCall}
onRejectToolCall={onRejectToolCall}
/>
))}
diff --git a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
index 8f2880bbbf..8d7efb80ac 100644
--- a/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
+++ b/web/src/features/in-app-agent/components/InAppAiAgentProvider.tsx
@@ -203,6 +203,7 @@ type InAppAiAgentContextType = {
options?: InAppAgentSubmitOptions,
) => Promise<boolean>;
approveToolCall: (approvalId: string) => Promise<void>;
+ alwaysAllowToolCall?: (approvalId: string) => Promise<void>;
rejectToolCall: (approvalId: string) => Promise<void>;
submitFeedback: (params: {
messageId: string;
@@ -790,6 +791,7 @@ function InAppAiAgentProviderInner({
runId: input.runId,
toolCallId: input.toolCallId,
approved: input.approved,
+ approvalScope: input.approvalScope,
}),
onHydratedSnapshot: ({ messages }) => {
performToolSideEffectsForCompletedToolCalls({
@@ -1227,6 +1229,7 @@ function InAppAiAgentProviderInner({
async (params: {
approval: InAppAgentPendingToolApproval;
approved: boolean;
+ approvalScope: "once" | "conversation";
conversationId: string;
}) => {
const runId =
@@ -1250,6 +1253,7 @@ function InAppAiAgentProviderInner({
runId,
toolCallId: params.approval.approvalRequest.toolCallId,
approved: params.approved,
+ approvalScope: params.approvalScope,
});
return true;
} catch (error) {
@@ -1261,7 +1265,11 @@ function InAppAiAgentProviderInner({
);
const resumeToolApproval = useCallback(
- async (approvalId: string, approved: boolean) => {
+ async (
+ approvalId: string,
+ approved: boolean,
+ approvalScope: "once" | "conversation" = "once",
+ ) => {
if (selectedConversationIsWriteLocked) {
setError({
type: "generic",
@@ -1286,12 +1294,14 @@ function InAppAiAgentProviderInner({
const decisionAccepted = await decideBackgroundToolApproval({
approval,
approved,
+ approvalScope,
conversationId: selectedConversationId,
});
if (decisionAccepted) {
capture("in_app_agent:tool_approval_decided", {
isApproved: approved,
toolName: approval.approvalRequest.toolName,
+ approvalScope,
});
}
},
@@ -1320,6 +1330,12 @@ function InAppAiAgentProviderInner({
[resumeToolApproval],
);
+ const alwaysAllowToolCall = useCallback(
+ (approvalId: string) =>
+ resumeToolApproval(approvalId, true, "conversation"),
+ [resumeToolApproval],
+ );
+
const rejectToolCall = useCallback(
(approvalId: string) => resumeToolApproval(approvalId, false),
[resumeToolApproval],
@@ -1354,11 +1370,13 @@ function InAppAiAgentProviderInner({
deleteConversation,
submit,
approveToolCall,
+ alwaysAllowToolCall,
rejectToolCall,
submitFeedback,
}),
[
approveToolCall,
+ alwaysAllowToolCall,
isExpanded,
conversations,
effectiveError,
diff --git a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts b/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
index 7e5490f7e4..5602e54b1c 100644
--- a/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
+++ b/web/src/features/in-app-agent/components/utils/utils.clienttest.ts
@@ -308,46 +308,6 @@ describe("getDrawerMessages", () => {
]);
});
- it.each([
- { error: null, isRunning: false, scenario: "the run stops" },
- {
- error: "The run was interrupted before the tool returned.",
- isRunning: true,
- scenario: "the run errors",
- },
- ])(
- "marks result-less tools failed when $scenario",
- ({ error, isRunning }) => {
- const mappedMessages = getDrawerMessages({
- error,
- isRunning,
- messages: [
- {
- id: "assistant-1",
- role: "assistant",
- content: "",
- toolCalls: [
- {
- id: "tool-call-1",
- type: "function",
- function: { name: "interrupted-tool", arguments: "{}" },
- },
- ],
- },
- ] satisfies AgUiMessage[],
- });
-
- expect(mappedMessages).toMatchObject([
- {
- content: {
- type: "toolGroup",
- tools: [{ name: "interrupted-tool", status: "failed" }],
- },
- },
- ]);
- },
- );
-
it("attaches docs sources to the answer after a search preamble", () => {
const docsResult = JSON.stringify({
_meta: {
diff --git a/web/src/features/in-app-agent/components/utils/utils.ts b/web/src/features/in-app-agent/components/utils/utils.ts
index c1ca7ea343..4d084bd321 100644
--- a/web/src/features/in-app-agent/components/utils/utils.ts
+++ b/web/src/features/in-app-agent/components/utils/utils.ts
@@ -399,6 +399,11 @@ export function getDrawerMessages({
} else {
resultState = "incomplete";
}
+
+ if (resultState === "incomplete" && !pendingApproval) {
+ return [];
+ }
+
const status = TOOL_CALL_STATUS_BY_RESULT_STATE[resultState];
return [
diff --git a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts b/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
index d9d13a4c3c..954f21fe54 100644
--- a/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
+++ b/web/src/features/in-app-agent/lib/backgroundExecutionSession.ts
@@ -21,6 +21,7 @@ export type ApprovalDecision = {
runId: string;
toolCallId: string;
approved: boolean;
+ approvalScope?: "once" | "conversation";
};
export type BackgroundExecutionRunView = {
diff --git a/web/src/features/in-app-agent/lib/display.ts b/web/src/features/in-app-agent/lib/display.ts
index 3a9fec35de..4f0a2c7d09 100644
--- a/web/src/features/in-app-agent/lib/display.ts
+++ b/web/src/features/in-app-agent/lib/display.ts
@@ -45,6 +45,10 @@ type InAppAgentDisplayMessage = AgUiMessage & {
feedbackMessageId?: string;
};
+type InAppAgentToolCall = NonNullable<
+ Extract<AgUiMessage, { role: "assistant" }>["toolCalls"]
+>[number];
+
const InAppAgentDisplayPlacementSchema = z.object({
anchorMessageId: z.string(),
order: z.number(),
@@ -271,6 +275,21 @@ export function projectInAppAgentMessagesForDisplay(
): InAppAgentDisplayMessage[] {
// Canonical messages stay untouched for persistence and subsequent runs.
const messageIds = new Set(messages.map((message) => message.id));
+ const firstToolCallMessageIds = new Map<string, string>();
+ for (const message of messages) {
+ if (message.role !== "assistant") {
+ continue;
+ }
+
+ for (const toolCall of message.toolCalls ?? []) {
+ if (
+ toolCall.function.name !== IN_APP_AGENT_REDIRECT_TOOL_NAME &&
+ !firstToolCallMessageIds.has(toolCall.id)
+ ) {
+ firstToolCallMessageIds.set(toolCall.id, message.id);
+ }
+ }
+ }
const placementsByAnchor = new Map<
string,
Array<{ order: number; message: InAppAgentDisplayMessage }>
@@ -302,7 +321,8 @@ export function projectInAppAgentMessagesForDisplay(
const placement = state.toolCallPlacements[toolCall.id];
if (
!placement ||
- toolCall.function.name === IN_APP_AGENT_REDIRECT_TOOL_NAME
+ toolCall.function.name === IN_APP_AGENT_REDIRECT_TOOL_NAME ||
+ firstToolCallMessageIds.get(toolCall.id) !== message.id
) {
continue;
}
@@ -340,6 +360,13 @@ export function projectInAppAgentMessagesForDisplay(
}
}
+ const isDuplicateToolCallForMessage = (
+ toolCall: InAppAgentToolCall,
+ messageId: string,
+ ) =>
+ toolCall.function.name !== IN_APP_AGENT_REDIRECT_TOOL_NAME &&
+ firstToolCallMessageIds.get(toolCall.id) !== messageId;
+
return messages.flatMap<InAppAgentDisplayMessage>((message) => {
const projectedMessage =
message.role === "assistant"
@@ -349,22 +376,37 @@ export function projectInAppAgentMessagesForDisplay(
state.textByMessageId[message.id]?.nativeContent ??
message.content,
toolCalls: message.toolCalls?.filter((toolCall) => {
+ if (isDuplicateToolCallForMessage(toolCall, message.id)) {
+ return false;
+ }
+
+ if (toolCall.function.name === IN_APP_AGENT_REDIRECT_TOOL_NAME) {
+ return true;
+ }
+
const placement = state.toolCallPlacements[toolCall.id];
- return (
- toolCall.function.name === IN_APP_AGENT_REDIRECT_TOOL_NAME ||
- !placement ||
- !messageIds.has(placement.anchorMessageId)
- );
+ return !placement || !messageIds.has(placement.anchorMessageId);
}),
}
: message;
+ const isEmptyDuplicateToolCallMessage =
+ message.role === "assistant" &&
+ projectedMessage.role === "assistant" &&
+ !projectedMessage.content?.trim() &&
+ Boolean(message.toolCalls?.length) &&
+ message.toolCalls?.every((toolCall) =>
+ isDuplicateToolCallForMessage(toolCall, message.id),
+ );
+ const projectedMessages = isEmptyDuplicateToolCallMessage
+ ? []
+ : [projectedMessage];
const placements = placementsByAnchor.get(message.id);
if (!placements) {
- return [projectedMessage];
+ return projectedMessages;
}
return [
- projectedMessage,
+ ...projectedMessages,
...placements
.sort((left, right) => left.order - right.order)
.map(({ message: placedMessage }) => placedMessage),
diff --git a/web/src/features/in-app-agent/server/backgroundRunService.ts b/web/src/features/in-app-agent/server/backgroundRunService.ts
index 144a5e35ee..b9d9542f08 100644
--- a/web/src/features/in-app-agent/server/backgroundRunService.ts
+++ b/web/src/features/in-app-agent/server/backgroundRunService.ts
@@ -23,6 +23,7 @@ import {
type AgUiRunAgentInput,
} from "@langfuse/shared/in-app-agent";
import { parseInAppAgentInterruptEvent } from "@langfuse/shared/in-app-agent/server/human-in-the-loop";
+import { getInAppAgentPrefixedToolName } from "@langfuse/shared/in-app-agent/server/tools";
import {
ensureOwnedConversation,
getConversationEvents,
@@ -339,6 +340,7 @@ export async function decideBackgroundApproval(params: {
runId: string;
toolCallId: string;
approved: boolean;
+ approvalScope?: "once" | "conversation";
userId: string;
model: string | undefined;
}) {
@@ -364,17 +366,29 @@ export async function decideBackgroundApproval(params: {
);
}
- const approvalRequest = events.find(
- (persisted) =>
- persisted.runId === params.runId &&
- parseInAppAgentInterruptEvent(persisted.event)?.toolCallId ===
- params.toolCallId,
- );
+ let approvalRequest: ReturnType<typeof parseInAppAgentInterruptEvent>;
+ for (const persisted of events) {
+ if (persisted.runId !== params.runId) {
+ continue;
+ }
+
+ const parsedRequest = parseInAppAgentInterruptEvent(persisted.event);
+ if (parsedRequest?.toolCallId === params.toolCallId) {
+ approvalRequest = parsedRequest;
+ break;
+ }
+ }
if (!approvalRequest) {
throw new LangfuseNotFoundError("Approval request not found");
}
+ // Resolve the granted tool from the persisted interrupt, never client input.
+ const alwaysAllowToolName =
+ params.approvalScope === "conversation" && params.approved
+ ? getInAppAgentPrefixedToolName(approvalRequest.toolName)
+ : undefined;
+
const continuationRun = await decideToolApproval({
prisma: params.prisma,
projectId: params.projectId,
@@ -383,6 +397,7 @@ export async function decideBackgroundApproval(params: {
continuationRunId: createInAppAgentRunId(),
toolCallId: params.toolCallId,
approved: params.approved,
+ alwaysAllowToolName,
decidedByUserId: params.userId,
model: params.model,
});
diff --git a/web/src/features/in-app-agent/server/router.ts b/web/src/features/in-app-agent/server/router.ts
index 19ddd0ec32..cd86dc43a7 100644
--- a/web/src/features/in-app-agent/server/router.ts
+++ b/web/src/features/in-app-agent/server/router.ts
@@ -75,6 +75,11 @@ const DecideToolApprovalInput = ConversationIdInput.extend({
runId: z.string(),
toolCallId: z.string(),
approved: z.boolean(),
+ // "conversation" also approves this call; it never means approve-without-run.
+ approvalScope: z.enum(["once", "conversation"]).default("once"),
+}).refine((input) => input.approved || input.approvalScope === "once", {
+ message: "A rejection cannot grant a tool",
+ path: ["approvalScope"],
});
const SubmitFeedbackInput = ConversationIdInput.extend({
@@ -260,6 +265,7 @@ export const inAppAgentRouter = createTRPCRouter({
runId: input.runId,
toolCallId: input.toolCallId,
approved: input.approved,
+ approvalScope: input.approvalScope,
userId: ctx.session.user.id,
model: env.LANGFUSE_AWS_BEDROCK_MODEL,
});
diff --git a/web/src/features/mcp/server/registry.ts b/web/src/features/mcp/server/registry.ts
index adbe3a061a..16669b2967 100644
--- a/web/src/features/mcp/server/registry.ts
+++ b/web/src/features/mcp/server/registry.ts
@@ -176,8 +176,13 @@ class ToolRegistry {
return tool.definition.annotations?.readOnlyHint === true;
}
- if (context.inAppAgent.permissions === "single-tool-override") {
- return context.inAppAgent.allowedToolName === tool.definition.name;
+ if (context.inAppAgent.permissions === "tool-allowlist") {
+ return (
+ tool.definition.annotations?.readOnlyHint === true ||
+ context.inAppAgent.allowedToolNames.some(
+ (allowedToolName) => allowedToolName === tool.definition.name,
+ )
+ );
}
return false;
diff --git a/web/src/features/mcp/types.ts b/web/src/features/mcp/types.ts
index 3e13e3dae8..9694069d49 100644
--- a/web/src/features/mcp/types.ts
+++ b/web/src/features/mcp/types.ts
@@ -65,16 +65,12 @@ export interface ServerContext {
inAppAgent?: InAppAgentContext;
}
-/**
- * In-app-agent MCP access is explicit: `read` allows only tools annotated with `readOnlyHint`.
- * To allow mutating operations, `single-tool-override` can include a specific MCP registry tool name that the in-app agent is allowed to invoke.
- * Human approval is enforced earlier in the in-app agent runtime before any override is minted.
- */
+/** In-app agent MCP access is read-only unless a prior approval mints a mutating-tool allowlist. */
export type InAppAgentContext =
| {
permissions: "read";
}
| {
- permissions: "single-tool-override";
- allowedToolName: McpToolName;
+ permissions: "tool-allowlist";
+ allowedToolNames: McpToolName[];
};
diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.ts
index 1ea3c8d826..9252579fdf 100644
--- a/web/src/pages/api/public/mcp/index.ts
+++ b/web/src/pages/api/public/mcp/index.ts
@@ -210,8 +210,8 @@ export function getInAppAgentContext(
return parsedOverride.success
? {
- permissions: "single-tool-override",
- allowedToolName: parsedOverride.data.toolName,
+ permissions: "tool-allowlist",
+ allowedToolNames: parsedOverride.data.toolNames,
}
: { permissions: "read" };
}
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
index 3e2f69ec57..ea29795b1d 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.test.ts
@@ -34,6 +34,13 @@ type AgentScenario = (ctx: {
};
signal: AbortSignal;
options: {
+ langfuseMcp: {
+ toolPolicy: {
+ available: ReadonlySet<string>;
+ autoApproved: ReadonlySet<string>;
+ };
+ runOverride?: string;
+ };
onEvent: (event: unknown) => Promise<void> | void;
onApprovedToolCallExecuted?: () => Promise<void> | void;
onComplete: () => Promise<void>;
@@ -141,6 +148,7 @@ async function seedBackgroundRun(opts?: {
request?: unknown;
status?: string;
aiFeaturesEnabled?: boolean;
+ alwaysAllowedTools?: string[];
}) {
const { projectId } = await createOrgProjectAndApiKey();
const project = await prisma.project.findUniqueOrThrow({
@@ -165,6 +173,7 @@ async function seedBackgroundRun(opts?: {
projectId,
createdByUserId: user.id,
title: "background test",
+ alwaysAllowedTools: opts?.alwaysAllowedTools,
},
});
const run = await prisma.inAppAgentRun.create({
@@ -193,8 +202,11 @@ const getInAppAgentApiKeys = (projectId: string) =>
/** A run resuming an approved tool call whose interrupt event is persisted on a parked parent run. */
async function seedApprovedContinuation(opts?: {
context?: Array<{ description: string; value: string }>;
+ alwaysAllowedTools?: string[];
}) {
- const seeded = await seedBackgroundRun();
+ const seeded = await seedBackgroundRun({
+ alwaysAllowedTools: opts?.alwaysAllowedTools,
+ });
const { projectId, conversation, run, user } = seeded;
const parentRun = await prisma.inAppAgentRun.create({
data: {
@@ -265,6 +277,26 @@ describe("executeInAppAgentRun", () => {
expect((await getRun(projectId, run.id)).status).toBe("SUCCEEDED");
});
+ it("applies persisted grants to the next continuation's MCP policy", async () => {
+ const { projectId, run } = await seedApprovedContinuation({
+ alwaysAllowedTools: ["langfuse_upsertDataset"],
+ });
+
+ scenarioRef.current = async ({ options }) => {
+ expect(options.langfuseMcp.toolPolicy.autoApproved).toContain(
+ "upsertDataset",
+ );
+ expect(JSON.parse(options.langfuseMcp.runOverride ?? "")).toEqual({
+ toolName: "createTextPrompt",
+ toolNames: ["createTextPrompt", "upsertDataset"],
+ });
+ await options.onComplete();
+ await options.onFinish();
+ };
+
+ await executeInAppAgentRun({ projectId, runId: run.id });
+ });
+
it("executes a queued run to SUCCEEDED with persisted events and full MCP-key lifecycle", async () => {
const { projectId, conversation, run } = await seedBackgroundRun();
diff --git a/worker/src/features/in-app-agent/executeInAppAgentRun.ts b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
index e7e4e78057..823ea21c1a 100644
--- a/worker/src/features/in-app-agent/executeInAppAgentRun.ts
+++ b/worker/src/features/in-app-agent/executeInAppAgentRun.ts
@@ -37,7 +37,9 @@ import {
heartbeatClaimedRun,
IN_APP_AGENT_HEARTBEAT_INTERVAL_MS,
isInAppAgentConversationWriteLocked,
- isMcpToolName,
+ createInAppAgentToolPolicy,
+ getInAppAgentMcpAllowedToolNames,
+ getInAppAgentRegistryToolName,
maybeInferAndPersistConversationTitle,
parseInAppAgentInterruptEvent,
shouldFlushPersistedEvent,
@@ -277,14 +279,29 @@ export async function executeInAppAgentRun(params: {
isApprovedContinuation =
request.kind === "approvalDecision" && request.approved;
const approvedRegistryToolName = isApprovedContinuation
- ? getRegistryToolName(approvalRequest?.toolName)
- : undefined;
- const runOverride = approvedRegistryToolName
- ? await createInAppAgentMcpRunOverride({
- toolName: approvedRegistryToolName,
- })
+ ? getInAppAgentRegistryToolName(approvalRequest?.toolName)
: undefined;
+ const userAccess: InAppAgentUserAccess = {
+ projectRole: access.projectRole,
+ isAdmin: access.isAdmin,
+ };
+
+ // Rebuild each run so grants invalidated by role changes drop out.
+ const toolPolicy = createInAppAgentToolPolicy({
+ userAccess,
+ alwaysAllowedTools: conversation.alwaysAllowedTools,
+ });
+
+ const allowedToolNames = getInAppAgentMcpAllowedToolNames(
+ toolPolicy,
+ approvedRegistryToolName,
+ );
+ const runOverride =
+ allowedToolNames.length > 0
+ ? await createInAppAgentMcpRunOverride({ toolNames: allowedToolNames })
+ : undefined;
+
// ---- Sandbox (session created/resumed lazily per tool call). ----
const sandboxProviderType = getDefaultInAppAgentSandboxProviderType();
const sandboxProvider =
@@ -377,11 +394,6 @@ export async function executeInAppAgentRun(params: {
let interruptRequest: InAppAgentToolApprovalRequest | undefined;
- const userAccess: InAppAgentUserAccess = {
- projectRole: access.projectRole,
- isAdmin: access.isAdmin,
- };
-
const stream = await createAgUiStream({
input: agentInput,
signal: abortController.signal,
@@ -511,7 +523,7 @@ export async function executeInAppAgentRun(params: {
url: getLangfuseMcpUrl(),
publicKey: mcpApiKey.publicKey,
secretKey: mcpApiKey.secretKey,
- userAccess,
+ toolPolicy,
runOverride,
},
redirectAction: {
@@ -642,16 +654,6 @@ function findPersistedApprovalRequest(
return undefined;
}
-function getRegistryToolName(toolName: string | undefined) {
- if (!toolName?.startsWith("langfuse_")) {
- return undefined;
- }
-
- const registryToolName = toolName.slice("langfuse_".length);
-
- return isMcpToolName(registryToolName) ? registryToolName : undefined;
-}
-
function getLangfuseMcpUrl(): string {
if (!env.NEXTAUTH_URL) {
throw new InAppAgentRunInitError(