use crate::config::Config;
use crate::error::{PluginError, Result as PluginResult};
use crate::mcp::{McpPluginServer, McpRequestHandler};
use std::sync::Arc;
#[derive(Clone)]
pub struct AppState {
pub handler: Arc<McpRequestHandler>,
}
impl AppState {
pub fn new(config: Config) -> PluginResult<Self> {
if let Some(parent) = config.templates_dir.parent()
&& !parent.as_os_str().is_empty()
&& !parent.exists()
{
std::fs::create_dir_all(parent).map_err(|e| {
PluginError::Other(format!(
"cannot create templates directory {}: {e}",
parent.display()
))
})?;
}
let server = Arc::new(McpPluginServer::new_with_file_storage(
config.templates_dir.clone(),
));
let handler = Arc::new(McpRequestHandler::new(Arc::clone(&server), config));
Ok(Self { handler })
}
}