Skip to content

feat(core): pluggable tool call interceptors for per-invocation validation, rewriting, and audit - #3657

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/tool-call-interceptor
Oct 20, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
xzxiaoshan:feat/tool-call-interceptor

Conversation

@xzxiaoshan

Copy link
Copy Markdown
Collaborator

Resolves #3653

Summary

Adds a Toolkit-layer, per-invocation extension point over tool arguments, as specified in #3653. It runs after schema validation and preset merging and before the dispatch record and real execution, so it sees the exact arguments the tool would receive — plus the caller identity — and decides what actually executes.

  • ToolCallInterceptor — onion-style hook with order()-based chain assembly (same convention as MiddlewareBase#order()). Supported actions: allow, policy deny (DENIED result, auto-recorded as a denied decision), reject as invalid (ToolResultBlock.error), argument rewrite (may override presets), audit, and suspend for HITL (ToolSuspendException).
  • ToolInvocation — immutable call snapshot: tool name, resolved AgentTool (metadata dispatch such as isReadOnly()), preset-merged input (null-tolerant, key order preserved), runtime context, and agent identity (agentId / agentName, null on agentless direct calls). Copy-on-write rewriting via withInput.
  • ToolkitConfig — build-time registration: interceptor(...) appends (repeatable), interceptors(List) replaces; List.copyOf defensive copy; interceptors propagate automatically via Toolkit#copy() (harness subagents).
  • Single insertion point in ToolExecutor#executeCore, covering all locally executed channels: agent acting batches (Toolkit#callTools, with timeout/retry/scheduling as today), direct Toolkit#callTool calls, MCP tools including runtime-registered ones, and subagent toolkits. External tools (externalTool=true / SchemaOnlyTool) short-circuit to suspended before the chain, as today.

Session-record integrity: the tool/dispatch record moved to the end of the chain, so it always captures the arguments that actually executed; denied calls leave no misleading allowed/dispatch record.

With no interceptors registered (the default), executeCore keeps its existing code path unchanged — the public API change is purely additive (two new types plus a ToolkitConfig option); no existing signature, serialized state, or event payload is modified.

Docs: new "Tool call interceptors" section in docs/v2 (EN & ZH) building-blocks/tool.md, plus layering guidance cross-referenced from permission-system.md and middleware.md.

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.

[Feature]: Per-call tool argument interception at the Toolkit layer (validate / rewrite / deny / audit)

2 participants