gabriel / musehub public
test_mcp_registry_metadata.py python
126 lines 6.0 KB
Raw
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ breaking 1 day ago
1 """MCP registry metadata agrees with behaviour (musehub#292, musehub#324).
2
3 * MCPA_01–03 (#292): every tool reports all four MCP hints; delete / remove / revoke / transfer tools are destructive.
4 * MCPM_01 (#324 D1): the read-only tools that need a signature report ``readOnlyHint: true``.
5 * MCPM_02 (#324 D1): the signature gate does not call a read a "Write tool".
6 * MCPM_03 (#324 D2, PostgreSQL): tasks are listed highest priority first, as the tool descriptions say.
7 * MCPM_04 (#324 D3): no docstring in ``musehub/mcp/`` states a tool count.
8 """
9
10 from __future__ import annotations
11
12 import ast
13 import re
14 from collections.abc import Iterator
15 from pathlib import Path
16
17 import pytest
18 from fastapi import FastAPI
19 from fastapi.testclient import TestClient
20 from sqlalchemy.ext.asyncio import AsyncSession
21
22 _HINTS = ("readOnlyHint", "destructiveHint", "idempotentHint", "openWorldHint")
23 _DESTRUCTIVE = (
24 "muse_mist_delete", "musehub_delete_coord_reservation", "musehub_delete_issue_comment", "musehub_delete_label",
25 "musehub_delete_release_asset", "musehub_delete_repo", "musehub_delete_user_account", "musehub_delete_webhook",
26 "musehub_remove_collaborator", "musehub_remove_issue_label", "musehub_remove_proposal_reviewer",
27 "musehub_revoke_attestation", "musehub_transfer_repo_ownership",
28 )
29
30
31 def _listed() -> dict[str, dict[str, object]]:
32 from musehub.mcp.dispatcher import _handle_tools_list
33
34 return {t["name"]: t["annotations"] for t in _handle_tools_list()["tools"]} # type: ignore[index]
35
36
37 # ── #292 ─────────────────────────────────────────────────────────────────────
38
39
40 def test_mcpa_01_every_tool_sets_all_four_hints() -> None:
41 missing = {n: [h for h in _HINTS if not isinstance(a.get(h), bool)] for n, a in _listed().items()}
42 assert {n: m for n, m in missing.items() if m} == {}
43
44
45 @pytest.mark.parametrize("name", _DESTRUCTIVE)
46 def test_mcpa_02_the_thirteen_delete_remove_revoke_transfer_tools_are_destructive(name: str) -> None:
47 assert _listed()[name]["destructiveHint"] is True
48
49
50 def test_mcpa_03_every_tool_whose_name_deletes_removes_revokes_or_transfers_is_destructive() -> None:
51 verbs = re.compile(r"^(muse|musehub)_(mist_)?(delete|remove|revoke|transfer)_?")
52 named = {n for n in _listed() if verbs.match(n)}
53 assert named == set(_DESTRUCTIVE)
54 assert {n for n, a in _listed().items() if a["destructiveHint"]} == named | {"muse_push"}
55
56
57 # ── #324 ─────────────────────────────────────────────────────────────────────
58
59
60 def test_mcpm_01_signed_read_tools_report_read_only() -> None:
61 from musehub.mcp import scope_policy
62 from musehub.mcp.tools import MUSEHUB_WRITE_TOOL_NAMES
63
64 listed = _listed()
65 reads = scope_policy.READ_TOOLS - {"musehub_accept_collaborator_invite"}
66 assert len(reads) == 15 and reads <= MUSEHUB_WRITE_TOOL_NAMES
67 assert {n for n in reads if listed[n]["readOnlyHint"] is not True} == set()
68 # Accepting an invitation changes state: it needs no scope, but it is not read-only.
69 assert listed["musehub_accept_collaborator_invite"]["readOnlyHint"] is False
70 # Every other signed tool still reports not read-only.
71 assert {n for n in MUSEHUB_WRITE_TOOL_NAMES - reads if listed[n]["readOnlyHint"]} == set()
72
73
74 @pytest.fixture()
75 def anonymous_client() -> Iterator[TestClient]:
76 from musehub.api.routes import mcp as route
77 from musehub.auth.request_signing import optional_signed_request
78 from musehub.rate_limits import limiter
79
80 app = FastAPI()
81 app.state.limiter = limiter
82 app.include_router(route.router)
83 app.dependency_overrides[optional_signed_request] = lambda: None
84 with TestClient(app) as c:
85 yield c
86
87
88 @pytest.mark.parametrize("name", ["musehub_list_webhooks", "musehub_create_issue"])
89 def test_mcpm_02_the_signature_gate_names_the_tool_not_a_write(anonymous_client: TestClient, name: str) -> None:
90 r = anonymous_client.post("/mcp", json={"jsonrpc": "2.0", "id": 1, "method": "tools/call",
91 "params": {"name": name, "arguments": {}}})
92 assert r.status_code == 401 and r.json()["error"]["code"] == -32001
93 msg = r.json()["error"]["message"]
94 assert "Write tools" not in msg and f"'{name}' requires a fresh MSign signature" in msg
95
96
97 @pytest.mark.asyncio
98 async def test_mcpm_03_tasks_are_listed_highest_priority_first(db_session: AsyncSession) -> None:
99 from musehub.db import coord_models as _cm
100 from musehub.mcp.tools import MCP_TOOLS
101 from musehub.services.musehub_coord_server import list_tasks
102 from tests.factories import create_repo
103
104 descriptions = {t["name"]: t["description"] for t in MCP_TOOLS}
105 assert "highest priority first" in descriptions["musehub_list_coord_tasks"]
106 repo = await create_repo(db_session, slug="mcpm-03-priority")
107 for i, prio in enumerate((10, 90, 50, 90)):
108 db_session.add(_cm.MusehubCoordTask(task_id=f"t{i}-{prio}", repo_id=repo.repo_id, queue="q",
109 priority=prio, payload={}))
110 await db_session.flush()
111 tasks = await list_tasks(db_session, repo.repo_id)
112 assert [t.priority for t in tasks] == [90, 90, 50, 10]
113 assert [t.task_id for t in tasks][:2] == ["t1-90", "t3-90"] # equal priority: oldest first
114
115
116 def test_mcpm_04_no_mcp_docstring_states_a_tool_count() -> None:
117 root = Path(__file__).resolve().parents[1] / "musehub" / "mcp"
118 stale = []
119 for path in root.rglob("*.py"):
120 tree = ast.parse(path.read_text(encoding="utf-8"))
121 nodes = [tree, *(n for n in ast.walk(tree) if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)))]
122 for node in nodes:
123 doc = ast.get_docstring(node) or ""
124 if re.search(r"\b(all )?\d+ (musehub_\* / muse_\* )?tool(s| definitions)\b|\(\d+ standard", doc):
125 stale.append(f"{path.relative_to(root)}:{getattr(node, 'name', '<module>')}")
126 assert stale == []
File History 3 commits
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ 1 day ago
sha256:87f4c4d976afad92db715748495c09600f4aafed5f924e6b4b1664aab5a86fad merge: rc26 — MCP wave (#291 #292 #295 #324) Opus 5.5 minor ⚠ 1 day ago
sha256:ff23c9c281bee839f187bbaf3d30de9e88d331e3c404ed45e2845954f04cad82 fix(mcp): tools/list sets all four hints from one registry … Opus 5.5 minor ⚠ 2 days ago