Skip to main content

base/interface/
mcp_servers.rs

1//! `McpServers` — the MCP servers a session is connected to, as the turn loop
2//! uses them.
3//!
4//! Through this the kernel offers their tools to the model, carries their
5//! instructions in the prompt, inlines `@server:uri` references, and runs
6//! their prompts as `/mcp__<server>__<prompt>` commands. The mcp module owns
7//! the connections; a session assembled without it has [`NoMcpServers`].
8
9use std::sync::Arc;
10
11use async_trait::async_trait;
12
13use crate::context::McpServerInstruction;
14use crate::tool::Tool;
15
16#[async_trait]
17pub trait McpServers: Send + Sync {
18    /// The servers' tools, as tools a session can dispatch to.
19    fn tools(&self) -> Vec<Arc<dyn Tool>>;
20    /// Lists the servers' tools again.
21    async fn refresh_tools(&mut self);
22    /// Lists again what a server announced has changed since this last
23    /// looked; whether anything had.
24    async fn refresh_if_changed(&mut self) -> bool;
25    fn instructions(&self) -> Vec<McpServerInstruction>;
26    /// The resources `text` references, rendered for the model; `None` when
27    /// it references none.
28    async fn resolve_resource_refs(&self, text: &str) -> Option<String>;
29    fn prompt_commands(&self) -> Vec<McpPromptCommand>;
30    fn has_prompt_command(&self, command: &str) -> bool;
31    /// Never fails: an unreachable server or a refused prompt comes back as
32    /// text the model can read, flagged with `is_error`.
33    async fn invoke_prompt_command(&self, command: &str, raw_args: &str) -> McpPromptInvocation;
34    /// The same connections, as servers another session can own — what a
35    /// delegate is granted: only `servers`, or every one when `None`. A named
36    /// server not connected here is left out.
37    fn granted(&self, servers: Option<&[String]>) -> Box<dyn McpServers>;
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub struct McpPromptCommand {
42    pub command: String,
43    pub description: String,
44    pub argument_hint: String,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct McpPromptInvocation {
49    pub text: String,
50    pub is_error: bool,
51}
52
53pub struct NoMcpServers;
54
55#[async_trait]
56impl McpServers for NoMcpServers {
57    fn tools(&self) -> Vec<Arc<dyn Tool>> {
58        Vec::new()
59    }
60    async fn refresh_tools(&mut self) {}
61    async fn refresh_if_changed(&mut self) -> bool {
62        false
63    }
64    fn instructions(&self) -> Vec<McpServerInstruction> {
65        Vec::new()
66    }
67    async fn resolve_resource_refs(&self, _: &str) -> Option<String> {
68        None
69    }
70    fn prompt_commands(&self) -> Vec<McpPromptCommand> {
71        Vec::new()
72    }
73    fn has_prompt_command(&self, _: &str) -> bool {
74        false
75    }
76    async fn invoke_prompt_command(&self, command: &str, _: &str) -> McpPromptInvocation {
77        McpPromptInvocation {
78            text: format!("no MCP server provides `/{command}`"),
79            is_error: true,
80        }
81    }
82    fn granted(&self, _: Option<&[String]>) -> Box<dyn McpServers> {
83        Box::new(NoMcpServers)
84    }
85}