poly-mcp 0.5.0

A comprehensive MCP server with filesystem, diagnostics, scripting, time, network, context, git, input, clipboard, transform, and screen control tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Runtime configuration for enabling/disabling modules and individual tools.
///
/// Tools are gated at two levels:
/// 1. **Compile-time** - Cargo feature flags (e.g., `--no-default-features --features "filesystem,git"`)
/// 2. **Runtime** - This config, via CLI flags or a JSON config file
///
/// A tool is available only if its module is both compiled in AND runtime-enabled.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolConfig {
    /// Per-module enable/disable toggles
    #[serde(default)]
    pub modules: ModuleConfig,

    /// Tool-level filtering (applied after module filtering)
    #[serde(default)]
    pub tools: ToolFilterConfig,
}

/// Per-module enable/disable toggles. All default to `true`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleConfig {
    #[serde(default = "yes")]
    pub filesystem: bool,
    #[serde(default = "yes")]
    pub diagnostics: bool,
    #[serde(default = "yes")]
    pub silent: bool,
    #[serde(default = "yes")]
    pub time: bool,
    #[serde(default = "yes")]
    pub network: bool,
    #[serde(default = "yes")]
    pub context: bool,
    #[serde(default = "yes")]
    pub git: bool,
    #[serde(default = "yes")]
    pub input: bool,
    #[serde(default = "yes")]
    pub gitent: bool,
    #[serde(default = "yes")]
    pub clipboard: bool,
    #[serde(default = "yes")]
    pub transform: bool,
    #[serde(default = "yes")]
    pub screen: bool,
    #[serde(default = "yes")]
    pub browser: bool,
}

/// Tool-level whitelist/blacklist filtering.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ToolFilterConfig {
    /// Whitelist: if non-empty, ONLY these tools are available (takes precedence)
    #[serde(default)]
    pub enabled: Vec<String>,
    /// Blacklist: these specific tools are disabled
    #[serde(default)]
    pub disabled: Vec<String>,
}

fn yes() -> bool {
    true
}

impl Default for ToolConfig {
    fn default() -> Self {
        Self {
            modules: ModuleConfig::default(),
            tools: ToolFilterConfig::default(),
        }
    }
}

impl Default for ModuleConfig {
    fn default() -> Self {
        Self {
            filesystem: true,
            diagnostics: true,
            silent: true,
            time: true,
            network: true,
            context: true,
            git: true,
            input: true,
            gitent: true,
            clipboard: true,
            transform: true,
            screen: true,
            browser: true,
        }
    }
}

impl ModuleConfig {
    /// Create a config with all modules disabled.
    pub fn all_disabled() -> Self {
        Self {
            filesystem: false,
            diagnostics: false,
            silent: false,
            time: false,
            network: false,
            context: false,
            git: false,
            input: false,
            gitent: false,
            clipboard: false,
            transform: false,
            screen: false,
            browser: false,
        }
    }

    /// Set a module by name. Supports short aliases (fs, diag, net, ctx, clip).
    pub fn set(&mut self, name: &str, enabled: bool) {
        match name {
            "filesystem" | "fs" => self.filesystem = enabled,
            "diagnostics" | "diag" => self.diagnostics = enabled,
            "silent" => self.silent = enabled,
            "time" => self.time = enabled,
            "network" | "net" => self.network = enabled,
            "context" | "ctx" => self.context = enabled,
            "git" => self.git = enabled,
            "input" => self.input = enabled,
            "gitent" => self.gitent = enabled,
            "clipboard" | "clip" => self.clipboard = enabled,
            "transform" => self.transform = enabled,
            "screen" => self.screen = enabled,
            "browser" => self.browser = enabled,
            _ => eprintln!("Warning: unknown module '{}', ignoring", name),
        }
    }

    /// Check if a module is enabled by name.
    pub fn is_enabled(&self, name: &str) -> bool {
        match name {
            "filesystem" => self.filesystem,
            "diagnostics" => self.diagnostics,
            "silent" => self.silent,
            "time" => self.time,
            "network" => self.network,
            "context" => self.context,
            "git" => self.git,
            "input" => self.input,
            "gitent" => self.gitent,
            "clipboard" => self.clipboard,
            "transform" => self.transform,
            "screen" => self.screen,
            "browser" => self.browser,
            _ => false,
        }
    }

