Skip to main content
Glama
README.md
# SolGuard MCP — Fail-Closed Solana Agent Policy Gate, Red-Team Harness & Audit Ledger

Built from scratch starting **December 2, 2026** for the Superteam Earn *"Long Tail: Build on Solana with CoinGecko Data"* hackathon (**AI Agents** track).

- **Live Web Observability Console:** https://cocvn157.github.io/solguard-mcp-agent/
- **GitHub Repository:** https://github.com/cocvn157/solguard-mcp-agent
- **Demo Video (`1280×720` HD, `44.0s`, `2.16 MB`):** [Watch / Download `solguard-mcp-demo.mp4`](https://cdn.jsdelivr.net/gh/cocvn157/solguard-mcp-agent@256aad3406490b44cbd7bbf09147b6dd7f72afdb/solguard-mcp-demo.mp4) (`media` branch)

**SolGuard MCP** prevents autonomous AI trading agents and LLM MCP clients from executing unsafe Solana long-tail swaps or prompt-injected trades. Every trade proposal passes through a deterministic **7-invariant fail-closed policy gate**, gets stamped with a SHA-256 `policyDecisionHash`, and commits into a **cryptographically hash-chained audit ledger** before any simulated or devnet execution can proceed.

> **Attribution & Scope Notice:** Market prices, trending pool liquidity, `gt_score`, and holder concentration telemetry are fetched live from CoinGecko (`Data provided by CoinGecko`). Policy verdicts (`ALLOW` / `DENY`), slippage curves, and audit ledger hashes are computed independently by SolGuard MCP (`SolGuard Independent Policy Decision`).

---

## Screenshots

### 1. Live Autonomous Agent Cycle (`ALLOW`) & Policy Slippage Frontier
![SolGuard Live Agent Cycle](screenshots/01-agent-cycle-allow.png)

### 2. Adversarial Prompt-Injection Trap Blocked (`DENY` — Token-2022 Permanent Delegate)
![SolGuard Prompt Attack Blocked](screenshots/02-prompt-attack-blocked.png)

### 3. Live MCP JSON-RPC 2.0 Tool Inspector & SHA-256 Hash-Chained Ledger
![SolGuard MCP Inspector and Hash Ledger](screenshots/03-mcp-inspector-ledger.png)

---

## Architecture: 6 Real MCP Tools (`src/mcp/handlers.ts` & `src/mcp/server.ts`)

Run the real Node `stdio` JSON-RPC 2.0 MCP server (`protocolVersion: "2024-11-05"`):

```bash
npm run mcp
```

Or connect any MCP client (`Claude Desktop`, `Cursor`, `Antigravity`):

```json
{
  "mcpServers": {
    "solguard": {
      "command": "npx",
      "args": ["tsx", "src/mcp/server.ts"]
    }
  }
}
```

| Tool Name | Purpose |
|---|---|
| `solguard_scan_longtail` | Scans CoinGecko trending Solana pools under policy depth & volatility bounds |
| `solguard_policy_gate` | Runs 7-invariant Solana RPC + CoinGecko gate, returning `ALLOW`/`DENY` + `maxSafeTradeUsd` + SHA-256 `policyDecisionHash` |
| `solguard_rebalance_plan` | Computes portfolio drift and CoinGecko-priced rebalance legs capped at `maxTradeUsd` |
| `solguard_simulate_prompt_attack` | Executes 3 prompt-injection red-team probes (`thin_pool_rug`, `token2022_delegate_trap`, `whale_slippage_blowout`) |
| `solguard_append_ledger_exec` | Commits verified decisions into a SHA-256 hash-chained audit log (`GENESIS -> prevHash -> entryHash`) |
| `solguard_verify_ledger` | Verifies chain integrity, counts `ALLOW`/`DENY`, and totals blocked USD exposure |

---

## How Coingecko API is used

1. **Trending Long-Tail Opportunity Feed (`GET /api/v2/networks/solana/trending_pools?page=1&include=base_token`):** Powers `solguard_scan_longtail`, `agent:cycle`, and the live CoinGecko scanner panel in the web console.
2. **Pool Liquidity & 24h Flow (`GET /api/v2/networks/solana/tokens/{mint}?include=top_pools`):** Supplies top DEX reserves, pool creation age, 24h buys/sells, and volume USD to compute constant-product (`x * y = k`) swap price impact and round-trip cost, ensuring the agent stays inside `maxSlippageBps` and computes quantitative `maxSafeTradeUsd` remediation.
3. **Holder & Honeypot Metadata (`GET /api/v2/networks/solana/tokens/{mint}/info`):** Evaluates top-10 concentration, developer holding %, and `is_honeypot` while distinguishing established protocol/custody vaults (`reserve >= $500k` and `gt_score >= 75`) from unvetted meme concentration.
4. **Portfolio Rebalance Pricing (`GET /api/v3/simple/token_price/solana`):** Powers `solguard_rebalance_plan` using live USD spot prices across `SOL`, `USDC`, `JUP`, and `BONK`.
5. **Resilient Cross-Runtime Fetching (`src/services/coingecko.ts`):** Uses a `1100ms` pacing queue, `180s` TTL cache (`Map` in Node / `sessionStorage` in browser), exponential retry on rate limits, and optional `api/cg.ts` Vercel serverless proxy (`COINGECKO_API_KEY`).

---

## How Solana is used

1. **Live Mainnet-Beta JSON-RPC Account Verification (`src/services/solanaRpc.ts`):** Calls `https://solana-rpc.publicnode.com` (`getAccountInfo` with `jsonParsed` encoding + `getSlot`) in both Node CLI/MCP and the browser console to decode:
   - Classic SPL (`TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`), Wrapped SOL native mint, and Token-2022 (`TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`) accounts.
   - Live `mintAuthority`, `freezeAuthority`, supply, and all active Token-2022 `extensions[]` (`permanentDelegate`, `transferHook`, `mintCloseAuthority`, `transferFeeConfig` basis points).
2. **Adversarial Guardrails:** Blocks prompt-injected allocations targeting mints with active freeze authorities, shallow AMM pools, or Token-2022 `permanentDelegate` overrides (demonstrated live against `PYUSD` Token-2022 on mainnet).
3. **Live Mainnet Slot Anchoring:** Every policy decision and hash-chained ledger trace captures the current Solana mainnet slot for reproducible audit trails.

---

## Local Setup, CLI Cycle & Verification

```bash
git clone https://github.com/cocvn157/solguard-mcp-agent.git
cd solguard-mcp-agent
cp .env.example .env
npm install
npm test
npm run build
npm run agent:cycle
```

> **Windows PowerShell:** Use `npm.cmd` / `npx.cmd`. Runs `$0` capital out of the box with no API keys required (`COINGECKO_API_KEY=` optional in `.env.example` for Vercel proxy).

- **Vitest Suite (`npm test`):** **37 tests** across 5 files (`policyGate`, `hashLedger`, `mcpServer` JSON-RPC 2.0 dispatch, `parsers`, `rebalance`).
- **Live Autonomous Cycle (`npm run agent:cycle`):** Scans CoinGecko trending Solana pools, evaluates JUP & top trending mints against Solana RPC + CoinGecko, blocks all 3 prompt-injection red-team traps, and verifies the SHA-256 hash chain.

---

## Attribution & License

Data provided by [CoinGecko](https://www.coingecko.com/en/api). Licensed under the [MIT License](LICENSE).