Refactor into package, add 6 new tools, add 96 tests
Split the monolithic syncthing_mcp.py into a proper src/syncthing_mcp/ package with focused modules: client, models, registry, server, and tool submodules (system, folders, devices, config, instances). New tools: browse_folder, file_info, folder_need (file-level queries), override_folder, revert_folder (conflict resolution), check_upgrade (system management). Bumps tool count from 30 to 36. Adds comprehensive test suite (96 tests) using pytest + respx for HTTP mocking, covering client methods, registry logic, model validation, and all tool functions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
5b54c09cd5
commit
3a33d34bd1
26 files changed
+3398
-1897
No files matched your search
@@ -0,0 +1,3 @@
|
||||
"""Syncthing MCP Server — manage Syncthing instances via MCP tools."""
|
||||
|
||||
__version__ = "0.2.0"
|
||||
@@ -0,0 +1,11 @@
|
||||
"""Entry point for `python -m syncthing_mcp` and the `syncthing-mcp` console script."""
|
||||
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
def main() -> None:
|
||||
mcp.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,106 @@
|
||||
"""HTTP client for a single Syncthing instance."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
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
|
||||
|
||||
def _headers(self) -> dict[str, str]:
|
||||
return {
|
||||
"X-API-Key": self.api_key,
|
||||
"Accept": "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()
|
||||
|
||||
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}"
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Pydantic input models for Syncthing MCP tools."""
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
|
||||
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
|
||||
)
|
||||
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,
|
||||
)
|
||||
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/resume", min_length=1
|
||||
)
|
||||
instance: str | None = Field(None, description="Instance name")
|
||||
|
||||
|
||||
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")
|
||||
|
||||
|
||||
# --- New models for added tools ---
|
||||
|
||||
|
||||
class BrowseFolderInput(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", str_strip_whitespace=True)
|
||||
folder_id: str = Field(..., description="Syncthing folder ID", min_length=1)
|
||||
prefix: str | None = Field(
|
||||
None,
|
||||
description="Path prefix to browse (e.g. 'Documents/reports'). Omit for root.",
|
||||
)
|
||||
levels: int | None = Field(
|
||||
None,
|
||||
description="How many directory levels deep to return (default: 1).",
|
||||
ge=0,
|
||||
)
|
||||
instance: str | None = Field(None, description="Instance name")
|
||||
|
||||
|
||||
class FileInfoInput(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", str_strip_whitespace=True)
|
||||
folder_id: str = Field(..., description="Syncthing folder ID", min_length=1)
|
||||
file_path: str = Field(
|
||||
..., description="Relative path of the file within the folder", min_length=1
|
||||
)
|
||||
instance: str | None = Field(None, description="Instance name")
|
||||
|
||||
|
||||
class FolderNeedInput(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", str_strip_whitespace=True)
|
||||
folder_id: str = Field(..., description="Syncthing folder ID", min_length=1)
|
||||
page: int = Field(1, description="Page number (1-based)", ge=1)
|
||||
per_page: int = Field(50, description="Items per page", ge=1, le=500)
|
||||
instance: str | None = Field(None, description="Instance name")
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Instance registry — load Syncthing instances from environment variables."""
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
from syncthing_mcp.client import SyncthingClient
|
||||
|
||||
|
||||
def format_bytes(n: int) -> str:
|
||||
"""Human-readable byte size."""
|
||||
for unit in ("B", "KB", "MB", "GB", "TB"):
|
||||
if abs(n) < 1024:
|
||||
return f"{n:.1f} {unit}"
|
||||
n /= 1024
|
||||
return f"{n:.1f} PB"
|
||||
|
||||
|
||||
def load_instances() -> dict[str, SyncthingClient]:
|
||||
"""Build instance registry from environment variables.
|
||||
|
||||
Supports two modes:
|
||||
- Multi-instance via SYNCTHING_INSTANCES (JSON object)
|
||||
- Single-instance via SYNCTHING_API_KEY + SYNCTHING_URL
|
||||
"""
|
||||
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)}
|
||||
|
||||
|
||||
# Module-level registry, initialised at import time.
|
||||
_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 get_all_instances() -> dict[str, SyncthingClient]:
|
||||
"""Return the full instance registry."""
|
||||
return _instances
|
||||
|
||||
|
||||
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}"
|
||||
|
||||
|
||||
def reload_instances() -> None:
|
||||
"""Re-read environment and rebuild the instance registry (for testing)."""
|
||||
global _instances
|
||||
_instances = load_instances()
|
||||
@@ -0,0 +1,29 @@
|
||||
"""FastMCP server creation and lifespan."""
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
from syncthing_mcp.registry import get_all_instances
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def app_lifespan(app):
|
||||
import sys
|
||||
|
||||
instances = get_all_instances()
|
||||
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 {}
|
||||
|
||||
|
||||
mcp = FastMCP("syncthing_mcp", lifespan=app_lifespan)
|
||||
|
||||
# Import all tool modules so they register with `mcp` via decorators.
|
||||
import syncthing_mcp.tools # noqa: E402, F401
|
||||
@@ -0,0 +1,7 @@
|
||||
"""Import all tool sub-modules so their @mcp.tool decorators run at import time."""
|
||||
|
||||
from syncthing_mcp.tools import config # noqa: F401
|
||||
from syncthing_mcp.tools import devices # noqa: F401
|
||||
from syncthing_mcp.tools import folders # noqa: F401
|
||||
from syncthing_mcp.tools import instances # noqa: F401
|
||||
from syncthing_mcp.tools import system # noqa: F401
|
||||
@@ -0,0 +1,426 @@
|
||||
"""Config mutation tools: pending devices/folders, ignores, accept/reject."""
|
||||
|
||||
import json
|
||||
|
||||
from syncthing_mcp.models import (
|
||||
AcceptDeviceInput,
|
||||
AcceptFolderInput,
|
||||
DeviceInput,
|
||||
EmptyInput,
|
||||
FolderInput,
|
||||
RejectFolderInput,
|
||||
SetDefaultIgnoresInput,
|
||||
SetIgnoresInput,
|
||||
)
|
||||
from syncthing_mcp.registry import get_instance, handle_error_global
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# 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)
|
||||
pending = await client._get("/rest/cluster/pending/devices")
|
||||
pending_info = pending.get(params.device_id, {})
|
||||
name = params.name
|
||||
if not name:
|
||||
name = pending_info.get("name", params.device_id[:8])
|
||||
defaults = await client._get("/rest/config/defaults/device")
|
||||
new_device = defaults.copy()
|
||||
new_device["deviceID"] = params.device_id
|
||||
new_device["name"] = name
|
||||
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)
|
||||
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."}
|
||||
)
|
||||
offering_devices = list(folder_pending.get("offeredBy", {}).keys())
|
||||
first_offer = next(iter(folder_pending.get("offeredBy", {}).values()), {})
|
||||
label = first_offer.get("label", params.folder_id)
|
||||
status = await client._get("/rest/system/status")
|
||||
my_id = status.get("myID", "")
|
||||
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
|
||||
device_list = [{"deviceID": my_id}]
|
||||
for did in offering_devices:
|
||||
device_list.append({"deviceID": did})
|
||||
new_folder["devices"] = device_list
|
||||
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)
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Ignore Patterns
|
||||
# =====================================================================
|
||||
|
||||
|
||||
@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)
|
||||
@@ -0,0 +1,142 @@
|
||||
"""Device completion and connection tools."""
|
||||
|
||||
import json
|
||||
|
||||
from syncthing_mcp.models import DeviceInput, EmptyInput
|
||||
from syncthing_mcp.registry import (
|
||||
format_bytes,
|
||||
get_instance,
|
||||
handle_error_global,
|
||||
)
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_list_devices",
|
||||
annotations={
|
||||
"title": "List All Devices",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_list_devices(params: EmptyInput) -> str:
|
||||
"""List all configured devices with their names, connection status, and last seen time.
|
||||
|
||||
Returns:
|
||||
str: JSON array of devices with deviceID, name, connected, paused, address, lastSeen.
|
||||
"""
|
||||
try:
|
||||
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"]
|
||||
conn = conn_data.get(did, {})
|
||||
stat = stats.get(did, {})
|
||||
result.append(
|
||||
{
|
||||
"deviceID": did,
|
||||
"name": dev.get("name", did[:8]),
|
||||
"connected": conn.get("connected", False),
|
||||
"paused": conn.get("paused", False),
|
||||
"address": conn.get("address", ""),
|
||||
"lastSeen": stat.get("lastSeen", ""),
|
||||
"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_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)
|
||||
@@ -0,0 +1,724 @@
|
||||
"""Folder status, completion, replication, operations, and file-level query tools."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from syncthing_mcp.models import (
|
||||
BrowseFolderInput,
|
||||
FileInfoInput,
|
||||
FolderInput,
|
||||
FolderNeedInput,
|
||||
PauseFolderInput,
|
||||
EmptyInput,
|
||||
)
|
||||
from syncthing_mcp.registry import (
|
||||
format_bytes,
|
||||
get_instance,
|
||||
handle_error_global,
|
||||
)
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Folder Status & Replication
|
||||
# =====================================================================
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_folder_status",
|
||||
annotations={
|
||||
"title": "Folder Status",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_folder_status(params: FolderInput) -> str:
|
||||
"""Get detailed status for a specific folder — local/global file counts, bytes, sync state.
|
||||
|
||||
Note: This is an expensive call on the Syncthing side. Use sparingly.
|
||||
|
||||
Args:
|
||||
params: FolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: JSON with globalBytes, globalFiles, localBytes, localFiles, needBytes,
|
||||
needFiles, state, stateChanged, and human-readable size strings.
|
||||
"""
|
||||
try:
|
||||
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"),
|
||||
"globalBytes": status.get("globalBytes"),
|
||||
"globalSize": format_bytes(status.get("globalBytes", 0)),
|
||||
"localFiles": status.get("localFiles"),
|
||||
"localBytes": status.get("localBytes"),
|
||||
"localSize": format_bytes(status.get("localBytes", 0)),
|
||||
"needFiles": status.get("needFiles"),
|
||||
"needBytes": status.get("needBytes"),
|
||||
"needSize": format_bytes(status.get("needBytes", 0)),
|
||||
"inSyncFiles": status.get("inSyncFiles"),
|
||||
"inSyncBytes": status.get("inSyncBytes"),
|
||||
"globalDeleted": status.get("globalDeleted"),
|
||||
"localDeleted": status.get("localDeleted"),
|
||||
"ignorePatterns": status.get("ignorePatterns"),
|
||||
"lastScan": folder_stats.get("lastScan", ""),
|
||||
"lastFile": folder_stats.get("lastFile", {}),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_folder_completion",
|
||||
annotations={
|
||||
"title": "Folder Completion by Device",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_folder_completion(params: FolderInput) -> str:
|
||||
"""Get the replication completion percentage for a folder across ALL devices that share it.
|
||||
|
||||
This is the key tool for determining whether a folder is fully replicated
|
||||
on remote devices before removing it locally.
|
||||
|
||||
Args:
|
||||
params: FolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: JSON with folder info and per-device completion percentages,
|
||||
including whether each device is connected and has a valid remote state.
|
||||
"""
|
||||
try:
|
||||
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 client._get("/rest/system/status")
|
||||
my_id = status.get("myID", "")
|
||||
|
||||
folder_cfg = None
|
||||
for f in config.get("folders", []):
|
||||
if f["id"] == params.folder_id:
|
||||
folder_cfg = f
|
||||
break
|
||||
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", [])
|
||||
}
|
||||
completions = []
|
||||
for dev in folder_cfg.get("devices", []):
|
||||
did = dev.get("deviceID", "")
|
||||
if did == my_id:
|
||||
continue
|
||||
try:
|
||||
comp = await client._get(
|
||||
"/rest/db/completion",
|
||||
params={"folder": params.folder_id, "device": did},
|
||||
)
|
||||
connected = conn_data.get(did, {}).get("connected", False)
|
||||
completions.append(
|
||||
{
|
||||
"deviceID": did,
|
||||
"deviceName": devices_map.get(did, did[:8]),
|
||||
"connected": connected,
|
||||
"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),
|
||||
"needDeletes": comp.get("needDeletes", 0),
|
||||
"remoteState": comp.get("remoteState", "unknown"),
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
completions.append(
|
||||
{
|
||||
"deviceID": did,
|
||||
"deviceName": devices_map.get(did, did[:8]),
|
||||
"connected": False,
|
||||
"completion": None,
|
||||
"error": "Could not query completion for this device.",
|
||||
}
|
||||
)
|
||||
|
||||
fully_replicated_count = sum(
|
||||
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,
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_replication_report",
|
||||
annotations={
|
||||
"title": "Replication Report — Safe to Remove?",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_replication_report(params: EmptyInput) -> str:
|
||||
"""Generate a comprehensive replication report for ALL folders on this device.
|
||||
|
||||
For each folder, shows:
|
||||
- Local disk usage (bytes and human-readable)
|
||||
- Number of remote devices sharing it
|
||||
- How many of those have a 100% complete, valid replica
|
||||
- A 'safeToRemove' flag: True when at least one remote device has a full valid replica
|
||||
- A 'reclaimableBytes' total for folders that are safe to remove
|
||||
|
||||
This is the primary tool for deciding which folders to remove to free local disk space.
|
||||
|
||||
Returns:
|
||||
str: JSON with per-folder replication analysis and a summary section with
|
||||
total reclaimable space.
|
||||
"""
|
||||
try:
|
||||
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 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", [])
|
||||
}
|
||||
|
||||
folders = config.get("folders", [])
|
||||
report = []
|
||||
total_reclaimable = 0
|
||||
|
||||
for folder_cfg in folders:
|
||||
fid = folder_cfg["id"]
|
||||
label = folder_cfg.get("label", fid)
|
||||
paused = folder_cfg.get("paused", False)
|
||||
|
||||
try:
|
||||
fstatus = await client._get("/rest/db/status", params={"folder": fid})
|
||||
except Exception:
|
||||
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")
|
||||
|
||||
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 client._get(
|
||||
"/rest/db/completion",
|
||||
params={"folder": fid, "device": did},
|
||||
)
|
||||
device_completions.append(
|
||||
{
|
||||
"deviceID": did,
|
||||
"deviceName": devices_map.get(did, did[:8]),
|
||||
"connected": connected,
|
||||
"completion": round(comp.get("completion", 0), 2),
|
||||
"remoteState": comp.get("remoteState", "unknown"),
|
||||
"needBytes": comp.get("needBytes", 0),
|
||||
}
|
||||
)
|
||||
except Exception:
|
||||
device_completions.append(
|
||||
{
|
||||
"deviceID": did,
|
||||
"deviceName": devices_map.get(did, did[:8]),
|
||||
"connected": connected,
|
||||
"completion": None,
|
||||
"remoteState": "unknown",
|
||||
}
|
||||
)
|
||||
|
||||
fully_replicated = [
|
||||
d
|
||||
for d in device_completions
|
||||
if d.get("completion") == 100 and d.get("remoteState") == "valid"
|
||||
]
|
||||
safe = len(fully_replicated) >= 1 and state == "idle" and not paused
|
||||
|
||||
if safe:
|
||||
total_reclaimable += local_bytes
|
||||
|
||||
report.append(
|
||||
{
|
||||
"folder": fid,
|
||||
"label": label,
|
||||
"path": folder_cfg.get("path", ""),
|
||||
"type": folder_cfg.get("type", "sendreceive"),
|
||||
"paused": paused,
|
||||
"state": state,
|
||||
"localBytes": local_bytes,
|
||||
"localSize": format_bytes(local_bytes),
|
||||
"globalBytes": global_bytes,
|
||||
"globalSize": format_bytes(global_bytes),
|
||||
"totalRemoteDevices": len(remote_devices),
|
||||
"fullyReplicatedOn": len(fully_replicated),
|
||||
"fullyReplicatedDevices": [d["deviceName"] for d in fully_replicated],
|
||||
"safeToRemove": safe,
|
||||
"devices": device_completions,
|
||||
}
|
||||
)
|
||||
|
||||
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")),
|
||||
"totalReclaimableBytes": total_reclaimable,
|
||||
"totalReclaimableSize": format_bytes(total_reclaimable),
|
||||
},
|
||||
"folders": report,
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# Folder Operations
|
||||
# =====================================================================
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_pause_folder",
|
||||
annotations={
|
||||
"title": "Pause a Folder",
|
||||
"readOnlyHint": False,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_pause_folder(params: PauseFolderInput) -> str:
|
||||
"""Pause syncing for a specific folder. Useful before removing data locally.
|
||||
|
||||
This does NOT delete data — it only pauses sync activity so you can safely
|
||||
remove the local copy without Syncthing propagating the deletion to other devices.
|
||||
|
||||
Args:
|
||||
params: PauseFolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: Confirmation message.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
folder_cfg = await client._get(f"/rest/config/folders/{params.folder_id}")
|
||||
folder_cfg["paused"] = True
|
||||
await client._patch(f"/rest/config/folders/{params.folder_id}", body=folder_cfg)
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "paused",
|
||||
"folder": params.folder_id,
|
||||
"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_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_resume_folder",
|
||||
annotations={
|
||||
"title": "Resume a Folder",
|
||||
"readOnlyHint": False,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_resume_folder(params: PauseFolderInput) -> str:
|
||||
"""Resume syncing for a previously paused folder.
|
||||
|
||||
WARNING: If you deleted local data while paused, resuming will cause Syncthing
|
||||
to either re-download the data (receive-only/sendreceive) or propagate deletions
|
||||
to other devices (sendreceive). Understand the folder type before resuming.
|
||||
|
||||
Args:
|
||||
params: PauseFolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: Confirmation message with warnings.
|
||||
"""
|
||||
try:
|
||||
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
|
||||
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."
|
||||
),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_scan_folder",
|
||||
annotations={
|
||||
"title": "Trigger Folder Scan",
|
||||
"readOnlyHint": False,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_scan_folder(params: FolderInput) -> str:
|
||||
"""Request an immediate rescan of a folder to refresh its status.
|
||||
|
||||
Useful to ensure completion data is up to date before deciding to remove files.
|
||||
|
||||
Args:
|
||||
params: FolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: Confirmation that scan was requested.
|
||||
"""
|
||||
try:
|
||||
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_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)
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# New: File-level queries
|
||||
# =====================================================================
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_browse_folder",
|
||||
annotations={
|
||||
"title": "Browse Folder Contents",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_browse_folder(params: BrowseFolderInput) -> str:
|
||||
"""Browse the contents of a folder at a given path prefix.
|
||||
|
||||
Returns a directory-style listing of files and subdirectories known to
|
||||
Syncthing's database — useful for inspecting what is being synced without
|
||||
direct filesystem access.
|
||||
|
||||
Args:
|
||||
params: BrowseFolderInput with folder_id, optional prefix and levels.
|
||||
|
||||
Returns:
|
||||
str: JSON with the folder contents at the requested path.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
query: dict[str, str] = {"folder": params.folder_id}
|
||||
if params.prefix:
|
||||
query["prefix"] = params.prefix
|
||||
if params.levels is not None:
|
||||
query["levels"] = str(params.levels)
|
||||
result = await client._get("/rest/db/browse", params=query)
|
||||
return json.dumps(
|
||||
{
|
||||
"folder": params.folder_id,
|
||||
"instance": client.name,
|
||||
"prefix": params.prefix or "",
|
||||
"entries": result,
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_file_info",
|
||||
annotations={
|
||||
"title": "File Info",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_file_info(params: FileInfoInput) -> str:
|
||||
"""Get detailed information about a specific file within a folder.
|
||||
|
||||
Returns version vectors, availability on devices, modification time,
|
||||
size, and whether the file is locally present. Useful for diagnosing
|
||||
sync conflicts or understanding file state across the cluster.
|
||||
|
||||
Args:
|
||||
params: FileInfoInput with folder_id and file_path.
|
||||
|
||||
Returns:
|
||||
str: JSON with file metadata including global and local info.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
result = await client._get(
|
||||
"/rest/db/file",
|
||||
params={"folder": params.folder_id, "file": params.file_path},
|
||||
)
|
||||
# Enrich with human-readable sizes
|
||||
for key in ("global", "local"):
|
||||
entry = result.get(key)
|
||||
if isinstance(entry, dict) and "size" in entry:
|
||||
entry["sizeFormatted"] = format_bytes(entry["size"])
|
||||
return json.dumps(
|
||||
{
|
||||
"folder": params.folder_id,
|
||||
"file": params.file_path,
|
||||
"instance": client.name,
|
||||
"availability": result.get("availability"),
|
||||
"global": result.get("global"),
|
||||
"local": result.get("local"),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_folder_need",
|
||||
annotations={
|
||||
"title": "Folder Need (Out-of-Sync Files)",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_folder_need(params: FolderNeedInput) -> str:
|
||||
"""List files that the folder still needs — items that are out of sync locally.
|
||||
|
||||
Supports pagination for large result sets.
|
||||
|
||||
Args:
|
||||
params: FolderNeedInput with folder_id, page, per_page.
|
||||
|
||||
Returns:
|
||||
str: JSON with lists of files needed (progress, queued, rest) and a total page count.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
result = await client._get(
|
||||
"/rest/db/need",
|
||||
params={
|
||||
"folder": params.folder_id,
|
||||
"page": str(params.page),
|
||||
"perpage": str(params.per_page),
|
||||
},
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
"folder": params.folder_id,
|
||||
"instance": client.name,
|
||||
"page": result.get("page", params.page),
|
||||
"perpage": result.get("perpage", params.per_page),
|
||||
"progress": result.get("progress", []),
|
||||
"queued": result.get("queued", []),
|
||||
"rest": result.get("rest", []),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# New: Conflict resolution
|
||||
# =====================================================================
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_override_folder",
|
||||
annotations={
|
||||
"title": "Override Remote Changes (Send-Only)",
|
||||
"readOnlyHint": False,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_override_folder(params: FolderInput) -> str:
|
||||
"""Override remote changes on a send-only folder, making local state authoritative.
|
||||
|
||||
Use this when a send-only folder shows 'out of sync' items that you want
|
||||
to push to remote devices. Only meaningful for folders configured as
|
||||
'sendonly'.
|
||||
|
||||
Args:
|
||||
params: FolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: Confirmation that the override was requested.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
await client._post("/rest/db/override", params={"folder": params.folder_id})
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "override_requested",
|
||||
"folder": params.folder_id,
|
||||
"instance": client.name,
|
||||
"message": (
|
||||
f"Override requested for folder '{params.folder_id}'. "
|
||||
"Local state will be pushed to remote devices."
|
||||
),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_revert_folder",
|
||||
annotations={
|
||||
"title": "Revert Local Changes (Receive-Only)",
|
||||
"readOnlyHint": False,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_revert_folder(params: FolderInput) -> str:
|
||||
"""Revert local changes on a receive-only folder, pulling the remote state.
|
||||
|
||||
Use this when a receive-only folder has local modifications that should be
|
||||
discarded in favour of the remote version.
|
||||
|
||||
Args:
|
||||
params: FolderInput with folder_id.
|
||||
|
||||
Returns:
|
||||
str: Confirmation that the revert was requested.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
await client._post("/rest/db/revert", params={"folder": params.folder_id})
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "revert_requested",
|
||||
"folder": params.folder_id,
|
||||
"instance": client.name,
|
||||
"message": (
|
||||
f"Revert requested for folder '{params.folder_id}'. "
|
||||
"Local changes will be replaced by the remote state."
|
||||
),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
@@ -0,0 +1,104 @@
|
||||
"""Tools for instance management and listing folders (config-level)."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from syncthing_mcp.models import EmptyInput
|
||||
from syncthing_mcp.registry import (
|
||||
format_bytes,
|
||||
get_all_instances,
|
||||
get_instance,
|
||||
handle_error_global,
|
||||
)
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
@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 get_all_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)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_list_folders",
|
||||
annotations={
|
||||
"title": "List All Folders",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_list_folders(params: EmptyInput) -> str:
|
||||
"""List all configured Syncthing folders with their labels, paths, type, and shared devices.
|
||||
|
||||
Returns:
|
||||
str: JSON array of folders with id, label, path, type, paused, and list of shared device names/IDs.
|
||||
"""
|
||||
try:
|
||||
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", [])
|
||||
}
|
||||
result = []
|
||||
for f in folders:
|
||||
shared = []
|
||||
for d in f.get("devices", []):
|
||||
did = d.get("deviceID", "")
|
||||
shared.append({"deviceID": did, "name": devices.get(did, did[:8])})
|
||||
result.append(
|
||||
{
|
||||
"id": f["id"],
|
||||
"label": f.get("label", f["id"]),
|
||||
"path": f.get("path", ""),
|
||||
"type": f.get("type", "sendreceive"),
|
||||
"paused": f.get("paused", False),
|
||||
"sharedWith": shared,
|
||||
"numDevices": len(shared),
|
||||
}
|
||||
)
|
||||
return json.dumps(result, indent=2)
|
||||
except Exception as e:
|
||||
return handle_error_global(e)
|
||||
@@ -0,0 +1,450 @@
|
||||
"""System status, health, errors, log, restart, and upgrade tools."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from syncthing_mcp.models import EmptyInput
|
||||
from syncthing_mcp.registry import (
|
||||
format_bytes,
|
||||
get_instance,
|
||||
handle_error_global,
|
||||
)
|
||||
from syncthing_mcp.server import mcp
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="syncthing_system_status",
|
||||
annotations={
|
||||
"title": "System Status",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_system_status(params: EmptyInput) -> str:
|
||||
"""Get Syncthing system status including this device's ID, name, uptime, and version.
|
||||
|
||||
Returns:
|
||||
str: JSON with myID, device name, uptime, version, and connection stats.
|
||||
"""
|
||||
try:
|
||||
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", []):
|
||||
if dev.get("deviceID") == my_id:
|
||||
my_name = dev.get("name", my_id[:8])
|
||||
break
|
||||
return json.dumps(
|
||||
{
|
||||
"instance": client.name,
|
||||
"myID": my_id,
|
||||
"deviceName": my_name,
|
||||
"uptime": status.get("uptime"),
|
||||
"version": version.get("version"),
|
||||
"os": version.get("os"),
|
||||
"arch": version.get("arch"),
|
||||
"numFolders": len(config.get("folders", [])),
|
||||
"numDevices": len(config.get("devices", [])),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except Exception as e:
|
||||
if isinstance(e, ValueError):
|
||||
return handle_error_global(e)
|
||||
try:
|
||||
return get_instance(None).handle_error(e)
|
||||
except Exception:
|
||||
return handle_error_global(e)
|
||||
|
||||
|
||||
@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",
|
||||
},
|
||||
)
|
||||
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_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):
|
||||
pass # Expected — Syncthing closes the connection as it restarts
|
||||
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_check_upgrade",
|
||||
annotations={
|
||||
"title": "Check for Upgrade",
|
||||
"readOnlyHint": True,
|
||||
"destructiveHint": False,
|
||||
"idempotentHint": True,
|
||||
"openWorldHint": False,
|
||||
},
|
||||
)
|
||||
async def syncthing_check_upgrade(params: EmptyInput) -> str:
|
||||
"""Check if a newer version of Syncthing is available.
|
||||
|
||||
Returns:
|
||||
str: JSON with running version, latest version, and whether an upgrade
|
||||
is available.
|
||||
"""
|
||||
try:
|
||||
client = get_instance(params.instance)
|
||||
version = await client._get("/rest/system/version")
|
||||
try:
|
||||
upgrade = await client._get("/rest/system/upgrade")
|
||||
return json.dumps(
|
||||
{
|
||||
"instance": client.name,
|
||||
"running": version.get("version"),
|
||||
"latest": upgrade.get("latest"),
|
||||
"newer": upgrade.get("newer", False),
|
||||
"majorNewer": upgrade.get("majorNewer", False),
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
except httpx.HTTPStatusError as ue:
|
||||
if ue.response.status_code == 501:
|
||||
return json.dumps(
|
||||
{
|
||||
"instance": client.name,
|
||||
"running": version.get("version"),
|
||||
"message": "Upgrade check not available (disabled or unsupported).",
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
raise
|
||||
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)
|
||||
|
||||
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", {})
|
||||
|
||||
online_count = sum(1 for c in conn_data.values() if c.get("connected"))
|
||||
total_remote = len(conn_data)
|
||||
|
||||
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)
|
||||
|
||||
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)")
|
||||
|
||||
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)
|
||||
Reference in new issue
Block a user