|
| 1 | +import type * as acp from "@agentclientprotocol/sdk"; |
| 2 | +import path from "node:path"; |
| 3 | +import {afterEach, beforeEach, expect, it} from "vitest"; |
| 4 | +import {ApprovalOptionId} from "../../../ApprovalOptionId"; |
| 5 | +import { |
| 6 | + createAuthenticatedFixture, |
| 7 | + createPermissionResponse, |
| 8 | + describeE2E, |
| 9 | + expectEndTurn, |
| 10 | + type PermissionResponder, |
| 11 | + type SpawnedAgentFixture, |
| 12 | +} from "./acp-e2e-test-utils"; |
| 13 | + |
| 14 | +const MCP_SERVER_NAME = "integration-mcp"; |
| 15 | +const MCP_ECHO_MESSAGE = "mcp approval e2e"; |
| 16 | + |
| 17 | +function createMcpServer(): acp.McpServerStdio { |
| 18 | + return { |
| 19 | + name: MCP_SERVER_NAME, |
| 20 | + command: process.execPath, |
| 21 | + args: [path.join(process.cwd(), "node_modules/mcp-hello-world/build/stdio.js")], |
| 22 | + env: [], |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function isMcpPermissionRequest(request: acp.RequestPermissionRequest): boolean { |
| 27 | + return request.toolCall.kind === "execute" && request._meta?.["is_mcp_tool_approval"] === true; |
| 28 | +} |
| 29 | + |
| 30 | +function createMcpPermissionResponder(optionId: ApprovalOptionId): PermissionResponder { |
| 31 | + return (request) => createPermissionResponse(isMcpPermissionRequest(request) ? optionId : null); |
| 32 | +} |
| 33 | + |
| 34 | +describeE2E("E2E MCP approval tests", () => { |
| 35 | + let fixture: SpawnedAgentFixture; |
| 36 | + let sessionId: string; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + fixture = await createAuthenticatedFixture(); |
| 40 | + sessionId = (await fixture.createSession([createMcpServer()])).sessionId; |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + await fixture.dispose(); |
| 45 | + }); |
| 46 | + |
| 47 | + function expectMcpToolPermissionRequest(): void { |
| 48 | + const requests = fixture.readPermissionRequests(sessionId, "execute"); |
| 49 | + expect(requests.length).toBe(1); |
| 50 | + expect(isMcpPermissionRequest(requests[0]!)).toBe(true); |
| 51 | + } |
| 52 | + |
| 53 | + it("executes an approved MCP tool call", async () => { |
| 54 | + fixture.setPermissionResponder(createMcpPermissionResponder(ApprovalOptionId.AllowOnce)); |
| 55 | + |
| 56 | + await fixture.expectPromptText( |
| 57 | + sessionId, |
| 58 | + `Use the ${MCP_SERVER_NAME} MCP echo tool with message "${MCP_ECHO_MESSAGE}". Reply with exactly the tool result and no extra text.`, |
| 59 | + (text) => expect(text).toContain(`You said: ${MCP_ECHO_MESSAGE}`), |
| 60 | + ); |
| 61 | + expectMcpToolPermissionRequest(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("ends turn when MCP tool call is rejected", async () => { |
| 65 | + fixture.setPermissionResponder(createMcpPermissionResponder(ApprovalOptionId.RejectOnce)); |
| 66 | + |
| 67 | + expectEndTurn(await fixture.connection.prompt({ |
| 68 | + sessionId, |
| 69 | + prompt: [{ |
| 70 | + type: "text", |
| 71 | + text: `Use the ${MCP_SERVER_NAME} MCP echo tool with message "${MCP_ECHO_MESSAGE}". Stop if the tool call is rejected.`, |
| 72 | + }], |
| 73 | + })); |
| 74 | + expectMcpToolPermissionRequest(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments