icarus_core/
server.rs

1//! Server trait and implementation for Icarus MCP servers
2
3use crate::error::Result;
4use crate::tool::IcarusTool;
5use async_trait::async_trait;
6use candid::Principal;
7use rmcp::ServerHandler;
8
9/// Version information for server upgrades
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Version {
12    pub major: u8,
13    pub minor: u8,
14    pub patch: u8,
15}
16
17impl Version {
18    pub fn new(major: u8, minor: u8, patch: u8) -> Self {
19        Self {
20            major,
21            minor,
22            patch,
23        }
24    }
25}
26
27/// Core trait for Icarus MCP servers that extends rmcp's ServerHandler
28#[async_trait]
29pub trait IcarusServer: ServerHandler + Send + Sync {
30    /// Called when the canister is first initialized
31    async fn on_canister_init(&mut self) -> Result<()> {
32        Ok(())
33    }
34
35    /// Called during canister upgrades
36    async fn on_canister_upgrade(&mut self, _from_version: Version) -> Result<()> {
37        Ok(())
38    }
39
40    /// Called before canister upgrade to save state
41    async fn on_pre_upgrade(&mut self) -> Result<()> {
42        Ok(())
43    }
44
45    /// Called after canister upgrade to restore state
46    async fn on_post_upgrade(&mut self) -> Result<()> {
47        Ok(())
48    }
49
50    /// Get the canister principal
51    fn canister_id(&self) -> Option<Principal> {
52        None
53    }
54
55    /// Register a tool with the server
56    fn register_tool(&mut self, tool: Box<dyn IcarusTool>) -> Result<()>;
57}