Skip to content

feat(core): declarative tool permissions with an approval callback - #105

Merged
gopaljigaur merged 3 commits into
mainfrom
feat/tool-permissions
Oct 15, 2026
Merged

gopaljigaur merged 3 commits into
mainfrom
feat/tool-permissions

Conversation

@gopaljigaur

@gopaljigaur gopaljigaur commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Adds declarative tool permissions, built as one ToolMiddleware, and a list of denied calls on RunResult.

What changed

  • cyclops/core/permissions.py: ToolPermissions(allow=, ask=, deny=, approve=, default="allow"). Rules are tool names or patterns matched with fnmatch.fnmatchcase. A call takes the strictest rule it matches: deny, then ask, then allow; a call that matches none gets default. An "ask" call awaits approve(call) with the ToolCallContext, which returns True to allow, False to deny, or Deny(reason) to deny with a reason. ask rules or default="ask" without approve raise ValueError.
  • RunResult.denied_tool_calls: List[DeniedToolCall] (id, name, arguments, reason). When middleware returns Deny, the rendered result carries the reason, and _ToolExecutor records it where it appends to tool_calls: after the step, in the model's order, so a step that fails records nothing. It covers every middleware and the on_tool_start hook as well, with no state in the middleware. The list lives on the run's result, so it is per run.
  • ToolPermissions and DeniedToolCall are exported from cyclops and cyclops.core.
  • Docs: a "Tool permissions" section in the hooks guide, including how approvals behave with parallel calls, the new types in the API reference, and the new field in the cost tracking field table, in docs/ and the website.

Decisions

  • The approval result follows the bool | ToolApproved | ToolDenied shape Pydantic AI uses for approvals, with the existing Deny as the denied object, so a reason is optional.
  • The approval is awaited in place, as before_tool already can, instead of pausing the run and resuming it later as the OpenAI Agents SDK interruptions and LangGraph interrupts do. That needs no new run state and keeps this inside the middleware.
  • Deny over ask over allow is the precedence common permission rule systems use.
  • A callback that raises fails the run like any raising middleware does today; the tool does not run.
  • MCP annotations are not rules of their own: the callback sees call.annotations (destructive, read_only and so on) and can decide on them.
  • A denied call still reaches the model as an error tool result, exactly as a Deny does today.

Tests

tests/test_permissions.py covers allow, deny, the default, ask approved, ask refused, ask denied with a reason, patterns and precedence, a callback that raises, the denied list being per run, a naive mode denial without an id, read-only approvals running concurrently and recorded in model order, other approvals one at a time, a failed step recording no denial, and cancelling during an approval. test_middleware.py checks a plain Deny and the on_tool_start deny are recorded too. uv run pytest, uv run pre-commit run --all-files and uv run mypy cyclops pass.

Closes #64

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precedence, the fnmatchcase matching, the ValueError, the default and the raising callback all behave as described, and CI, pytest, pre-commit and mypy pass. Three findings:

  1. denied_tool_calls is recorded in completion order and survives a failed step, so it can disagree with tool_calls (inline on tool_execution.py).
  2. The docs do not say that approvals for tools not marked read-only are asked one at a time and hold a parallel slot while pending (inline on docs/guide/hooks.md).
  3. Missing tests for parallel calls, the hook's deny being recorded, and cancellation during an approval (inline on tests/test_permissions.py).

Comment thread cyclops/core/tool_execution.py Resolved
Comment thread docs/guide/hooks.md Resolved
Comment thread tests/test_permissions.py Resolved
ToolPermissions is a ToolMiddleware with allow, ask and deny rules over
tool names or fnmatch patterns. A call takes the strictest rule it
matches (deny, then ask, then allow) or the default; an ask call awaits
an async approve callback that returns a bool or a Deny.

RunResult.denied_tool_calls lists every call middleware denied in the
run.
A denial was recorded inside its call's task, so parallel calls could be
listed in the order they were answered, and a failed step could report a
denial for a call that tool_calls does not list. The rendered result now
carries the deny reason and the executor records it where it appends to
tool_calls.

Tests cover parallel approvals, the on_tool_start deny hook, cancelling
during an approval and a step that fails after a denial.
@gopaljigaur
gopaljigaur force-pushed the feat/tool-permissions branch from a6e90e2 to fb20720 Compare October 15, 2026 03:07

@gopaljigaur gopaljigaur left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings.

@gopaljigaur
gopaljigaur merged commit d39da36 into main Oct 15, 2026
10 checks passed
@gopaljigaur
gopaljigaur deleted the feat/tool-permissions branch October 15, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Declarative tool permissions with an approval callback

1 participant