-
Notifications
You must be signed in to change notification settings - Fork 219
host-mcp: shared browser-approval primitives + cloud browser e2e #1014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // Browser approval of a gated MCP action, end to end through the real console. | ||
| // | ||
| // A `require_approval` policy turns a built-in tool into an action that pauses | ||
| // for a human. The MCP session runs in `elicitation_mode=browser`, so the gated | ||
| // `execute` does not let the model resume inline — it pauses and hands back an | ||
| // `approvalUrl`. A real browser (signed in as the same identity) opens that | ||
| // console page and clicks Approve / Decline; meanwhile `resume` long-polls for | ||
| // the decision. Approve lets the tool run and return its result; Decline blocks | ||
| // it. This is the leg unit tests structurally cannot cover: a human clicking the | ||
| // button in the rendered ResumeApprovalPage. | ||
| // | ||
| // The policy is removed in an `ensuring` finalizer — a leaked require_approval | ||
| // gate on a shared built-in tool would pause unrelated scenarios. | ||
| // | ||
| // Lives under cloud/ for now because cloud is the only host wired for browser | ||
| // approval; it moves to scenarios/ (cross-target) as self-host and Cloudflare | ||
| // gain the feature. | ||
| import { expect } from "@effect/vitest"; | ||
| import { Effect } from "effect"; | ||
| import { composePluginApi } from "@executor-js/api/server"; | ||
|
|
||
| import { scenario } from "../src/scenario"; | ||
| import { Api, Browser, Mcp, Target } from "../src/services"; | ||
| import { type McpBrowserApproval, parseBrowserApproval } from "../src/surfaces/mcp"; | ||
| import type { BrowserSurface } from "../src/surfaces/browser"; | ||
| import type { Identity } from "../src/target"; | ||
|
|
||
| const coreApi = composePluginApi([] as const); | ||
|
|
||
| // Gating a built-in read tool keeps the scenario hermetic — no external server | ||
| // to host a destructive tool. The gate, not the tool, is what's under test: any | ||
| // action the engine pauses on flows through the same approval path. | ||
| const GATE_TOOL = "executor.coreTools.policies.list"; | ||
|
|
||
| // The gated call returns the policy listing, which includes the policy we just | ||
| // created — so the created policy's id appears in the result iff the tool | ||
| // actually ran (i.e. the human approved). | ||
| const GATED_CODE = ` | ||
| const result = await tools.executor.coreTools.policies.list({}); | ||
| return JSON.stringify(result); | ||
| `; | ||
|
|
||
| /** Open the console approval page as `identity` and click Approve or Decline. */ | ||
| const decideInBrowser = ( | ||
| browser: BrowserSurface, | ||
| identity: Identity, | ||
| approval: McpBrowserApproval, | ||
| decision: "Approve" | "Decline", | ||
| ): Effect.Effect<void> => | ||
| browser.session(identity, async ({ page, step }) => { | ||
| await step( | ||
| `Open the approval page and ${decision.toLowerCase()} the paused action`, | ||
| async () => { | ||
| await page.goto(approval.approvalUrl, { waitUntil: "networkidle" }); | ||
| await page.getByRole("button", { name: decision }).click(); | ||
| // The page confirms the decision was recorded ("Approve sent" / "Decline sent"). | ||
| await page.getByText(`${decision} sent`).waitFor(); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| scenario( | ||
| "MCP · a gated action approved in the browser runs to completion", | ||
| { timeout: 180_000 }, | ||
| Effect.gen(function* () { | ||
| const target = yield* Target; | ||
| const api = yield* Api; | ||
| const browser = yield* Browser; | ||
| const mcp = yield* Mcp; | ||
| const identity = yield* target.newIdentity(); | ||
| const client = yield* api.client(coreApi, identity); | ||
|
|
||
| const policy = yield* client.policies.create({ | ||
| payload: { owner: "org", pattern: GATE_TOOL, action: "require_approval" }, | ||
| }); | ||
|
|
||
| yield* Effect.gen(function* () { | ||
| const session = mcp.session(identity, { elicitationMode: "browser" }); | ||
| const tools = yield* session.listTools(); | ||
| expect(tools).toContain("execute"); | ||
|
|
||
| const paused = yield* session.call("execute", { code: GATED_CODE }); | ||
| const approval = parseBrowserApproval(paused); | ||
| expect(approval.approvalUrl, "approval URL targets the resume page").toContain( | ||
| `/resume/${approval.executionId}`, | ||
| ); | ||
|
|
||
| // `resume` blocks for the human's decision; approve it in the browser | ||
| // concurrently, then the resumed call returns the gated tool's result. | ||
| const [resumed] = yield* Effect.all( | ||
| [ | ||
| session.awaitResume(approval.executionId), | ||
| decideInBrowser(browser, identity, approval, "Approve"), | ||
| ], | ||
| { concurrency: "unbounded" }, | ||
| ); | ||
|
|
||
| expect(resumed.ok, "the approved execution completed without error").toBe(true); | ||
| expect(resumed.text, "the gated tool ran and returned the policy listing").toContain( | ||
| policy.id, | ||
| ); | ||
| }).pipe( | ||
| Effect.ensuring( | ||
| client.policies | ||
| .remove({ params: { policyId: policy.id }, payload: { owner: "org" } }) | ||
| .pipe(Effect.ignore), | ||
| ), | ||
| ); | ||
| }), | ||
| ); | ||
|
|
||
| scenario( | ||
| "MCP · a gated action declined in the browser is blocked", | ||
| { timeout: 180_000 }, | ||
| Effect.gen(function* () { | ||
| const target = yield* Target; | ||
| const api = yield* Api; | ||
| const browser = yield* Browser; | ||
| const mcp = yield* Mcp; | ||
| const identity = yield* target.newIdentity(); | ||
| const client = yield* api.client(coreApi, identity); | ||
|
|
||
| const policy = yield* client.policies.create({ | ||
| payload: { owner: "org", pattern: GATE_TOOL, action: "require_approval" }, | ||
| }); | ||
|
|
||
| yield* Effect.gen(function* () { | ||
| const session = mcp.session(identity, { elicitationMode: "browser" }); | ||
| yield* session.listTools(); | ||
|
|
||
| const paused = yield* session.call("execute", { code: GATED_CODE }); | ||
| const approval = parseBrowserApproval(paused); | ||
|
|
||
| const [resumed] = yield* Effect.all( | ||
| [ | ||
| session.awaitResume(approval.executionId), | ||
| decideInBrowser(browser, identity, approval, "Decline"), | ||
| ], | ||
| { concurrency: "unbounded" }, | ||
| ); | ||
|
|
||
| // The decision propagated (resume returned rather than hanging) and the | ||
| // gated tool never ran — its output (the policy id) is absent. | ||
| expect(resumed.text, "the gated tool did not run after a decline").not.toContain(policy.id); | ||
| }).pipe( | ||
| Effect.ensuring( | ||
| client.policies | ||
| .remove({ params: { policyId: policy.id }, payload: { owner: "org" } }) | ||
| .pipe(Effect.ignore), | ||
| ), | ||
| ); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sessionUrlis built by string-concatenating ontotarget.mcpUrlrather than using URL APIs. Iftarget.mcpUrlever carries an existing query string (e.g. from a configured override likehttp://localhost:3000/mcp?trace=1), the result would be…/mcp?trace=1?elicitation_mode=browser— a URL that no parser will interpret as two separate parameters. UsingURL+searchParamsavoids this silently.