Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions shared/glean/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
closeCallbackServer,
} from "./auth-callback-server.js";
import { handleFindSkills } from "./tools/find-skills.js";
import { handleRunTool, runToolAnnotations } from "./tools/run-tool.js";
import { handleRunTool, runToolAnnotations, shouldAskForToolApproval } from "./tools/run-tool.js";
import { evictStaleSkills } from "./skill-writer.js";
import {
loadServerUrl,
Expand Down Expand Up @@ -174,8 +174,10 @@ function getOAuthProvider(): GleanOAuthClientProvider {
return oauthProvider;
}

function getRemoteClientOpts(): RemoteClientOptions {
const supportsElicitation = !!server.getClientCapabilities()?.elicitation;
async function getRemoteClientOpts(): Promise<RemoteClientOptions> {
const supportsElicitation =
!!server.getClientCapabilities()?.elicitation &&
(await shouldAskForToolApproval());
return {
authProvider: getOAuthProvider(),
...(supportsElicitation
Expand Down Expand Up @@ -373,7 +375,7 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
try {
remoteClient = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
`tools-list-${process.pid}`,
);
} catch (err) {
Expand Down Expand Up @@ -455,7 +457,7 @@ async function connectWithSignIn(
try {
const client = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
`setup-${process.pid}`,
);
return { ok: true, client };
Expand Down Expand Up @@ -492,7 +494,7 @@ async function connectWithSignIn(
try {
const client = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
`setup-${process.pid}`,
);
// Unexpectedly connected without needing auth — done.
Expand Down Expand Up @@ -534,7 +536,7 @@ async function connectWithSignIn(
try {
const client = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
`setup-${process.pid}`,
);
return { ok: true, client };
Expand Down Expand Up @@ -650,7 +652,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
}
const dispatchCtx: DispatchContext = {
serverUrl,
remoteClientOpts: getRemoteClientOpts(),
remoteClientOpts: await getRemoteClientOpts(),
authRedirectText: AUTH_REDIRECT_TO_SETUP_TEXT,
logLine,
};
Expand Down Expand Up @@ -684,7 +686,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
remoteClient = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
sessionId,
);
} catch (err) {
Expand Down Expand Up @@ -748,7 +750,7 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
remoteClient = await createRemoteClient(
serverUrl,
getRemoteClientOpts(),
await getRemoteClientOpts(),
sessionId,
);
} catch (err) {
Expand Down
13 changes: 10 additions & 3 deletions shared/glean/mcp/src/tools/run-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ async function currentPermissionMode(): Promise<string | null> {
}
}

export async function shouldAskForToolApproval(): Promise<boolean> {
return process.env.ENABLE_HITL === "true" &&
(await currentPermissionMode()) !== "bypassPermissions";
}

function humanizeMs(ms: number): string {
const seconds = Math.round(ms / 1000);
if (seconds < 120) return `${seconds}s`;
Expand Down Expand Up @@ -482,9 +487,11 @@ export async function handleRunTool(
}

const remoteArgs = buildRemoteArgs(serverId, toolName, resolvedArgs);
// Read-only tools are exempt from Glean's configurable approval requirements,
// so they do not need an approval lookup.
if (isKnownReadOnlyTool(toolMetadata, serverId, toolName)) {
// Opt-outs skip the entire approval flow, without reading or saving preferences.
if (
!(await shouldAskForToolApproval()) ||
isKnownReadOnlyTool(toolMetadata, serverId, toolName)
) {
return callRemoteTool(remoteClient, "run_tool", remoteArgs);
}

Expand Down
23 changes: 23 additions & 0 deletions shared/glean/mcp/tests/run-tool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
buildRemoteArgs,
FileArgsError,
getToolApproval,
shouldAskForToolApproval,
handleRunTool,
runToolAnnotations,
elicitationFailureText,
Expand Down Expand Up @@ -380,6 +381,28 @@ describe("handleRunTool (HITL)", () => {
vi.unstubAllEnvs();
});

it.each([
{ hitl: "false", mode: "default", expected: false },
{ hitl: "false", mode: "bypassPermissions", expected: false },
{ hitl: undefined, mode: "default", expected: false },
{ hitl: "true", mode: "bypassPermissions", expected: false },
{ hitl: "true", mode: "default", expected: true },
{ hitl: "true", mode: undefined, expected: true },
])("approval guard: HITL=$hitl, mode=$mode", async ({ hitl, mode, expected }) => {
vi.stubEnv("ENABLE_HITL", hitl);
vi.stubEnv("CLAUDE_PLUGIN_DATA", tmpDir);
vi.stubEnv("GLEAN_SESSION_ID", "guard-session");
if (mode) await writeModeMarker(tmpDir, "guard-session", mode);
await expect(shouldAskForToolApproval()).resolves.toBe(expected);
if (!expected) {
const remote = makeRemote();
const server = makeServer({ elicitation: true });
await handleRunTool(remote, server, tmpDir, baseArgs, ALL_ON);
expect(server.elicitInput).not.toHaveBeenCalled();
expect(remote.callTool.mock.calls.map(([request]) => request.name)).toEqual(["run_tool"]);
}
});

it("does not elicit when the client lacks elicitation capability", async () => {
vi.stubEnv("ENABLE_HITL", "true");
const remote = makeRemote();
Expand Down
Loading