|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Patch Hermes mcp_tool.py to stamp the active profile before each deepsql MCP call. |
| 3 | +
|
| 4 | +The DeepSQL MCP stdio server is process-global; it reads |
| 5 | +DEEPSQL_ACTOR_PROFILE_FILE on every tool call to pick the correct |
| 6 | +profiles/u-<user>/deepsql.token. Hermes must write that file immediately |
| 7 | +before invoking tools on server ``deepsql``. |
| 8 | +""" |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +MARKER = "deepsql_stamp_active_mcp_profile" |
| 15 | +NEEDLE = " gate_error = _trust_gate_check(server_name, tool_name)" |
| 16 | +INSERT = f""" gate_error = _trust_gate_check(server_name, tool_name) |
| 17 | + # {MARKER}: per-request profile for shared DeepSQL MCP auth |
| 18 | + if server_name == "deepsql": |
| 19 | + _stamp_deepsql_active_mcp_profile() |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +def patch(path: Path) -> int: |
| 24 | + text = path.read_text(encoding="utf-8") |
| 25 | + if MARKER in text: |
| 26 | + print(f"already patched: {path}") |
| 27 | + return 0 |
| 28 | + if NEEDLE not in text: |
| 29 | + print(f"ERROR: needle not found in {path}", file=sys.stderr) |
| 30 | + return 1 |
| 31 | + |
| 32 | + helper = ''' |
| 33 | +
|
| 34 | +def _stamp_deepsql_active_mcp_profile() -> None: |
| 35 | + """Write the active Hermes profile id for the shared DeepSQL MCP subprocess.""" |
| 36 | + import os |
| 37 | + from pathlib import Path as _Path |
| 38 | +
|
| 39 | + actor_file = (os.environ.get("DEEPSQL_ACTOR_PROFILE_FILE") or "").strip() |
| 40 | + if not actor_file: |
| 41 | + return |
| 42 | + profile = None |
| 43 | + try: |
| 44 | + from api.profiles import get_active_profile_name as _webui_active |
| 45 | + profile = _webui_active() |
| 46 | + except Exception: |
| 47 | + try: |
| 48 | + from hermes_cli.profiles import get_active_profile_name as _cli_active |
| 49 | + profile = _cli_active() |
| 50 | + except Exception: |
| 51 | + profile = None |
| 52 | + if not profile or profile == "default": |
| 53 | + return |
| 54 | + try: |
| 55 | + p = _Path(actor_file) |
| 56 | + p.parent.mkdir(parents=True, exist_ok=True) |
| 57 | + p.write_text(str(profile).strip() + "\\n", encoding="utf-8") |
| 58 | + except Exception: |
| 59 | + pass |
| 60 | +''' |
| 61 | + text = text.replace(NEEDLE, INSERT, 1) |
| 62 | + anchor = "def _make_tool_handler(server_name: str, tool_name: str, tool_timeout: float):" |
| 63 | + if anchor not in text: |
| 64 | + print(f"ERROR: handler anchor not found in {path}", file=sys.stderr) |
| 65 | + return 1 |
| 66 | + text = text.replace(anchor, helper + "\n" + anchor, 1) |
| 67 | + path.write_text(text, encoding="utf-8") |
| 68 | + print(f"patched {path}") |
| 69 | + return 0 |
| 70 | + |
| 71 | + |
| 72 | +def main() -> int: |
| 73 | + if len(sys.argv) != 2: |
| 74 | + print("usage: stamp-deepsql-mcp-actor.py <path/to/mcp_tool.py>", file=sys.stderr) |
| 75 | + return 2 |
| 76 | + return patch(Path(sys.argv[1])) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + raise SystemExit(main()) |
0 commit comments