import os from mcp.server import MCPServer from mcp.server.transport_security import TransportSecuritySettings from pg_cve_mcp import __version__, cve_utils from pg_cve_mcp.data import load_data mcp = MCPServer("pg-cve-mcp", version=__version__) def transport_security_settings() -> TransportSecuritySettings: raw = os.environ.get("PG_CVE_MCP_ALLOWED_HOSTS", "*") hosts = [host.strip() for host in raw.split(",") if host.strip()] if not hosts or "*" in hosts: return TransportSecuritySettings(enable_dns_rebinding_protection=False) return TransportSecuritySettings( enable_dns_rebinding_protection=True, allowed_hosts=hosts, ) def _get_data(): return load_data() @mcp.tool() def get_cves(version: str) -> dict: """Get the complete security report for a PostgreSQL version. Returns EOL and yanked status, the latest minor in the branch, summary stats (CVE count, max CVSS, exploit count) and the full list of CVEs sorted by CVSS score (highest first). For end-of-life releases a synthetic CVE-EOL-WARNING entry (CVSS 10.0) is prepended. Args: version: A PostgreSQL version string (e.g. "16.4", "15.8", "14.0"). """ data = _get_data() return cve_utils.get_cves(data, version) @mcp.tool() def compare_versions(v1: str, v2: str) -> dict: """Compare the CVE profiles of two PostgreSQL versions side by side. Shows CVEs fixed in v2 (present in v1 but not v2), new CVEs in v2, and CVEs still present in both, all sorted by CVSS score. Flags whether either version is yanked or end-of-life. Args: v1: First PostgreSQL version (e.g. "16.3"). v2: Second PostgreSQL version (e.g. "16.4"). """ data = _get_data() return cve_utils.compare_versions(data, v1, v2) @mcp.tool() def get_upgrade_path(from_version: str) -> dict: """Suggest the recommended upgrade target for a PostgreSQL version. Returns the latest minor in the same branch (the safe, low-effort upgrade) with the CVEs it fixes. If the branch is end-of-life or the next one to reach end-of-life, a major upgrade is also recommended: it targets the previous, already-hardened major (N-1) rather than the brand-new one, mirroring how a cautious DBA would advise. Args: from_version: The currently installed PostgreSQL version (e.g. "16.3"). """ data = _get_data() return cve_utils.get_upgrade_path(data, from_version) @mcp.tool() def get_exploits() -> list: """List all CVEs with known public exploits, sorted by CVSS score. """ data = _get_data() return cve_utils.get_exploits(data) @mcp.tool() def get_summary() -> dict: """Get an overview of the tracked PostgreSQL CVE database. Returns total unique CVEs, known exploits, yanked releases (with reasons), the next major to reach EOL, active/EOL major counts and the dataset's last_updated date. """ data = _get_data() return cve_utils.get_summary(data)