From b131f788704e320202fc00963c85db15897f9d2f Mon Sep 17 00:00:00 2001 From: dasemu Date: Thu, 8 Oct 2026 06:54:04 +0200 Subject: [PATCH] feat: add Umami analytics MCP server Moved from an untracked deployment on ZenvairoMCP (pct 210) into its own repo so it can be built and run via remote-mcp-manager. requirements.txt generated via pip freeze from the live venv; nixpacks.toml sets the explicit start command. Co-Authored-By: Claude Sonnet 5 --- .gitignore | 2 + nixpacks.toml | 2 + requirements.txt | 39 ++++++++++++++ server.py | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 nixpacks.toml create mode 100644 requirements.txt create mode 100644 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a60b85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc diff --git a/nixpacks.toml b/nixpacks.toml new file mode 100644 index 0000000..c457df5 --- /dev/null +++ b/nixpacks.toml @@ -0,0 +1,2 @@ +[start] +cmd = "python server.py" diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c17449c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,39 @@ +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.14.2 +attrs==26.1.0 +certifi==2026.7.22 +cffi==2.1.1 +click==8.4.2 +cryptography==50.0.0 +h11==0.16.0 +httpcore==1.0.9 +httpcore2==2.10.0 +httpx==0.28.1 +httpx2==2.10.0 +idna==3.18 +jsonschema==4.26.0 +jsonschema-specifications==2025.9.1 +markdown-it-py==4.2.0 +mcp==2.0.0 +mcp-types==2.0.0 +mdurl==0.1.2 +opentelemetry-api==1.44.0 +pycparser==3.0 +pydantic==2.13.4 +pydantic_core==2.46.4 +Pygments==2.20.0 +PyJWT==2.13.0 +python-dotenv==1.2.2 +python-multipart==0.0.32 +referencing==0.37.0 +rich==15.0.0 +rpds-py==2026.6.3 +shellingham==1.5.4 +sse-starlette==3.4.8 +starlette==1.6.0 +truststore==0.10.4 +typer==0.27.1 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +uvicorn==0.52.2 diff --git a/server.py b/server.py new file mode 100644 index 0000000..fc9a48f --- /dev/null +++ b/server.py @@ -0,0 +1,136 @@ +import os +import threading +from datetime import datetime, timezone + +import httpx +from mcp.server.mcpserver import MCPServer + +UMAMI_HOST = os.environ["UMAMI_HOST"].rstrip("/") +UMAMI_USERNAME = os.environ["UMAMI_USERNAME"] +UMAMI_PASSWORD = os.environ["UMAMI_PASSWORD"] +BIND_HOST = os.environ.get("BIND_HOST", "127.0.0.1") +BIND_PORT = int(os.environ.get("BIND_PORT", "8083")) + +METRIC_TYPES = {"url", "referrer", "browser", "os", "device", "country", "event", "query"} + +mcp = MCPServer("umami") + +_token_lock = threading.Lock() +_token: str | None = None + + +def _login() -> str: + global _token + with httpx.Client(timeout=30.0) as c: + r = c.post(f"{UMAMI_HOST}/api/auth/login", + json={"username": UMAMI_USERNAME, "password": UMAMI_PASSWORD}) + r.raise_for_status() + _token = r.json()["token"] + return _token + + +def _request(method: str, path: str, **kwargs) -> httpx.Response: + global _token + with _token_lock: + if _token is None: + _login() + with httpx.Client(timeout=30.0) as c: + headers = kwargs.pop("headers", {}) + headers["Authorization"] = f"Bearer {_token}" + r = c.request(method, f"{UMAMI_HOST}/api{path}", headers=headers, **kwargs) + if r.status_code == 401: + with _token_lock: + _login() + headers["Authorization"] = f"Bearer {_token}" + r = c.request(method, f"{UMAMI_HOST}/api{path}", headers=headers, **kwargs) + r.raise_for_status() + return r + + +def _to_ms(value: str) -> int: + """Accept an ISO date/datetime string (e.g. '2026-08-01' or '2026-08-01T00:00:00Z') + or a raw millisecond epoch string, and return milliseconds since epoch.""" + value = value.strip() + if value.isdigit(): + return int(value) + text = value.replace("Z", "+00:00") + dt = datetime.fromisoformat(text) + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return int(dt.timestamp() * 1000) + + +@mcp.tool() +def list_websites() -> list[dict]: + """List all Umami websites visible to this account, including websites owned by + teams/organizations this account owns. Use list_teams + list_team_websites to also + see websites in teams this account is only a member (not owner) of.""" + r = _request("GET", "/websites", params={"pageSize": 100, "includeTeams": "true"}) + data = r.json().get("data", []) + return [{"id": w.get("id"), "name": w.get("name"), "domain": w.get("domain"), + "created_at": w.get("createdAt")} for w in data] + + +@mcp.tool() +def list_teams() -> list[dict]: + """List Umami teams/organizations this account belongs to.""" + r = _request("GET", "/teams", params={"pageSize": 100}) + data = r.json().get("data", []) + return [{"id": t.get("id"), "name": t.get("name")} for t in data] + + +@mcp.tool() +def list_team_websites(team_id: str) -> list[dict]: + """List all websites belonging to a specific Umami team/organization (use list_teams + to find team ids).""" + r = _request("GET", f"/teams/{team_id}/websites", params={"pageSize": 100}) + data = r.json().get("data", []) + return [{"id": w.get("id"), "name": w.get("name"), "domain": w.get("domain"), + "created_at": w.get("createdAt")} for w in data] + + +@mcp.tool() +def get_website_stats(website_id: str, start_at: str, end_at: str) -> dict: + """Get summary stats (pageviews, visitors, visits, bounces, total time) for a + website between start_at and end_at (ISO date/datetime strings, e.g. '2026-08-01').""" + r = _request("GET", f"/websites/{website_id}/stats", + params={"startAt": _to_ms(start_at), "endAt": _to_ms(end_at)}) + return r.json() + + +@mcp.tool() +def get_pageviews(website_id: str, start_at: str, end_at: str, unit: str = "day", + timezone_name: str = "UTC") -> dict: + """Get pageview and session timeseries for a website between start_at and end_at. + unit: 'hour'|'day'|'month'. timezone_name: IANA timezone, e.g. 'Europe/Madrid'.""" + r = _request("GET", f"/websites/{website_id}/pageviews", + params={"startAt": _to_ms(start_at), "endAt": _to_ms(end_at), + "unit": unit, "timezone": timezone_name}) + return r.json() + + +@mcp.tool() +def get_metrics(website_id: str, start_at: str, end_at: str, metric_type: str, + limit: int = 20) -> list[dict]: + """Get a breakdown of a metric for a website between start_at and end_at. + metric_type must be one of: url, referrer, browser, os, device, country, event, query.""" + if metric_type not in METRIC_TYPES: + raise ValueError(f"metric_type must be one of {sorted(METRIC_TYPES)}") + r = _request("GET", f"/websites/{website_id}/metrics", + params={"startAt": _to_ms(start_at), "endAt": _to_ms(end_at), + "type": metric_type, "limit": limit}) + return r.json() + + +@mcp.tool() +def get_active_visitors(website_id: str) -> int: + """Get the number of currently active (real-time) visitors on a website.""" + r = _request("GET", f"/websites/{website_id}/active") + data = r.json() + if isinstance(data, list): + return len(data) + return data.get("visitors", data) + + +if __name__ == "__main__": + mcp.run(transport="streamable-http", host=BIND_HOST, port=BIND_PORT)