|
| 1 | +"""Public MCP server — serves DataCivicLab context artifacts from the context branch. |
| 2 | +
|
| 3 | +Exposes three read-only resources built by the CI workflow: |
| 4 | + context://session_bootstrap — session_bootstrap.md |
| 5 | + context://workspace_triage — workspace_triage.json |
| 6 | + context://topic_index — topic_index.json |
| 7 | +
|
| 8 | +One optional tool: |
| 9 | + refresh_context — triggers a new CI build (requires GITHUB_TOKEN with workflow scope) |
| 10 | +
|
| 11 | +Configuration via environment variables: |
| 12 | + ACB_REPO GitHub repo (default: dataciviclab/agent-context-builder) |
| 13 | + ACB_BRANCH Branch where artifacts are published (default: context) |
| 14 | + GITHUB_TOKEN Required only for the refresh_context tool |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +import requests |
| 20 | +from mcp.server.fastmcp import FastMCP |
| 21 | + |
| 22 | +_REPO = os.environ.get("ACB_REPO", "dataciviclab/agent-context-builder") |
| 23 | +_BRANCH = os.environ.get("ACB_BRANCH", "context") |
| 24 | +_RAW_BASE = f"https://raw.githubusercontent.com/{_REPO}/{_BRANCH}" |
| 25 | +_API_BASE = f"https://api.github.com/repos/{_REPO}" |
| 26 | + |
| 27 | +mcp = FastMCP( |
| 28 | + "dataciviclab-context", |
| 29 | + instructions=( |
| 30 | + "DataCivicLab context artifacts, generated from GitHub every 6 hours. " |
| 31 | + "Start with session_bootstrap for a quick orientation, then use " |
| 32 | + "workspace_triage for machine-readable state and topic_index for " |
| 33 | + "targeted exploration by topic." |
| 34 | + ), |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def _fetch(path: str) -> str: |
| 39 | + """Fetch a file from the context branch.""" |
| 40 | + token = os.environ.get("GITHUB_TOKEN") |
| 41 | + headers = {"Authorization": f"token {token}"} if token else {} |
| 42 | + response = requests.get(f"{_RAW_BASE}/{path}", headers=headers, timeout=10) |
| 43 | + response.raise_for_status() |
| 44 | + return response.text |
| 45 | + |
| 46 | + |
| 47 | +@mcp.resource("context://session_bootstrap") |
| 48 | +def session_bootstrap() -> str: |
| 49 | + """Orientamento rapido: repo attivi, PR aperte, discussion, stato locale, topic.""" |
| 50 | + return _fetch("session_bootstrap.md") |
| 51 | + |
| 52 | + |
| 53 | +@mcp.resource("context://workspace_triage") |
| 54 | +def workspace_triage() -> str: |
| 55 | + """Triage machine-readable: PR, issue, discussion, stato git per repo, warning.""" |
| 56 | + return _fetch("workspace_triage.json") |
| 57 | + |
| 58 | + |
| 59 | +@mcp.resource("context://topic_index") |
| 60 | +def topic_index() -> str: |
| 61 | + """Lookup per topic: repo rilevanti, path, prossimo passo suggerito.""" |
| 62 | + return _fetch("topic_index.json") |
| 63 | + |
| 64 | + |
| 65 | +@mcp.tool() |
| 66 | +def refresh_context() -> str: |
| 67 | + """Triggera un nuovo build del contesto su CI. |
| 68 | +
|
| 69 | + Richiede GITHUB_TOKEN con scope workflow. |
| 70 | + Gli artifact aggiornati saranno disponibili entro ~1 minuto. |
| 71 | + """ |
| 72 | + token = os.environ.get("GITHUB_TOKEN") |
| 73 | + if not token: |
| 74 | + return ( |
| 75 | + "Errore: GITHUB_TOKEN non impostato. " |
| 76 | + "Serve un token con scope 'workflow' per triggerare il build." |
| 77 | + ) |
| 78 | + |
| 79 | + response = requests.post( |
| 80 | + f"{_API_BASE}/actions/workflows/build-context.yml/dispatches", |
| 81 | + json={"ref": "main"}, |
| 82 | + headers={ |
| 83 | + "Authorization": f"token {token}", |
| 84 | + "Accept": "application/vnd.github+json", |
| 85 | + }, |
| 86 | + timeout=10, |
| 87 | + ) |
| 88 | + if response.status_code == 204: |
| 89 | + return "Build triggerato. Gli artifact saranno aggiornati entro ~1 minuto." |
| 90 | + return f"Errore: {response.status_code} — {response.text}" |
| 91 | + |
| 92 | + |
| 93 | +def main() -> None: |
| 94 | + """Entry point per l'MCP server.""" |
| 95 | + mcp.run() |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments