a2a-x402-guard-mcp
# a2a-x402-guard-mcp
<p align="center">
<img src="https://raw.githubusercontent.com/khoap1220-hue/a2a-x402-guard-mcp/main/assets/hero-x402-guard.svg" alt="a2a-x402-guard-mcp Architecture Banner" width="100%" />
</p>
<p align="center">
<a href="https://registry.modelcontextprotocol.io/?q=a2a-x402-guard-mcp"><img src="https://img.shields.io/badge/MCP_Registry-io.github.khoap1220--hue%2Fa2a--x402--guard--mcp-10b981" alt="Official Anthropic MCP Registry" /></a>
<a href="https://github.com/khoap1220-hue/a2a-x402-guard-mcp/releases/latest"><img src="https://img.shields.io/badge/Desktop_Bundle-.mcpb_v0.3-f59e0b" alt=".mcpb Bundle" /></a>
<a href="https://github.com/khoap1220-hue/a2a-x402-guard-mcp/actions"><img src="https://img.shields.io/github/actions/workflow/status/khoap1220-hue/a2a-x402-guard-mcp/ci.yml?branch=main&label=CI%20(Python%203.10--3.13)" alt="CI Status" /></a>
<a href="https://khoaphan.dev/a2a-iq/"><img src="https://img.shields.io/badge/A2A_v1.0.1-x402_%2B_AP2_%2B_Ed25519-059669" alt="A2A v1.0.1 + x402 + AP2" /></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/License-MIT-34d399" alt="MIT License" /></a>
</p>
**`a2a-x402-guard-mcp`** (`v1.0.0`) is a **zero-dependency (`dependencies = []`, 100% Python >= 3.10 stdlib)** deterministic security guard for **`x402` HTTP 402 Micropayments**, **A2A `AP2` (`Agent Payments Protocol`) Mandates**, **Integer Minor-Unit Spend Caps**, and **`S4-REJECT-005`-Safe A2A `v1.0.1` `agent-card.json` Signing**, packaged as a 1-click `.mcpb` desktop bundle (`< 50ms` cold boot).
Autonomous agents interacting with paid HTTP 402 endpoints or A2A `AP2` merchants face five concrete failure modes: IEEE-754 floating-point rounding drift (`0.1 + 0.2 != 0.3`), rogue/spoofed ERC-20 or SPL token contracts in `PAYMENT-REQUIRED` challenges, EIP-3009 authorization replay or unbounded `validBefore` windows, tampered `CartMandate` line-item totals, and broken `agent-card.json` JWS signatures caused by placing `"x402"` at the ProtoJSON top level (`S4-REJECT-005`). `a2a-x402-guard-mcp` eliminates all five deterministically with zero external `pip` dependencies.
---
## Core Engineering Capabilities
- **Multi-Chain CAIP-2 `x402` Header Forensics:** Parses and audits `PAYMENT-REQUIRED`, `X-PAYMENT`, and `PAYMENT-SIGNATURE` headers (JSON, base64, and base64url) across `eip155:8453` (Base Mainnet USDC EIP-3009 `TransferWithAuthorization`), `eip155:84532` (Base Sepolia USDC), `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp` (Solana USDC & SOL), and `stacks:1` (`sBTC` SIP-010).
- **`SPEC_CLASS_09` 100% Integer Minor-Unit Spend Gate:** Strictly rejects `float` inputs and negative slippage (`slippage_bps < 0`), computes exact integer basis-point fees (`net_minor = amount_minor * (10000 - fee_bps) // 10000`), enforces `max_per_tx_minor` and `daily_cap_minor`, and blocks nonce replays via stateless sets or an atomic thread-safe SQLite WAL ledger (`BEGIN IMMEDIATE`).
- **Pure-Python AP2 `IntentMandate` & `CartMandate` Verifier:** Verifies ISO-4217 and crypto minor-unit exponents (`0` JPY/VND, `2` USD/EUR, `3` KWD, `6` USDC, `8` sBTC, `9` SOL, `18` WEI), line-item sum conservation, `human_confirmation_threshold_minor`, and pure-Python RFC 8032 Ed25519 + RFC 8785 JCS signatures.
- **`S4-REJECT-005`-Safe A2A `v1.0.1` `agent-card.json` Scaffolder:** Emits spec-compliant HTTP 402 `PAYMENT-REQUIRED` headers alongside an Ed25519-signed `/.well-known/agent-card.json` + `/.well-known/jwks.json` bundle that embeds `x402` and `AP2` metadata inside `capabilities.extensions[]` and `skills[]` so `a2a-python`, `@a2a-js/sdk`, and `a2a-go` ProtoJSON JWS verifiers agree bit-for-bit.
- **`SPEC_CLASS_14` Settlement Webhook & DLP Guard:** Verifies facilitator webhooks with constant-time `hmac.compare_digest` (`HMAC-SHA256`) or `Ed25519`, deduplicates `idempotency_key`, and automatically redacts accidental API keys, secrets, and payment card digits.
---
## Quick Install
### Option 1: 1-Click `.mcpb` Bundle (Claude Desktop)
Download [`a2a-x402-guard-mcp.mcpb`](https://github.com/khoap1220-hue/a2a-x402-guard-mcp/releases/download/v1.0.0/a2a-x402-guard-mcp.mcpb) from the [v1.0.0 Release](https://github.com/khoap1220-hue/a2a-x402-guard-mcp/releases/tag/v1.0.0) and open it in Claude Desktop (`manifest_version: "0.3"`, zero `pip` install step).
### Option 2: `pip` / `uvx` CLI & MCP Server
```bash
pip install a2a-x402-guard-mcp
a2a-x402-guard-mcp selftest --json
```
### Option 3: MCP Client Configuration (`claude_desktop_config.json` / Cursor / VS Code)
```json
{
"mcpServers": {
"a2a-x402-guard": {
"command": "a2a-x402-guard-mcp-server",
"env": {
"X402_DEFAULT_MAX_TX_USDC_ATOMIC": "5000000"
}
}
}
}
```
---
## 6 Deterministic MCP Tools (`readOnlyHint = True`)
| Tool | Purpose & Enforced Invariants |
| :--- | :--- |
| `x402_inspect_payment_header` | Audits `PAYMENT-REQUIRED`, `X-PAYMENT`, and `PAYMENT-SIGNATURE` headers across Base, Base Sepolia, Solana, and Stacks; flags IEEE-754 floats, rogue token contracts, payee mismatches, expired `validBefore`, and future `validAfter`. |
| `x402_enforce_spend_policy` | Enforces `SPEC_CLASS_09` integer-only minor-unit math, `fee_bps`, `slippage_bps >= 0`, per-tx & daily spend ceilings, CAIP-2/payee allowlists, and stateless or SQLite WAL `idempotency_nonce` replay locking. |
| `ap2_verify_cart_mandate` | Verifies A2A AP2 `IntentMandate` & `CartMandate` line-item integer sums, currency exponents (`0/2/3/6/8/9/18`), TTL expiry, human confirmation gates, and pure-Python RFC 8032 Ed25519 + RFC 8785 JCS signatures. |
| `x402_scaffold_agent_card_extension` | Generates HTTP 402 `PAYMENT-REQUIRED` JSON/base64 headers and an `S4-REJECT-005`-compliant Ed25519-signed `/.well-known/agent-card.json` + `/.well-known/jwks.json` deployment bundle. |
| `x402_convert_atomic_units` | Exact `Decimal` string <-> integer minor/atomic unit converter (`USD`, `EUR`, `VND`, `JPY`, `KWD`, `USDC`, `sBTC`, `SOL`, `WEI`) with `SUB_ATOMIC_DUST_REJECTED` rounding protection. |
| `x402_verify_settlement_webhook` | Constant-time `hmac.compare_digest` / Ed25519 webhook signature verifier with replay-window enforcement, `idempotency_key` deduplication, and `SPEC_CLASS_14` DLP secret/PAN scrubbing. |
---
## Built-in Prompts & Reference Resources
- **Slash Prompts (`3`):** `audit-402-payment-challenge`, `verify-ap2-cart-mandate`, `add-x402-pricing-to-agent-card`
- **Resources (`2`):**
- `x402-guard://spec/caip2-assets-and-decimals` — Canonical Base/Sepolia USDC, Solana USDC/SOL, Stacks sBTC asset descriptors & minor-unit exponents.
- `x402-guard://checklists/spec-class-09-payment-invariants` — Production payment security invariants, linked to [khoaphan.dev/a2a-iq/](https://khoaphan.dev/a2a-iq/) and [khoaphan.dev/webhooks/](https://khoaphan.dev/webhooks/).
---
## Engineering & Support Workflow (Transparent Written-First A2A Operation)
Maintained by **Khoa Phan** via an open, transparent **A2A (AI-Orchestrated Senior Engineering)** workflow:
- **100% Written Asynchronous Communication:** All bug reports, integration specifications, and custom security engagements operate strictly via written channels ([GitHub Issues](https://github.com/khoap1220-hue/a2a-x402-guard-mcp/issues), Pull Requests, and written async briefs at [khoaphan.dev/services/](https://khoaphan.dev/services/?ref=gh-a2a-x402-guard-mcp) — zero live meetings) backed by deterministic Code-First PoCs and automated `pytest` suites.
- **Interactive Benchmarks & Guides:** Explore live A2A v1.0.1 conformance benchmarks at [khoaphan.dev/a2a-iq/](https://khoaphan.dev/a2a-iq/) and webhook hardening patterns at [khoaphan.dev/webhooks/](https://khoaphan.dev/webhooks/).
## License
[MIT](LICENSE) © 2026 Khoa Phan.
TDQS
Scored across 6 tools
Each tool targets a distinct phase of the payment lifecycle: policy enforcement, header parsing, mandate verification, artifact scaffolding, unit conversion, and webhook verification. Only minor overlap exists between x402_enforce_spend_policy and ap2_verify_cart_mandate, since both validate amounts, payees, and thresholds, but their inputs (transactions vs. mandate envelopes) are clearly separated.
All six names follow a verb_noun pattern (enforce_spend_policy, inspect_payment_header, convert_atomic_units, etc.), so they are predictable and readable. The only deviation is the ap2_ prefix on ap2_verify_cart_mandate versus x402_ elsewhere, though this usefully signals the protocol family the tool handles.
Six tools is well-scoped for a focused x402/AP2 guard server, and each maps to a distinct responsibility (enforce, inspect, verify mandates, scaffold, convert, verify webhooks). No tool appears redundant or vestigial.
The surface covers the core guard workflow: policy enforcement, header audit, mandate verification, artifact generation, unit math, and webhook verification. Minor gaps remain: there is no payer-side X-PAYMENT payload construction or signing tool, and no way to query or reset the cumulative spend ledger that x402_enforce_spend_policy maintains, but agents can work around these with the scaffolding and enforcement tools.