Agent-side risk halt for Alexa+ purchases, shipped as a self-hosted MCP add-on
(Streamable HTTP, MCP spec 2025-11-25+). It scores every attempt_purchase,
auto-completes everyday buys, and pauses high-risk ones until the human approves
on a separate channel. HALT explanations are generated with Amazon Bedrock.
The system that requests a risky action is never the system that approves it. The agent has a tool to attempt a purchase — no tool to approve one it halted.
- Tier 1 —
mcp_server/(agent channel): the MCP server Alexa+ talks to. Exposesattempt_purchase,get_session_status,get_audit_trail. No approve/deny tool here. - Tier 2 —
web_api/(human channel): a separate FastAPI service, reachable only through an authenticated end-user channel. The only place a HALTED action can be approved or denied.
Built against the Alexa+ MCP QuickStart Guide and Design Guide.
- Transport — Streamable HTTP, MCP spec 2025-11-25+ (legacy SSE is not accepted).
- Onboarding —
addon-package/addon.json(QuickStart schema). The Alexa AI CLI (alexa-ai deploy) is gated behind Private Preview (npm install -g @alexa-ai/clireturns 404; seedocs/friction_log.md#1). - Tools — one tool per customer intent; "declare only what you honor".
categoryis a closed enum (no free text to mis-spell), and there is noagent_idargument — identity comes from the authenticated request context, never from the model. - Error contract — invalid
amount/item/merchant/categoryraise proper MCP errors (isError: true) instead of silently scoring garbage input. - Receipts — every completed purchase writes a receipt (
data/receipts_ledger.jsonl- optional email), a certification requirement.
The dashed arrows are deliberate. HALTED → Alexa app: no network path — only an action_id the customer carries to a separate service. Ledger API → actions_log.db: Tier 2 never opens the SQLite file; it goes through the token-gated HTTP ledger API, which exposes read-only reads and one atomic resolve.
Both tiers fail closed without OAuth; set ALGUARD_DEV_MODE=1 for local dev.
# Prerequisites: Python 3.11+
# 1. Create a virtual environment
python3 -m venv .venv
# 2. Activate it
source .venv/bin/activate # macOS / Linux
# .venv\Scripts\activate # Windows (PowerShell)
# 3. Install dependencies
pip install -r requirements.txt # runtime deps (pinned)
pip install -r requirements-dev.txt # pytest + test-only deps
# 4. Run the tests
pytest -q # 72 tests, no MCP transport neededmacOS / Linux:
source .venv/bin/activate
# terminal 1 — Tier 1 (MCP server, port 8000)
ALGUARD_DEV_MODE=1 python3 -m mcp_server.server
# terminal 2 — internal ledger API (port 8015) — Tier 2 calls this over HTTP
python3 -m web_api.ledger_api
# terminal 3 — Tier 2 (resolution API, port 8010)
ALGUARD_DEV_MODE=1 ALGUARD_DEV_USER=UNVERIFIED_DEV_AGENT python3 -m web_api.resolve_action
# terminal 4 — demo client (pick one)
python3 demo/golden_path.py # full end-to-end demo
# python3 demo/agent_demo.py # poisoned-page demo
# python3 demo/agent_demo.py # poisoned-page demo
# terminal 5 — browser UI backend (loopback, port 8020; requires ALGUARD_DEMO_UI=1)
ALGUARD_DEMO_UI=1 python3 -m web_api.demo_serverWindows (PowerShell):
.venv\Scripts\activate
# terminal 1 — Tier 1 (MCP server, port 8000)
$env:ALGUARD_DEV_MODE="1"; python -m mcp_server.server
# terminal 2 — internal ledger API (port 8015)
python -m web_api.ledger_api
# terminal 3 — Tier 2 (resolution API, port 8010)
$env:ALGUARD_DEV_MODE="1"; $env:ALGUARD_DEV_USER="UNVERIFIED_DEV_AGENT"; python -m web_api.resolve_action
# terminal 4 — demo client
python demo/golden_path.py
# terminal 5 — browser UI backend (loopback, port 8020; requires ALGUARD_DEMO_UI=1)
$env:ALGUARD_DEMO_UI="1"; python -m web_api.demo_serverReal Alexa+ onboarding: alexa-ai configure → tunnel port 8000 → fill the REPLACE_ME
URLs in addon-package/addon.json → alexa-ai deploy → test in the Web Simulator.
mcp_server/utils/bedrock_explain.py turns the risk engine's internal vocabulary into a
calm customer-facing sentence via the Bedrock converse API — the primary path, integrated
and ready to run. It is deliberately non-critical: a Bedrock call never changes the
OK/MONITOR/HALTED decision, only the wording. For resilience, if Bedrock is momentarily
unreachable it falls back to a deterministic local sentence (labeled local-fallback), so
the customer always gets an explanation and the decision path never depends on an external
call. Env: AWS_REGION, ALGUARD_BEDROCK_MODEL_ID. IAM: bedrock:InvokeModel.
- A running OAuth authorization server (the token verifier is real; account linking and Alexa's redirect URIs need the Amazon developer console / Private Preview).
- Real merchant execution after approval (Checkout /
ChargePermissionId).
| Variable | Purpose |
|---|---|
OAUTH_JWKS_URL, OAUTH_ISSUER_URL, OAUTH_AUDIENCE |
OAuth 2.1 resource-server auth (Tier 1) |
OAUTH_TIER2_AUDIENCE, OAUTH_TIER2_SCOPES |
Tier 2 auth (own audience + approve scope) |
ALGUARD_DEV_MODE=1 |
Opt into unauthenticated local dev |
ALGUARD_DEV_USER |
Tier 2 dev identity |
AWS_REGION, ALGUARD_BEDROCK_MODEL_ID |
Bedrock HALT explanations |
ALGUARD_DB_PATH |
Relocate the actions ledger |
ALGUARD_UNKNOWN_MERCHANT_CAP |
Unknown-merchant cap (default $250) |
ALGUARD_LEDGER_API_TOKEN |
Shared secret guarding the internal ledger API (port 8015) |
ALGUARD_DEMO_UI=1 |
Opt in to run the loopback-only demo UI backend (port 8020) |
ALGUARD_RATE_LIMIT_PER_MIN |
Per-owner attempt_purchase throttle (default 120, 0 disables) |
ALGUARD_RETENTION_DAYS |
Prune terminal actions older than N days (0 = keep forever) |
OAUTH_ALLOWED_CLIENTS |
Optional azp/client_id allowlist for bearer tokens |
mcp_server/ Tier 1 MCP server + risk engine
server.py attempt_purchase / get_session_status / get_audit_trail
utils/ scoring, circuit breaker, receipts, auth, Tier2Store
web_api/ Tier 2 human-approval service + demo_server (UI backend)
demo/ golden_path, agent_demo, showcase (demo clients)
tests/ 72 pytest tests
addon-package/ addon.json (Alexa+ add-on manifest)
data/ SQLite schema + receipts ledger
Alguard treats every value supplied by the calling agent as untrusted input.
- Untrusted input —
category,amount,merchant,item,session_idall come from the agent; the server validates them, re-derives severity, and scopes all history, limits and the circuit breaker to the verified OAuthsub. - Hard rules (always halt): >$1000 single purchase; >$1500/h or >$3000/24h; high-severity category; retrying a merchant halted and never approved in 24h; >$250 at an unapproved merchant.
- Cold start — a small purchase at an unknown merchant is MONITOR (still completes); unknown merchants are only halted above the cap or via another hard rule.
- Merchant trust — only from a human APPROVED resolution, never a static allowlist.
- Circuit breaker — an open (unresolved/denied) HALT blocks further non-low purchases for that user for 24h.
- Authentication — both tiers fail closed: no OAuth → Tier 1 won't start, Tier 2
returns 503, unless
ALGUARD_DEV_MODE=1. - Tier 2 — own audience +
approvescope; approvals owner-bound, expire in 30 min, single atomic UPDATE; reads via read-onlyTier2Store(mcp_server/utils/tier2_store.py). - Atomic dedup — the duplicate check + insert run in one
BEGIN IMMEDIATEtransaction (insert_action_if_not_duplicate), so two concurrent identical attempts across processes resolve to a single row (the in-process lock is only a fast path, not the guarantee). - Serialized trust updates —
bump_agent_trustruns underBEGIN IMMEDIATE, closing the read-modify-write race on trust scores across concurrent resolutions. - Crash consistency —
reconcile_unfulfilled()heals the window between resolve and fulfill: any APPROVED (or OK/MONITOR) action whose fulfillment is still NULL is fulfilled and receipted exactly once on startup. The receipts ledger dedupes byaction_id. - Token-gated ledger API — Tier 2's route to the ledger (port 8015) is loopback-bound and,
when
ALGUARD_LEDGER_API_TOKENis set, requires that shared secret on every route. - Demo UI fails closed — the demo backend (port 8020) refuses to start unless
ALGUARD_DEMO_UI=1; it is loopback-only and never the production approval path. - Rate limit — per-owner throttle on
attempt_purchase(ALGUARD_RATE_LIMIT_PER_MIN, default 120/min) bounds a hostile agent's ability to flood the risk engine. - Client allowlist (optional) —
OAUTH_ALLOWED_CLIENTSrestricts tokens byazp/client_id. - Retention —
maintenance()expires stale halts, prunes terminal actions pastALGUARD_RETENTION_DAYS, and checkpoints the WAL to bound ledger growth.
- Not in the payment path — Alexa+ can buy without calling it; a real gate needs
Checkout /
ChargePermissionId. - Category truth is heuristic — the right source is the MCC/merchant data of the payment rail.
- Nothing is charged — fulfillment is
simulated. - Shared SQLite file — Tier 1 and Tier 2 still share one file. Tier 2 access is
narrowed (read-only reads + one atomic
resolveviaTier2Store, now token-gated on the ledger API). A full 3-service split (Tier 2 writing to a dedicated approvals store that Tier 1 reads) remains future work.