权限¶
权限模块为 ToolRegistry 中的工具执行控制提供授权框架。它实现了基于规则的策略引擎和用于交互式授权的处理器协议。
概述¶
该模块由四个主要组件组成:
- PermissionResult -- 三态枚举(
ALLOW、DENY、ASK) - PermissionRule -- 匹配谓词与结果的配对
- PermissionPolicy -- 有序规则集合,采用首次匹配生效的评估方式
- PermissionHandler / AsyncPermissionHandler -- 用于解析
ASK结果的协议
使用指南和示例请参阅权限系统。
类型¶
toolregistry.permissions.types ¶
Core types for the permission system.
PermissionRequest ¶
Bases: BaseModel
Context passed to a PermissionHandler when a rule returns ASK.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_name |
str
|
Name of the tool being invoked. |
parameters |
dict[str, Any]
|
Arguments the caller intends to pass. |
reason |
str
|
Human-readable explanation of why confirmation is needed. |
rule_name |
str
|
Name of the rule that triggered the ASK result. |
metadata |
ToolMetadata
|
The tool's ToolMetadata for handler reference. |
PermissionResult ¶
Bases: str, Enum
Three-state permission decision.
Attributes:
| Name | Type | Description |
|---|---|---|
ALLOW |
The tool call is permitted. |
|
DENY |
The tool call is rejected. |
|
ASK |
The decision should be delegated to a PermissionHandler. |
处理器协议¶
toolregistry.permissions.handler ¶
Permission handler protocols for tool authorization.
AsyncPermissionHandler ¶
Bases: Protocol
Asynchronous handler invoked when a permission rule returns ASK.
Same contract as PermissionHandler but for async contexts.
Example
PermissionHandler ¶
Bases: Protocol
Synchronous handler invoked when a permission rule returns ASK.
Implementations decide whether to allow or deny a tool call, typically by prompting the user or consulting an external policy service.
Example
策略和规则¶
toolregistry.permissions.policy ¶
Permission policy: composable rules with first-match-wins evaluation.
PermissionPolicy ¶
Bases: BaseModel
An ordered collection of permission rules with a fallback.
Evaluation follows first-match-wins semantics: rules are checked in
list order and the first rule whose match returns True
produces the final decision (subject to handler resolution for
ASK). If no rule matches, fallback is used.
Attributes:
| Name | Type | Description |
|---|---|---|
rules |
list[PermissionRule]
|
Ordered list of permission rules. |
fallback |
PermissionResult
|
Result when no rule matches. Defaults to |
handler |
PermissionHandler | AsyncPermissionHandler | None
|
Optional handler invoked when a rule returns |
evaluate ¶
Evaluate rules against a tool call.
Returns:
| Type | Description |
|---|---|
PermissionResult | PermissionRule
|
The matched |
PermissionResult | PermissionRule
|
|
Source code in src/toolregistry/permissions/policy.py
PermissionRule ¶
Bases: BaseModel
A single permission rule that maps a match predicate to a result.
Rules are evaluated in order; the first rule whose match returns
True determines the outcome.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable identifier for this rule. |
match |
Callable[[Tool, dict[str, Any]], bool]
|
Predicate receiving |
result |
PermissionResult
|
The permission decision when the rule matches. |
reason |
str
|
Explanation surfaced in |
内置规则¶
toolregistry.permissions.builtin_rules ¶
Built-in permission rules for common tool classification patterns.