Files
ai-agent/backend/mcpserver/tools.py
gabrielvidalandClaude Opus 5.5 c7da0f5008 feat(authz): roles, scoped credentials and one permission table for every route
- authz/catalog.py: 42 named permissions in groups and tiers (person, admin,
  host); authz/roles.py: built-in Operator (the owner), Admin (assignable with
  team mode), Member, Guest, Viewer + custom clones, in roles.json
- a person = role + allow/deny overrides (authz/policy.py); users.json's old
  switches (cli, mcp, createProjects, scratchBash, codeMode,
  computers.enabled) migrate once to exactly what they allowed, backup kept
- authz/scopes.py: what a credential acting for someone keeps (cli,
  worker.read|run, bot, phone) — a scope only narrows
- authz/routes.py: (method, path) -> permission for all 310 /api routes; the
  gate asks Caller.allows() for every caller kind, replacing GUEST_ROUTES,
  CLI_ROUTES and the worker READ/RUN_ROUTES regex lists
- golden route x caller matrix (authz/matrix.golden.txt, test_authz_matrix)
- the claude bot reads the admin app minus people/security/the machine; the
  phone key reads its unread notifications only
- loopback without the host token is logged (loopback_tokenless), refusals
  are perm_denied lines; the hub's own MCP calls send the token
- /api/roles CRUD, /api/me and /api/users carry role/allow/deny/perms
- worker-bound scheduled items and conversation-meta respect the binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:59:05 +01:00

59 lines
2.2 KiB
Python

"""The ``ai-agent`` CLI's MCP tools (``cli/mcp_tools.py``) inside the
backend: loaded from ``cli/`` by path, and run against the backend's own API
over loopback — as the caller a connection is for.
The image ships the CLI as ``/app/cli`` (beside the backend's modules); in the
repo it is ``../cli``. ``cli/`` is put at the **end** of ``sys.path`` (the CLI
modules import each other by name), so no backend module is ever shadowed.
"""
from __future__ import annotations
import os
import pathlib
import sys
_ROOT = pathlib.Path(__file__).resolve().parent.parent
CLI_DIR = next((d for d in (_ROOT / "cli", _ROOT.parent / "cli")
if (d / "mcp_tools.py").is_file()), None)
# The backend's own port inside the container (docker-entrypoint.sh); a
# loopback caller is the owner — the gate's trusted peer.
SELF_URL = os.environ.get("MCP_SELF_URL", "http://127.0.0.1:8080").rstrip("/")
def _load():
if CLI_DIR is None:
return None, None, None
if str(CLI_DIR) not in sys.path:
sys.path.append(str(CLI_DIR))
import ai_agent # noqa: E402 — from cli/
import mcp_server # noqa: E402
import mcp_tools # noqa: E402
# ai_agent inserts its own dir first on import: move it to the back.
while str(CLI_DIR) in sys.path:
sys.path.remove(str(CLI_DIR))
sys.path.append(str(CLI_DIR))
return ai_agent, mcp_server, mcp_tools
aa, server, tools = _load()
class ReadOnlyHub(aa.Hub if aa else object):
"""A read-scoped connection's hub: safe methods only, whatever a tool
tries (the tool list already leaves the others out)."""
def call(self, method, path, body=None, query=None, timeout=30):
if method.upper() not in ("GET", "HEAD"):
raise aa.HubError(403, "this connection is read-only")
return super().call(method, path, body, query, timeout)
def hub_for(grant: dict, headers: dict | None = None):
cls = ReadOnlyHub if grant.get("scope") == "read" else aa.Hub
from core.auth import INTERNAL_API_TOKEN
if INTERNAL_API_TOKEN:
# the hub calling itself: never a tokenless loopback call in the audit
headers = {**(headers or {}), "X-Internal-Auth": INTERNAL_API_TOKEN}
return cls(base=SELF_URL, headers=headers)