Download examples/github_mcp_server.py from markedet/agent-integration-guides: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/datasets/markedet/agent-integration-guides/resolve/main/examples/github_mcp_server.py
- Command line
-
hf download hf://datasets/markedet/agent-integration-guides/examples/github_mcp_server.py
-
curl -L -o github_mcp_server.py https://huggingface.co/datasets/markedet/agent-integration-guides/resolve/main/examples/github_mcp_server.py
2.58 kB
| """A minimal MCP server that wraps part of an official, documented API. | |
| It exposes two read-only tools backed by the GitHub REST API: | |
| - get_repo(owner, repo) | |
| - list_releases(owner, repo, limit) | |
| Auth: optional GITHUB_TOKEN environment variable (never printed or returned). | |
| Run over stdio (default): python github_mcp_server.py | |
| Run over Streamable HTTP: python github_mcp_server.py --http | |
| Requires: pip install "mcp>=2,<3" (the MCPServer class is from SDK v2) | |
| License: Apache-2.0. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import re | |
| import sys | |
| from typing import Any | |
| from urllib.parse import quote | |
| from mcp.server import MCPServer | |
| from apiclient import get_json, paginate | |
| API = "https://api.github.com" | |
| NAME = r"^[A-Za-z0-9_.-]{1,100}$" | |
| mcp = MCPServer("github-readonly-example") | |
| def _headers() -> dict[str, str]: | |
| h = { | |
| "Accept": "application/vnd.github+json", | |
| "X-GitHub-Api-Version": "2022-11-28", | |
| "User-Agent": "example-agent/0.1", | |
| } | |
| token = os.environ.get("GITHUB_TOKEN") | |
| if token: | |
| h["Authorization"] = f"Bearer {token}" | |
| return h | |
| def _check(owner: str, repo: str) -> None: | |
| if not (re.match(NAME, owner) and re.match(NAME, repo)): | |
| raise ValueError("owner and repo may only contain letters, digits, '_', '.', '-'") | |
| def get_repo(owner: str, repo: str) -> dict[str, Any]: | |
| """Return basic public facts about a GitHub repository.""" | |
| _check(owner, repo) | |
| data, _ = get_json(f"{API}/repos/{quote(owner)}/{quote(repo)}", _headers()) | |
| keys = ("full_name", "description", "html_url", "stargazers_count", "open_issues_count", "license", "pushed_at") | |
| out = {k: data.get(k) for k in keys} | |
| if out["license"]: | |
| out["license"] = out["license"].get("spdx_id") | |
| return out | |
| def list_releases(owner: str, repo: str, limit: int = 5) -> list[dict[str, Any]]: | |
| """Return the newest releases (tag, name, date, url). `limit` is 1 to 30.""" | |
| _check(owner, repo) | |
| limit = max(1, min(int(limit), 30)) | |
| url = f"{API}/repos/{quote(owner)}/{quote(repo)}/releases?per_page={limit}" | |
| items = [] | |
| for rel in paginate(url, _headers(), max_pages=1): | |
| items.append({k: rel.get(k) for k in ("tag_name", "name", "published_at", "html_url")}) | |
| if len(items) >= limit: | |
| break | |
| return items | |
| if __name__ == "__main__": | |
| # In stdio mode stdout carries protocol messages: log to stderr only. | |
| if "--http" in sys.argv: | |
| mcp.run(transport="streamable-http") | |
| else: | |
| mcp.run(transport="stdio") | |