mod mcp;
pub mod completion;
pub mod error;
pub mod notifications;
pub mod prompt;
pub use prompt::RegisteredPrompt;
pub mod requests;
pub mod resource;
pub mod roots;
pub mod sampling;
pub mod tool;
pub use mcp::McpServer;
pub use error::{ErrorCode, JsonRpcError, ServerError};
pub use notifications::{Notification, NotificationHandler, NotificationSender};
pub use requests::{Request, RequestHandler};
use crate::types::{Implementation, ServerCapabilities};
use crate::transport::Transport;
use std::sync::Arc;
type Result<T> = std::result::Result<T, ServerError>;
pub struct Server {
#[allow(dead_code)]
server_info: Implementation,
capabilities: ServerCapabilities,
request_handler: Option<Arc<dyn RequestHandler>>,
notification_handler: Option<Arc<dyn NotificationHandler>>,
notification_sender: Option<Arc<dyn NotificationSender>>,
}
impl Server {
pub fn new(server_info: Implementation) -> Self {
Self {
server_info,
capabilities: Default::default(),
request_handler: None,
notification_handler: None,
notification_sender: None,
}
}
pub async fn connect(&self, _transport: impl Transport) -> Result<()> {
if self.request_handler.is_none() {
return Err(ServerError::new(
ErrorCode::HandlerNotSet,
"Request handler not set",
));
}
if self.notification_handler.is_none() {
return Err(ServerError::new(
ErrorCode::HandlerNotSet,
"Notification handler not set",
));
}
Ok(())
}
pub async fn listen(&self) -> Result<()> {
Ok(())
}
pub fn register_capabilities(&mut self, capabilities: ServerCapabilities) {
self.capabilities = capabilities;
}
pub fn set_request_handler(&mut self, handler: impl RequestHandler + 'static) {
self.request_handler = Some(Arc::new(handler));
}
pub fn set_notification_handler(&mut self, handler: impl NotificationHandler + 'static) {
self.notification_handler = Some(Arc::new(handler));
}
pub fn set_notification_sender(&mut self, sender: impl NotificationSender + 'static) {
self.notification_sender = Some(Arc::new(sender));
}
pub async fn send_notification(&self, notification: Notification) -> Result<()> {
if let Some(sender) = &self.notification_sender {
sender.send(notification).await.map_err(|e|
ServerError::new(ErrorCode::InternalError, format!("Failed to send notification: {}", e)))?;
} else {
return Err(ServerError::new(
ErrorCode::HandlerNotSet,
"Notification sender not set",
));
}
Ok(())
}
}