Skip to main content

aion_server/mcp/
runtime.rs

1//! The MCP runtime: the server object and its construction from config.
2
3use std::sync::Arc;
4use std::time::Duration;
5
6use aion_mcp::protocol::meta::Implementation;
7use aion_mcp::{McpServer, McpServerConfig};
8
9use crate::config::ResolvedMcpConfig;
10use crate::{ServerError, ServerState};
11
12use super::service::AionToolService;
13
14/// The MCP server plus everything it needs, constructed once at startup.
15pub struct McpRuntime {
16    server: McpServer<AionToolService>,
17}
18
19impl McpRuntime {
20    /// Build the runtime from validated configuration.
21    ///
22    /// The advertised server identity is this binary's own name and version —
23    /// self-reported, unverified, and used for nothing but display, exactly as
24    /// the revision says such an identity must be.
25    ///
26    /// # Errors
27    ///
28    /// [`ServerError::Config`] when a published tool schema fails to compile.
29    pub fn build(state: ServerState, config: &ResolvedMcpConfig) -> Result<Self, ServerError> {
30        let service =
31            AionToolService::new(state, Duration::from_millis(config.await_poll_interval_ms))?;
32        let server = McpServer::new(
33            Arc::new(service),
34            McpServerConfig::new(
35                Implementation::new("aion", env!("CARGO_PKG_VERSION")),
36                config.discover_ttl_ms,
37                config.tools_list_ttl_ms,
38                config.task_ttl_ms,
39                config.task_poll_interval_ms,
40                config.allowed_origins.clone(),
41            ),
42        );
43        Ok(Self { server })
44    }
45
46    /// The MCP server.
47    pub(crate) const fn server(&self) -> &McpServer<AionToolService> {
48        &self.server
49    }
50}