Skip to content

Commit b144314

Browse files
lu-zeroclaude
andcommitted
test(mcp): assert permission ask payload from tool wrapper
Fixes anomalyco#19549 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 80becbc commit b144314

1 file changed

Lines changed: 99 additions & 2 deletions

File tree

‎packages/opencode/test/session/prompt.test.ts‎

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { Cause, Deferred, Duration, Effect, Exit, Fiber, Layer } from "effect"
1010
import path from "path"
1111
import { fileURLToPath } from "url"
1212
import { NamedError } from "@opencode-ai/core/util/error"
13+
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
14+
import { dynamicTool, jsonSchema } from "ai"
1315
import { Agent as AgentSvc } from "../../src/agent/agent"
1416
import { BackgroundJob } from "@/background/job"
1517
import { Command } from "../../src/command"
@@ -168,7 +170,11 @@ const blockingProcessor = Layer.succeed(
168170
}),
169171
)
170172

171-
function makePrompt(input?: { mcpInstructions?: MCP.ServerInstructions[]; processor?: "blocking" }) {
173+
function makePrompt(input?: {
174+
mcpInstructions?: MCP.ServerInstructions[]
175+
processor?: "blocking"
176+
mcp?: Layer.Layer<MCP.Service>
177+
}) {
172178
const deps = Layer.mergeAll(
173179
Session.defaultLayer,
174180
Snapshot.defaultLayer,
@@ -181,7 +187,7 @@ function makePrompt(input?: { mcpInstructions?: MCP.ServerInstructions[]; proces
181187
Config.defaultLayer,
182188
ProviderSvc.defaultLayer,
183189
lsp,
184-
makeMcp(input?.mcpInstructions),
190+
input?.mcp ?? makeMcp(input?.mcpInstructions),
185191
FSUtil.defaultLayer,
186192
BackgroundJob.defaultLayer,
187193
status,
@@ -2406,3 +2412,94 @@ noLLMServer.instance(
24062412
}),
24072413
30_000,
24082414
)
2415+
2416+
// MCP wrapper permission payload — see packages/opencode/src/session/tools.ts.
2417+
// The wrapper must forward the tool name as the permission key/pattern and the
2418+
// raw tool arguments as metadata so SDK consumers can reason about the call.
2419+
const mcpWithEchoTool = Layer.succeed(
2420+
MCP.Service,
2421+
MCP.Service.of({
2422+
status: () => Effect.succeed({}),
2423+
clients: () => Effect.succeed({}),
2424+
instructions: () => Effect.succeed([]),
2425+
tools: () =>
2426+
Effect.succeed({
2427+
echo_say: dynamicTool({
2428+
description: "echo back the greeting",
2429+
inputSchema: jsonSchema({
2430+
type: "object",
2431+
properties: { greeting: { type: "string" } },
2432+
required: ["greeting"],
2433+
additionalProperties: false,
2434+
}),
2435+
execute: async () => ({ content: [{ type: "text" as const, text: "ok" }] }),
2436+
}),
2437+
}),
2438+
prompts: () => Effect.succeed({}),
2439+
resources: () => Effect.succeed({}),
2440+
resourceTemplates: () => Effect.succeed({}),
2441+
add: () => Effect.succeed({ status: { status: "disabled" as const } }),
2442+
connect: () => Effect.void,
2443+
disconnect: () => Effect.void,
2444+
getPrompt: () => Effect.succeed(undefined),
2445+
readResource: () => Effect.succeed(undefined),
2446+
startAuth: () => Effect.die("unexpected MCP auth in prompt-effect tests"),
2447+
authenticate: () => Effect.die("unexpected MCP auth in prompt-effect tests"),
2448+
finishAuth: () => Effect.die("unexpected MCP auth in prompt-effect tests"),
2449+
removeAuth: () => Effect.void,
2450+
supportsOAuth: () => Effect.succeed(false),
2451+
hasStoredTokens: () => Effect.succeed(false),
2452+
getAuthStatus: () => Effect.succeed("not_authenticated" as const),
2453+
}),
2454+
)
2455+
2456+
const mcpIt = testEffect(Layer.mergeAll(TestLLMServer.layer, makePrompt({ mcp: mcpWithEchoTool })))
2457+
2458+
mcpIt.instance("mcp tool ask publishes tool name and args (regression: #19549)", () =>
2459+
Effect.gen(function* () {
2460+
const { llm } = yield* useServerConfig(providerCfg)
2461+
const events = yield* EventV2Bridge.Service
2462+
const seen = yield* Deferred.make<PermissionV1.Request>()
2463+
const unsub = yield* events.listen((event) => {
2464+
if (event.type === Permission.Event.Asked.type)
2465+
Deferred.doneUnsafe(seen, Effect.succeed(event.data as PermissionV1.Request))
2466+
return Effect.void
2467+
})
2468+
yield* Effect.addFinalizer(() => unsub)
2469+
2470+
const prompt = yield* SessionPrompt.Service
2471+
const permission = yield* Permission.Service
2472+
const sessions = yield* Session.Service
2473+
const session = yield* sessions.create({
2474+
title: "MCP wrapper ask",
2475+
permission: [{ permission: "*", pattern: "*", action: "ask" }],
2476+
})
2477+
2478+
yield* prompt.prompt({
2479+
sessionID: session.id,
2480+
agent: "build",
2481+
noReply: true,
2482+
parts: [{ type: "text", text: "say hi" }],
2483+
})
2484+
yield* llm.tool("echo_say", { greeting: "hi" })
2485+
yield* llm.text("done")
2486+
2487+
const fiber = yield* prompt.loop({ sessionID: session.id }).pipe(Effect.forkChild)
2488+
2489+
const request = yield* Deferred.await(seen).pipe(
2490+
Effect.timeoutOrElse({
2491+
duration: Duration.seconds(5),
2492+
orElse: () => Effect.fail(new Error("timed out waiting for permission.asked")),
2493+
}),
2494+
)
2495+
expect(request.permission).toBe("echo_say")
2496+
expect(request.patterns).toEqual(["echo_say"])
2497+
expect(request.always).toEqual(["echo_say"])
2498+
expect(request.metadata).toEqual({ greeting: "hi" })
2499+
2500+
yield* permission.reply({ requestID: request.id, reply: "once" })
2501+
2502+
const exit = yield* Fiber.await(fiber)
2503+
expect(Exit.isSuccess(exit)).toBe(true)
2504+
}),
2505+
)

0 commit comments

Comments
 (0)