Skip to main content

tea_coding/
mcp.rs

1use tea_mcp::{McpManager, McpServerHealth, McpServerId};
2use tea_protocol::ProtocolTimestamp;
3use tea_tools::ToolName;
4
5/// One safe immutable MCP tool binding exposed to product hosts.
6///
7/// Remote descriptions, annotations, executable details, environment values,
8/// stderr, and result bodies intentionally do not cross this boundary.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct McpCatalogEntry {
11    server_id: McpServerId,
12    tool_name: ToolName,
13}
14
15impl McpCatalogEntry {
16    pub(crate) fn new(server_id: McpServerId, tool_name: ToolName) -> Self {
17        Self {
18            server_id,
19            tool_name,
20        }
21    }
22
23    /// Returns the configured server that owns this exact tool binding.
24    #[must_use]
25    pub const fn server_id(&self) -> &McpServerId {
26        &self.server_id
27    }
28
29    /// Returns the frozen local tool alias.
30    #[must_use]
31    pub const fn tool_name(&self) -> &ToolName {
32        &self.tool_name
33    }
34}
35
36/// Safe immutable MCP lifecycle and catalog projection for product hosts.
37#[derive(Debug, Clone, PartialEq, Eq, Default)]
38pub struct McpServiceSnapshot {
39    servers: Vec<McpServerHealth>,
40    catalog: Vec<McpCatalogEntry>,
41}
42
43impl McpServiceSnapshot {
44    pub(crate) fn new(servers: Vec<McpServerHealth>, catalog: Vec<McpCatalogEntry>) -> Self {
45        Self { servers, catalog }
46    }
47
48    /// Returns server health in canonical server-ID order.
49    #[must_use]
50    pub fn servers(&self) -> &[McpServerHealth] {
51        &self.servers
52    }
53
54    /// Returns enabled frozen aliases in canonical local-alias order.
55    #[must_use]
56    pub fn catalog(&self) -> &[McpCatalogEntry] {
57        &self.catalog
58    }
59}
60
61pub(crate) fn snapshot(
62    manager: Option<&McpManager>,
63    observed_at: ProtocolTimestamp,
64) -> Result<McpServiceSnapshot, tea_mcp::McpError> {
65    let Some(manager) = manager else {
66        return Ok(McpServiceSnapshot::default());
67    };
68    let servers = manager.health(observed_at)?;
69    let catalog = manager
70        .catalog()
71        .bindings()
72        .map(|binding| {
73            McpCatalogEntry::new(binding.server_id().clone(), binding.spec().name().clone())
74        })
75        .collect();
76    Ok(McpServiceSnapshot::new(servers, catalog))
77}