protocol.py
python
sha256:e519738f2102d625d47828a17d113283cd744577b43551013cb668fd04f1c3fe
chore(mwp8/phase5): tick all RG checkboxes; close-out doc (…
Sonnet 4.6
18 days ago
| 1 | """Protocol introspection endpoints. |
| 2 | |
| 3 | All endpoints are public (no auth required) — they expose static metadata |
| 4 | about the server's wire contract, not any user data. |
| 5 | |
| 6 | Endpoints: |
| 7 | GET /protocol — version + hash + counts |
| 8 | GET /protocol/events.json — canonical event type list |
| 9 | GET /protocol/tools.json — MCP tool catalogue |
| 10 | GET /protocol/schema.json — aggregated schema document |
| 11 | """ |
| 12 | |
| 13 | from fastapi import APIRouter |
| 14 | from fastapi.responses import JSONResponse |
| 15 | |
| 16 | from musehub.mcp.tools.musehub import MUSEHUB_TOOLS |
| 17 | from musehub.protocol.events import EVENT_REGISTRY |
| 18 | from musehub.types.json_types import JSONObject |
| 19 | from musehub.protocol.responses import ProtocolInfoResponse, build_protocol_info, compute_protocol_hash |
| 20 | |
| 21 | router = APIRouter(prefix="/protocol", tags=["Protocol"]) |
| 22 | |
| 23 | def _build_schema() -> JSONObject: |
| 24 | """Assemble the aggregated schema document used for hashing and the schema endpoint.""" |
| 25 | return { |
| 26 | "events": sorted(EVENT_REGISTRY), |
| 27 | "tools": [{"name": t["name"], "description": t.get("description", "")} for t in MUSEHUB_TOOLS], |
| 28 | } |
| 29 | |
| 30 | @router.get( |
| 31 | "", |
| 32 | response_model=ProtocolInfoResponse, |
| 33 | summary="Protocol info — version, hash, counts", |
| 34 | ) |
| 35 | async def get_protocol_info() -> ProtocolInfoResponse: |
| 36 | """Return the current protocol version, a stability hash, and entity counts.""" |
| 37 | schema = _build_schema() |
| 38 | return build_protocol_info( |
| 39 | event_count=len(EVENT_REGISTRY), |
| 40 | tool_count=len(MUSEHUB_TOOLS), |
| 41 | schema=schema, |
| 42 | ) |
| 43 | |
| 44 | @router.get( |
| 45 | "/events.json", |
| 46 | summary="Canonical event type list", |
| 47 | ) |
| 48 | async def get_events_json() -> JSONResponse: |
| 49 | """Return the sorted list of all recognised event type strings.""" |
| 50 | return JSONResponse({"events": sorted(EVENT_REGISTRY)}) |
| 51 | |
| 52 | @router.get( |
| 53 | "/tools.json", |
| 54 | summary="MCP tool catalogue", |
| 55 | ) |
| 56 | async def get_tools_json() -> JSONResponse: |
| 57 | """Return the full MCP tool catalogue with name and description.""" |
| 58 | tools = [ |
| 59 | {"name": t["name"], "description": t.get("description", "")} |
| 60 | for t in MUSEHUB_TOOLS |
| 61 | ] |
| 62 | return JSONResponse({"tools": tools}) |
| 63 | |
| 64 | @router.get( |
| 65 | "/schema.json", |
| 66 | summary="Aggregated protocol schema", |
| 67 | ) |
| 68 | async def get_schema_json() -> JSONResponse: |
| 69 | """Return the aggregated schema document and its stability hash.""" |
| 70 | schema = _build_schema() |
| 71 | return JSONResponse({ |
| 72 | "schema": schema, |
| 73 | "hash": compute_protocol_hash(schema), |
| 74 | }) |
File History
2 commits
sha256:e519738f2102d625d47828a17d113283cd744577b43551013cb668fd04f1c3fe
chore(mwp8/phase5): tick all RG checkboxes; close-out doc (…
Sonnet 4.6
18 days ago
sha256:0e5174da777684df43b2db71f282079030ef28bacffb93e19c29ee712b2a8bc4
test(mwp8/phase0): audit — repair+force-push leave stale ca…
Sonnet 4.6
20 days ago