MetaMask / MetaMask/mcp-x402

[Feature Request] Per-agent spending limits for x402 wallet signing

Open
#8 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3
Forks
2
PR merge metrics
No merged PRs in 30d

Description

# [Feature Request] Per-agent spending limits for x402 wallet signing

## Problem

MetaMask's x402 MCP server provides a convenient tool for agents to sign x402 payment headers using private keys, but there's a critical gap: **no spending controls**.

This creates serious risks in production:
- **Loop vulnerability**: An agent stuck in a retry loop could sign 100+ transactions before human intervention
- **No per-agent budgets**: In multi-agent systems, one compromised agent can drain the entire wallet
- **No approval chains**: High-value transactions ($1000+) should require human approval before signing
- **Zero audit trail**: "Which agent authorized the $500 payment at 3am?" is impossible to answer

The `CreateX402PaymentHeader` tool is powerful but dangerous without a policy layer between agent intent and wallet signing.

## Proposed Solution

Add an optional policy enforcement layer using [PaySentry](https://github.com/mkmkkkkk/paysentry) as middleware between the MCP server and the signing operation.

### Example integration

```typescript
import { PaySentryX402Adapter } from '@paysentry/x402';
import { PolicyEngine, blockAbove, requireApprovalAbove, allowAll } from '@paysentry/control';
import { SpendTracker } from '@paysentry/observe';

// 1. Define policies (before signing happens)
const engine = new PolicyEngine();
engine.loadPolicy({
id: 'metamask-x402',
name: 'MetaMask Agent Spending Policy',
enabled: true,
rules: [
blockAbove(1000, 'USDC'), // Hard block above $1000
requireApprovalAbove(100, 'USDC'), // Human approval above $100
allowAll(),
],
budgets: [
{ window: 'daily', maxAmount: 500, currency: 'USDC', agentId: 'research-bot' },
],
});

// 2. Wrap x402 server with PaySentry adapter
const adapter = new PaySentryX402Adapter(
{ policyEngine: engine, spendTracker: new SpendTracker() },
{ circuitBreaker: { failureThreshold: 5, recoveryTimeoutMs: 30_000 } },
);

// 3. Register lifecycle hooks (onBeforeVerify, onAfterSettle, etc.)
adapter.withLifecycleHooks(yourX402Server);
```

This adds:
- **Pre-signature policy checks**: Payment request is evaluated against rules *before* calling `CreateX402PaymentHeader`
- **Per-agent budgets**: Track spend by `agentId` across daily/monthly windows
- **Approval workflows**: Block signing until human approves high-value transactions
- **Circuit breaker for facilitator failures**: If x402 facilitator repeatedly fails settlement, stop signing new payment headers ([ref: coinbase/x402#803](https://github.com/coinbase/x402/issues/803))
- **Audit trail**: Immutable log of every policy decision + signature + settlement outcome

## Why this matters for MetaMask users

Enterprise users deploying autonomous agents with MetaMask wallets need **governance before signature**, not just after-the-fact blockchain monitoring.

Current flow (risky):
```
Agent intent → CreateX402PaymentHeader → Signature → Payment settles
```

With PaySentry middleware:
```
Agent intent → Policy check → [ALLOW/DENY/APPROVE] → CreateX402PaymentHeader → ...
```

## References

- PaySentry repo: https://github.com/mkmkkkkk/paysentry
- E2E example with circuit breaker: https://github.com/mkmkkkkk/paysentry/blob/main/examples/05-x402-e2e.ts
- Test coverage: 79 tests across policy engine, spend tracking, dispute resolution
- npm packages: `@paysentry/core`, `@paysentry/control`, `@paysentry/observe`, `@paysentry/x402`

## Implementation options

1. **Optional integration**: Add `policyEngine?: PolicyEngine` config option to the MCP server, inject policy check before signing
2. **Middleware pattern**: Expose lifecycle hooks that external systems (like PaySentry) can register
3. **Reference implementation**: Add a `examples/with-spending-limits/` showing PaySentry integration

Happy to contribute a PR if this aligns with MetaMask's roadmap. The x402 protocol is powerful — let's make it safe for production agent deployments.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the MCP server entry point that exposes CreateX402PaymentHeader and review how signing currently occurs. Compare the optional integration, middleware hooks, and reference implementation options before confirming scope; done should include agreed policy checks before signing, per-agent budgets, approvals, circuit-breaker behavior, and an audit trail.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, payments, security
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.