Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ── Agent identity (display / grant metadata) ───────────────────────────────
AGENT_ACTOR_ID=0x...

# ── Trust Gateway (mpcp-gateway) — required ───────────────────────────────────
MPCP_GATEWAY_URL=http://localhost:8080
MPCP_GATEWAY_API_KEY=replace-with-owner-api-key

# Purpose on every GatewaySession.fetch — must match issued grant purposes
# MPCP_SESSION_PURPOSE=inference:llm

# ── MPCP Policy Authority ────────────────────────────────────────────────────
POLICY_AUTHORITY_URL=http://localhost:3100

# ── Grant persistence ────────────────────────────────────────────────────────
GRANT_FILE=./grant.json

# ── Inference ────────────────────────────────────────────────────────────────
HYPERBOLIC_URL=https://hyperbolic-x402.vercel.app/v1/chat/completions
HYPERBOLIC_MODEL=meta-llama/Meta-Llama-3.1-405B-Instruct

# ─────────────────────────────────────────────────────────────────────────────
# Web app (web/.env) — signer UI for demos; agent server uses the vars above.
# ─────────────────────────────────────────────────────────────────────────────
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
dist/
.env
grant.json
web/.next/
web/tsconfig.tsbuildinfo
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# mpcp-hyperbolic-agent

An AI agent that uses [MPCP](https://mpcp-protocol.github.io/spec/) to enforce human-approved spending limits on [Hyperbolic](https://hyperbolic.xyz/) inference calls.

The agent pays for its own LLM inference via [x402](https://github.com/coinbase/x402). MPCP provides the spending guardrails: a human issues a PolicyGrant defining a budget ceiling and allowed purposes. The agent cannot overspend or operate outside those bounds — cryptographically, not just by convention.

---

## Phase 1 — Agent-side enforcement

This repo implements Phase 1: the minimal loop that proves MPCP correctly bounds a real agent making real paid inference calls.

```
Human / Operator
↓ POST /grants (one-time)
mpcp-policy-authority
↓ SignedPolicyGrant (ceiling: $1 USDC, purposes: ["compute:inference"])
Agent (mpcp-wallet-sdk session)
↓ session.createSba({ amount, currency: "USDC", rail: "evm" })
→ throws MpcpBudgetExceededError if ceiling exceeded ✗
→ throws MpcpGrantRevokedError if grant revoked ✗
→ returns SignedBudgetAuthorization ✓
↓ x402 EIP-712 USDC payment on Base Sepolia
Hyperbolic inference endpoint
↓ LLM response
Agent produces answer
```

---

## Prerequisites

- Node.js 22+
- A funded Base Sepolia wallet (USDC testnet tokens)
- A running `mpcp-policy-authority` (see [mpcp-policy-authority](https://github.com/mpcp-protocol/mpcp-policy-authority))
- An EC P-256 PKCS8 PEM key for MPCP SBA signing

### Generate an MPCP signing key

```bash
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 \
| openssl pkcs8 -topk8 -nocrypt -out sba-key.pem
```

---

## Setup

```bash
# 1. Install dependencies
npm install

# 2. Configure
cp .env.example .env
# Edit .env with your wallet key, address, and signing key

# 3. Issue a grant (one-time)
npm run issue-grant

# 4. Run the agent
npm run run-agent "What is the capital of France?"
```

Or run both steps together:

```bash
npm start
```

---

## Environment variables

| Variable | Required | Description |
|---|---|---|
| `AGENT_PRIVATE_KEY` | ✓ | Base Sepolia wallet private key (`0x...`) |
| `AGENT_ADDRESS` | ✓ | Agent EVM address |
| `MPCP_SBA_SIGNING_KEY_PEM` | ✓ | PKCS8 PEM key for SBA signing |
| `POLICY_AUTHORITY_URL` | — | PA URL (default: `http://localhost:3100`) |
| `GRANT_FILE` | — | Grant JSON path (default: `./grant.json`) |
| `HYPERBOLIC_URL` | — | Inference endpoint |
| `HYPERBOLIC_MODEL` | — | Model name |
| `RPC_URL` | — | Base Sepolia RPC (default: public endpoint) |

---

## Success criteria (Phase 1)

- [x] Agent answers a question; `session.remaining()` decreases
- [x] Budget exceeded → `MpcpBudgetExceededError` thrown before any x402 payment
- [x] Operator calls `POST /revoke` → next `session.createSba()` throws `MpcpGrantRevokedError`
- [x] Full run from grant issuance to answered question with no manual steps beyond `npm start`

---

## Project structure

```
src/
config.ts — env loading and constants
wallet.ts — MPCP session setup and budget tracking
x402.ts — MPCP-aware x402 fetch wrapper
policy.ts — PolicyGrant issuance via PA REST API
agent.ts — inference agent (Vercel AI SDK + Hyperbolic)
scripts/
issue-grant.ts — CLI: issue a test grant and write to disk
run-agent.ts — CLI: run the agent with a sample question
```

---

## Roadmap

See [ROADMAP.md](./ROADMAP.md) for Phase 2 (full gateway stack) and Phase 3 (Virtuals integration).
Loading