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