    /// List of all enabled module names.
    #[allow(dead_code)]
    pub fn enabled_list(&self) -> Vec<&'static str> {
        let mut v = Vec::new();
        if self.filesystem {
            v.push("filesystem");
        }
        if self.diagnostics {
            v.push("diagnostics");
        }
        if self.silent {
            v.push("silent");
        }
        if self.time {
            v.push("time");
        }
        if self.network {
            v.push("network");
        }
        if self.context {
            v.push("context");
        }
        if self.git {
            v.push("git");
        }
        if self.input {
            v.push("input");
        }
        if self.gitent {
            v.push("gitent");
        }
        if self.clipboard {
            v.push("clipboard");
        }
        if self.transform {
            v.push("transform");
        }
        if self.screen {
            v.push("screen");
        }
        if self.browser {
            v.push("browser");
        }
        v
    }
}

impl ToolConfig {
    // ── Constructors ────────────────────────────────────────────────

    /// Config with only the specified modules enabled (all others disabled).
    ///
    /// Accepts full names or aliases: `fs`, `diag`, `net`, `ctx`, `clip`.
    ///
    /// ```
    /// use poly_mcp::ToolConfig;
    /// let cfg = ToolConfig::only(&["fs", "git"]);
    /// assert!(cfg.modules.filesystem);
    /// assert!(cfg.modules.git);
    /// assert!(!cfg.modules.network);
    /// ```
    pub fn only(modules: &[&str]) -> Self {
        let mut config = Self {
            modules: ModuleConfig::all_disabled(),
            tools: ToolFilterConfig::default(),
        };
        for m in modules {
            config.modules.set(m, true);
        }
        config
    }

    /// Config with all modules enabled except the specified ones.
    ///
    /// ```
    /// use poly_mcp::ToolConfig;
    /// let cfg = ToolConfig::without(&["silent", "input"]);
    /// assert!(cfg.modules.filesystem);
    /// assert!(!cfg.modules.silent);
    /// ```
    pub fn without(modules: &[&str]) -> Self {
        let mut config = Self::default();
        for m in modules {
            config.modules.set(m, false);
        }
        config
    }

    // ── Chainable modifiers ─────────────────────────────────────────

    /// Enable a module by name. Chainable.
    pub fn enable_module(mut self, name: &str) -> Self {
        self.modules.set(name, true);
        self
    }

    /// Disable a module by name. Chainable.
    pub fn disable_module(mut self, name: &str) -> Self {
        self.modules.set(name, false);
        self
    }

    /// Add a tool to the blacklist. Chainable.
    ///
    /// ```
    /// use poly_mcp::ToolConfig;
    /// let cfg = ToolConfig::only(&["fs", "git"])
    ///     .disable_tool("fs_delete")
    ///     .disable_tool("git_tag");
    /// assert!(!cfg.is_tool_enabled("fs_delete"));
    /// assert!(cfg.is_tool_enabled("fs_read"));
    /// ```
    pub fn disable_tool(mut self, name: impl Into<String>) -> Self {
        let name = name.into();
        if !self.tools.disabled.contains(&name) {
            self.tools.disabled.push(name);
        }
        self
    }

    /// Set the tool whitelist: only these specific tools will be available
    /// (from their respective enabled modules). Chainable.
    ///
    /// ```
    /// use poly_mcp::ToolConfig;
    /// let cfg = ToolConfig::default()
    ///     .enable_only_tools(&["fs_read", "git_status"]);
    /// assert!(cfg.is_tool_enabled("fs_read"));
    /// assert!(!cfg.is_tool_enabled("fs_write"));
    /// ```
    pub fn enable_only_tools(mut self, names: &[&str]) -> Self {
        self.tools.enabled = names.iter().map(|s| s.to_string()).collect();
        self
    }

    // ── Loading ─────────────────────────────────────────────────────

    /// Load config from a JSON file.
    pub fn from_file(path: &Path) -> anyhow::Result<Self> {
        let content = std::fs::read_to_string(path)?;
        let config: Self = serde_json::from_str(&content)?;
        Ok(config)
    }

    /// Apply CLI-style overrides on top of the current config.
    ///
    /// - `enable_modules`: comma-separated whitelist (disables all others)
    /// - `disable_modules`: comma-separated blacklist
    /// - `disable_tools`: comma-separated tool blacklist
    pub fn apply_cli(
        &mut self,
        enable_modules: Option<&str>,
        disable_modules: Option<&str>,
        disable_tools: Option<&str>,
    ) {
        // Whitelist mode: disable all, then enable specified
        if let Some(modules) = enable_modules {
            self.modules = ModuleConfig::all_disabled();
            for m in modules.split(',').map(str::trim).filter(|s| !s.is_empty()) {
                self.modules.set(m, true);
            }
        }

        // Blacklist mode
        if let Some(modules) = disable_modules {
            for m in modules.split(',').map(str::trim).filter(|s| !s.is_empty()) {
                self.modules.set(m, false);
            }
        }

        // Tool blacklist
        if let Some(tools) = disable_tools {
            for t in tools.split(',').map(str::trim).filter(|s| !s.is_empty()) {
                if !self.tools.disabled.contains(&t.to_string()) {
                    self.tools.disabled.push(t.to_string());
                }
            }
        }
    }

    // ── Queries ─────────────────────────────────────────────────────

    /// Check if a tool is available given both module and tool-level filters.
    pub fn is_tool_enabled(&self, tool_name: &str) -> bool {
        // Module-level check
        let module = tool_module(tool_name);
        if !self.modules.is_enabled(module) {
            return false;
        }

        // Tool whitelist check
        if !self.tools.enabled.is_empty()
            && !self.tools.enabled.iter().any(|t| t == tool_name)
        {
            return false;
        }

        // Tool blacklist check
        if self.tools.disabled.iter().any(|t| t == tool_name) {
            return false;
        }

        true
    }

    /// Pretty-print the default config as JSON.
    pub fn generate_default() -> String {
        serde_json::to_string_pretty(&Self::default()).unwrap()
    }
}

/// Check whether a module is compiled in (via Cargo feature flags).
pub fn is_module_compiled(name: &str) -> bool {
    match name {
        "filesystem" => cfg!(feature = "filesystem"),
        "diagnostics" => cfg!(feature = "diagnostics"),
        "silent" => cfg!(feature = "silent"),
        "time" => cfg!(feature = "time"),
        "network" => cfg!(feature = "network"),
        "context" => cfg!(feature = "context"),
        "git" => cfg!(feature = "git"),
        "input" => cfg!(feature = "input"),
        "gitent" => cfg!(feature = "gitent"),
        "clipboard" => cfg!(feature = "clipboard"),
        "transform" => cfg!(feature = "transform"),
        "screen" => cfg!(feature = "screen"),
        "browser" => cfg!(feature = "browser"),
        _ => false,
    }
}

/// Map a tool name prefix to its module name.
pub fn tool_module(name: &str) -> &str {
    if name.starts_with("fs_") {
        "filesystem"
    } else if name.starts_with("diagnostics_") {
        "diagnostics"
    } else if name.starts_with("silent_") {
        "silent"
    } else if name.starts_with("time_") {
        "time"
    } else if name.starts_with("net_") {
        "network"
    } else if name.starts_with("ctx_") {
        "context"
    } else if name.starts_with("git_") {
        "git"
    } else if name.starts_with("input_") {
        "input"
    } else if name.starts_with("gitent_") {
        "gitent"
    } else if name.starts_with("clip_") {
        "clipboard"
    } else if name.starts_with("transform_") {
        "transform"
    } else if name.starts_with("screen_") {
        "screen"
    } else if name.starts_with("browser_") {
        "browser"
    } else {
        "unknown"
    }
}

/// All known module names (compile-time constant).
#[allow(dead_code)]
pub const ALL_MODULE_NAMES: &[&str] = &[
    "filesystem",
    "diagnostics",
    "silent",
    "time",
    "network",
    "context",
    "git",
    "input",
    "gitent",
    "clipboard",
    "transform",
    "screen",
    "browser",
];

/// Module metadata for display.
pub struct ModuleInfo {
    pub name: &'static str,
    pub description: &'static str,
    pub tool_count: usize,
    pub tools: &'static [&'static str],
}

/// Get metadata for all modules (regardless of compile/runtime state).
pub fn all_module_info() -> Vec<ModuleInfo> {
    vec![
        ModuleInfo {
            name: "filesystem",
            description: "File and directory operations",
            tool_count: 17,
            tools: &[
                "fs_read", "fs_write", "fs_move", "fs_copy", "fs_create", "fs_delete",
                "fs_move_desktop", "fs_find", "fs_ld", "fs_stat", "fs_permissions",
                "fs_watch", "fs_snapshot", "fs_tree", "fs_grep", "fs_tail", "fs_replace",
            ],
        },
        ModuleInfo {
            name: "diagnostics",
            description: "Language-agnostic error detection",
            tool_count: 1,
            tools: &["diagnostics_get"],
        },
        ModuleInfo {
            name: "silent",
            description: "Bash scripting and resource monitoring",
            tool_count: 2,
            tools: &["silent_script", "silent_resources"],
        },
        ModuleInfo {
            name: "time",
            description: "Time management, scheduling & timekeeping",
            tool_count: 7,
            tools: &[
                "time_now", "time_sleep", "time_schedule",
                "time_timezone", "time_stopwatch", "time_timer", "time_alarm",
            ],
        },
        ModuleInfo {
            name: "network",
            description: "HTTP requests and package queries",
            tool_count: 6,
            tools: &["net_fetch", "net_cargo", "net_node", "net_python", "net_apt", "net_ping"],
        },
        ModuleInfo {
            name: "context",
            description: "Token counting and cost estimation",
            tool_count: 7,
            tools: &[
                "ctx_context", "ctx_compact", "ctx_remove", "ctx_token_count",
                "ctx_memory_store", "ctx_memory_recall", "ctx_estimate_cost",
            ],
        },
        ModuleInfo {
            name: "git",
            description: "Complete git operations",
            tool_count: 8,
            tools: &[
                "git_status", "git_diff", "git_commit", "git_branch",
                "git_checkout", "git_blame", "git_log", "git_tag",
            ],
        },
        ModuleInfo {
            name: "input",
            description: "User interaction and notifications",
            tool_count: 6,
            tools: &[
                "input_notify", "input_prompt", "input_select", "input_progress",
                "input_clipboard_read", "input_clipboard_write",
            ],
        },
        ModuleInfo {
            name: "gitent",
            description: "Agent-centric version control tracking",
            tool_count: 7,
            tools: &[
                "gitent_init", "gitent_status", "gitent_track", "gitent_commit",
                "gitent_log", "gitent_diff", "gitent_rollback",
            ],
        },
        ModuleInfo {
            name: "clipboard",
            description: "Session copy/paste with tags",
            tool_count: 5,
            tools: &["clip_copy_file", "clip_copy", "clip_paste_file", "clip_paste", "clip_clear"],
        },
        ModuleInfo {
            name: "transform",
            description: "Text & data processing",
            tool_count: 7,
            tools: &[
                "transform_diff", "transform_encode", "transform_hash", "transform_regex",
                "transform_json", "transform_text", "transform_archive",
            ],
        },
        ModuleInfo {
            name: "screen",
            description: "Screen capture, input simulation, window & element control",
            tool_count: 18,
            tools: &[
                "screen_capture", "screen_click", "screen_type", "screen_key",
                "screen_move", "screen_scroll", "screen_drag", "screen_info",
                "screen_windows", "screen_focus", "screen_wait",
                "screen_ocr", "screen_find", "screen_elements",
                "screen_clipboard", "screen_pixel", "screen_diff", "screen_process",
            ],
        },
        ModuleInfo {
            name: "browser",
            description: "Headless browser control via Chrome DevTools Protocol",
            tool_count: 14,
            tools: &[
                "browser_launch", "browser_navigate", "browser_screenshot",
                "browser_content", "browser_query", "browser_click",
                "browser_fill", "browser_eval", "browser_tabs", "browser_close",
                "browser_wait", "browser_scroll", "browser_hover", "browser_select",
            ],
        },
    ]
}