Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.
Python agentic-aiai-securityalert-triageblue-teamcybersecuritydetection-engineeringincident-responselanggraphllm-agentsmcpmitre-attackmodel-context-protocolpurple-teamsecurity-operationsself-hostedsiemsoarsocthreat-detectionthreat-intelligence
feat(api): authorize, audit and approve every inbound MCP tool call
Beenu Arora6a8e73cff02c
diff --git a/services/api/app/api/v1/endpoints/mcp_access.py b/services/api/app/api/v1/endpoints/mcp_access.pynew file mode 100644index 000000000..ff807b990--- /dev/null+++ b/services/api/app/api/v1/endpoints/mcp_access.py@@ -0,0 +1,427 @@+"""The authority behind AiSOC's own MCP server, once it is on the network.++Depth plan 9.1 (parity 6.6).++Five routes:++* ``POST /mcp/tool-calls/authorize`` — called by ``services/mcp`` before it+ dispatches a tool. Answers allow / deny / approval_required and writes the+ audit row. This is the only route the MCP process needs to be able to call.+* ``POST /mcp/tool-calls/{call_id}/complete`` — called after, with the+ outcome and latency. Fire-and-forget from the MCP side.+* ``GET /mcp/tools`` — the catalogue with this tenant's effective switches.+* ``PUT /mcp/tools/{tool_name}`` — a tenant administrator flips one.+* ``GET /mcp/approvals`` and ``POST /mcp/approvals/{id}/decide`` — the queue+ for anything that is not a read, and the human decision on it.++Why the audit row is written here and not in the MCP process+------------------------------------------------------------+``audit_log`` is append-only and hash-chained per tenant, and the chain's+appender holds a row lock. A second writer outside this service would either+need the database directly — a second place that can write a customer's+audit history — or would fork the chain. More to the point: the MCP process+is the thing being accounted for, and a subject that writes its own record+is not an account of it. ``services/mcp`` emits telemetry about itself as+well, and that is a different artefact with a different trust level.++Why a call produces two rows+----------------------------+``mcp:tool_call`` is written at the decision, carrying the decision. The+``audit_log`` immutability trigger refuses UPDATE, which is deliberate and+correct, so the outcome cannot be filled in later on the same row;+``mcp:tool_result`` is the second row and carries the outcome and latency.+Both carry the same ``resource_id``, which is the call id, so they join.++The first row alone satisfies "an audit row for every call, reads included":+it is written *before* the tool runs, so a call that crashes the process+still has a record. The second is the part a reader wants and the part that+can legitimately be missing.++The tenant is never read from the request+-----------------------------------------+Every route here takes it from the authenticated principal. The MCP server+presents the caller's own credential, forwarded verbatim, rather than a+service token plus a tenant it names — so there is no tenant parameter to+get wrong, and a compromised MCP deployment cannot ask on behalf of a tenant+whose credential it does not hold.+"""++from __future__ import annotations++import uuid+from datetime import UTC, datetime+from typing import Annotated, Any++import structlog+from fastapi import APIRouter, Depends, HTTPException, Request, status+from pydantic import BaseModel, Field+from sqlalchemy import select++from app.api.v1.deps import AuthUser, require_permission+from app.db.rls import TenantDBSession+from app.models.mcp_access import McpToolApproval, McpToolGrant+from app.services.audit import emit_audit+from app.services.mcp_access import (+ Decision,+ authorize,+ effective_tools,+ tool_catalogue,+)++logger = structlog.get_logger()++router = APIRouter(prefix="/mcp", tags=["mcp"])++#: Outcomes the MCP server may report. Closed on purpose: a free-text+#: outcome column is one nobody can aggregate, and "success" spelled three+#: ways is how a usage meter reads zero.+COMPLETION_OUTCOMES: frozenset[str] = frozenset(+ {"success", "error", "invalid_arguments", "unknown_tool", "denied"}+)+++# ---------------------------------------------------------------------------+# Wire models+# ---------------------------------------------------------------------------+++class AuthorizeRequest(BaseModel):+ """What ``services/mcp`` asks before dispatching.++ ``arguments`` is sent in full rather than as key names because the+ approval fingerprint has to bind to the specific call. It is hashed on+ arrival and never stored, and the audit row records the argument *names*+ only — the values routinely carry hostnames and lake queries.+ """++ tool_name: str = Field(min_length=1, max_length=120)+ arguments: dict[str, Any] = Field(default_factory=dict)+++class AuthorizeResponse(BaseModel):+ call_id: str+ outcome: str+ reason: str+ approval_id: str | None = None+ required_permission: str = ""+++class CompleteRequest(BaseModel):+ outcome: str+ latency_ms: int = Field(ge=0, le=86_400_000)+ error_kind: str | None = Field(default=None, max_length=120)+++class ToolStateModel(BaseModel):+ name: str+ read_only: bool+ enabled: bool+ #: ``tenant`` when somebody decided, ``default`` when nobody has. The+ #: console renders the two differently; collapsing them would let an+ #: operator read "nobody has thought about this" as "we chose this".+ source: str+ required_permission: str+ requires_approval: bool+ decided_by: str+++class ToolGrantRequest(BaseModel):+ enabled: bool+++class ApprovalModel(BaseModel):+ id: str+ tool_name: str+ argument_fingerprint: str+ requested_by: str+ state: str+ decided_by: str | None+ decision_note: str+ expires_at: datetime+ created_at: datetime+++class ApprovalDecisionRequest(BaseModel):+ approve: bool+ note: str = Field(default="", max_length=500)+++# ---------------------------------------------------------------------------+# The two routes services/mcp calls+# ---------------------------------------------------------------------------++[email protected]("/tool-calls/authorize", response_model=AuthorizeResponse)+async def authorize_tool_call(+ body: AuthorizeRequest,+ request: Request,+ db: TenantDBSession,+ current_user: AuthUser,+) -> AuthorizeResponse:+ """Decide one tool call and record the decision.++ No ``require_permission`` dependency: the permission a call needs depends+ on the tool, and :func:`app.services.mcp_access.authorize` resolves it+ from the catalogue and checks it through the principal's own path. A+ blanket dependency here would be a second, coarser authority in front of+ the specific one.+ """+ call_id = uuid.uuid4()++ def permits(permission: str) -> bool:+ """The principal's own check, turned into a predicate.++ Deliberately ``require_permission`` and not a set membership test.+ That method *is* the one permission path — scopes, then the RBAC+ tables widened by live elevation, then the static map, then any+ attribute condition the tenant configured. Reimplementing any part+ of it here would be a second authority that can disagree with the+ first, which is the shape that produced GHSA-pm3f-h6gc-rvgp.+ """+ try:+ current_user.require_permission(permission)+ except HTTPException:+ return False+ return True++ decision: Decision = await authorize(+ db=db,+ tenant_id=current_user.tenant_id,+ tool_name=body.tool_name,+ arguments=body.arguments,+ has_permission=permits,+ requested_by=current_user.api_key_prefix or current_user.email,+ )++ await emit_audit(+ db=db,+ tenant_id=current_user.tenant_id,+ actor_id=current_user.user_id,+ actor_email=current_user.email,+ api_key_prefix=getattr(current_user, "api_key_prefix", None),+ action="mcp:tool_call",+ resource="mcp_tool_call",+ resource_id=str(call_id),+ changes={+ "tool_name": body.tool_name,+ # Names, never values. Which parameters a call carried is the+ # signal an investigator needs; the values are the sensitive half+ # and this row is read by anyone with audit access.+ "argument_keys": sorted(body.arguments),+ "decision": decision.outcome,+ "reason": decision.reason,+ "required_permission": decision.required_permission,+ **({"approval_id": str(decision.approval_id)} if decision.approval_id else {}),+ },+ request=request,+ )+ await db.commit()++ return AuthorizeResponse(+ call_id=str(call_id),+ outcome=decision.outcome,+ reason=decision.reason,+ approval_id=str(decision.approval_id) if decision.approval_id else None,+ required_permission=decision.required_permission,+ )++[email protected]("/tool-calls/{call_id}/complete", status_code=status.HTTP_204_NO_CONTENT)+async def complete_tool_call(+ call_id: uuid.UUID,+ body: CompleteRequest,+ request: Request,+ db: TenantDBSession,+ current_user: AuthUser,+) -> None:+ """Record how a call that was allowed turned out."""+ if body.outcome not in COMPLETION_OUTCOMES:+ raise HTTPException(+ status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,+ detail=f"Unknown outcome: {body.outcome}. Expected one of {sorted(COMPLETION_OUTCOMES)}.",+ )+ await emit_audit(+ db=db,+ tenant_id=current_user.tenant_id,+ actor_id=current_user.user_id,+ actor_email=current_user.email,+ api_key_prefix=getattr(current_user, "api_key_prefix", None),+ action="mcp:tool_result",+ resource="mcp_tool_call",+ resource_id=str(call_id),+ changes={+ "outcome": body.outcome,+ "latency_ms": body.latency_ms,+ **({"error_kind": body.error_kind} if body.error_kind else {}),+ },+ request=request,+ )+ await db.commit()+++# ---------------------------------------------------------------------------+# Administration+# ---------------------------------------------------------------------------++[email protected]("/tools", response_model=list[ToolStateModel])+async def list_tools(+ db: TenantDBSession,+ current_user: AuthUser,+ _: Annotated[None, Depends(require_permission("settings:read"))],+) -> list[ToolStateModel]:+ return [ToolStateModel(**row) for row in await effective_tools(db, current_user.tenant_id)]++[email protected]("/tools/{tool_name}", response_model=ToolStateModel)+async def set_tool_grant(+ tool_name: str,+ body: ToolGrantRequest,+ request: Request,+ db: TenantDBSession,+ current_user: AuthUser,+ _: Annotated[None, Depends(require_permission("settings:write"))],+) -> ToolStateModel:+ if tool_name not in tool_catalogue():+ raise HTTPException(+ status_code=status.HTTP_404_NOT_FOUND,+ detail=f"Unknown tool: {tool_name}. GET /api/v1/mcp/tools lists what this deployment serves.",+ )+ actor = current_user.api_key_prefix or current_user.email+ row = (+ await db.execute(+ select(McpToolGrant).where(+ McpToolGrant.tenant_id == current_user.tenant_id,+ McpToolGrant.tool_name == tool_name,+ )+ )+ ).scalar_one_or_none()+ if row is None:+ row = McpToolGrant(+ tenant_id=current_user.tenant_id,+ tool_name=tool_name,+ enabled=body.enabled,+ decided_by=actor,+ )+ db.add(row)+ else:+ row.enabled = body.enabled+ row.decided_by = actor+ row.updated_at = datetime.now(UTC)++ await emit_audit(+ db=db,+ tenant_id=current_user.tenant_id,+ actor_id=current_user.user_id,+ actor_email=current_user.email,+ api_key_prefix=getattr(current_user, "api_key_prefix", None),+ action="mcp:tool_grant",+ resource="mcp_tool_grant",+ resource_id=tool_name,+ changes={"enabled": body.enabled},+ request=request,+ )+ await db.commit()++ states = {entry["name"]: entry for entry in await effective_tools(db, current_user.tenant_id)}+ return ToolStateModel(**states[tool_name])++[email protected]("/approvals", response_model=list[ApprovalModel])+async def list_approvals(+ db: TenantDBSession,+ current_user: AuthUser,+ _: Annotated[None, Depends(require_permission("settings:read"))],+ state: str = "pending",+) -> list[ApprovalModel]:+ stmt = (+ select(McpToolApproval)+ .where(McpToolApproval.tenant_id == current_user.tenant_id)+ .order_by(McpToolApproval.created_at.desc())+ .limit(200)+ )+ if state != "all":+ stmt = stmt.where(McpToolApproval.state == state)+ rows = (await db.execute(stmt)).scalars().all()+ return [+ ApprovalModel(+ id=str(row.id),+ tool_name=row.tool_name,+ argument_fingerprint=row.argument_fingerprint,+ requested_by=row.requested_by,+ state=row.state,+ decided_by=row.decided_by,+ decision_note=row.decision_note,+ expires_at=row.expires_at,+ created_at=row.created_at,+ )+ for row in rows+ ]++[email protected]("/approvals/{approval_id}/decide", response_model=ApprovalModel)+async def decide_approval(+ approval_id: uuid.UUID,+ body: ApprovalDecisionRequest,+ request: Request,+ db: TenantDBSession,+ current_user: AuthUser,+ _: Annotated[None, Depends(require_permission("settings:write"))],+) -> ApprovalModel:+ row = (+ await db.execute(+ select(McpToolApproval).where(+ McpToolApproval.id == approval_id,+ McpToolApproval.tenant_id == current_user.tenant_id,+ )+ )+ ).scalar_one_or_none()+ if row is None:+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No such approval")+ if row.state != "pending":+ raise HTTPException(+ status_code=status.HTTP_409_CONFLICT,+ detail=f"Approval {approval_id} is already {row.state}.",+ )+ # Separation of duties, the same rule the response-action queue follows:+ # the caller who asked cannot be the caller who agrees. `requested_by` is+ # the key prefix or the email, whichever authenticated the request.+ requester = row.requested_by+ actor = current_user.api_key_prefix or current_user.email+ if requester and requester == actor:+ raise HTTPException(+ status_code=status.HTTP_403_FORBIDDEN,+ detail="The principal that requested this call cannot approve it.",+ )++ row.state = "approved" if body.approve else "denied"+ row.decided_by = actor+ row.decided_at = datetime.now(UTC)+ row.decision_note = body.note++ await emit_audit(+ db=db,+ tenant_id=current_user.tenant_id,+ actor_id=current_user.user_id,+ actor_email=current_user.email,+ api_key_prefix=getattr(current_user, "api_key_prefix", None),+ action="mcp:approval_decision",+ resource="mcp_tool_approval",+ resource_id=str(approval_id),+ changes={"tool_name": row.tool_name, "state": row.state},+ request=request,+ )+ await db.commit()+ await db.refresh(row)++ return ApprovalModel(+ id=str(row.id),+ tool_name=row.tool_name,+ argument_fingerprint=row.argument_fingerprint,+ requested_by=row.requested_by,+ state=row.state,+ decided_by=row.decided_by,+ decision_note=row.decision_note,+ expires_at=row.expires_at,+ created_at=row.created_at,+ )diff --git a/services/api/app/api/v1/router.py b/services/api/app/api/v1/router.pyindex 5cadb3d76..2018e9296 100644--- a/services/api/app/api/v1/router.py+++ b/services/api/app/api/v1/router.py@@ -57,6 +57,7 @@ from app.api.v1.endpoints import (llm_credentials,llm_status,marketplace,+ mcp_access,mcp_servers,metrics,mfa,@@ -162,6 +163,12 @@ api_router.include_router(plugins.router)api_router.include_router(community.router)api_router.include_router(marketplace.router)api_router.include_router(mcp_servers.router)+# Depth 9.1 (parity 6.6): the *inbound* MCP surface. `mcp_servers` above is+# the outbound registry -- third-party servers the agent may call. These+# routes are the authority behind AiSOC's own MCP server once it is on the+# network: per-tenant tool grants, an approval queue for anything that is+# not a read, and the audit row for every call including the reads.+api_router.include_router(mcp_access.router)# Gap-closure Phase 6.1 and 6.2: tenant-authored investigation skills:# authored in YAML, validated against the tools this tenant's agent can# actually call, backtested through the Phase 1 replay with and without the