Skip to main content

causa_kernel/ports/
source.rs

1//! `DynamicToolSource` port — the seam external tool catalogs (MCP
2//! servers, plugin hosts) fill in so their tools can enter the model's
3//! `ToolSurface` next to local Rust tools.
4//!
5//! It is a kernel contract: third parties implement it against the facts
6//! crate alone (`causa-extension` depends only on this crate, exactly as
7//! `causa-provider` implements `ModelGateway`). The aggregation and
8//! dispatch logic that *consumes* the port lives in `causa_runtime`'s
9//! executor.
10
11use async_trait::async_trait;
12
13use crate::context::tool_data::ToolResultPayload;
14use crate::ports::control::CallControl;
15use crate::ports::tool::{ToolCallContext, ToolDefinition};
16
17/// One external tool catalog. Implementors own their connection, their
18/// naming (see the `mcp_{server_id}_{tool}` namespace convention in
19/// `causa-extension`), and their change notifications.
20#[async_trait]
21pub trait DynamicToolSource: Send + Sync {
22    /// Stable catalog identity — also the dispatch namespace key.
23    fn id(&self) -> &str;
24
25    /// Change signal for executor-side caching: bump whenever the tool
26    /// listing may have changed (e.g. MCP `tools/list_changed`). Consumers
27    /// may reuse a listing while the observed version remains unchanged.
28    /// This signal does not make version/list reads an atomic snapshot or
29    /// replace an invocation binding to concrete targets.
30    fn version(&self) -> u64 {
31        0
32    }
33
34    /// The current tool listing. Failure means acquisition failed, not an
35    /// empty catalog. The reference executor propagates refresh errors and
36    /// does not substitute an old listing or skip the source. Existing
37    /// successful bindings retain the targets they already acquired.
38    async fn list(&self) -> Result<Vec<ToolDefinition>, SourceError>;
39
40    /// Execute one call against the catalog. `call.input.tool_name` arrives in
41    /// the source's namespace; implementors de-namespace it (and reject
42    /// names outside their namespace). Sources return the recorded result
43    /// only — handling `UnknownOutcome` belongs to the execution consumer,
44    /// not source vocabulary.
45    ///
46    /// `store` is the host's artifact store when one is configured. A source
47    /// whose tools produce images persists the bytes there and attaches
48    /// [`crate::context::block::MediaRef`]s to the result's media instead of
49    /// degrading them to text; a source that never produces media ignores it.
50    async fn invoke(
51        &self,
52        call: &ToolCallContext,
53        control: &CallControl,
54        store: Option<&dyn crate::ports::tool::ArtifactStore>,
55    ) -> Result<ToolResultPayload, ToolExecutionError>;
56}
57
58/// Listing failure — the catalog as a whole is unusable right now.
59#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
60#[non_exhaustive]
61pub enum SourceError {
62    /// Connection failure or server offline; retryable (transient).
63    #[error("tool source unavailable: {0}")]
64    Unavailable(String),
65    /// Protocol-level failure; not retryable (permanent).
66    #[error("tool source protocol error: {0}")]
67    Protocol(String),
68}
69
70/// Single-call failure inside a source. The executor's tool adapter
71/// (`ToolBridge` in `causa_runtime`) turns every variant into a structured
72/// tool result the model reads; the status mapping lives there, not here.
73///
74/// Exhaustive on purpose: the runtime maps every variant to a result status, so a new one is a breaking change.
75#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
76pub enum ToolExecutionError {
77    /// The call may or may not have run server-side — the outcome is
78    /// unknown, not failed (the executor marks it accordingly).
79    #[error("tool call timed out")]
80    TimedOut,
81    /// Cancelled before completion; the outcome is unknown.
82    #[error("tool call cancelled")]
83    Cancelled,
84    /// The name is not in this source's namespace.
85    #[error("unknown tool: {0}")]
86    UnknownTool(String),
87    /// Connection failure or server offline (transient).
88    #[error("tool source unavailable: {0}")]
89    Unavailable(String),
90    /// Protocol-level failure (permanent).
91    #[error("tool source protocol error: {0}")]
92    Protocol(String),
93}