use serde_json::{json, Value};
use anyhow::Result;
use crate::config::{ToolConfig, all_module_info, is_module_compiled};
#[cfg(feature = "clipboard")]
use crate::modules::clipboard::ClipboardModule;
#[cfg(feature = "context")]
use crate::modules::context::ContextModule;
#[cfg(feature = "diagnostics")]
use crate::modules::diagnostics::DiagnosticsModule;
#[cfg(feature = "filesystem")]
use crate::modules::filesystem::FilesystemModule;
#[cfg(feature = "git")]
use crate::modules::git::GitModule;
#[cfg(feature = "input")]
use crate::modules::input::InputModule;
#[cfg(feature = "network")]
use crate::modules::network::NetworkModule;
#[cfg(feature = "silent")]
use crate::modules::silent::SilentModule;
#[cfg(feature = "time")]
use crate::modules::time::TimeModule;
#[cfg(feature = "transform")]
use crate::modules::transform::TransformModule;
#[cfg(feature = "gitent")]
use crate::modules::gitent::GitentModule;
pub struct PolyMcp {
config: ToolConfig,
#[cfg(feature = "filesystem")]
filesystem: FilesystemModule,
#[cfg(feature = "diagnostics")]
diagnostics: DiagnosticsModule,
#[cfg(feature = "silent")]
silent: SilentModule,
#[cfg(feature = "time")]
time: TimeModule,
#[cfg(feature = "network")]
network: NetworkModule,
#[cfg(feature = "context")]
context: ContextModule,
#[cfg(feature = "git")]
git: GitModule,
#[cfg(feature = "input")]
input: InputModule,
#[cfg(feature = "gitent")]
gitent: GitentModule,
#[cfg(feature = "clipboard")]
clipboard: ClipboardModule,
#[cfg(feature = "transform")]
transform: TransformModule,
}
impl PolyMcp {
pub fn new(config: ToolConfig) -> Self {
Self {
config,
#[cfg(feature = "filesystem")]
filesystem: FilesystemModule::new(),
#[cfg(feature = "diagnostics")]
diagnostics: DiagnosticsModule::new(),
#[cfg(feature = "silent")]
silent: SilentModule::new(),
#[cfg(feature = "time")]
time: TimeModule::new(),
#[cfg(feature = "network")]
network: NetworkModule::new(),
#[cfg(feature = "context")]
context: ContextModule::new(),
#[cfg(feature = "git")]
git: GitModule::new(),
#[cfg(feature = "input")]
input: InputModule::new(),
#[cfg(feature = "gitent")]
gitent: GitentModule::new(),
#[cfg(feature = "clipboard")]
clipboard: ClipboardModule::new(),
#[cfg(feature = "transform")]
transform: TransformModule::new(),
}
}
pub fn with_default_config() -> Self {
Self::new(ToolConfig::default())
}
pub fn config(&self) -> &ToolConfig {
&self.config
}
pub fn set_config(&mut self, config: ToolConfig) {
self.config = config;
}
pub fn active_module_count(&self) -> usize {
all_module_info()
.iter()
.filter(|info| is_module_compiled(info.name) && self.config.modules.is_enabled(info.name))
.count()
}
pub fn get_server_info(&self) -> Value {
json!({
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "poly-mcp",
"version": env!("CARGO_PKG_VERSION")
}
})
}
pub fn list_tools(&self) -> Value {
let mut tools: Vec<Value> = Vec::new();
#[cfg(feature = "filesystem")]
if self.config.modules.filesystem {
tools.extend(self.filesystem.get_tools());
}
#[cfg(feature = "diagnostics")]
if self.config.modules.diagnostics {
tools.extend(self.diagnostics.get_tools());
}
#[cfg(feature = "silent")]
if self.config.modules.silent {
tools.extend(self.silent.get_tools());
}
#[cfg(feature = "time")]
if self.config.modules.time {
tools.extend(self.time.get_tools());
}
#[cfg(feature = "network")]
if self.config.modules.network {
tools.extend(self.network.get_tools());
}
#[cfg(feature = "context")]
if self.config.modules.context {
tools.extend(self.context.get_tools());
}
#[cfg(feature = "git")]
if self.config.modules.git {
tools.extend(self.git.get_tools());
}
#[cfg(feature = "input")]
if self.config.modules.input {
tools.extend(self.input.get_tools());
}
#[cfg(feature = "gitent")]
if self.config.modules.gitent {
tools.extend(self.gitent.get_tools());
}
#[cfg(feature = "clipboard")]
if self.config.modules.clipboard {
tools.extend(self.clipboard.get_tools());
}
#[cfg(feature = "transform")]
if self.config.modules.transform {
tools.extend(self.transform.get_tools());
}
if !self.config.tools.enabled.is_empty() {
tools.retain(|t| {
let name = t["name"].as_str().unwrap_or("");
self.config.tools.enabled.iter().any(|e| e == name)
});
}
if !self.config.tools.disabled.is_empty() {
tools.retain(|t| {
let name = t["name"].as_str().unwrap_or("");
!self.config.tools.disabled.iter().any(|d| d == name)
});
}
json!({ "tools": tools })
}
pub async fn call_tool(&mut self, name: &str, arguments: Option<Value>) -> Result<Value> {
if !self.config.is_tool_enabled(name) {
return Err(anyhow::anyhow!(
"Tool '{}' is disabled by configuration",
name
));
}
#[allow(unused_variables)]
let args = arguments.unwrap_or(json!({}));
match name {
#[cfg(feature = "filesystem")]
"fs_read" => self.filesystem.read(args).await,
#[cfg(feature = "filesystem")]
"fs_write" => self.filesystem.write(args).await,
#[cfg(feature = "filesystem")]
"fs_move" => self.filesystem.move_file(args).await,
#[cfg(feature = "filesystem")]
"fs_copy" => self.filesystem.copy(args).await,
#[cfg(feature = "filesystem")]
"fs_create" => self.filesystem.create(args).await,
#[cfg(feature = "filesystem")]
"fs_delete" => self.filesystem.delete(args).await,
#[cfg(feature = "filesystem")]
"fs_move_desktop" => self.filesystem.move_desktop(args).await,
#[cfg(feature = "filesystem")]
"fs_find" => self.filesystem.find(args).await,
#[cfg(feature = "filesystem")]
"fs_ld" => self.filesystem.ld(args).await,
#[cfg(feature = "filesystem")]
"fs_stat" => self.filesystem.stat(args).await,
#[cfg(feature = "filesystem")]
"fs_permissions" => self.filesystem.permissions(args).await,
#[cfg(feature = "filesystem")]
"fs_watch" => self.filesystem.watch(args).await,
#[cfg(feature = "filesystem")]
"fs_snapshot" => self.filesystem.snapshot(args).await,
#[cfg(feature = "filesystem")]
"fs_tree" => self.filesystem.tree(args).await,
#[cfg(feature = "filesystem")]
"fs_grep" => self.filesystem.grep(args).await,
#[cfg(feature = "filesystem")]
"fs_tail" => self.filesystem.tail(args).await,
#[cfg(feature = "filesystem")]
"fs_replace" => self.filesystem.replace(args).await,
#[cfg(feature = "diagnostics")]
"diagnostics_get" => self.diagnostics.get(args).await,
#[cfg(feature = "silent")]
"silent_script" => self.silent.script(args).await,
#[cfg(feature = "silent")]
"silent_resources" => self.silent.resources(args).await,
#[cfg(feature = "time")]
"time_now" => self.time.now(args).await,
#[cfg(feature = "time")]
"time_sleep" => self.time.sleep(args).await,
#[cfg(feature = "time")]
"time_schedule" => self.time.schedule(args).await,
#[cfg(feature = "time")]
"time_timezone" => self.time.timezone(args).await,
#[cfg(feature = "time")]
"time_stopwatch" => self.time.stopwatch(args).await,
#[cfg(feature = "time")]
"time_timer" => self.time.timer(args).await,
#[cfg(feature = "time")]
"time_alarm" => self.time.alarm(args).await,
#[cfg(feature = "network")]
"net_fetch" => self.network.fetch(args).await,
#[cfg(feature = "network")]
"net_cargo" => self.network.cargo(args).await,
#[cfg(feature = "network")]
"net_node" => self.network.node(args).await,
#[cfg(feature = "network")]
"net_python" => self.network.python(args).await,
#[cfg(feature = "network")]
"net_apt" => self.network.apt(args).await,
#[cfg(feature = "network")]
"net_ping" => self.network.ping(args).await,
#[cfg(feature = "context")]
"ctx_context" => self.context.context(args).await,
#[cfg(feature = "context")]
"ctx_compact" => self.context.compact_context(args).await,
#[cfg(feature = "context")]
"ctx_remove" => self.context.remove_context(args).await,
#[cfg(feature = "context")]
"ctx_token_count" => self.context.token_count(args).await,
#[cfg(feature = "context")]
"ctx_memory_store" => self.context.memory_store(args).await,
#[cfg(feature = "context")]
"ctx_memory_recall" => self.context.memory_recall(args).await,
#[cfg(feature = "context")]
"ctx_estimate_cost" => self.context.estimate_cost(args).await,
#[cfg(feature = "git")]
"git_status" => self.git.status(args).await,
#[cfg(feature = "git")]
"git_diff" => self.git.diff(args).await,
#[cfg(feature = "git")]
"git_commit" => self.git.commit(args).await,
#[cfg(feature = "git")]
"git_branch" => self.git.branch(args).await,
#[cfg(feature = "git")]
"git_checkout" => self.git.checkout(args).await,
#[cfg(feature = "git")]
"git_blame" => self.git.blame(args).await,
#[cfg(feature = "git")]
"git_log" => self.git.log(args).await,
#[cfg(feature = "git")]
"git_tag" => self.git.tag(args).await,
#[cfg(feature = "input")]
"input_notify" => self.input.notify(args).await,
#[cfg(feature = "input")]
"input_prompt" => self.input.prompt_user(args).await,
#[cfg(feature = "input")]
"input_select" => self.input.select(args).await,
#[cfg(feature = "input")]
"input_progress" => self.input.progress(args).await,
#[cfg(feature = "input")]
"input_clipboard_read" => self.input.clipboard_read(args).await,
#[cfg(feature = "input")]
"input_clipboard_write" => self.input.clipboard_write(args).await,
#[cfg(feature = "gitent")]
"gitent_init" => self.gitent.init(args).await,
#[cfg(feature = "gitent")]
"gitent_status" => self.gitent.status(args).await,
#[cfg(feature = "gitent")]
"gitent_track" => self.gitent.track(args).await,
#[cfg(feature = "gitent")]
"gitent_commit" => self.gitent.commit(args).await,
#[cfg(feature = "gitent")]
"gitent_log" => self.gitent.log(args).await,
#[cfg(feature = "gitent")]
"gitent_diff" => self.gitent.diff(args).await,
#[cfg(feature = "gitent")]
"gitent_rollback" => self.gitent.rollback(args).await,
#[cfg(feature = "clipboard")]
"clip_copy_file" => self.clipboard.copy_file(args).await,
#[cfg(feature = "clipboard")]
"clip_copy" => self.clipboard.copy(args).await,
#[cfg(feature = "clipboard")]
"clip_paste_file" => self.clipboard.paste_file(args).await,
#[cfg(feature = "clipboard")]
"clip_paste" => self.clipboard.paste(args).await,
#[cfg(feature = "clipboard")]
"clip_clear" => self.clipboard.clear(args).await,
#[cfg(feature = "transform")]
"transform_diff" => self.transform.diff(args).await,
#[cfg(feature = "transform")]
"transform_encode" => self.transform.encode(args).await,
#[cfg(feature = "transform")]
"transform_hash" => self.transform.hash(args).await,
#[cfg(feature = "transform")]
"transform_regex" => self.transform.regex_op(args).await,
#[cfg(feature = "transform")]
"transform_json" => self.transform.json_op(args).await,
#[cfg(feature = "transform")]
"transform_text" => self.transform.text(args).await,
#[cfg(feature = "transform")]
"transform_archive" => self.transform.archive(args).await,
_ => Err(anyhow::anyhow!("Unknown tool: {}", name)),
}
}
}