Phase 5: AI/MCP Bearer-Auth + Delegationstoken + Audit
Check Cross-Plugin Imports / check (push) Has been cancelled

5.1 Delegationstoken (app/core/delegation_token.py):
- HMAC-SHA256 signiert mit SECRET_KEY, max 60s Lifetime
- Payload: user_id, tenant_id, agent_id, audience, expires_at, token_id
- Statelose Verifikation, Audience-Check, Expiry-Check

5.2 MCP Bearer-Auth:
- app/core/api_token.py: Token Service (create, verify, revoke, list)
- app/deps.py: get_current_user_bearer + get_current_user_or_bearer
- app/routes/api_tokens.py: Token CRUD Routes (create, list, revoke)
- MCP Server Routes: get_current_user_or_bearer akzeptiert Session + Bearer

5.3 Methodenrechte:
- MCP nutzt bereits mcp:read/mcp:write basierend auf tool_def.required_permission

5.5 Audit:
- MCP Tool-Ausfuehrung wird protokolliert (log_audit mit correlation_id)

Tests: 13/13 bestanden (7 API Token + 6 Delegation Token)
This commit is contained in:
Agent Zero
2026-08-03 14:06:55 +02:00
parent ea797b033a
commit 8ad0a19f25
7 changed files with 659 additions and 2 deletions
+18 -2
View File
@@ -10,7 +10,7 @@ from fastapi import APIRouter, Depends, Header, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from app.core.db import get_db
from app.deps import get_current_user, require_permission
from app.deps import get_current_user, get_current_user_or_bearer, require_permission
from app.plugins.builtins.mcp_server.schemas import (
McpServerConfig,
McpToolDefinition,
@@ -58,10 +58,11 @@ async def execute_mcp_tool(
tool_name: str,
request: McpToolExecuteRequest,
db: AsyncSession = Depends(get_db),
current_user: dict[str, Any] = Depends(get_current_user),
current_user: dict[str, Any] = Depends(get_current_user_or_bearer),
) -> McpToolExecuteResponse:
"""Execute an MCP tool by name with provided arguments.
Accepts session cookie OR Bearer token (for programmatic access).
Requires mcp:read for read tools, mcp:write for write tools.
"""
tool_def = get_tool_definition(tool_name)
@@ -93,8 +94,23 @@ async def execute_mcp_tool(
"user_id": current_user.get("user_id"),
"role": current_user.get("role"),
"permissions": current_user.get("permissions", []),
"auth_method": current_user.get("_auth_method", "session"),
}
# Audit log
from app.core.audit import log_audit
import uuid as uuid_mod
correlation_id = str(uuid_mod.uuid4())
await log_audit(
db,
tenant_id=uuid.UUID(current_user["tenant_id"]),
user_id=uuid.UUID(current_user["user_id"]),
action="mcp.tool.execute",
entity_type="mcp_tool",
entity_id=tool_name,
details={"tool": tool_name, "arguments": request.arguments, "correlation_id": correlation_id, "auth_method": context["auth_method"]},
)
try:
result = await handler(db, request.arguments, context)
await db.commit()