run_clustering
Perform clustering on the active dataset by running PCA, nearest neighbor calculation, Leiden clustering, and UMAP, then update the 3D visualization with new cluster labels.
Instructions
Run MilliMap's clustering pipeline on the active dataset.
Runs PCA → neighbors (n_neighbors) → Leiden (resolution) → UMAP using Scanpy and updates the 3D view in MilliMap with the new cluster labels.
Args: resolution: Leiden resolution (higher = more clusters). Default 0.5. n_neighbors: k for the neighbors graph. Default 15.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resolution | No | ||
| n_neighbors | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- src/millimap_mcp/server.py:258-272 (handler)The run_clustering tool handler — a FastMCP tool decorated function that forwards the request to the MilliMap desktop app via HTTP POST to /tool. It passes resolution and n_neighbors parameters to the MilliMap backend which runs PCA, neighbors graph, Leiden clustering, and UMAP.
@mcp.tool() def run_clustering(resolution: float = 0.5, n_neighbors: int = 15) -> str: """Run MilliMap's clustering pipeline on the active dataset. Runs PCA → neighbors (n_neighbors) → Leiden (resolution) → UMAP using Scanpy and updates the 3D view in MilliMap with the new cluster labels. Args: resolution: Leiden resolution (higher = more clusters). Default 0.5. n_neighbors: k for the neighbors graph. Default 15. """ return _fmt_json(_post_tool("run_clustering", { "resolution": resolution, "n_neighbors": n_neighbors, })) - src/millimap_mcp/server.py:258-258 (registration)Registration of run_clustering as an MCP tool via the @mcp.tool() decorator on the FastMCP instance named 'mcp' (line 22).
@mcp.tool() - src/millimap_mcp/server.py:265-267 (schema)Input schema for run_clustering: resolution (float, default 0.5) and n_neighbors (int, default 15), defined via type hints and docstring.
Args: resolution: Leiden resolution (higher = more clusters). Default 0.5. n_neighbors: k for the neighbors graph. Default 15. - src/millimap_mcp/server.py:33-58 (helper)Helper function _post_tool that makes the HTTP POST call to the MilliMap desktop app at http://host:port/tool, used by run_clustering to delegate execution.
def _post_tool(name: str, args: dict, timeout: float = 600.0) -> dict: ctrl = _load_control() if not ctrl or not ctrl.get("port"): return { "ok": False, "error": ( f"MilliMap control endpoint not found at {CONTROL_PATH}. " "Make sure MilliMap is running with a dataset loaded." ), } host = ctrl.get("host", "127.0.0.1") port = int(ctrl["port"]) url = f"http://{host}:{port}/tool" data = json.dumps({"name": name, "args": args}).encode("utf-8") req = urllib.request.Request( url, data=data, headers={"Content-Type": "application/json"}, method="POST", ) try: with urllib.request.urlopen(req, timeout=timeout) as resp: return json.loads(resp.read().decode("utf-8")) except urllib.error.URLError as exc: return {"ok": False, "error": f"connection failed: {exc.reason}"} except Exception as exc: return {"ok": False, "error": f"HTTP call failed: {exc}"} - src/millimap_mcp/server.py:77-78 (helper)Helper _fmt_json that converts the tool response dict to a JSON string, used by run_clustering to format its return value.
def _fmt_json(payload: Any) -> str: return json.dumps(payload, indent=2, default=str)