Skip to content

Commit b0da2f1

Browse files
author
Karl Kurzer
committed
feat(permission): granular pattern matching for MCP tools
Serialize MCP tool arguments into a deterministic key=value string and pass it as the permission pattern instead of always using "*". This lets users write wildcard rules in opencode.json that match against specific MCP tool inputs, e.g.: "outlook_calendar_meeting": { "*": "ask", "*operation=read*": "allow", "*operation=delete*": "deny" } Also display MCP tool arguments in the TUI permission prompt so users can see what they are approving. Closes anomalyco#12358
1 parent 9b20679 commit b0da2f1

4 files changed

Lines changed: 110 additions & 3 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/routes/session/permission.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { LANGUAGE_EXTENSIONS } from "@/lsp/language"
1414
import { Keybind } from "@/util/keybind"
1515
import { Locale } from "@/util/locale"
1616
import { Global } from "@/global"
17+
import { PermissionNext } from "@/permission/next"
1718
import { useDialog } from "../../ui/dialog"
1819

1920
type PermissionStage = "permission" | "always" | "reject"
@@ -260,7 +261,10 @@ export function PermissionPrompt(props: { request: PermissionRequest }) {
260261
<TextBody icon="⟳" title="Continue after repeated failures" />
261262
</Match>
262263
<Match when={true}>
263-
<TextBody icon="⚙" title={`Call tool ` + props.request.permission} />
264+
{(() => {
265+
const detail = PermissionNext.serialize(input() as Record<string, unknown>) || undefined
266+
return <TextBody icon="⚙" title={`Call tool ` + props.request.permission} description={detail} />
267+
})()}
264268
</Match>
265269
</Switch>
266270
}

‎packages/opencode/src/permission/next.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ export namespace PermissionNext {
274274
}
275275
}
276276

277+
export function serialize(args: Record<string, unknown>): string {
278+
return Object.entries(args)
279+
.sort(([a], [b]) => a.localeCompare(b))
280+
.map(([k, v]) => `${k}=${v === null || typeof v !== "object" ? v : JSON.stringify(v)}`)
281+
.join(", ")
282+
}
283+
277284
export async function list() {
278285
return state().then((x) => Object.values(x.pending).map((x) => x.info))
279286
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,11 @@ export namespace SessionPrompt {
750750
},
751751
)
752752

753+
const pattern = PermissionNext.serialize(args as Record<string, unknown>)
753754
await ctx.ask({
754755
permission: key,
755-
metadata: {},
756-
patterns: ["*"],
756+
metadata: args,
757+
patterns: pattern ? [pattern] : ["*"],
757758
always: ["*"],
758759
})
759760

‎packages/opencode/test/permission/next.test.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,98 @@ test("ask - allows all patterns when all match allow rules", async () => {
688688
},
689689
})
690690
})
691+
692+
// serialize tests
693+
694+
test("serialize - basic key-value pairs sorted alphabetically", () => {
695+
const result = PermissionNext.serialize({ operation: "read", meetingId: "AAMk123" })
696+
expect(result).toBe("meetingId=AAMk123, operation=read")
697+
})
698+
699+
test("serialize - empty object returns empty string", () => {
700+
expect(PermissionNext.serialize({})).toBe("")
701+
})
702+
703+
test("serialize - single key-value pair", () => {
704+
expect(PermissionNext.serialize({ operation: "read" })).toBe("operation=read")
705+
})
706+
707+
test("serialize - numeric values", () => {
708+
expect(PermissionNext.serialize({ count: 5, page: 1 })).toBe("count=5, page=1")
709+
})
710+
711+
test("serialize - boolean values", () => {
712+
expect(PermissionNext.serialize({ active: true, deleted: false })).toBe("active=true, deleted=false")
713+
})
714+
715+
test("serialize - nested objects are JSON stringified", () => {
716+
const result = PermissionNext.serialize({ filter: { date: "2024-01-01" }, operation: "read" })
717+
expect(result).toBe(`filter={"date":"2024-01-01"}, operation=read`)
718+
})
719+
720+
test("serialize - null and undefined values", () => {
721+
const result = PermissionNext.serialize({ a: null, b: undefined })
722+
expect(result).toBe("a=null, b=undefined")
723+
})
724+
725+
test("serialize - array values are JSON stringified", () => {
726+
const result = PermissionNext.serialize({ ids: [1, 2, 3] })
727+
expect(result).toBe("ids=[1,2,3]")
728+
})
729+
730+
// MCP permission pattern matching integration tests
731+
732+
test("evaluate - MCP tool with serialized args matches wildcard pattern", () => {
733+
const ruleset = PermissionNext.fromConfig({
734+
outlook_calendar_meeting: {
735+
"*": "ask",
736+
"*operation=read*": "allow",
737+
},
738+
})
739+
const serialized = PermissionNext.serialize({ operation: "read", meetingId: "AAMk123" })
740+
const result = PermissionNext.evaluate("outlook_calendar_meeting", serialized, ruleset)
741+
expect(result.action).toBe("allow")
742+
})
743+
744+
test("evaluate - MCP tool with non-matching operation falls to default ask", () => {
745+
const ruleset = PermissionNext.fromConfig({
746+
outlook_calendar_meeting: {
747+
"*": "ask",
748+
"*operation=read*": "allow",
749+
},
750+
})
751+
const serialized = PermissionNext.serialize({ operation: "create", subject: "Sprint Review" })
752+
const result = PermissionNext.evaluate("outlook_calendar_meeting", serialized, ruleset)
753+
expect(result.action).toBe("ask")
754+
})
755+
756+
test("evaluate - MCP tool deny destructive operations", () => {
757+
const ruleset = PermissionNext.fromConfig({
758+
outlook_calendar_meeting: {
759+
"*": "ask",
760+
"*operation=read*": "allow",
761+
"*operation=delete*": "deny",
762+
},
763+
})
764+
const serialized = PermissionNext.serialize({ operation: "delete", meetingId: "AAMk123" })
765+
const result = PermissionNext.evaluate("outlook_calendar_meeting", serialized, ruleset)
766+
expect(result.action).toBe("deny")
767+
})
768+
769+
test("evaluate - MCP tool blanket allow", () => {
770+
const ruleset = PermissionNext.fromConfig({
771+
slack_read_messages: "allow",
772+
})
773+
const serialized = PermissionNext.serialize({ channel: "general" })
774+
const result = PermissionNext.evaluate("slack_read_messages", serialized, ruleset)
775+
expect(result.action).toBe("allow")
776+
})
777+
778+
test("evaluate - MCP tool with empty args and blanket allow", () => {
779+
const ruleset = PermissionNext.fromConfig({
780+
my_mcp_tool: "allow",
781+
})
782+
// empty args -> PermissionNext.serialize returns "" -> pattern falls back to "*"
783+
const result = PermissionNext.evaluate("my_mcp_tool", "*", ruleset)
784+
expect(result.action).toBe("allow")
785+
})

0 commit comments

Comments
 (0)