GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — agentguard/mcp/src/index.ts at main · CodeMasterLabs/agentguard Expand file tree/ Copy pathindex.tsMore file actions 191 lines (179 loc) · 9.04 KB / Copy pathindex.tsFile metadata and controls191 lines (179 loc) · 9.04 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 #!/usr/bin/env node // MCP server: gives an AI agent (Claude Code, Codex, any MCP client) a wallet it can // only spend from through AgentGuard. The agent never sees a key that can move funds. import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; import { AgentGuardAgent, NATIVE, REASON_TEXT, formatUnits, parseUnits, type Attestation, type KeyPairString, type SpendRequest, } from "@agentguard/sdk"; import { findAsset, getQuote, swapStatus, type OneClickConfig } from "./oneclick.js"; function env(name: string, required = true): string { const v = process.env[name]; if (required && !v) throw new Error(`missing env ${name}`); return v ?? ""; } const vaultId = Number(env("AGENTGUARD_VAULT_ID")); const oracleUrl = env("AGENTGUARD_ORACLE_URL", false); const oracleToken = env("AGENTGUARD_ORACLE_TOKEN", false); // A TEE verdict over the on-chain transport takes ~10 s, up to ~2 min when OutLayer is slow. // Bounded, because payments run one at a time: a hung oracle would block every later one. const oracleTimeoutMs = Number(process.env.AGENTGUARD_ORACLE_TIMEOUT_MS || 150_000); const agent = new AgentGuardAgent({ contractId: env("AGENTGUARD_CONTRACT"), rpcUrl: process.env.NEAR_RPC_URL, agentId: env("AGENT_ACCOUNT_ID"), privateKey: env("AGENT_PRIVATE_KEY") as KeyPairString, }); const text = (t: string) => ({ content: [{ type: "text" as const, text: t }] }); let queue: Promise<unknown> = Promise.resolve(); /** Run payments one after another, in arrival order; a failure does not block the next. */ function serialized<T>(job: () => Promise<T>): Promise<T> { const run = queue.then(job, job); queue = run.catch(() => undefined); return run; } const oneclick: OneClickConfig = { baseUrl: process.env.ONECLICK_URL || "https://1click.chaindefuser.com", jwt: process.env.ONECLICK_JWT || undefined, }; async function fetchVerdict(request: SpendRequest, context: string, intentsQuote?: unknown): Promise<Attestation | undefined> { if (!oracleUrl) return undefined; const res = await fetch(`${oracleUrl.replace(/\/$/, "")}/verdict`, { method: "POST", headers: { "content-type": "application/json", ...(oracleToken ? { authorization: `Bearer ${oracleToken}` } : {}) }, body: JSON.stringify({ contract_id: agent.contractId, vault_id: vaultId, request, context, intents_quote: intentsQuote }), signal: AbortSignal.timeout(oracleTimeoutMs), }); if (!res.ok) throw new Error(`oracle answered ${res.status}: ${await res.text()}`); return (await res.json()) as Attestation; } const server = new McpServer({ name: "agentguard", version: "0.1.0" }); server.registerTool( "agentguard_budget", { description: "Show what this agent may spend: balances, per-token limits, what is left this period, " + "whether the vault is paused, and payments waiting for the owner. Call before paying.", inputSchema: {}, }, async () => { const v = await agent.getVault(vaultId); if (!v) return text(`Vault ${vaultId} not found.`); const lines = [`Vault ${vaultId} (owner ${v.owner})${v.paused ? " — PAUSED, all spends are denied" : ""}`]; for (const [token, l] of Object.entries(v.policy.tokens)) { const d = await agent.tokenDecimals(token); const f = (x: string) => formatUnits(x, d); const spent = v.usage.spent[token] ?? "0"; const left = BigInt(l.period_limit) > BigInt(spent) ? (BigInt(l.period_limit) - BigInt(spent)).toString() : "0"; lines.push( `- ${token}: balance ${f(v.balances[token] ?? "0")}, max ${f(l.max_per_tx)}/tx, ` + `${f(left)} left of ${f(l.period_limit)} per ${v.policy.period_secs}s, ` + `auto-approved up to ${f(l.approval_threshold)}`, ); } if (v.policy.allowed_recipients) lines.push(`Allowed recipients: ${v.policy.allowed_recipients.join(", ")}`); if (v.pending.length) lines.push(`Waiting for owner: ${v.pending.map((p) => `#${p.id} (${p.reason})`).join(", ")}`); return text(lines.join("\n")); }, ); server.registerTool( "agentguard_pay", { description: "Pay from the AgentGuard vault. The vault's policy decides: the payment is executed, " + "held for the owner's approval, or denied with a reason. Never retry a denied payment " + "with a different recipient or split amount to get around a limit. " + "For a recipient on another chain (base:, eth:, sol:, ...) the vault's NEP-141 token is " + "swapped and delivered through NEAR Intents; say which token the recipient should get.", inputSchema: { token: z.string().describe(`"${NATIVE}" for NEAR, or a NEP-141 token contract id (use wrap.near, not "near", for cross-chain)`), amount: z.string().describe('Human amount of `token` to spend, e.g. "2.5"'), recipient: z.string().describe('"<chain>:<address>", e.g. "near:api.testnet" or "base:0xabc..."'), receive_token: z .string() .optional() .describe('Cross-chain only: symbol the recipient gets on their chain, e.g. "USDC"'), destination_asset: z.string().optional().describe("Cross-chain only: exact 1Click assetId, instead of receive_token"), pay_to: z .string() .optional() .describe("Advanced: NEAR account that receives the funds. Leave empty; it is filled in for you."), purpose: z.string().max(256).describe("Why you are paying, in one sentence."), context: z .string() .max(4000) .optional() .describe("The text that led to this payment (page, message, tool output). Sent to the risk oracle only."), }, }, ({ token, amount, recipient, receive_token, destination_asset, pay_to, purpose, context }) => // One payment at a time: each reads the vault nonce, gets a verdict bound to it, and // spends it. Two in flight would share a nonce and the second would be rejected. serialized(async () => { const [rawChain, address = ""] = recipient.split(/:(.*)/s); // The contract accepts lowercase chain names only. const chain = rawChain.toLowerCase(); recipient = `${chain}:${address}`; const raw = parseUnits(amount, await agent.tokenDecimals(token)); let payTo = pay_to ?? (chain === "near" ? address : undefined); let intentsQuote: Awaited<ReturnType<typeof getQuote>> | undefined; let delivery = ""; if (!payTo) { if (token === NATIVE) return text('Cross-chain payments spend a NEP-141 token: use token "wrap.near" (wrapped NEAR) or another token in the vault.'); if (!destination_asset && !receive_token) return text("For a cross-chain recipient, set receive_token (e.g. \"USDC\")."); const vault = await agent.getVault(vaultId); if (!vault) return text(`Vault ${vaultId} not found.`); const destinationAsset = destination_asset ?? (await findAsset(oneclick, chain, receive_token!)); // Refunds of a failed swap go to the vault owner, never to the agent or the recipient. intentsQuote = await getQuote(oneclick, { token, amount: raw, destinationAsset, recipient: address, refundTo: vault.owner }); payTo = intentsQuote.quote.depositAddress!; delivery = ` Expected delivery: ${intentsQuote.quote.amountOutFormatted} (at least ${intentsQuote.quote.minAmountOut} base units), deposit ${payTo}.`; } const request = await agent.buildRequest(vaultId, { token, amount: raw, recipient, pay_to: payTo, purpose }); const attestation = await fetchVerdict(request, context ?? "", intentsQuote); const r = await agent.requestSpend(vaultId, request, attestation); switch (r.status) { case "executed": return text( `Paid ${amount} ${token} to ${recipient} (nonce ${r.nonce}).${delivery}` + (intentsQuote ? ` Track it with agentguard_swap_status and deposit_address ${payTo}.` : ""), ); case "pending": return text(`Held for the owner's approval as #${r.pending_id}: ${REASON_TEXT[r.reason]}`); case "denied": return text(`Denied (${r.reason}): ${REASON_TEXT[r.reason]} Do not try to work around this.`); } }), ); server.registerTool( "agentguard_pending", { description: "Check whether a held payment is still waiting. A missing id means the owner approved or rejected it.", inputSchema: { pending_id: z.number().int().nonnegative() }, }, async ({ pending_id }) => { const p = (await agent.getPending(vaultId)).find((x) => x.id === pending_id); return text(p ? `#${pending_id} is still waiting: ${REASON_TEXT[p.reason]}` : `#${pending_id} is no longer pending.`); }, ); server.registerTool( "agentguard_swap_status", { description: "Status of a cross-chain payment by its NEAR Intents deposit address (PENDING_DEPOSIT, PROCESSING, SUCCESS, REFUNDED, FAILED).", inputSchema: { deposit_address: z.string() }, }, async ({ deposit_address }) => { const s = await swapStatus(oneclick, deposit_address); const tx = s.swapDetails?.destinationChainTxHashes?.[0]; return text(`${s.status}${tx ? ` — delivered in ${tx.explorerUrl}` : ""}`); }, ); await server.connect(new StdioServerTransport()); |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |