diff --git a/README.md b/README.md index d0c76e1..f51add4 100644 --- a/README.md +++ b/README.md @@ -12,21 +12,104 @@ This MCP server exposes that information as structured tool calls, so you can as ## Tools +### Instance Management + +| Tool | Description | Mutating | +|------|-------------|----------| +| `syncthing_list_instances` | List all configured instances, probe availability | No | + +### System Status & Monitoring + | Tool | Description | Mutating | |------|-------------|----------| | `syncthing_system_status` | Device ID, name, uptime, version | No | | `syncthing_list_folders` | All folders with paths, types, and shared device lists | No | | `syncthing_list_devices` | All devices with connection status and last seen | No | +| `syncthing_connections` | Active connection details (addresses, crypto, throughput) | No | +| `syncthing_system_errors` | Recent system errors and warnings | No | +| `syncthing_clear_errors` | Clear the system error log | Yes | +| `syncthing_system_log` | Recent log entries with timestamps and levels | No | +| `syncthing_recent_changes` | Recent file change events (local + remote) | No | +| `syncthing_health_summary` | Single-call health overview: status, alerts, folder/device/pending counts | No | + +### Folder Status & Replication + +| Tool | Description | Mutating | +|------|-------------|----------| | `syncthing_folder_status` | Detailed status for a single folder (files, bytes, sync state) | No | | `syncthing_folder_completion` | Per-device completion % for a folder | No | | `syncthing_replication_report` | Analyses all folders, flags safe-to-remove, calculates reclaimable space | No | -| `syncthing_device_completion` | Aggregated completion for a device across all shared folders | No | -| `syncthing_connections` | Active connection details (addresses, crypto, throughput) | No | | `syncthing_folder_errors` | Current sync errors for a folder | No | + +### Device Status + +| Tool | Description | Mutating | +|------|-------------|----------| +| `syncthing_device_completion` | Aggregated completion for a device across all shared folders | No | + +### Folder Operations + +| Tool | Description | Mutating | +|------|-------------|----------| | `syncthing_scan_folder` | Trigger an immediate rescan | Yes | | `syncthing_pause_folder` | Pause a folder (prevents deletion propagation) | Yes | | `syncthing_resume_folder` | Resume a paused folder | Yes | +### Config Mutation: Pending Devices & Folders + +| Tool | Description | Mutating | +|------|-------------|----------| +| `syncthing_pending_devices` | List pending device connection requests | No | +| `syncthing_pending_folders` | List pending folder share offers | No | +| `syncthing_accept_device` | Accept a pending device (adds to config with defaults) | Yes | +| `syncthing_reject_device` | Dismiss a pending device request | Yes | +| `syncthing_accept_folder` | Accept a pending folder offer (uses folder defaults template) | Yes | +| `syncthing_reject_folder` | Dismiss a pending folder offer | Yes | + +### Config Mutation: Restart & Ignore Patterns + +| Tool | Description | Mutating | +|------|-------------|----------| +| `syncthing_restart_required` | Check if config changes require a restart | No | +| `syncthing_restart` | Restart the Syncthing service | Yes | +| `syncthing_get_ignores` | Get .stignore patterns for a folder | No | +| `syncthing_set_ignores` | Set .stignore patterns for a folder (replaces all) | Yes | +| `syncthing_get_default_ignores` | Get default ignore patterns for new folders | No | +| `syncthing_set_default_ignores` | Set default ignore patterns for new folders | Yes | + +## Multi-Instance Support + +Connect to multiple Syncthing nodes simultaneously by setting the `SYNCTHING_INSTANCES` environment variable with a JSON object: + +```json +{ + "mini": {"url": "http://mini.local:8384", "api_key": "abc123"}, + "tn-sb": {"url": "http://tn-sb.local:8384", "api_key": "def456"}, + "tn-mr2": {"url": "http://tn-mr2.local:8384", "api_key": "ghi789"} +} +``` + +Every tool accepts an optional `instance` parameter to target a specific node. When only one instance is configured, the parameter can be omitted. + +### Claude Desktop config (multi-instance) + +```json +{ + "mcpServers": { + "syncthing": { + "command": "uv", + "args": [ + "--directory", "/path/to/syncthing-mcp", + "run", "syncthing_mcp.py" + ], + "env": { + "SYNCTHING_INSTANCES": "{\"mini\":{\"url\":\"http://mini.local:8384\",\"api_key\":\"abc123\"},\"tn-sb\":{\"url\":\"http://tn-sb.local:8384\",\"api_key\":\"def456\"}}" + } + } + } +} +``` + ## Safe Removal Workflow 1. Run `syncthing_replication_report` — review which folders show `safeToRemove: true` @@ -61,7 +144,7 @@ Open the Syncthing web UI → Actions → Settings → API Key. Alternatively, find it in your Syncthing config file under ``. -### Configure Claude Desktop +### Configure Claude Desktop (single instance) Add to your Claude Desktop MCP config (`~/Library/Application Support/Claude/claude_desktop_config.json` on macOS): @@ -87,27 +170,27 @@ Add to your Claude Desktop MCP config (`~/Library/Application Support/Claude/cla | Variable | Required | Default | Description | |----------|----------|---------|-------------| -| `SYNCTHING_API_KEY` | Yes | — | API key from Syncthing settings | -| `SYNCTHING_URL` | No | `http://localhost:8384` | Base URL for the Syncthing REST API | +| `SYNCTHING_API_KEY` | Yes* | — | API key (single-instance mode) | +| `SYNCTHING_URL` | No | `http://localhost:8384` | Base URL (single-instance mode) | +| `SYNCTHING_INSTANCES` | No | — | JSON object for multi-instance mode (overrides the above) | + +\* Required unless `SYNCTHING_INSTANCES` is set. ## Roadmap -This server is the foundation for a broader goal: a **Syncthing dataset dashboard** that provides ongoing visibility into synchronisation health across a fleet of devices. - Planned directions include: -- **Multi-instance support** — query multiple Syncthing nodes (e.g. NAS, servers, laptops) from a single MCP session, enabling cross-fleet replication views -- **Event stream integration** — subscribe to Syncthing's `/rest/events` endpoint for real-time sync status, conflict detection, and error alerting -- **Historical tracking** — persist folder size and completion data over time to surface trends (growth rate, sync frequency, stale folders) -- **Conflict management** — surface and resolve sync conflicts through tool calls rather than manual file inspection -- **Ignore pattern management** — read and update `.stignore` rules per folder +- **Event stream integration** — subscribe to Syncthing's `/rest/events` endpoint for real-time sync status and conflict detection +- **Historical tracking** — persist folder size and completion data over time to surface trends +- **Conflict management** — surface and resolve sync conflicts through tool calls - **Folder lifecycle tools** — add/remove folders and device sharing relationships programmatically -- **Dashboard artifact** — a standalone HTML/React view that visualises replication topology, per-folder health, and reclaimable space across all nodes +- **Dashboard artifact** — a standalone HTML/React view that visualises replication topology across all nodes ## Performance Notes - `syncthing_folder_status` and `syncthing_replication_report` call Syncthing's `/rest/db/status` endpoint, which the Syncthing docs describe as "an expensive call, increasing CPU and RAM usage on the device." Use judiciously on low-power hardware. - The replication report makes one `/rest/db/completion` call per remote device per folder. On setups with many folders and devices, this can generate significant API traffic. +- `syncthing_health_summary` calls `/rest/db/status` for each folder — equivalent cost to the replication report. ## Contributing diff --git a/syncthing_mcp.py b/syncthing_mcp.py index 88f0d5f..45c23cb 100644 --- a/syncthing_mcp.py +++ b/syncthing_mcp.py @@ -1,88 +1,145 @@ """ Syncthing MCP Server -An MCP server for interacting with the Syncthing REST API. -Provides tools for querying folder status, device connections, -and replication completeness — designed to help identify which -folders are safely replicated and can be removed locally to -free disk space. +An MCP server for interacting with one or more Syncthing instances via their +REST API. Provides tools for querying folder status, device connections, +replication completeness, config mutation (accept/reject pending, restart, +ignore patterns), and expanded monitoring. Configuration via environment variables: + + Single instance (backward-compatible): SYNCTHING_API_KEY - Required. API key for Syncthing REST API. SYNCTHING_URL - Optional. Base URL (default: http://localhost:8384) + + Multiple instances: + SYNCTHING_INSTANCES - JSON object mapping instance names to config: + { + "mini": {"url": "http://mini.local:8384", "api_key": "xxx"}, + "tn-sb": {"url": "http://tn-sb.local:8384", "api_key": "yyy"} + } + When set, SYNCTHING_URL and SYNCTHING_API_KEY are ignored. """ import json import os from contextlib import asynccontextmanager -from typing import Any, Dict, List, Optional +from typing import Any import httpx from mcp.server.fastmcp import FastMCP from pydantic import BaseModel, ConfigDict, Field -# --------------------------------------------------------------------------- -# Configuration -# --------------------------------------------------------------------------- - -SYNCTHING_URL = os.environ.get("SYNCTHING_URL", "http://localhost:8384").rstrip("/") -SYNCTHING_API_KEY = os.environ.get("SYNCTHING_API_KEY", "") # --------------------------------------------------------------------------- -# HTTP client helpers +# Syncthing HTTP Client # --------------------------------------------------------------------------- -def _headers() -> dict[str, str]: - return { - "X-API-Key": SYNCTHING_API_KEY, - "Accept": "application/json", - } +class SyncthingClient: + """HTTP client for a single Syncthing instance.""" + def __init__(self, name: str, url: str, api_key: str) -> None: + self.name = name + self.url = url.rstrip("/") + self.api_key = api_key -async def _get(path: str, params: dict | None = None) -> Any: - """Perform an authenticated GET against the Syncthing REST API.""" - async with httpx.AsyncClient(timeout=30.0) as client: - resp = await client.get( - f"{SYNCTHING_URL}{path}", - headers=_headers(), - params=params, - ) - resp.raise_for_status() - return resp.json() + def _headers(self) -> dict[str, str]: + return { + "X-API-Key": self.api_key, + "Accept": "application/json", + } - -async def _post(path: str, params: dict | None = None, body: Any = None) -> Any: - """Perform an authenticated POST against the Syncthing REST API.""" - async with httpx.AsyncClient(timeout=30.0) as client: - resp = await client.post( - f"{SYNCTHING_URL}{path}", - headers=_headers(), - params=params, - json=body, - ) - resp.raise_for_status() - if resp.headers.get("content-type", "").startswith("application/json"): + async def _get(self, path: str, params: dict | None = None) -> Any: + """Authenticated GET against this instance.""" + async with httpx.AsyncClient(timeout=30.0) as client: + resp = await client.get( + f"{self.url}{path}", + headers=self._headers(), + params=params, + ) + resp.raise_for_status() return resp.json() - return {"status": "ok"} + + async def _post(self, path: str, params: dict | None = None, body: Any = None) -> Any: + """Authenticated POST against this instance.""" + async with httpx.AsyncClient(timeout=30.0) as client: + resp = await client.post( + f"{self.url}{path}", + headers=self._headers(), + params=params, + json=body, + ) + resp.raise_for_status() + ct = resp.headers.get("content-type", "") + if ct.startswith("application/json") and resp.content: + return resp.json() + return {"status": "ok"} + + async def _patch(self, path: str, body: Any = None) -> Any: + """Authenticated PATCH against this instance.""" + async with httpx.AsyncClient(timeout=30.0) as client: + resp = await client.patch( + f"{self.url}{path}", + headers=self._headers(), + json=body, + ) + resp.raise_for_status() + ct = resp.headers.get("content-type", "") + if ct.startswith("application/json") and resp.content: + return resp.json() + return {"status": "ok"} + + async def _put(self, path: str, body: Any = None) -> Any: + """Authenticated PUT against this instance.""" + async with httpx.AsyncClient(timeout=30.0) as client: + resp = await client.put( + f"{self.url}{path}", + headers=self._headers(), + json=body, + ) + resp.raise_for_status() + ct = resp.headers.get("content-type", "") + if ct.startswith("application/json") and resp.content: + return resp.json() + return {"status": "ok"} + + async def _delete(self, path: str, params: dict | None = None) -> Any: + """Authenticated DELETE against this instance.""" + async with httpx.AsyncClient(timeout=30.0) as client: + resp = await client.delete( + f"{self.url}{path}", + headers=self._headers(), + params=params, + ) + resp.raise_for_status() + ct = resp.headers.get("content-type", "") + if ct.startswith("application/json") and resp.content: + return resp.json() + return {"status": "ok"} + + def handle_error(self, e: Exception) -> str: + """Consistent error formatting referencing this instance.""" + prefix = f"[{self.name}] " if self.name != "default" else "" + if isinstance(e, httpx.HTTPStatusError): + status = e.response.status_code + if status == 401: + return f"{prefix}Error 401: Unauthorized. Check API key for instance '{self.name}'." + if status == 403: + return f"{prefix}Error 403: Forbidden. API key may lack permissions." + if status == 404: + return f"{prefix}Error 404: Not found. Check the folder/device ID. Detail: {e.response.text}" + return f"{prefix}Error {status}: {e.response.text}" + if isinstance(e, httpx.ConnectError): + return f"{prefix}Error: Cannot connect to Syncthing at {self.url}. Is it running?" + if isinstance(e, httpx.TimeoutException): + return f"{prefix}Error: Request timed out. Syncthing may be busy or unreachable." + return f"{prefix}Error: {type(e).__name__}: {e}" -def _handle_api_error(e: Exception) -> str: - """Consistent error formatting.""" - if isinstance(e, httpx.HTTPStatusError): - status = e.response.status_code - if status == 401: - return "Error 401: Unauthorized. Check SYNCTHING_API_KEY is correct." - if status == 403: - return "Error 403: Forbidden. API key may lack permissions." - if status == 404: - return f"Error 404: Not found. Check the folder/device ID. Detail: {e.response.text}" - return f"Error {status}: {e.response.text}" - if isinstance(e, httpx.ConnectError): - return f"Error: Cannot connect to Syncthing at {SYNCTHING_URL}. Is it running?" - if isinstance(e, httpx.TimeoutException): - return "Error: Request timed out. Syncthing may be busy or unreachable." - return f"Error: {type(e).__name__}: {e}" +# --------------------------------------------------------------------------- +# Instance registry +# --------------------------------------------------------------------------- def _format_bytes(n: int) -> str: @@ -94,15 +151,76 @@ def _format_bytes(n: int) -> str: return f"{n:.1f} PB" +def _load_instances() -> dict[str, SyncthingClient]: + """Build instance registry from environment variables.""" + instances_json = os.environ.get("SYNCTHING_INSTANCES", "").strip() + + if instances_json: + try: + cfg = json.loads(instances_json) + except json.JSONDecodeError as exc: + raise ValueError(f"Invalid SYNCTHING_INSTANCES JSON: {exc}") from exc + if not isinstance(cfg, dict) or not cfg: + raise ValueError("SYNCTHING_INSTANCES must be a non-empty JSON object.") + instances: dict[str, SyncthingClient] = {} + for name, entry in cfg.items(): + if not isinstance(entry, dict): + raise ValueError(f"Instance '{name}' config must be a JSON object.") + url = entry.get("url", "http://localhost:8384") + api_key = entry.get("api_key", "") + instances[name] = SyncthingClient(name, url, api_key) + return instances + + # Backward-compatible single-instance fallback + url = os.environ.get("SYNCTHING_URL", "http://localhost:8384") + api_key = os.environ.get("SYNCTHING_API_KEY", "") + return {"default": SyncthingClient("default", url, api_key)} + + +_instances: dict[str, SyncthingClient] = _load_instances() + + +def _get_instance(instance: str | None = None) -> SyncthingClient: + """Resolve an instance by name, or auto-select when there is only one.""" + if instance is None: + if len(_instances) == 1: + return next(iter(_instances.values())) + names = list(_instances.keys()) + raise ValueError( + f"Multiple instances configured ({names}). " + "Specify 'instance' parameter to choose one." + ) + if instance not in _instances: + raise ValueError( + f"Instance '{instance}' not found. Available: {list(_instances.keys())}" + ) + return _instances[instance] + + +def _handle_error_global(e: Exception) -> str: + """Fallback error handler when instance cannot be determined.""" + if isinstance(e, ValueError): + return f"Error: {e}" + return f"Error: {type(e).__name__}: {e}" + + # --------------------------------------------------------------------------- -# Lifespan — validate connectivity on startup +# Lifespan # --------------------------------------------------------------------------- @asynccontextmanager async def app_lifespan(app): - if not SYNCTHING_API_KEY: - print("WARNING: SYNCTHING_API_KEY is not set. API calls will fail.") + import sys + + missing = [n for n, c in _instances.items() if not c.api_key] + if missing: + print(f"WARNING: API key missing for instance(s): {missing}", file=sys.stderr) + print( + f"Syncthing MCP: {len(_instances)} instance(s) configured — " + f"{list(_instances.keys())}", + file=sys.stderr, + ) yield {} @@ -118,30 +236,152 @@ mcp = FastMCP("syncthing_mcp", lifespan=app_lifespan) class EmptyInput(BaseModel): model_config = ConfigDict(extra="forbid") + instance: str | None = Field( + None, description="Instance name. Omit if only one instance is configured." + ) class FolderInput(BaseModel): model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) - folder_id: str = Field(..., description="Syncthing folder ID (e.g. 'abcd-1234')", min_length=1) + folder_id: str = Field( + ..., description="Syncthing folder ID (e.g. 'abcd-1234')", min_length=1 + ) + instance: str | None = Field(None, description="Instance name") class DeviceInput(BaseModel): model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) - device_id: str = Field(..., description="Syncthing device ID (long alphanumeric string with dashes)", min_length=1) + device_id: str = Field( + ..., + description="Syncthing device ID (long alphanumeric string with dashes)", + min_length=1, + ) + instance: str | None = Field(None, description="Instance name") class FolderDeviceInput(BaseModel): model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) folder_id: str = Field(..., description="Syncthing folder ID", min_length=1) device_id: str = Field(..., description="Syncthing device ID", min_length=1) + instance: str | None = Field(None, description="Instance name") class PauseFolderInput(BaseModel): model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) - folder_id: str = Field(..., description="Syncthing folder ID to pause", min_length=1) + folder_id: str = Field( + ..., description="Syncthing folder ID to pause/resume", min_length=1 + ) + instance: str | None = Field(None, description="Instance name") -# ===== Tools ===== +class AcceptDeviceInput(BaseModel): + model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) + device_id: str = Field( + ..., description="Device ID to accept (from pending list)", min_length=1 + ) + name: str | None = Field( + None, + description="Friendly name to assign. If omitted, uses the name from the pending request.", + ) + instance: str | None = Field(None, description="Instance name") + + +class AcceptFolderInput(BaseModel): + model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) + folder_id: str = Field( + ..., description="Folder ID to accept (from pending list)", min_length=1 + ) + path: str | None = Field( + None, + description="Local path for the folder. If omitted, uses Syncthing's default path.", + ) + instance: str | None = Field(None, description="Instance name") + + +class RejectFolderInput(BaseModel): + model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) + folder_id: str = Field( + ..., description="Folder ID to reject", min_length=1 + ) + device_id: str | None = Field( + None, + description="Device ID that offered the folder. If omitted, rejects from all devices.", + ) + instance: str | None = Field(None, description="Instance name") + + +class SetIgnoresInput(BaseModel): + model_config = ConfigDict(extra="forbid", str_strip_whitespace=True) + folder_id: str = Field(..., description="Folder ID", min_length=1) + patterns: list[str] = Field( + ..., + description="List of ignore patterns (e.g. ['*.tmp', '.DS_Store', '// #include'])", + ) + instance: str | None = Field(None, description="Instance name") + + +class SetDefaultIgnoresInput(BaseModel): + model_config = ConfigDict(extra="forbid") + lines: list[str] = Field( + ..., + description="Default ignore patterns for new folders (e.g. ['.DS_Store', 'Thumbs.db'])", + ) + instance: str | None = Field(None, description="Instance name") + + +# ===================================================================== +# TOOLS — Instance Management +# ===================================================================== + + +@mcp.tool( + name="syncthing_list_instances", + annotations={ + "title": "List Configured Instances", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_list_instances(params: EmptyInput) -> str: + """List all configured Syncthing instances and probe their availability. + + Returns: + str: JSON array with instance name, URL, availability, and device ID. + """ + results = [] + for name, client in _instances.items(): + entry: dict[str, Any] = {"name": name, "url": client.url} + try: + status = await client._get("/rest/system/status") + version = await client._get("/rest/system/version") + config = await client._get("/rest/config") + my_id = status.get("myID", "") + my_name = my_id[:8] + for dev in config.get("devices", []): + if dev.get("deviceID") == my_id: + my_name = dev.get("name", my_id[:8]) + break + entry.update( + { + "available": True, + "myID": my_id, + "deviceName": my_name, + "version": version.get("version"), + "numFolders": len(config.get("folders", [])), + "numDevices": len(config.get("devices", [])), + } + ) + except Exception as exc: + entry.update({"available": False, "error": str(exc)}) + results.append(entry) + return json.dumps(results, indent=2) + + +# ===================================================================== +# TOOLS — System Status & Info (existing, refactored) +# ===================================================================== @mcp.tool( @@ -161,10 +401,10 @@ async def syncthing_system_status(params: EmptyInput) -> str: str: JSON with myID, device name, uptime, version, and connection stats. """ try: - status = await _get("/rest/system/status") - version = await _get("/rest/system/version") - config = await _get("/rest/config") - # Find local device name + client = _get_instance(params.instance) + status = await client._get("/rest/system/status") + version = await client._get("/rest/system/version") + config = await client._get("/rest/config") my_id = status.get("myID", "") my_name = my_id[:8] for dev in config.get("devices", []): @@ -173,6 +413,7 @@ async def syncthing_system_status(params: EmptyInput) -> str: break return json.dumps( { + "instance": client.name, "myID": my_id, "deviceName": my_name, "uptime": status.get("uptime"), @@ -185,7 +426,9 @@ async def syncthing_system_status(params: EmptyInput) -> str: indent=2, ) except Exception as e: - return _handle_api_error(e) + if isinstance(e, ValueError): + return _handle_error_global(e) + return _get_instance(None).handle_error(e) if len(_instances) == 1 else _handle_error_global(e) @mcp.tool( @@ -205,10 +448,13 @@ async def syncthing_list_folders(params: EmptyInput) -> str: str: JSON array of folders with id, label, path, type, paused, and list of shared device names/IDs. """ try: - config = await _get("/rest/config") + client = _get_instance(params.instance) + config = await client._get("/rest/config") folders = config.get("folders", []) - devices = {d["deviceID"]: d.get("name", d["deviceID"][:8]) for d in config.get("devices", [])} - + devices = { + d["deviceID"]: d.get("name", d["deviceID"][:8]) + for d in config.get("devices", []) + } result = [] for f in folders: shared = [] @@ -228,7 +474,7 @@ async def syncthing_list_folders(params: EmptyInput) -> str: ) return json.dumps(result, indent=2) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) @mcp.tool( @@ -248,11 +494,11 @@ async def syncthing_list_devices(params: EmptyInput) -> str: str: JSON array of devices with deviceID, name, connected, paused, address, lastSeen. """ try: - config = await _get("/rest/config") - connections = await _get("/rest/system/connections") - stats = await _get("/rest/stats/device") + client = _get_instance(params.instance) + config = await client._get("/rest/config") + connections = await client._get("/rest/system/connections") + stats = await client._get("/rest/stats/device") conn_data = connections.get("connections", {}) - result = [] for dev in config.get("devices", []): did = dev["deviceID"] @@ -272,7 +518,12 @@ async def syncthing_list_devices(params: EmptyInput) -> str: ) return json.dumps(result, indent=2) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Folder Status & Replication (existing, refactored) +# ===================================================================== @mcp.tool( @@ -298,13 +549,14 @@ async def syncthing_folder_status(params: FolderInput) -> str: needFiles, state, stateChanged, and human-readable size strings. """ try: - status = await _get("/rest/db/status", params={"folder": params.folder_id}) - stats = await _get("/rest/stats/folder") + client = _get_instance(params.instance) + status = await client._get("/rest/db/status", params={"folder": params.folder_id}) + stats = await client._get("/rest/stats/folder") folder_stats = stats.get(params.folder_id, {}) - return json.dumps( { "folder": params.folder_id, + "instance": client.name, "state": status.get("state"), "stateChanged": status.get("stateChanged"), "globalFiles": status.get("globalFiles"), @@ -327,7 +579,7 @@ async def syncthing_folder_status(params: FolderInput) -> str: indent=2, ) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) @mcp.tool( @@ -354,13 +606,13 @@ async def syncthing_folder_completion(params: FolderInput) -> str: including whether each device is connected and has a valid remote state. """ try: - config = await _get("/rest/config") - connections = await _get("/rest/system/connections") + client = _get_instance(params.instance) + config = await client._get("/rest/config") + connections = await client._get("/rest/system/connections") conn_data = connections.get("connections", {}) - status = await _get("/rest/system/status") + status = await client._get("/rest/system/status") my_id = status.get("myID", "") - # Find the folder config folder_cfg = None for f in config.get("folders", []): if f["id"] == params.folder_id: @@ -369,15 +621,17 @@ async def syncthing_folder_completion(params: FolderInput) -> str: if not folder_cfg: return json.dumps({"error": f"Folder '{params.folder_id}' not found in config."}) - devices_map = {d["deviceID"]: d.get("name", d["deviceID"][:8]) for d in config.get("devices", [])} - + devices_map = { + d["deviceID"]: d.get("name", d["deviceID"][:8]) + for d in config.get("devices", []) + } completions = [] for dev in folder_cfg.get("devices", []): did = dev.get("deviceID", "") if did == my_id: - continue # skip self + continue try: - comp = await _get( + comp = await client._get( "/rest/db/completion", params={"folder": params.folder_id, "device": did}, ) @@ -409,13 +663,15 @@ async def syncthing_folder_completion(params: FolderInput) -> str: ) fully_replicated_count = sum( - 1 for c in completions if c.get("completion") == 100 and c.get("remoteState") == "valid" + 1 + for c in completions + if c.get("completion") == 100 and c.get("remoteState") == "valid" ) - return json.dumps( { "folder": params.folder_id, "label": folder_cfg.get("label", params.folder_id), + "instance": client.name, "totalRemoteDevices": len(completions), "fullyReplicatedOn": fully_replicated_count, "devices": completions, @@ -423,7 +679,7 @@ async def syncthing_folder_completion(params: FolderInput) -> str: indent=2, ) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) @mcp.tool( @@ -453,12 +709,16 @@ async def syncthing_replication_report(params: EmptyInput) -> str: total reclaimable space. """ try: - config = await _get("/rest/config") - connections = await _get("/rest/system/connections") + client = _get_instance(params.instance) + config = await client._get("/rest/config") + connections = await client._get("/rest/system/connections") conn_data = connections.get("connections", {}) - status = await _get("/rest/system/status") + status = await client._get("/rest/system/status") my_id = status.get("myID", "") - devices_map = {d["deviceID"]: d.get("name", d["deviceID"][:8]) for d in config.get("devices", [])} + devices_map = { + d["deviceID"]: d.get("name", d["deviceID"][:8]) + for d in config.get("devices", []) + } folders = config.get("folders", []) report = [] @@ -469,34 +729,25 @@ async def syncthing_replication_report(params: EmptyInput) -> str: label = folder_cfg.get("label", fid) paused = folder_cfg.get("paused", False) - # Get local folder status try: - fstatus = await _get("/rest/db/status", params={"folder": fid}) + fstatus = await client._get("/rest/db/status", params={"folder": fid}) except Exception: - report.append( - { - "folder": fid, - "label": label, - "error": "Could not get folder status.", - } - ) + report.append({"folder": fid, "label": label, "error": "Could not get folder status."}) continue local_bytes = fstatus.get("localBytes", 0) global_bytes = fstatus.get("globalBytes", 0) state = fstatus.get("state", "unknown") - # Check completion on each remote device remote_devices = [ d for d in folder_cfg.get("devices", []) if d.get("deviceID") != my_id ] - device_completions = [] for dev in remote_devices: did = dev.get("deviceID", "") connected = conn_data.get(did, {}).get("connected", False) try: - comp = await _get( + comp = await client._get( "/rest/db/completion", params={"folder": fid, "device": did}, ) @@ -545,19 +796,17 @@ async def syncthing_replication_report(params: EmptyInput) -> str: "globalSize": _format_bytes(global_bytes), "totalRemoteDevices": len(remote_devices), "fullyReplicatedOn": len(fully_replicated), - "fullyReplicatedDevices": [ - d["deviceName"] for d in fully_replicated - ], + "fullyReplicatedDevices": [d["deviceName"] for d in fully_replicated], "safeToRemove": safe, "devices": device_completions, } ) - # Sort: safe-to-remove folders first, then by size descending report.sort(key=lambda x: (-int(x.get("safeToRemove", False)), -x.get("localBytes", 0))) return json.dumps( { + "instance": client.name, "summary": { "totalFolders": len(report), "safeToRemoveCount": sum(1 for r in report if r.get("safeToRemove")), @@ -569,7 +818,12 @@ async def syncthing_replication_report(params: EmptyInput) -> str: indent=2, ) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Folder Operations (existing, refactored) +# ===================================================================== @mcp.tool( @@ -595,29 +849,25 @@ async def syncthing_pause_folder(params: PauseFolderInput) -> str: str: Confirmation message. """ try: - # Get current folder config - folder_cfg = await _get(f"/rest/config/folders/{params.folder_id}") + client = _get_instance(params.instance) + folder_cfg = await client._get(f"/rest/config/folders/{params.folder_id}") folder_cfg["paused"] = True - # PATCH the folder config - async with httpx.AsyncClient(timeout=30.0) as client: - resp = await client.patch( - f"{SYNCTHING_URL}/rest/config/folders/{params.folder_id}", - headers=_headers(), - json=folder_cfg, - ) - resp.raise_for_status() + await client._patch(f"/rest/config/folders/{params.folder_id}", body=folder_cfg) return json.dumps( { "status": "paused", "folder": params.folder_id, - "message": f"Folder '{params.folder_id}' is now paused. " - "Syncthing will NOT sync changes (including deletions) for this folder. " - "You can now safely remove the local data without affecting remote copies.", + "instance": client.name, + "message": ( + f"Folder '{params.folder_id}' is now paused. " + "Syncthing will NOT sync changes (including deletions) for this folder. " + "You can now safely remove the local data without affecting remote copies." + ), }, indent=2, ) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) @mcp.tool( @@ -644,144 +894,28 @@ async def syncthing_resume_folder(params: PauseFolderInput) -> str: str: Confirmation message with warnings. """ try: - folder_cfg = await _get(f"/rest/config/folders/{params.folder_id}") + client = _get_instance(params.instance) + folder_cfg = await client._get(f"/rest/config/folders/{params.folder_id}") folder_type = folder_cfg.get("type", "sendreceive") folder_cfg["paused"] = False - async with httpx.AsyncClient(timeout=30.0) as client: - resp = await client.patch( - f"{SYNCTHING_URL}/rest/config/folders/{params.folder_id}", - headers=_headers(), - json=folder_cfg, - ) - resp.raise_for_status() + await client._patch(f"/rest/config/folders/{params.folder_id}", body=folder_cfg) return json.dumps( { "status": "resumed", "folder": params.folder_id, "folderType": folder_type, + "instance": client.name, "message": f"Folder '{params.folder_id}' is now active again (type: {folder_type}).", - "warning": "If local data was deleted while paused, behaviour depends on folder type: " - "'sendreceive' may propagate deletions to peers; " - "'receiveonly' will re-download missing files.", + "warning": ( + "If local data was deleted while paused, behaviour depends on folder type: " + "'sendreceive' may propagate deletions to peers; " + "'receiveonly' will re-download missing files." + ), }, indent=2, ) except Exception as e: - return _handle_api_error(e) - - -@mcp.tool( - name="syncthing_device_completion", - annotations={ - "title": "Device Completion (All Folders)", - "readOnlyHint": True, - "destructiveHint": False, - "idempotentHint": True, - "openWorldHint": False, - }, -) -async def syncthing_device_completion(params: DeviceInput) -> str: - """Get the aggregated sync completion for a specific remote device across all shared folders. - - Args: - params: DeviceInput with device_id. - - Returns: - str: JSON with overall completion %, globalBytes, needBytes for that device. - """ - try: - comp = await _get("/rest/db/completion", params={"device": params.device_id}) - return json.dumps( - { - "deviceID": params.device_id, - "completion": round(comp.get("completion", 0), 2), - "globalBytes": comp.get("globalBytes", 0), - "globalSize": _format_bytes(comp.get("globalBytes", 0)), - "needBytes": comp.get("needBytes", 0), - "needSize": _format_bytes(comp.get("needBytes", 0)), - "needItems": comp.get("needItems", 0), - "remoteState": comp.get("remoteState", "unknown"), - }, - indent=2, - ) - except Exception as e: - return _handle_api_error(e) - - -@mcp.tool( - name="syncthing_connections", - annotations={ - "title": "Active Connections", - "readOnlyHint": True, - "destructiveHint": False, - "idempotentHint": True, - "openWorldHint": False, - }, -) -async def syncthing_connections(params: EmptyInput) -> str: - """Get current connection details for all devices. - - Returns: - str: JSON with per-device connection info including address, - bytes transferred, crypto, and connection type. - """ - try: - connections = await _get("/rest/system/connections") - config = await _get("/rest/config") - devices_map = {d["deviceID"]: d.get("name", d["deviceID"][:8]) for d in config.get("devices", [])} - - result = [] - for did, conn in connections.get("connections", {}).items(): - result.append( - { - "deviceID": did, - "deviceName": devices_map.get(did, did[:8]), - "connected": conn.get("connected", False), - "paused": conn.get("paused", False), - "address": conn.get("address", ""), - "type": conn.get("type", ""), - "crypto": conn.get("crypto", ""), - "inBytesTotal": conn.get("inBytesTotal", 0), - "outBytesTotal": conn.get("outBytesTotal", 0), - } - ) - return json.dumps(result, indent=2) - except Exception as e: - return _handle_api_error(e) - - -@mcp.tool( - name="syncthing_folder_errors", - annotations={ - "title": "Folder Errors", - "readOnlyHint": True, - "destructiveHint": False, - "idempotentHint": True, - "openWorldHint": False, - }, -) -async def syncthing_folder_errors(params: FolderInput) -> str: - """Get current sync errors for a specific folder. - - Args: - params: FolderInput with folder_id. - - Returns: - str: JSON with error list and count. - """ - try: - errors = await _get("/rest/folder/errors", params={"folder": params.folder_id}) - return json.dumps( - { - "folder": params.folder_id, - "errorCount": len(errors.get("errors", []) or []), - "errors": errors.get("errors", []), - "page": errors.get("page", 1), - }, - indent=2, - ) - except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) @mcp.tool( @@ -806,17 +940,935 @@ async def syncthing_scan_folder(params: FolderInput) -> str: str: Confirmation that scan was requested. """ try: - await _post("/rest/db/scan", params={"folder": params.folder_id}) + client = _get_instance(params.instance) + await client._post("/rest/db/scan", params={"folder": params.folder_id}) return json.dumps( { "status": "scan_requested", "folder": params.folder_id, + "instance": client.name, "message": "Scan initiated. Folder status will update shortly.", }, indent=2, ) except Exception as e: - return _handle_api_error(e) + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Device Status (existing, refactored) +# ===================================================================== + + +@mcp.tool( + name="syncthing_device_completion", + annotations={ + "title": "Device Completion (All Folders)", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_device_completion(params: DeviceInput) -> str: + """Get the aggregated sync completion for a specific remote device across all shared folders. + + Args: + params: DeviceInput with device_id. + + Returns: + str: JSON with overall completion %, globalBytes, needBytes for that device. + """ + try: + client = _get_instance(params.instance) + comp = await client._get( + "/rest/db/completion", params={"device": params.device_id} + ) + return json.dumps( + { + "deviceID": params.device_id, + "instance": client.name, + "completion": round(comp.get("completion", 0), 2), + "globalBytes": comp.get("globalBytes", 0), + "globalSize": _format_bytes(comp.get("globalBytes", 0)), + "needBytes": comp.get("needBytes", 0), + "needSize": _format_bytes(comp.get("needBytes", 0)), + "needItems": comp.get("needItems", 0), + "remoteState": comp.get("remoteState", "unknown"), + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_connections", + annotations={ + "title": "Active Connections", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_connections(params: EmptyInput) -> str: + """Get current connection details for all devices. + + Returns: + str: JSON with per-device connection info including address, + bytes transferred, crypto, and connection type. + """ + try: + client = _get_instance(params.instance) + connections = await client._get("/rest/system/connections") + config = await client._get("/rest/config") + devices_map = { + d["deviceID"]: d.get("name", d["deviceID"][:8]) + for d in config.get("devices", []) + } + result = [] + for did, conn in connections.get("connections", {}).items(): + result.append( + { + "deviceID": did, + "deviceName": devices_map.get(did, did[:8]), + "connected": conn.get("connected", False), + "paused": conn.get("paused", False), + "address": conn.get("address", ""), + "type": conn.get("type", ""), + "crypto": conn.get("crypto", ""), + "inBytesTotal": conn.get("inBytesTotal", 0), + "outBytesTotal": conn.get("outBytesTotal", 0), + } + ) + return json.dumps(result, indent=2) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_folder_errors", + annotations={ + "title": "Folder Errors", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_folder_errors(params: FolderInput) -> str: + """Get current sync errors for a specific folder. + + Args: + params: FolderInput with folder_id. + + Returns: + str: JSON with error list and count. + """ + try: + client = _get_instance(params.instance) + errors = await client._get( + "/rest/folder/errors", params={"folder": params.folder_id} + ) + return json.dumps( + { + "folder": params.folder_id, + "instance": client.name, + "errorCount": len(errors.get("errors", []) or []), + "errors": errors.get("errors", []), + "page": errors.get("page", 1), + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Config Mutation: Pending Devices & Folders +# ===================================================================== + + +@mcp.tool( + name="syncthing_pending_devices", + annotations={ + "title": "List Pending Device Requests", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_pending_devices(params: EmptyInput) -> str: + """List remote devices that have tried to connect but are not yet configured. + + Returns: + str: JSON object keyed by device ID, each with name, address, and time. + """ + try: + client = _get_instance(params.instance) + pending = await client._get("/rest/cluster/pending/devices") + return json.dumps( + {"instance": client.name, "pendingDevices": pending}, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_pending_folders", + annotations={ + "title": "List Pending Folder Offers", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_pending_folders(params: EmptyInput) -> str: + """List folders that remote devices have offered to share but are not yet accepted. + + Returns: + str: JSON object keyed by folder ID, each listing offering devices with labels. + """ + try: + client = _get_instance(params.instance) + pending = await client._get("/rest/cluster/pending/folders") + return json.dumps( + {"instance": client.name, "pendingFolders": pending}, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_accept_device", + annotations={ + "title": "Accept Pending Device", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": False, + "openWorldHint": False, + }, +) +async def syncthing_accept_device(params: AcceptDeviceInput) -> str: + """Accept a pending device by adding it to the Syncthing configuration. + + The device will be added with 'dynamic' addressing. You can optionally + provide a friendly name. + + Args: + params: AcceptDeviceInput with device_id and optional name. + + Returns: + str: Confirmation message with the device details. + """ + try: + client = _get_instance(params.instance) + + # Check it's actually pending + pending = await client._get("/rest/cluster/pending/devices") + pending_info = pending.get(params.device_id, {}) + + # Determine name + name = params.name + if not name: + name = pending_info.get("name", params.device_id[:8]) + + # Get default device config as template + defaults = await client._get("/rest/config/defaults/device") + new_device = defaults.copy() + new_device["deviceID"] = params.device_id + new_device["name"] = name + + # POST single device object to /rest/config/devices + await client._post("/rest/config/devices", body=new_device) + + return json.dumps( + { + "status": "accepted", + "deviceID": params.device_id, + "name": name, + "instance": client.name, + "message": f"Device '{name}' ({params.device_id[:8]}...) has been added.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_reject_device", + annotations={ + "title": "Reject Pending Device", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_reject_device(params: DeviceInput) -> str: + """Dismiss a pending device connection request. + + This only clears the pending notification. The device can try again later; + for permanent blocking, use Syncthing's ignore-device feature via the web UI. + + Args: + params: DeviceInput with device_id. + + Returns: + str: Confirmation message. + """ + try: + client = _get_instance(params.instance) + await client._delete( + "/rest/cluster/pending/devices", + params={"device": params.device_id}, + ) + return json.dumps( + { + "status": "rejected", + "deviceID": params.device_id, + "instance": client.name, + "message": f"Pending device '{params.device_id[:8]}...' has been dismissed.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_accept_folder", + annotations={ + "title": "Accept Pending Folder Offer", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": False, + "openWorldHint": False, + }, +) +async def syncthing_accept_folder(params: AcceptFolderInput) -> str: + """Accept a pending folder share offer by adding it to the configuration. + + Uses the default folder configuration as a template. If no path is provided, + Syncthing's default folder path is used. + + Args: + params: AcceptFolderInput with folder_id and optional path. + + Returns: + str: Confirmation with assigned path and sharing details. + """ + try: + client = _get_instance(params.instance) + + # Look up pending info + pending = await client._get("/rest/cluster/pending/folders") + folder_pending = pending.get(params.folder_id, {}) + if not folder_pending: + return json.dumps( + {"error": f"Folder '{params.folder_id}' not found in pending offers."} + ) + + # Determine offering devices + offering_devices = list(folder_pending.get("offeredBy", {}).keys()) + + # Get label from the first offering device's info + first_offer = next(iter(folder_pending.get("offeredBy", {}).values()), {}) + label = first_offer.get("label", params.folder_id) + + # Get my device ID + status = await client._get("/rest/system/status") + my_id = status.get("myID", "") + + # Get default folder config as template + defaults = await client._get("/rest/config/defaults/folder") + new_folder = defaults.copy() + new_folder["id"] = params.folder_id + new_folder["label"] = label + + if params.path: + new_folder["path"] = params.path + + # Build device list: self + all offering devices + device_list = [{"deviceID": my_id}] + for did in offering_devices: + device_list.append({"deviceID": did}) + new_folder["devices"] = device_list + + # POST single folder object + await client._post("/rest/config/folders", body=new_folder) + + return json.dumps( + { + "status": "accepted", + "folderID": params.folder_id, + "label": label, + "path": new_folder.get("path", "(default)"), + "sharedWith": offering_devices, + "instance": client.name, + "message": ( + f"Folder '{label}' ({params.folder_id}) has been added " + f"at '{new_folder.get('path', '(default)')}'." + ), + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_reject_folder", + annotations={ + "title": "Reject Pending Folder Offer", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_reject_folder(params: RejectFolderInput) -> str: + """Dismiss a pending folder share offer. + + Args: + params: RejectFolderInput with folder_id and optional device_id. + If device_id is omitted, rejects the offer from all devices. + + Returns: + str: Confirmation message. + """ + try: + client = _get_instance(params.instance) + delete_params: dict[str, str] = {"folder": params.folder_id} + if params.device_id: + delete_params["device"] = params.device_id + await client._delete("/rest/cluster/pending/folders", params=delete_params) + return json.dumps( + { + "status": "rejected", + "folderID": params.folder_id, + "deviceID": params.device_id or "(all)", + "instance": client.name, + "message": f"Pending folder offer for '{params.folder_id}' has been dismissed.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Config Mutation: Restart & Ignore Patterns +# ===================================================================== + + +@mcp.tool( + name="syncthing_restart_required", + annotations={ + "title": "Check if Restart Required", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_restart_required(params: EmptyInput) -> str: + """Check if Syncthing requires a restart for configuration changes to take effect. + + Returns: + str: JSON with restartRequired boolean. + """ + try: + client = _get_instance(params.instance) + result = await client._get("/rest/config/restart-required") + return json.dumps( + { + "instance": client.name, + "restartRequired": result.get("requiresRestart", False), + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_restart", + annotations={ + "title": "Restart Syncthing", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_restart(params: EmptyInput) -> str: + """Restart the Syncthing service to apply pending configuration changes. + + WARNING: This temporarily stops synchronization on all folders for this instance. + + Returns: + str: Confirmation message. + """ + try: + client = _get_instance(params.instance) + try: + await client._post("/rest/system/restart") + except (httpx.ConnectError, httpx.RemoteProtocolError): + # Expected — Syncthing closes the connection as it restarts + pass + return json.dumps( + { + "status": "restart_initiated", + "instance": client.name, + "message": ( + f"Syncthing on '{client.name}' is restarting. " + "Connection may be lost briefly. Sync resumes automatically." + ), + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_get_ignores", + annotations={ + "title": "Get Folder Ignore Patterns", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_get_ignores(params: FolderInput) -> str: + """Get the current .stignore patterns for a folder. + + Args: + params: FolderInput with folder_id. + + Returns: + str: JSON with ignore patterns list and whether the folder has expanded patterns. + """ + try: + client = _get_instance(params.instance) + result = await client._get( + "/rest/db/ignores", params={"folder": params.folder_id} + ) + return json.dumps( + { + "folder": params.folder_id, + "instance": client.name, + "patterns": result.get("ignore", []) or [], + "expanded": result.get("expanded", []) or [], + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_set_ignores", + annotations={ + "title": "Set Folder Ignore Patterns", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_set_ignores(params: SetIgnoresInput) -> str: + """Set the .stignore patterns for a folder. Replaces all existing patterns. + + Common patterns: '*.tmp', '.DS_Store', 'Thumbs.db', '(?d).Trash*', + '// #include common-ignores' (to include shared pattern files). + + Args: + params: SetIgnoresInput with folder_id and patterns list. + + Returns: + str: Confirmation with the applied patterns. + """ + try: + client = _get_instance(params.instance) + await client._post( + "/rest/db/ignores", + params={"folder": params.folder_id}, + body={"ignore": params.patterns}, + ) + return json.dumps( + { + "status": "updated", + "folder": params.folder_id, + "instance": client.name, + "patternCount": len(params.patterns), + "patterns": params.patterns, + "message": f"Ignore patterns for '{params.folder_id}' have been updated.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_get_default_ignores", + annotations={ + "title": "Get Default Ignore Patterns", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_get_default_ignores(params: EmptyInput) -> str: + """Get the default ignore patterns applied to newly created folders. + + Returns: + str: JSON with default ignore pattern lines. + """ + try: + client = _get_instance(params.instance) + result = await client._get("/rest/config/defaults/ignores") + return json.dumps( + { + "instance": client.name, + "lines": result.get("lines", []) or [], + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_set_default_ignores", + annotations={ + "title": "Set Default Ignore Patterns", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_set_default_ignores(params: SetDefaultIgnoresInput) -> str: + """Set the default ignore patterns for newly created folders. + + These patterns will be automatically applied when new folders are added. + + Args: + params: SetDefaultIgnoresInput with lines list. + + Returns: + str: Confirmation with the applied patterns. + """ + try: + client = _get_instance(params.instance) + await client._put( + "/rest/config/defaults/ignores", + body={"lines": params.lines}, + ) + return json.dumps( + { + "status": "updated", + "instance": client.name, + "lineCount": len(params.lines), + "lines": params.lines, + "message": "Default ignore patterns have been updated for new folders.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +# ===================================================================== +# TOOLS — Expanded Monitoring +# ===================================================================== + + +@mcp.tool( + name="syncthing_system_errors", + annotations={ + "title": "System Errors & Warnings", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_system_errors(params: EmptyInput) -> str: + """Get recent system errors and warnings from Syncthing. + + Returns: + str: JSON with error list, each containing 'when' timestamp and 'message'. + """ + try: + client = _get_instance(params.instance) + result = await client._get("/rest/system/error") + errors = result.get("errors", []) or [] + return json.dumps( + { + "instance": client.name, + "errorCount": len(errors), + "errors": errors, + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_clear_errors", + annotations={ + "title": "Clear System Errors", + "readOnlyHint": False, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_clear_errors(params: EmptyInput) -> str: + """Clear the system error log. + + Returns: + str: Confirmation message. + """ + try: + client = _get_instance(params.instance) + await client._post("/rest/system/error/clear") + return json.dumps( + { + "status": "cleared", + "instance": client.name, + "message": "System error log has been cleared.", + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_system_log", + annotations={ + "title": "System Log", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_system_log(params: EmptyInput) -> str: + """Get recent system log entries from Syncthing. + + Returns: + str: JSON with log messages, each containing 'when' timestamp, 'message', + and 'level'. + """ + try: + client = _get_instance(params.instance) + result = await client._get("/rest/system/log") + messages = result.get("messages", []) or [] + return json.dumps( + { + "instance": client.name, + "messageCount": len(messages), + "messages": messages, + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_recent_changes", + annotations={ + "title": "Recent File Changes", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_recent_changes(params: EmptyInput) -> str: + """Get recent file change events (local and remote) across all folders. + + Uses a non-blocking poll of the Syncthing event stream with limit=50. + + Returns: + str: JSON with recent LocalChangeDetected and RemoteChangeDetected events. + """ + try: + client = _get_instance(params.instance) + events = await client._get( + "/rest/events", + params={ + "events": "LocalChangeDetected,RemoteChangeDetected", + "limit": "50", + "timeout": "0", + }, + ) + # events is a list of event objects + if not isinstance(events, list): + events = [] + return json.dumps( + { + "instance": client.name, + "eventCount": len(events), + "events": events, + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) + + +@mcp.tool( + name="syncthing_health_summary", + annotations={ + "title": "Health Summary", + "readOnlyHint": True, + "destructiveHint": False, + "idempotentHint": True, + "openWorldHint": False, + }, +) +async def syncthing_health_summary(params: EmptyInput) -> str: + """Generate a single-call health overview: system status, folder states, + device connectivity, errors, and pending items. + + Designed for quick triage — aggregates data from multiple endpoints. + + Returns: + str: JSON with overall status ('good', 'warning', 'error'), summary + counts, alert list, and per-folder health. + """ + try: + client = _get_instance(params.instance) + + # Gather data + sys_status = await client._get("/rest/system/status") + config = await client._get("/rest/config") + sys_errors = await client._get("/rest/system/error") + connections = await client._get("/rest/system/connections") + + try: + pending_devices = await client._get("/rest/cluster/pending/devices") + except Exception: + pending_devices = {} + try: + pending_folders = await client._get("/rest/cluster/pending/folders") + except Exception: + pending_folders = {} + + folders = config.get("folders", []) + conn_data = connections.get("connections", {}) + + # Check device connectivity + online_count = sum(1 for c in conn_data.values() if c.get("connected")) + total_remote = len(conn_data) + + # Check folder states + folder_health = [] + paused_count = 0 + syncing_count = 0 + error_folders = 0 + + for f_cfg in folders: + fid = f_cfg["id"] + entry: dict[str, Any] = { + "id": fid, + "label": f_cfg.get("label", fid), + "paused": f_cfg.get("paused", False), + } + if f_cfg.get("paused", False): + paused_count += 1 + entry["state"] = "paused" + else: + try: + fstatus = await client._get( + "/rest/db/status", params={"folder": fid} + ) + state = fstatus.get("state", "unknown") + entry["state"] = state + entry["needBytes"] = fstatus.get("needBytes", 0) + entry["needSize"] = _format_bytes(fstatus.get("needBytes", 0)) + if state in ("syncing", "sync-preparing"): + syncing_count += 1 + elif state == "error": + error_folders += 1 + except Exception: + entry["state"] = "unreachable" + error_folders += 1 + folder_health.append(entry) + + # Build alerts + alerts: list[str] = [] + error_list = sys_errors.get("errors", []) or [] + if error_list: + alerts.append(f"{len(error_list)} system error(s)") + if error_folders > 0: + alerts.append(f"{error_folders} folder(s) in error state") + offline_count = total_remote - online_count + if offline_count > 0: + alerts.append(f"{offline_count} device(s) offline") + if paused_count > 0: + alerts.append(f"{paused_count} folder(s) paused") + if syncing_count > 0: + alerts.append(f"{syncing_count} folder(s) currently syncing") + num_pending_dev = len(pending_devices) if isinstance(pending_devices, dict) else 0 + num_pending_fld = len(pending_folders) if isinstance(pending_folders, dict) else 0 + if num_pending_dev > 0: + alerts.append(f"{num_pending_dev} pending device request(s)") + if num_pending_fld > 0: + alerts.append(f"{num_pending_fld} pending folder offer(s)") + + # Overall status + if error_folders > 0 or len(error_list) > 0: + overall = "error" + elif alerts: + overall = "warning" + else: + overall = "good" + + return json.dumps( + { + "instance": client.name, + "status": overall, + "uptime": sys_status.get("uptime"), + "summary": { + "totalFolders": len(folders), + "foldersIdle": sum( + 1 for f in folder_health if f.get("state") == "idle" + ), + "foldersSyncing": syncing_count, + "foldersPaused": paused_count, + "foldersError": error_folders, + "totalDevices": len(config.get("devices", [])), + "devicesOnline": online_count, + "devicesOffline": offline_count, + "systemErrors": len(error_list), + "pendingDevices": num_pending_dev, + "pendingFolders": num_pending_fld, + }, + "alerts": alerts, + "folders": folder_health, + }, + indent=2, + ) + except Exception as e: + return _handle_error_global(e) # ---------------------------------------------------------------------------