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::block::ToolCallPayload;
14use crate::context::tool_data::ToolResultPayload;
15use crate::ports::control::CallControl;
16use crate::ports::tool::ToolDefinition;
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`). The
28 /// executor caches the listing keyed by this value — a source whose
29 /// observed version matches the cache costs no re-list, and a source
30 /// without change notifications (the `0` default) is therefore listed
31 /// once and served from the cache until it bumps.
32 fn version(&self) -> u64 {
33 0
34 }
35
36 /// The current tool listing. Fails when the catalog is unreachable —
37 /// the executor then keeps serving the source's last good listing
38 /// (with a warning) and proceeds; a source with no cached listing yet
39 /// is skipped. Either way the turn is not interrupted.
40 async fn list(&self) -> Result<Vec<ToolDefinition>, SourceError>;
41
42 /// Execute one call against the catalog. `call.tool_name` arrives in
43 /// the source's namespace; implementors de-namespace it (and reject
44 /// names outside their namespace). Sources return the recorded result
45 /// only — what an `UnknownOutcome` result does next is harness
46 /// configuration, not source vocabulary.
47 async fn invoke(
48 &self,
49 call: &ToolCallPayload,
50 control: &CallControl,
51 ) -> Result<ToolResultPayload, ToolExecutionError>;
52
53 /// Execute one call with the host's artifact store available for
54 /// media ingest: a source whose tools produce images can persist the
55 /// bytes via `store` and attach [`crate::context::block::MediaRef`]s
56 /// to the result's `ToolResultPayload.media` instead of degrading
57 /// them to text. Additive with a delegating default, so sources that
58 /// never produce media keep their existing `invoke` only.
59 async fn invoke_with_store(
60 &self,
61 call: &ToolCallPayload,
62 control: &CallControl,
63 store: Option<&dyn crate::ports::tool::ArtifactStore>,
64 ) -> Result<ToolResultPayload, ToolExecutionError> {
65 let _ = store;
66 Self::invoke(self, call, control).await
67 }
68}
69
70/// Listing failure — the catalog as a whole is unusable right now.
71#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
72pub enum SourceError {
73 /// Connection failure or server offline; retryable (transient).
74 #[error("tool source unavailable: {0}")]
75 Unavailable(String),
76 /// Protocol-level failure; not retryable (permanent).
77 #[error("tool source protocol error: {0}")]
78 Protocol(String),
79}
80
81/// Single-call failure inside a source. The executor's tool adapter
82/// (`ToolBridge` in `causa_runtime`) turns every variant into a structured
83/// tool result the model reads; the status mapping lives there, not here.
84#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
85pub enum ToolExecutionError {
86 /// The call may or may not have run server-side — the outcome is
87 /// unknown, not failed (the executor marks it accordingly).
88 #[error("tool call timed out")]
89 TimedOut,
90 /// Cancelled before completion; the outcome is unknown.
91 #[error("tool call cancelled")]
92 Cancelled,
93 /// The name is not in this source's namespace.
94 #[error("unknown tool: {0}")]
95 UnknownTool(String),
96 /// Connection failure or server offline (transient).
97 #[error("tool source unavailable: {0}")]
98 Unavailable(String),
99 /// Protocol-level failure (permanent).
100 #[error("tool source protocol error: {0}")]
101 Protocol(String),
102}