stdio_server.py
file-level
1
files
1
commits
0
hotspots
0
🧊 dead
0
💥 blast risk
| 1 | """Muse MCP stdio transport — Claude Desktop / Cursor integration (§KD-5a.E). |
| 2 | |
| 3 | Entry point:: |
| 4 | |
| 5 | python -m muse.mcp.stdio_server |
| 6 | |
| 7 | The process CWD must be the bound Muse repository root (typically |
| 8 | ``knowtation-vault-smoke`` for KD-5b tests). |
| 9 | """ |
| 10 | |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import json |
| 14 | import logging |
| 15 | import sys |
| 16 | |
| 17 | from muse.mcp.server import MuseMCPServer |
| 18 | |
| 19 | logger = logging.getLogger(__name__) |
| 20 | |
| 21 | |
| 22 | def run_stdio() -> None: |
| 23 | """Read newline-delimited JSON-RPC from stdin; write responses to stdout.""" |
| 24 | server = MuseMCPServer() |
| 25 | logger.info( |
| 26 | "Muse MCP stdio started (domain=%s, knowtation=%s)", |
| 27 | server.capabilities.get("active_domain"), |
| 28 | server.capabilities.get("knowtation_mounted"), |
| 29 | ) |
| 30 | for line in sys.stdin: |
| 31 | line = line.strip() |
| 32 | if not line: |
| 33 | continue |
| 34 | try: |
| 35 | raw = json.loads(line) |
| 36 | except json.JSONDecodeError as exc: |
| 37 | sys.stdout.write( |
| 38 | json.dumps( |
| 39 | { |
| 40 | "jsonrpc": "2.0", |
| 41 | "id": None, |
| 42 | "error": {"code": -32700, "message": f"Parse error: {exc}"}, |
| 43 | } |
| 44 | ) |
| 45 | + "\n" |
| 46 | ) |
| 47 | sys.stdout.flush() |
| 48 | continue |
| 49 | resp = server.handle_request(raw) |
| 50 | if resp is not None: |
| 51 | sys.stdout.write(json.dumps(resp, default=str) + "\n") |
| 52 | sys.stdout.flush() |
| 53 | |
| 54 | |
| 55 | def main() -> None: |
| 56 | logging.basicConfig( |
| 57 | level=logging.INFO, |
| 58 | format="%(asctime)s %(levelname)s %(name)s: %(message)s", |
| 59 | stream=sys.stderr, |
| 60 | ) |
| 61 | run_stdio() |
| 62 | |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | main() |