Skip to main content

assay_core/mcp/tool_call_handler/
mod.rs

1//! Central tool call handler with mandate authorization.
2//!
3//! This module integrates policy evaluation, mandate authorization, and
4//! decision emission into a single handler that guarantees the always-emit
5//! invariant (I1).
6
7mod emit;
8mod evaluate;
9mod evaluate_next;
10mod types;
11
12pub use types::{HandleResult, ToolCallHandler, ToolCallHandlerConfig};
13
14use super::decision::DecisionEmitter;
15use super::identity::ToolIdentity;
16use super::jsonrpc::JsonRpcRequest;
17use super::lifecycle::LifecycleEmitter;
18use super::policy::{McpPolicy, PolicyState};
19use crate::runtime::{Authorizer, MandateData};
20use serde_json::Value;
21use std::sync::Arc;
22
23impl ToolCallHandler {
24    /// Create a new handler.
25    pub fn new(
26        policy: McpPolicy,
27        authorizer: Option<Authorizer>,
28        emitter: Arc<dyn DecisionEmitter>,
29        config: ToolCallHandlerConfig,
30    ) -> Self {
31        types::new_handler(policy, authorizer, emitter, config)
32    }
33
34    /// Set the lifecycle emitter for mandate.used events (P0-B).
35    pub fn with_lifecycle_emitter(self, emitter: Arc<dyn LifecycleEmitter>) -> Self {
36        types::with_lifecycle_emitter(self, emitter)
37    }
38
39    /// Handle a tool call with full authorization and always-emit guarantee.
40    ///
41    /// This is the main entry point that enforces invariant I1: exactly one
42    /// decision event is emitted for every tool call attempt.
43    pub fn handle_tool_call(
44        &self,
45        request: &JsonRpcRequest,
46        state: &mut PolicyState,
47        runtime_identity: Option<&ToolIdentity>,
48        mandate: Option<&MandateData>,
49        transaction_object: Option<&Value>,
50    ) -> HandleResult {
51        evaluate::handle_tool_call(
52            self,
53            request,
54            state,
55            runtime_identity,
56            mandate,
57            transaction_object,
58        )
59    }
60}
61
62#[cfg(test)]
63mod tests;