|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Validate .cursor-plugin/plugin.json against the vendored Cursor plugin schema. |
| 3 | +
|
| 4 | +The Claude Code CLI (`claude plugin validate`) only understands Claude manifests, |
| 5 | +so the Cursor manifest needs its own guard. This validates it against |
| 6 | +schemas/cursor-plugin.schema.json (Cursor's published draft-07 schema, which is |
| 7 | +additionalProperties:false — unknown fields fail, not just warn) and confirms the |
| 8 | +assets it declares (logo, skills) and the auto-detected mcp.json actually exist. |
| 9 | +
|
| 10 | +Exits non-zero on any failure so CI goes red. Requires: jsonschema. |
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import json |
| 15 | +import struct |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +ROOT = Path(__file__).resolve().parent.parent |
| 20 | +SCHEMA = ROOT / "schemas" / "cursor-plugin.schema.json" |
| 21 | +MANIFEST = ROOT / ".cursor-plugin" / "plugin.json" |
| 22 | + |
| 23 | +PNG_SIG = bytes.fromhex("89504e470d0a1a0a") |
| 24 | + |
| 25 | + |
| 26 | +def main() -> int: |
| 27 | + from jsonschema import Draft7Validator |
| 28 | + |
| 29 | + problems = 0 |
| 30 | + |
| 31 | + def ok(msg: str) -> None: |
| 32 | + print(f" [PASS] {msg}") |
| 33 | + |
| 34 | + def fail(msg: str) -> None: |
| 35 | + nonlocal problems |
| 36 | + problems += 1 |
| 37 | + print(f" [FAIL] {msg}") |
| 38 | + |
| 39 | + print("Cursor plugin manifest validation") |
| 40 | + |
| 41 | + if not MANIFEST.is_file(): |
| 42 | + print(f" [FAIL] {MANIFEST.relative_to(ROOT)} not found") |
| 43 | + return 1 |
| 44 | + if not SCHEMA.is_file(): |
| 45 | + print(f" [FAIL] {SCHEMA.relative_to(ROOT)} not found") |
| 46 | + return 1 |
| 47 | + |
| 48 | + manifest = json.loads(MANIFEST.read_text()) |
| 49 | + schema = json.loads(SCHEMA.read_text()) |
| 50 | + |
| 51 | + # 1) Schema conformance (catches the icon/author.url class of errors). |
| 52 | + errs = sorted(Draft7Validator(schema).iter_errors(manifest), key=lambda e: list(e.path)) |
| 53 | + if errs: |
| 54 | + fail(f"schema: {len(errs)} violation(s)") |
| 55 | + for e in errs: |
| 56 | + loc = "/".join(map(str, e.path)) or "(root)" |
| 57 | + print(f" - at {loc}: {e.message}") |
| 58 | + else: |
| 59 | + ok("schema: valid against schemas/cursor-plugin.schema.json") |
| 60 | + |
| 61 | + # 2) Declared logo exists, is relative, and is a square PNG. |
| 62 | + logo = manifest.get("logo") |
| 63 | + if not logo: |
| 64 | + fail("logo: not declared") |
| 65 | + elif logo.startswith(("/", "http://", "https://")): |
| 66 | + ok(f"logo: external/absolute ({logo}) — file check skipped") |
| 67 | + else: |
| 68 | + p = ROOT / logo |
| 69 | + if not p.is_file(): |
| 70 | + fail(f"logo: {logo} does not exist") |
| 71 | + else: |
| 72 | + b = p.read_bytes() |
| 73 | + if b[:8] != PNG_SIG: |
| 74 | + fail(f"logo: {logo} is not a valid PNG") |
| 75 | + else: |
| 76 | + w, h = struct.unpack(">II", b[16:24]) |
| 77 | + if w == h: |
| 78 | + ok(f"logo: {logo} is a {w}x{h} square PNG") |
| 79 | + else: |
| 80 | + fail(f"logo: {logo} is {w}x{h}, not square") |
| 81 | + |
| 82 | + # 3) Declared skills path resolves to a directory. |
| 83 | + skills = manifest.get("skills") |
| 84 | + if skills: |
| 85 | + sp = ROOT / str(skills).lstrip("./") |
| 86 | + if sp.is_dir(): |
| 87 | + ok(f"skills: {skills} exists") |
| 88 | + else: |
| 89 | + fail(f"skills: {skills} is missing") |
| 90 | + |
| 91 | + # 4) Auto-detected mcp.json at plugin root (optional, but if present must be sane). |
| 92 | + mcp = ROOT / "mcp.json" |
| 93 | + if mcp.is_file(): |
| 94 | + try: |
| 95 | + servers = json.loads(mcp.read_text()).get("mcpServers", {}) |
| 96 | + except json.JSONDecodeError as ex: |
| 97 | + fail(f"mcp.json: invalid JSON ({ex})") |
| 98 | + else: |
| 99 | + if isinstance(servers, dict) and servers: |
| 100 | + ok(f"mcp.json: present with {len(servers)} server(s)") |
| 101 | + else: |
| 102 | + fail("mcp.json: no mcpServers entries") |
| 103 | + else: |
| 104 | + ok("mcp.json: not present (no bundled MCP server)") |
| 105 | + |
| 106 | + print("OK" if problems == 0 else f"{problems} problem(s) — see above") |
| 107 | + return 0 if problems == 0 else 1 |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + sys.exit(main()) |
0 commit comments