Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
feat: update tests to use dynamic imports and improve mocking for Solana actions
nirholasccea38fb6f8e
diff --git a/api/_mcp/dispatch.js b/api/_mcp/dispatch.jsnew file mode 100644index 000000000..037c51ed3--- /dev/null+++ b/api/_mcp/dispatch.js@@ -0,0 +1,175 @@+import { hasScope } from '../_lib/auth.js';+import { recordEvent, logger } from '../_lib/usage.js';+import { priceFor, findActiveSubscription, resolveBillingMint } from '../_lib/pump-pricing.js';+import { TOOL_CATALOG, TOOLS } from './catalog.js';++export const PROTOCOL_VERSION = '2025-06-18';+const SERVER_INFO = { name: '3d-agent-mcp', version: '1.0.0' };+const log = logger('mcp');++export function ok(id, result) {+ return { jsonrpc: '2.0', id, result };+}++export function rpcError(code, message, data) {+ const e = new Error(message);+ e.code = code;+ e.data = data;+ return e;+}++export async function dispatch(msg, auth, _req) {+ const started = Date.now();+ const id = msg.id;+ const isNotification = id === undefined;++ try {+ if (msg.jsonrpc !== '2.0') throw rpcError(-32600, 'invalid Request');+ const method = msg.method;++ if (method === 'initialize') return ok(id, await onInitialize(msg.params, auth));+ if (method === 'ping') return ok(id, {});+ if (method === 'notifications/initialized') return null;+ if (method === 'tools/list')+ return ok(id, {+ tools: TOOL_CATALOG.map((t) => {+ const price = priceFor(t.name);+ return price+ ? {+ ...t,+ pricing: {+ amount_usdc: price.amount_usdc,+ currency: 'USDC',+ description: price.description,+ scheme: 'pump-agent-payments',+ prep_endpoint: '/api/pump/accept-payment-prep',+ confirm_endpoint: '/api/pump/accept-payment-confirm',+ recipient_mint: resolveBillingMint(),+ },+ }+ : t;+ }),+ });+ if (method === 'tools/call') return ok(id, await onToolCall(msg.params, auth, started));+ if (method === 'resources/list') return ok(id, { resources: [] });+ if (method === 'resources/templates/list') return ok(id, { resourceTemplates: [] });+ if (method === 'prompts/list') return ok(id, { prompts: [] });+ if (method === 'logging/setLevel') return ok(id, {});++ throw rpcError(-32601, `method not found: ${method}`);+ } catch (err) {+ log.warn('rpc_error', { method: msg.method, code: err.code, message: err.message });+ if (isNotification) return null;+ return {+ jsonrpc: '2.0',+ id,+ error: {+ code: err.code || -32603,+ message: err.message || 'internal error',+ data: err.data,+ },+ };+ }+}++function summarize(args) {+ const o = {};+ for (const [k, v] of Object.entries(args || {})) {+ o[k] = typeof v === 'string' && v.length > 64 ? v.slice(0, 64) + '…' : v;+ }+ return o;+}++async function onInitialize(_params, _auth) {+ return {+ protocolVersion: PROTOCOL_VERSION,+ serverInfo: SERVER_INFO,+ capabilities: {+ tools: { listChanged: false },+ resources: { listChanged: false, subscribe: false },+ logging: {},+ },+ instructions: [+ 'Render 3D avatars stored on three.ws as <model-viewer> HTML artifacts.',+ "Use list_my_avatars to see the user's avatars and render_avatar to get embeddable viewer HTML.",+ 'Public avatars can be discovered via search_public_avatars.',+ ].join(' '),+ };+}++async function onToolCall(params, auth, started) {+ const { name, arguments: args = {} } = params || {};+ const tool = TOOLS[name];+ if (!tool) throw rpcError(-32602, `unknown tool: ${name}`);+ if (tool.scope && !hasScope(auth.scope, tool.scope)) {+ throw rpcError(-32002, `insufficient scope, requires ${tool.scope}`);+ }++ // Pump-agent-payments gate. Free tools and paid-bearer-authenticated users+ // (existing subscription plans) bypass. x402 callers and anonymous payers+ // must either supply X-PAYMENT (handled at the HTTP layer above) or have+ // an active pump-agent-payments subscription window for this tool.+ const price = priceFor(name);+ if (price && auth.source === 'x402') {+ const billingMint = resolveBillingMint();+ const payerWallet = args.payer_wallet || auth.payer || null;+ if (billingMint && payerWallet) {+ const sub = await findActiveSubscription({+ mint: billingMint,+ network: process.env.PUMP_DEFAULT_NETWORK || 'mainnet',+ payerWallet,+ toolName: name,+ });+ if (!sub) {+ throw rpcError(-32402, 'payment required for this tool', {+ scheme: 'pump-agent-payments',+ tool: name,+ amount_usdc: price.amount_usdc,+ recipient_mint: billingMint,+ prep_endpoint: '/api/pump/accept-payment-prep',+ hint:+ 'POST a confirmed acceptPayment whose end_time > now() and tool_name matches this tool, then retry.',+ });+ }+ auth.subscription = sub;+ }+ }++ try {+ const result = await tool.handler(args, auth);+ recordEvent({+ userId: auth.userId,+ apiKeyId: auth.apiKeyId,+ clientId: auth.clientId,+ kind: 'tool_call',+ tool: name,+ latencyMs: Date.now() - started,+ meta: { args_summary: summarize(args) },+ });+ return result;+ } catch (err) {+ recordEvent({+ userId: auth.userId,+ apiKeyId: auth.apiKeyId,+ clientId: auth.clientId,+ kind: 'tool_call',+ tool: name,+ status: 'error',+ latencyMs: Date.now() - started,+ meta: { error: err.message },+ });+ // Only re-throw intentional JSON-RPC errors (integer codes); string codes are+ // postgres SQL states (e.g. '42P01') — sanitize those rather than leaking them.+ if (err.code && typeof err.code === 'number') throw err;+ // Framework convention: tool errors go in result.isError, not rpc error.+ // Detect postgres driver errors by their severity field and suppress internals.+ if (err.severity !== undefined || err.schema !== undefined) {+ log.error('tool_db_error', { tool: name, pg_code: err.code });+ return { content: [{ type: 'text', text: 'Error: internal error' }], isError: true };+ }+ return {+ content: [{ type: 'text', text: `Error: ${err.message || 'tool call failed'}` }],+ isError: true,+ };+ }+}diff --git a/api/_mcp/embed-policy.js b/api/_mcp/embed-policy.jsnew file mode 100644index 000000000..47e0a5127--- /dev/null+++ b/api/_mcp/embed-policy.js@@ -0,0 +1,37 @@+// Inline embed-policy helpers (api/_lib/embed-policy.js not yet shipped).+import { sql } from '../_lib/db.js';++function _mcpDefaultPolicy() {+ return {+ version: 1,+ origins: { mode: 'allowlist', hosts: [] },+ surfaces: { script: true, iframe: true, widget: true, mcp: true },+ };+}++function _mcpParsePolicy(p) {+ if (!p) return null;+ if (!('version' in p) && ('mode' in p || 'hosts' in p)) {+ // Old flat shape — only origins were configured; all surfaces (incl. mcp) allowed.+ return {+ ..._mcpDefaultPolicy(),+ origins: { mode: p.mode || 'allowlist', hosts: p.hosts ?? [] },+ };+ }+ return { ..._mcpDefaultPolicy(), ...p };+}++export async function readMcpPolicyByAvatar(avatarId) {+ try {+ const [row] = await sql`+ SELECT embed_policy FROM agent_identities+ WHERE avatar_id = ${avatarId} AND deleted_at IS NULL+ LIMIT 1+ `;+ if (!row) return null;+ return _mcpParsePolicy(row.embed_policy);+ } catch (err) {+ if (/column .* does not exist/i.test(String(err?.message))) return null;+ throw err;+ }+}diff --git a/api/_mcp/render.js b/api/_mcp/render.jsnew file mode 100644index 000000000..dbc6109fa--- /dev/null+++ b/api/_mcp/render.js@@ -0,0 +1,80 @@+// HTML rendering and CSS safety helpers for the render_avatar tool.++function esc(s) {+ return String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' })[c]);+}++function attr(s) {+ return String(s).replace(+ /[&<>"]/g,+ (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c],+ );+}++// CSS inputs land inside a <style> declaration (`background: <value>`), where+// HTML attribute-escaping does not defend against `;}body{…}` breakouts. Only+// allow a strict character class that cannot terminate the declaration/rule.+export function safeCssValue(s, fallback) {+ if (!s) return fallback;+ const str = String(s).trim();+ if (!/^[a-zA-Z0-9 .,%#()\-_/+]+$/.test(str)) return fallback;+ if (str.length > 120) return fallback;+ return str;+}++export function safeCssLength(s, fallback) {+ if (!s) return fallback;+ const str = String(s).trim();+ if (!/^[0-9]+(?:\.[0-9]+)?(?:px|em|rem|vh|vw|%)$|^auto$|^100%$/.test(str)) return fallback;+ return str;+}++// Posters are rendered as an attribute value that the browser fetches; restrict+// to https(:) to block `javascript:` and `data:` URLs that could execute code.+export function safeHttpsUrl(s) {+ if (!s) return undefined;+ try {+ const u = new URL(String(s));+ return u.protocol === 'https:' ? u.toString() : undefined;+ } catch {+ return undefined;+ }+}++export function renderModelViewerHtml({ src, name, poster, background, height, width, autoRotate, ar, cameraOrbit }) {+ const attrs = [+ `src="${attr(src)}"`,+ 'camera-controls',+ 'shadow-intensity="1"',+ 'exposure="1"',+ 'tone-mapping="aces"',+ autoRotate ? 'auto-rotate' : '',+ ar ? 'ar ar-modes="webxr scene-viewer quick-look"' : '',+ poster ? `poster="${attr(poster)}"` : '',+ cameraOrbit ? `camera-orbit="${attr(cameraOrbit)}"` : '',+ `alt="${attr(name || 'Avatar')}"`,+ ]+ .filter(Boolean)+ .join(' ');+ return [+ '<!doctype html>',+ '<html><head><meta charset="utf-8"><title>' + esc(name || 'Avatar') + '</title>',+ '<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/4.0.0/model-viewer.min.js"></script>',+ '<style>html,body{margin:0;height:100%;background:' + attr(background) + '}',+ 'model-viewer{width:' + attr(width) + ';height:' + attr(height) + ';--progress-bar-color:#6a5cff}</style>',+ '</head><body>',+ '<model-viewer ' + attrs + '></model-viewer>',+ '</body></html>',+ ].join('\n');+}++export function formatAvatarList(avatars, { public: isPublic = false } = {}) {+ if (!avatars.length) return 'No avatars found.';+ return avatars+ .map((a) => {+ const url = a.model_url ? ` — ${a.model_url}` : '';+ const vis = isPublic ? '' : ` [${a.visibility}]`;+ return `• ${a.name} (slug: ${a.slug}, id: ${a.id})${vis}${url}`;+ })+ .join('\n');+}diff --git a/api/_mcp/tools/avatars.js b/api/_mcp/tools/avatars.jsindex e75bea7fe..505935a32 100644--- a/api/_mcp/tools/avatars.js+++ b/api/_mcp/tools/avatars.js@@ -1,4 +1,3 @@-import { sql } from '../../_lib/db.js';import {listAvatars,getAvatar,@@ -7,6 +6,14 @@ import {resolveAvatarUrl,deleteAvatar,} from '../../_lib/avatars.js';+import {+ renderModelViewerHtml,+ formatAvatarList,+ safeCssValue,+ safeCssLength,+ safeHttpsUrl,+} from '../render.js';+import { readMcpPolicyByAvatar } from '../embed-policy.js';function rpcError(code, message, data) {const e = new Error(message);@@ -15,120 +22,6 @@ function rpcError(code, message, data) {return e;}-function _mcpDefaultPolicy() {- return {- version: 1,- origins: { mode: 'allowlist', hosts: [] },- surfaces: { script: true, iframe: true, widget: true, mcp: true },- };-}--function _mcpParsePolicy(p) {- if (!p) return null;- if (!('version' in p) && ('mode' in p || 'hosts' in p)) {- // Old flat shape — only origins were configured; all surfaces (incl. mcp) allowed.- return {- ..._mcpDefaultPolicy(),- origins: { mode: p.mode || 'allowlist', hosts: p.hosts ?? [] },- };- }- return { ..._mcpDefaultPolicy(), ...p };-}--async function _readMcpPolicyByAvatar(avatarId) {- try {- const [row] = await sql`- SELECT embed_policy FROM agent_identities- WHERE avatar_id = ${avatarId} AND deleted_at IS NULL- LIMIT 1- `;- if (!row) return null;- return _mcpParsePolicy(row.embed_policy);- } catch (err) {- if (/column .* does not exist/i.test(String(err?.message))) return null;- throw err;- }-}--function esc(s) {- return String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' })[c]);-}--function attr(s) {- return String(s).replace(- /[&<>"]/g,- (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c],- );-}--// CSS inputs land inside a <style> declaration (`background: <value>`), where-// HTML attribute-escaping does not defend against `;}body{…}` breakouts. Only-// allow a strict character class that cannot terminate the declaration/rule.-function safeCssValue(s, fallback) {- if (!s) return fallback;- const str = String(s).trim();- if (!/^[a-zA-Z0-9 .,%#()\-_/+]+$/.test(str)) return fallback;- if (str.length > 120) return fallback;- return str;-}--function safeCssLength(s, fallback) {- if (!s) return fallback;- const str = String(s).trim();- if (!/^[0-9]+(?:\.[0-9]+)?(?:px|em|rem|vh|vw|%)$|^auto$|^100%$/.test(str)) return fallback;- return str;-}--// Posters are rendered as an attribute value that the browser fetches; restrict-// to https(:) to block `javascript:` and `data:` URLs that could execute code.-function safeHttpsUrl(s) {- if (!s) return undefined;- try {- const u = new URL(String(s));- return u.protocol === 'https:' ? u.toString() : undefined;- } catch {- return undefined;- }-}--function renderModelViewerHtml({ src, name, poster, background, height, width, autoRotate, ar, cameraOrbit }) {- const attrs = [- `src="${attr(src)}"`,- 'camera-controls',- 'shadow-intensity="1"',- 'exposure="1"',- 'tone-mapping="aces"',- autoRotate ? 'auto-rotate' : '',- ar ? 'ar ar-modes="webxr scene-viewer quick-look"' : '',- poster ? `poster="${attr(poster)}"` : '',- cameraOrbit ? `camera-orbit="${attr(cameraOrbit)}"` : '',- `alt="${attr(name || 'Avatar')}"`,- ]- .filter(Boolean)- .join(' ');- return [- '<!doctype html>',- '<html><head><meta charset="utf-8"><title>' + esc(name || 'Avatar') + '</title>',- '<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/4.0.0/model-viewer.min.js"></script>',- '<style>html,body{margin:0;height:100%;background:' + attr(background) + '}',- 'model-viewer{width:' + attr(width) + ';height:' + attr(height) + ';--progress-bar-color:#6a5cff}</style>',- '</head><body>',- '<model-viewer ' + attrs + '></model-viewer>',- '</body></html>',- ].join('\n');-}--function formatAvatarList(avatars, { public: isPublic = false } = {}) {- if (!avatars.length) return 'No avatars found.';- return avatars- .map((a) => {- const url = a.model_url ? ` — ${a.model_url}` : '';- const vis = isPublic ? '' : ` [${a.visibility}]`;- return `• ${a.name} (slug: ${a.slug}, id: ${a.id})${vis}${url}`;- })- .join('\n');-}-export const toolDefs = [{name: 'list_my_avatars',@@ -272,7 +165,7 @@ export const toolDefs = [: null;if (!avatar) throw new Error('avatar not found');// surfaces.mcp gate — check if a registered agent owns this avatar- const _mcpPolicy = await _readMcpPolicyByAvatar(avatar.id);+ const _mcpPolicy = await readMcpPolicyByAvatar(avatar.id);if (_mcpPolicy && _mcpPolicy.surfaces?.mcp === false) {throw rpcError(-32000,diff --git a/api/mcp.js b/api/mcp.jsindex bebd8cb7f..b74cadd12 100644--- a/api/mcp.js+++ b/api/mcp.js@@ -1,110 +1,22 @@-// ─────────────────────────────────────────────────────────────────────────────-// MCP server — Streamable HTTP transport-// Spec: https://modelcontextprotocol.io/specification/2025-06-18/basic-//-// Endpoints at this path:-// POST /api/mcp JSON-RPC request(s); response is JSON (or SSE for streaming)-// GET /api/mcp (optional) SSE stream for server→client notifications-// DELETE /api/mcp terminate session-//-// Auth: Bearer access token (OAuth 2.1) OR Bearer API key.-// On 401, return WWW-Authenticate pointing to protected-resource metadata (RFC 9728).-// ─────────────────────────────────────────────────────────────────────────────--import { env } from './_lib/env.js';-import { sql } from './_lib/db.js';-import { authenticateBearer, extractBearer, hasScope } from './_lib/auth.js';+// MCP server — Streamable HTTP transport (MCP 2025-06-18, JSON-RPC 2.0)+// POST /api/mcp — tool calls GET /api/mcp — SSE DELETE /api/mcp — terminate sessionimport { cors, readJson, wrap } from './_lib/http.js';import { limits, clientIp } from './_lib/rate-limit.js';-import { recordEvent, logger } from './_lib/usage.js';-import {- listAvatars,- getAvatar,- getAvatarBySlug,- searchPublicAvatars,- resolveAvatarUrl,- deleteAvatar,-} from './_lib/avatars.js';-import { fetchModel, FetchModelError } from './_lib/fetch-model.js';-import { crawlAgentAttestations, KIND_MAP } from './_lib/solana-attestations.js';-import { pumpfunMcp, pumpfunBotEnabled } from './_lib/pumpfun-mcp.js';-import { inspectModel, suggestOptimizations } from './_lib/model-inspect.js';-import { validateBytes } from 'gltf-validator';-import {- X402Error,- paymentRequirements,- verifyPayment,- settlePayment,- encodePaymentResponseHeader,- send402,- resolveResourceUrl,-} from './_lib/x402-spec.js';-import {- priceFor,- findActiveSubscription,- resolveBillingMint,-} from './_lib/pump-pricing.js';-import { normalizeLegacyPolicy } from './_lib/embed-policy.js';--const PROTOCOL_VERSION = '2025-06-18';-const SERVER_INFO = { name: '3d-agent-mcp', version: '1.0.0' };-const log = logger('mcp');+import { settlePayment, encodePaymentResponseHeader } from './_lib/x402-spec.js';+import { PROTOCOL_VERSION, dispatch } from './_mcp/dispatch.js';+import { send401, sendJsonRpcError, authenticateRequest, handleSse, handleTerminate } from './_mcp/auth.js';+import { sendX402Error } from './_mcp/payments.js';export default wrap(async (req, res) => {if (cors(req, res, { methods: 'GET,POST,DELETE,OPTIONS' })) return;- // Unauthenticated discovery surface — DELETE and GET without bearer are OK to reject quickly.if (req.method === 'GET') return handleSse(req, res);if (req.method === 'DELETE') return handleTerminate(req, res);if (req.method !== 'POST') return send401(res, 'method not supported');- // Auth: either a Bearer token (OAuth/API key) OR an x402 X-PAYMENT header.- const bearer = extractBearer(req);- const paymentHeader = req.headers['x-payment'];- let auth = null;- let x402Ctx = null;-- if (bearer) {- auth = await authenticateBearer(bearer, { audience: env.MCP_RESOURCE });- if (!auth) return send401(res, 'missing or invalid access token');- } else if (paymentHeader) {- const requirements = paymentRequirements({- resource: resolveResourceUrl(req, '/api/mcp'),- description: 'MCP tool call',- });- try {- const verified = await verifyPayment({ paymentHeader, requirements });- x402Ctx = {- requirements,- requirement: verified.requirement,- paymentPayload: verified.paymentPayload,- payer: verified.payer,- };- } catch (err) {- return sendX402Error(res, requirements, err);- }- // Anonymous paid caller — synthesize an auth principal scoped to public-read tools.- // userId is null because usage_events.user_id is a UUID FK; the payer wallet is- // kept on the auth object so handlers and rate limits can key off of it.- auth = {- userId: null,- rateKey: `x402:${x402Ctx.payer || 'anon'}`,- // Pay-per-call callers have no user account, so they cannot read or- // write account-scoped data. They get only the no-scope public tools- // (search_public_avatars, validate/inspect/optimize_model, solana_*).- scope: '',- source: 'x402',- payer: x402Ctx.payer,- };- } else {- return send402(- res,- paymentRequirements({- resource: resolveResourceUrl(req, '/api/mcp'),- description: 'MCP tool call',- }),- );- }+ const result = await authenticateRequest(req, res);+ if (!result) return;+ const { auth, x402Ctx } = result;const ipRl = await limits.mcpIp(clientIp(req));if (!ipRl.success)@@ -121,9 +33,9 @@ export default wrap(async (req, res) => {const batch = Array.isArray(body) ? body : [body];// Per-request batch cap — each message can trigger DB queries, so an// unbounded batch multiplies rate-limited work by N against the user's budget.- if (batch.length > 32) {+ if (batch.length > 32)return sendJsonRpcError(res, null, -32600, 'batch too large (max 32)');- }+const responses = [];for (const msg of batch) {const r = await dispatch(msg, auth, req);@@ -150,1145 +62,3 @@ export default wrap(async (req, res) => {res.setHeader('mcp-protocol-version', PROTOCOL_VERSION);res.end(JSON.stringify(Array.isArray(body) ? responses : (responses[0] ?? null)));});--function sendX402Error(res, requirements, err) {- if (err instanceof X402Error) {- if (err.status === 402) return send402(res, requirements, err.message);- res.statusCode = err.status;- res.setHeader('content-type', 'application/json; charset=utf-8');- res.end(JSON.stringify({ error: err.code, error_description: err.message }));- return;- }- log.error('x402_unexpected', { message: err?.message });- res.statusCode = 500;- res.setHeader('content-type', 'application/json; charset=utf-8');- res.end(JSON.stringify({ error: 'internal', error_description: 'x402 processing failed' }));-}--// ── JSON-RPC dispatch ────────────────────────────────────────────────────────-async function dispatch(msg, auth, req) {- const started = Date.now();- const id = msg.id;- const isNotification = id === undefined;-- try {- if (msg.jsonrpc !== '2.0') throw rpcError(-32600, 'invalid Request');- const method = msg.method;-- if (method === 'initialize') return ok(id, await onInitialize(msg.params, auth));- if (method === 'ping') return ok(id, {});- if (method === 'notifications/initialized') return null;- if (method === 'tools/list')- return ok(id, {- tools: TOOL_CATALOG.map((t) => {- const price = priceFor(t.name);- return price- ? {- ...t,- pricing: {- amount_usdc: price.amount_usdc,- currency: 'USDC',- description: price.description,- scheme: 'pump-agent-payments',- prep_endpoint: '/api/pump/accept-payment-prep',- confirm_endpoint: '/api/pump/accept-payment-confirm',- recipient_mint: resolveBillingMint(),- },- }- : t;- }),- });- if (method === 'tools/call') return ok(id, await onToolCall(msg.params, auth, started));- if (method === 'resources/list') return ok(id, { resources: [] });- if (method === 'resources/templates/list') return ok(id, { resourceTemplates: [] });- if (method === 'prompts/list') return ok(id, { prompts: [] });- if (method === 'logging/setLevel') return ok(id, {});-- throw rpcError(-32601, `method not found: ${method}`);- } catch (err) {- log.warn('rpc_error', { method: msg.method, code: err.code, message: err.message });- if (isNotification) return null;- return {- jsonrpc: '2.0',- id,- error: {- code: err.code || -32603,- message: err.message || 'internal error',- data: err.data,- },- };- }-}--function ok(id, result) {- return { jsonrpc: '2.0', id, result };-}--function rpcError(code, message, data) {- const e = new Error(message);- e.code = code;- e.data = data;- return e;-}--// ── MCP: initialize ──────────────────────────────────────────────────────────-async function onInitialize(_params, _auth) {- return {- protocolVersion: PROTOCOL_VERSION,- serverInfo: SERVER_INFO,- capabilities: {- tools: { listChanged: false },- resources: { listChanged: false, subscribe: false },- logging: {},- },- instructions: [- 'Render 3D avatars stored on three.ws as <model-viewer> HTML artifacts.',- "Use list_my_avatars to see the user's avatars and render_avatar to get embeddable viewer HTML.",- 'Public avatars can be discovered via search_public_avatars.',- ].join(' '),- };-}--// ── MCP: tools/call ──────────────────────────────────────────────────────────-async function onToolCall(params, auth, started) {- const { name, arguments: args = {} } = params || {};- const tool = TOOLS[name];- if (!tool) throw rpcError(-32602, `unknown tool: ${name}`);- if (tool.scope && !hasScope(auth.scope, tool.scope)) {- throw rpcError(-32002, `insufficient scope, requires ${tool.scope}`);- }-- // Pump-agent-payments gate. Free tools and paid-bearer-authenticated users- // (existing subscription plans) bypass. x402 callers and anonymous payers- // must either supply X-PAYMENT (handled at the HTTP layer above) or have- // an active pump-agent-payments subscription window for this tool.- const price = priceFor(name);- if (price && auth.source === 'x402') {- const billingMint = resolveBillingMint();- const payerWallet = args.payer_wallet || auth.payer || null;- if (billingMint && payerWallet) {- const sub = await findActiveSubscription({- mint: billingMint,- network: process.env.PUMP_DEFAULT_NETWORK || 'mainnet',- payerWallet,- toolName: name,- });- if (!sub) {- throw rpcError(-32402, 'payment required for this tool', {- scheme: 'pump-agent-payments',- tool: name,- amount_usdc: price.amount_usdc,- recipient_mint: billingMint,- prep_endpoint: '/api/pump/accept-payment-prep',- hint:- 'POST a confirmed acceptPayment whose end_time > now() and tool_name matches this tool, then retry.',- });- }- auth.subscription = sub;- }- }-- try {- const result = await tool.handler(args, auth);- recordEvent({- userId: auth.userId,- apiKeyId: auth.apiKeyId,- clientId: auth.clientId,- kind: 'tool_call',- tool: name,- latencyMs: Date.now() - started,- meta: { args_summary: summarize(args) },- });- return result;- } catch (err) {- recordEvent({- userId: auth.userId,- apiKeyId: auth.apiKeyId,- clientId: auth.clientId,- kind: 'tool_call',- tool: name,- status: 'error',- latencyMs: Date.now() - started,- meta: { error: err.message },- });- // Only re-throw intentional JSON-RPC errors (integer codes); string codes are- // postgres SQL states (e.g. '42P01') — sanitize those rather than leaking them.- if (err.code && typeof err.code === 'number') throw err;- // Framework convention: tool errors go in result.isError, not rpc error.- // Detect postgres driver errors by their severity field and suppress internals.- if (err.severity !== undefined || err.schema !== undefined) {- log.error('tool_db_error', { tool: name, pg_code: err.code });- return { content: [{ type: 'text', text: 'Error: internal error' }], isError: true };- }- return {- content: [{ type: 'text', text: `Error: ${err.message || 'tool call failed'}` }],- isError: true,- };- }-}--// ── inline embed-policy helpers (prompt 02 / api/_lib/embed-policy.js not yet shipped) ──--function _mcpDefaultPolicy() {- return {- version: 1,- origins: { mode: 'allowlist', hosts: [] },- surfaces: { script: true, iframe: true, widget: true, mcp: true },- };-}--function _mcpParsePolicy(p) {- if (!p) return null;- if (!('version' in p) && ('mode' in p || 'hosts' in p)) {- // Old flat shape — only origins were configured; all surfaces (incl. mcp) allowed.- return {- ..._mcpDefaultPolicy(),- origins: { mode: p.mode || 'allowlist', hosts: p.hosts ?? [] },- };- }- return { ..._mcpDefaultPolicy(), ...p };-}--async function _readMcpPolicyByAvatar(avatarId) {- try {- const [row] = await sql`- SELECT embed_policy FROM agent_identities- WHERE avatar_id = ${avatarId} AND deleted_at IS NULL- LIMIT 1- `;- if (!row) return null;- return _mcpParsePolicy(row.embed_policy);- } catch (err) {- if (/column .* does not exist/i.test(String(err?.message))) return null;- throw err;- }-}--// ── tool catalog ─────────────────────────────────────────────────────────────-const TOOL_CATALOG = [- {- name: 'list_my_avatars',- title: 'List my avatars',- description:- "List the authenticated user's avatars. Returns id, name, slug, size, visibility, and direct model_url (when visibility permits).",- inputSchema: {- type: 'object',- properties: {- limit: { type: 'integer', minimum: 1, maximum: 100, default: 25 },- cursor: {- type: 'string',- description: 'Opaque pagination cursor from previous response.',- },- visibility: { type: 'string', enum: ['private', 'unlisted', 'public'] },- },- additionalProperties: false,- },- },- {- name: 'get_avatar',- title: 'Get avatar',- description:- 'Fetch a single avatar by id or by owner+slug. Returns metadata and a model_url (public/unlisted) or short-lived signed URL (private).',- inputSchema: {- type: 'object',- properties: {- id: { type: 'string', format: 'uuid' },- slug: { type: 'string' },- },- additionalProperties: false,- },- },- {- name: 'search_public_avatars',- title: 'Search public avatars',- description:- 'Search the public avatar gallery. Useful for finding characters to render without prior knowledge of an id.',- inputSchema: {- type: 'object',- properties: {- q: { type: 'string', description: 'Free-text search over name and description.' },- tag: { type: 'string', description: 'Filter to one tag.' },- limit: { type: 'integer', minimum: 1, maximum: 50, default: 12 },- },- additionalProperties: false,- },- },- {- name: 'render_avatar',- title: 'Render avatar',- description:- 'Produce an HTML <model-viewer> snippet that renders the given avatar. ' +- 'Return this text as an inline HTML artifact to display an interactive 3D avatar.',- inputSchema: {- type: 'object',- properties: {- id: { type: 'string', format: 'uuid' },- slug: { type: 'string' },- auto_rotate: { type: 'boolean', default: true },- background: {- type: 'string',- description: 'CSS background color or gradient.',- default: 'transparent',- },- height: { type: 'string', default: '480px' },- width: { type: 'string', default: '100%' },- camera_orbit: {- type: 'string',- description: 'model-viewer camera-orbit value, e.g. "0deg 80deg 2m".',- },- poster: {- type: 'string',- description: 'Optional poster image URL shown while loading.',- },- ar: {- type: 'boolean',- default: true,- description: 'Include AR button for mobile.',- },- },- additionalProperties: false,- },- },- {- name: 'delete_avatar',- title: 'Delete avatar',- description: 'Soft-delete an avatar you own. Requires avatars:delete scope.',- inputSchema: {- type: 'object',- properties: {- id: { type: 'string', format: 'uuid' },- confirm: {- type: 'boolean',- description: 'Must be true to confirm permanent deletion.',- },- },- required: ['id'],- additionalProperties: false,- },- },- {- name: 'validate_model',- title: 'Validate glTF/GLB model',- description:- 'Run the Khronos glTF-Validator against a remote GLB or glTF URL. Returns a structured report of errors, warnings, infos, and hints — the authoritative answer to "is this file spec-compliant?". SSRF-hardened: only public https URLs are fetched.',- inputSchema: {- type: 'object',- properties: {- url: {- type: 'string',- format: 'uri',- description: 'Public https URL of a .glb or .gltf file.',- },- max_issues: { type: 'integer', minimum: 1, maximum: 500, default: 100 },- },- required: ['url'],- additionalProperties: false,- },- },- {- name: 'inspect_model',- title: 'Inspect glTF/GLB model',- description:- 'Parse a remote GLB or glTF and return structural stats: scene/node/mesh counts, vertex and triangle totals, material and texture summaries, extensions used. Pure inspection — no optimization advice.',- inputSchema: {- type: 'object',- properties: {- url: {- type: 'string',- format: 'uri',- description: 'Public https URL of a .glb or .gltf file.',- },- },- required: ['url'],- additionalProperties: false,- },- },- {- name: 'optimize_model',- title: 'Suggest optimizations for a glTF/GLB model',- description:- 'Inspect the model and return actionable suggestions for reducing size and draw-call overhead: triangle budget, Draco/Meshopt compression, oversized textures, KTX2 transcoding, non-indexed primitives, redundant materials, and more.',- inputSchema: {- type: 'object',- properties: {- url: {- type: 'string',- format: 'uri',- description: 'Public https URL of a .glb or .gltf file.',- },- },- required: ['url'],- additionalProperties: false,- },- },- {- name: 'solana_agent_reputation',- title: 'Get Solana agent reputation',- description:- 'Computed reputation summary for a Solana-registered three.ws agent. Returns total/verified feedback counts, score averages (raw + verified-only), validation pass/fail, task acceptance, and dispute counts. Verified score only includes feedback whose task was acknowledged on-chain by the agent owner. Public; no auth required.',- inputSchema: {- type: 'object',- properties: {- asset: { type: 'string', description: 'Metaplex Core asset pubkey (the agent ID)' },- network: { type: 'string', enum: ['mainnet', 'devnet'], default: 'devnet' },- },- required: ['asset'],- additionalProperties: false,- },- },- {- name: 'solana_agent_attestations',- title: 'List Solana agent attestations',- description:- 'List recent on-chain attestations about a Solana-registered agent (feedback, validation, task offers, acceptances, disputes). Backed by the three.ws indexer for sub-100ms reads. Each row includes verified/disputed/revoked flags. Public; no auth required.',- inputSchema: {- type: 'object',- properties: {- asset: { type: 'string' },- kind: {- type: 'string',- enum: ['feedback', 'validation', 'task', 'accept', 'revoke', 'dispute', 'all'],- default: 'all',- },- network: { type: 'string', enum: ['mainnet', 'devnet'], default: 'devnet' },- limit: { type: 'integer', minimum: 1, maximum: 200, default: 50 },- },- required: ['asset'],- additionalProperties: false,- },- },- {- name: 'solana_agent_passport',- title: 'Get Solana agent passport',- description:- 'Full discovery card for a Solana agent: identity (Metaplex Core asset), owner wallet, reputation summary, latest validation result, and attestation schema endpoint. Equivalent to an ERC-8004 passport — use this when one tool call should answer "who is this agent and can I trust them?".',- inputSchema: {- type: 'object',- properties: {- asset: { type: 'string' },- network: { type: 'string', enum: ['mainnet', 'devnet'], default: 'devnet' },- },- required: ['asset'],- additionalProperties: false,- },- },- {- name: 'pumpfun_recent_claims',- title: 'Recent pump.fun claims',- description:- "Fetch the most recent pump.fun GitHub social-fee claim events with full enrichment: GitHub profile, X/Twitter follower data, influencer tier, first-time-claim flag, fake-claim detection, and AI summary. Use to answer 'what's happening on pump.fun right now?'.",- inputSchema: {- type: 'object',- properties: {- limit: { type: 'integer', minimum: 1, maximum: 50, default: 10 },- },- additionalProperties: false,- },- },- {- name: 'pumpfun_token_intel',- title: 'Pump.fun token intel',- description:- 'Full intel on a pump.fun token: graduation status, bonding-curve progress, creator profile, top holders, volume, bundle detection, and trust signals.',- inputSchema: {- type: 'object',- properties: {- mint: { type: 'string', description: 'SPL mint pubkey (base58).' },- },- required: ['mint'],- additionalProperties: false,- },- },- {- name: 'pumpfun_creator_intel',- title: 'Pump.fun creator intel',- description:- 'Reputation profile for a pump.fun creator wallet: prior launches, graduation rate, claim activity, and behavioural trust signals.',- inputSchema: {- type: 'object',- properties: {- wallet: { type: 'string', description: 'Solana wallet pubkey (base58).' },- },- required: ['wallet'],- additionalProperties: false,- },- },- {- name: 'pumpfun_recent_graduations',- title: 'Recent pump.fun graduations',- description:- 'Tokens that recently graduated from the bonding curve to PumpAMM, with creator + holder analysis.',- inputSchema: {- type: 'object',- properties: {- limit: { type: 'integer', minimum: 1, maximum: 50, default: 10 },- },- additionalProperties: false,- },- },- {- name: 'call_agent',- title: 'Call agent',- description:- 'Send a message to another three.ws agent and get its response. Use this to delegate specialized tasks.',- inputSchema: {- type: 'object',- properties: {- agent_id: { type: 'string', description: "The agent's ID" },- message: { type: 'string', description: 'The message to send' },- },- required: ['agent_id', 'message'],- additionalProperties: false,- },- },-];--const TOOLS = {- list_my_avatars: {- scope: 'avatars:read',- async handler(args, auth) {- const result = await listAvatars({- userId: auth.userId,- limit: args.limit || 25,- cursor: args.cursor,- visibility: args.visibility,- });- return {- content: [{ type: 'text', text: formatAvatarList(result.avatars) }],- structuredContent: result,- };- },- },-- get_avatar: {- scope: 'avatars:read',- async handler(args, auth) {- const avatar = args.id- ? await getAvatar({ id: args.id, requesterId: auth.userId })- : args.slug- ? await getAvatarBySlug({- ownerId: auth.userId,- slug: args.slug,- requesterId: auth.userId,- })- : null;- if (!avatar) throw new Error('avatar not found');- const urlInfo = await resolveAvatarUrl(avatar);- const merged = { ...avatar, ...urlInfo };- return {- content: [{ type: 'text', text: JSON.stringify(merged, null, 2) }],- structuredContent: merged,- };- },- },-- search_public_avatars: {- async handler(args, auth) {- // Unauthenticated callers (x402/anonymous) are capped at 10 to prevent bulk enumeration.- const maxLimit = auth.userId ? 50 : 10;- const result = await searchPublicAvatars({- q: args.q,- tag: args.tag,- limit: Math.min(args.limit || 12, maxLimit),- });- return {- content: [- { type: 'text', text: formatAvatarList(result.avatars, { public: true }) },- ],- structuredContent: result,- };- },- },-- render_avatar: {- scope: 'avatars:read',- async handler(args, auth) {- const avatar = args.id- ? await getAvatar({ id: args.id, requesterId: auth.userId })- : args.slug- ? await getAvatarBySlug({- ownerId: auth.userId,- slug: args.slug,- requesterId: auth.userId,- })- : null;- if (!avatar) throw new Error('avatar not found');- // surfaces.mcp gate — check if a registered agent owns this avatar- const _mcpPolicy = await _readMcpPolicyByAvatar(avatar.id);- if (_mcpPolicy && _mcpPolicy.surfaces?.mcp === false) {- throw rpcError(- -32000,- 'embed_denied_surface',- 'This agent disallows the MCP surface.',- );- }- const urlInfo = await resolveAvatarUrl(avatar, { expiresIn: 3600 });- const html = renderModelViewerHtml({- src: urlInfo.url,- name: avatar.name,- poster: safeHttpsUrl(args.poster),- background: safeCssValue(args.background, 'transparent'),- height: safeCssLength(args.height, '480px'),- width: safeCssLength(args.width, '100%'),- autoRotate: args.auto_rotate !== false,- ar: args.ar !== false,- cameraOrbit: safeCssValue(args.camera_orbit, ''),- });- // Keep chat text short so claude.ai doesn't dump the full HTML into the- // transcript. The HTML goes in the resource entry, which clients render- // as an inline artifact when mimeType is text/html.- const summary = `Rendered avatar "${avatar.name}". Display the attached text/html resource as an inline HTML artifact.`;- return {- content: [- { type: 'text', text: summary },- {- type: 'resource',- resource: {- uri: `avatar://${avatar.id}`,- mimeType: 'text/html',- text: html,- },- },- ],- structuredContent: { html, avatar: { ...avatar, ...urlInfo } },- };- },- },-- delete_avatar: {- scope: 'avatars:delete',- async handler(args, auth) {- if (!args.confirm) {- return {- content: [- {- type: 'text',- text: 'Set confirm: true to permanently delete this avatar.',- },- ],- isError: true,- };- }- const ok = await deleteAvatar({ id: args.id, userId: auth.userId });- if (!ok) throw new Error('avatar not found or not yours');- // TODO: audit_log table needed — INSERT (user_id, action='delete_avatar', resource_id, created_at)- return { content: [{ type: 'text', text: `Deleted avatar ${args.id}.` }] };- },- },-- validate_model: {- async handler(args, auth) {- const rl = await limits.mcpValidate(auth.userId || auth.rateKey);- if (!rl.success)- throw rpcError(-32000, 'rate_limited', {- retry_after: Math.ceil((rl.reset - Date.now()) / 1000),- });- const { bytes, url, filename } = await safeFetchModel(args.url);- const max = Math.min(Math.max(args.max_issues || 100, 1), 500);- const report = await validateBytes(bytes, { maxIssues: max, uri: filename });- const issues = report?.issues || {};- const summary = {- url,- filename,- fileSize: bytes.byteLength,- validatorVersion: report?.validatorVersion,- mimeType: report?.mimeType,- numErrors: issues.numErrors ?? 0,- numWarnings: issues.numWarnings ?? 0,- numInfos: issues.numInfos ?? 0,- numHints: issues.numHints ?? 0,- truncated: !!issues.truncated,- };- return {- content: [- { type: 'text', text: formatValidationSummary(summary, issues.messages || []) },- ],- structuredContent: {- ...summary,- messages: issues.messages || [],- info: report?.info || null,- },- };- },- },-- inspect_model: {- async handler(args, auth) {- const rl = await limits.mcpInspect(auth.userId || auth.rateKey);- if (!rl.success)- throw rpcError(-32000, 'rate_limited', {- retry_after: Math.ceil((rl.reset - Date.now()) / 1000),- });- const { bytes, url, filename } = await safeFetchModel(args.url);- const info = await inspectModel(bytes, { fileSize: bytes.byteLength });- return {- content: [{ type: 'text', text: formatInspection({ url, filename, ...info }) }],- structuredContent: { url, filename, ...info },- };- },- },-- optimize_model: {- async handler(args, auth) {- const rl = await limits.mcpOptimize(auth.userId || auth.rateKey);- if (!rl.success)- throw rpcError(-32000, 'rate_limited', {- retry_after: Math.ceil((rl.reset - Date.now()) / 1000),- });- const { bytes, url, filename } = await safeFetchModel(args.url);- const info = await inspectModel(bytes, { fileSize: bytes.byteLength });- const suggestions = suggestOptimizations(info);- return {- content: [{ type: 'text', text: formatSuggestions(suggestions) }],- structuredContent: { url, filename, suggestions, info },- };- },- },-- solana_agent_reputation: {- async handler(args) {- const data = await solanaReputation(args.asset, args.network || 'devnet');- return {- content: [{ type: 'text', text: JSON.stringify(data, null, 2) }],- structuredContent: data,- };- },- },-- solana_agent_attestations: {- async handler(args) {- const data = await solanaAttestations({- asset: args.asset,- kind: args.kind || 'all',- network: args.network || 'devnet',- limit: args.limit || 50,- });- return {- content: [{ type: 'text', text: JSON.stringify(data, null, 2) }],- structuredContent: data,- };- },- },-- solana_agent_passport: {- async handler(args) {- const data = await solanaPassport(args.asset, args.network || 'devnet');- return {- content: [{ type: 'text', text: JSON.stringify(data, null, 2) }],- structuredContent: data,- };- },- },-- pumpfun_recent_claims: {- async handler(args) {- return pumpfunToolResult(- await pumpfunMcp.recentClaims({ limit: clamp(args?.limit, 1, 50, 10) }),- );- },- },-- pumpfun_token_intel: {- async handler(args) {- if (!args?.mint) throw Object.assign(new Error('mint required'), { status: 400 });- return pumpfunToolResult(await pumpfunMcp.tokenIntel({ mint: args.mint }));- },- },-- pumpfun_creator_intel: {- async handler(args) {- if (!args?.wallet) throw Object.assign(new Error('wallet required'), { status: 400 });- return pumpfunToolResult(await pumpfunMcp.creatorIntel({ wallet: args.wallet }));- },- },-- pumpfun_recent_graduations: {- async handler(args) {- return pumpfunToolResult(- await pumpfunMcp.graduations({ limit: clamp(args?.limit, 1, 50, 10) }),- );- },- },-- call_agent: {- scope: 'avatars:read',- async handler(args, auth) {- if (!args?.agent_id) throw new Error('agent_id required');- if (!args?.message) throw new Error('message required');-- const rl = await limits.agentDelegate(auth.userId || auth.rateKey || 'anon');- if (!rl.success)- throw rpcError(-32000, 'rate_limited', {- retry_after: Math.ceil((rl.reset - Date.now()) / 1000),- });-- const [agent] = await sql`- SELECT id, name, description, embed_policy, meta- FROM agent_identities- WHERE id = ${args.agent_id} AND deleted_at IS NULL- `;- if (!agent) throw new Error('agent not found');-- const policy = normalizeLegacyPolicy(agent.embed_policy);- const model = policy?.brain?.model || 'claude-haiku-4-5-20251001';- const systemPrompt =- agent.meta?.brain?.instructions ||- `You are ${agent.name}. ${agent.description || ''}`.trim();-- const upstream = await fetch('https://api.anthropic.com/v1/messages', {- method: 'POST',- headers: {- 'content-type': 'application/json',- 'anthropic-version': '2023-06-01',- 'x-api-key': env.ANTHROPIC_API_KEY,- },- body: JSON.stringify({- model,- max_tokens: 1024,- system: systemPrompt,- messages: [{ role: 'user', content: args.message }],- }),- });-- if (!upstream.ok) throw new Error(`LLM call failed: ${upstream.status}`);-- const data = await upstream.json();- const response = data?.content?.[0]?.text || '';- return {- content: [{ type: 'text', text: response }],- structuredContent: { agentId: args.agent_id, response },- };- },- },-};--function pumpfunToolResult(r) {- if (!pumpfunBotEnabled()) {- return {- content: [{ type: 'text', text: 'pump.fun feed is not configured on this server.' }],- isError: true,- };- }- if (!r.ok) {- return {- content: [{ type: 'text', text: `pump.fun upstream error: ${r.error}` }],- isError: true,- };- }- const payload = Array.isArray(r.data) ? { items: r.data } : r.data;- return {- content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }],- structuredContent: payload,- };-}--function clamp(n, lo, hi, fallback) {- const x = Number(n);- if (!Number.isFinite(x)) return fallback;- return Math.max(lo, Math.min(hi, x));-}--// ─── Solana attestation tool helpers ────────────────────────────────────────--async function ensureWarm({ asset, network }) {- const [c] = await sql`select 1 from solana_attestations_cursor where agent_asset = ${asset} limit 1`;- if (c) return;- const [agent] = await sql`- select wallet_address as owner from agent_identities- where meta->>'sol_mint_address' = ${asset} and deleted_at is null limit 1- `;- try { await crawlAgentAttestations({ agentAsset: asset, network, ownerWallet: agent?.owner || null }); }- catch { /* return cached (empty) */ }-}--async function solanaReputation(asset, network) {- await ensureWarm({ asset, network });- const [fb] = await sql`- with feedback as (- select- f.disputed, (f.payload->>'score')::int as score,- exists (- select 1 from solana_attestations a- where a.agent_asset = f.agent_asset and a.kind='threews.accept.v1'- and a.payload->>'task_id' = f.payload->>'task_id'- and a.verified = true and f.payload->>'task_id' is not null- ) as task_accepted- from solana_attestations f- where f.agent_asset=${asset} and f.network=${network}- and f.kind='threews.feedback.v1' and f.revoked=false- )- select count(*)::int total,- count(*) filter (where task_accepted)::int verified,- count(*) filter (where disputed)::int disputed,- coalesce(avg(score),0)::float score_avg,- coalesce(avg(score) filter (where task_accepted),0)::float score_avg_verified- from feedback- `;- const [val] = await sql`- select count(*) filter (where (payload->>'passed')::bool)::int passed,- count(*) filter (where not (payload->>'passed')::bool)::int failed- from solana_attestations- where agent_asset=${asset} and network=${network}- and kind='threews.validation.v1' and revoked=false- `;- return {- agent: asset, network,- feedback: { ...fb,- score_avg: Number(fb.score_avg.toFixed(3)),- score_avg_verified: Number(fb.score_avg_verified.toFixed(3)) },- validation: val,- };-}--async function solanaAttestations({ asset, kind, network, limit }) {- await ensureWarm({ asset, network });- const wantKind = kind === 'all' ? null : KIND_MAP[kind];- const rows = wantKind- ? await sql`- select signature, slot, block_time, attester, kind, payload, verified, revoked, disputed- from solana_attestations- where agent_asset=${asset} and network=${network} and kind=${wantKind} and revoked=false- order by slot desc limit ${limit}- `- : await sql`- select signature, slot, block_time, attester, kind, payload, verified, revoked, disputed- from solana_attestations- where agent_asset=${asset} and network=${network} and revoked=false- order by slot desc limit ${limit}- `;- return { agent: asset, network, kind, count: rows.length, data: rows };-}--async function solanaPassport(asset, network) {- const [agent] = await sql`- select id, name, description, wallet_address as owner, meta- from agent_identities- where meta->>'sol_mint_address' = ${asset} and deleted_at is null limit 1- `;- const reputation = await solanaReputation(asset, network);- const recent = await solanaAttestations({ asset, kind: 'all', network, limit: 10 });- return {- agent: asset,- identity: agent ? {- id: agent.id, name: agent.name, description: agent.description,- owner: agent.owner, asset_pubkey: asset,- network: agent.meta?.network || network,- } : { agent_off_index: true, asset_pubkey: asset, network },- reputation: reputation.feedback,- validation: reputation.validation,- recent_attestations: recent.data,- schemas_url: `${env.APP_ORIGIN}/.well-known/agent-attestation-schemas`,- };-}--async function safeFetchModel(url) {- try {- return await fetchModel(url);- } catch (e) {- if (e instanceof FetchModelError) throw new Error(`fetch failed: ${e.message} (${e.code})`);- throw e;- }-}--function formatValidationSummary(s, messages) {- const head =- `glTF-Validator report for ${s.filename} (${(s.fileSize / 1024).toFixed(1)} KB)\n` +- `Errors: ${s.numErrors}, Warnings: ${s.numWarnings}, Infos: ${s.numInfos}, Hints: ${s.numHints}` +- (s.truncated ? ' (truncated)' : '');- if (!messages.length) return head;- const lines = messages.slice(0, 40).map((m) => {- const sev = ['ERR', 'WRN', 'INF', 'HNT'][m.severity] || '?';- const ptr = m.pointer ? ` @ ${m.pointer}` : '';- return ` [${sev}] ${m.code}: ${m.message}${ptr}`;- });- const more = messages.length > 40 ? `\n … ${messages.length - 40} more` : '';- return `${head}\n${lines.join('\n')}${more}`;-}--function formatInspection(info) {- const c = info.counts;- const tex = info.textures.length- ? info.textures- .map(- (t) =>- ` • ${t.name || '(unnamed)'} — ${t.mimeType} ${t.width}×${t.height}, ${(t.byteSize / 1024).toFixed(1)} KB`,- )- .join('\n')- : ' (none)';- return [- `Model: ${info.filename} (${(info.fileSize / 1024 / 1024).toFixed(2)} MB, ${info.container})`,- `Generator: ${info.generator || 'unknown'} · glTF ${info.version || '?'}`,- `Scenes: ${c.scenes}, Nodes: ${c.nodes}, Meshes: ${c.meshes}, Materials: ${c.materials}, Textures: ${c.textures}`,- `Animations: ${c.animations}, Skins: ${c.skins}`,- `Vertices: ${c.totalVertices.toLocaleString()}, Triangles: ${c.totalTriangles.toLocaleString()}`,- `Indexed primitives: ${c.indexedPrimitives}, Non-indexed: ${c.nonIndexedPrimitives}`,- `Extensions used: ${info.extensionsUsed.join(', ') || '(none)'}`,- `Textures:\n${tex}`,- ].join('\n');-}--function formatSuggestions(suggestions) {- if (!suggestions.length) return 'No suggestions.';- return suggestions- .map((s) => {- const tag =- { info: 'INFO', warn: 'WARN', critical: 'CRIT' }[s.severity] ||- s.severity.toUpperCase();- const est = s.estimate ? ` — ${s.estimate}` : '';- return `[${tag}] ${s.id}: ${s.message}${est}`;- })- .join('\n');-}--// ── <model-viewer> artifact builder ─────────────────────────────────────────-function renderModelViewerHtml({- src,- name,- poster,- background,- height,- width,- autoRotate,- ar,- cameraOrbit,-}) {- const attrs = [- `src="${attr(src)}"`,- 'camera-controls',- 'shadow-intensity="1"',- 'exposure="1"',- 'tone-mapping="aces"',- autoRotate ? 'auto-rotate' : '',- ar ? 'ar ar-modes="webxr scene-viewer quick-look"' : '',- poster ? `poster="${attr(poster)}"` : '',- cameraOrbit ? `camera-orbit="${attr(cameraOrbit)}"` : '',- `alt="${attr(name || 'Avatar')}"`,- ]- .filter(Boolean)- .join(' ');- return [- '<!doctype html>',- '<html><head><meta charset="utf-8"><title>' + esc(name || 'Avatar') + '</title>',- '<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/4.0.0/model-viewer.min.js"></script>',- '<style>html,body{margin:0;height:100%;background:' + attr(background) + '}',- 'model-viewer{width:' +- attr(width) +- ';height:' +- attr(height) +- ';--progress-bar-color:#6a5cff}</style>',- '</head><body>',- '<model-viewer ' + attrs + '></model-viewer>',- '</body></html>',- ].join('\n');-}--function formatAvatarList(avatars, { public: isPublic = false } = {}) {- if (!avatars.length) return 'No avatars found.';- return avatars- .map((a) => {- const url = a.model_url ? ` — ${a.model_url}` : '';- const vis = isPublic ? '' : ` [${a.visibility}]`;- return `• ${a.name} (slug: ${a.slug}, id: ${a.id})${vis}${url}`;- })- .join('\n');-}--// ── SSE / DELETE (session lifecycle) ────────────────────────────────────────-async function handleSse(req, res) {- // We don't hold long-lived server→client subscriptions yet; respond politely.- const bearer = extractBearer(req);- // Unauthenticated callers without an X-PAYMENT header get an x402 challenge- // so x402scan / x402 clients can discover the price. Invalid bearers still- // get 401 with WWW-Authenticate so OAuth clients can re-auth correctly.- if (!bearer && !req.headers['x-payment']) {- return send402(- res,- paymentRequirements({- resource: resolveResourceUrl(req, '/api/mcp'),- description: 'MCP tool call',- }),- );- }- const auth = await authenticateBearer(bearer, { audience: env.MCP_RESOURCE });- if (!auth) return send401(res, 'missing or invalid access token');- res.statusCode = 405;- res.setHeader('allow', 'POST, DELETE');- res.end();-}--async function handleTerminate(_req, res) {- // Stateless per-request server — nothing to tear down.- res.statusCode = 204;- res.end();-}--// ── error helpers ────────────────────────────────────────────────────────────-function quoteString(s) {- return `"${String(s).replace(/[\\"]/g, '\\$&')}"`;-}--function send401(res, msg) {- const resource = env.MCP_RESOURCE;- res.statusCode = 401;- res.setHeader(- 'www-authenticate',- `Bearer resource_metadata=${quoteString(`${env.APP_ORIGIN}/.well-known/oauth-protected-resource`)}, resource=${quoteString(resource)}`,- );- res.setHeader('content-type', 'application/json; charset=utf-8');- res.end(JSON.stringify({ error: 'unauthorized', error_description: msg }));-}--function sendJsonRpcError(res, id, code, message, data) {- res.statusCode = 200;- res.setHeader('content-type', 'application/json; charset=utf-8');- res.end(JSON.stringify({ jsonrpc: '2.0', id: id ?? null, error: { code, message, data } }));-}--// ── misc ─────────────────────────────────────────────────────────────────────-function summarize(args) {- const o = {};- for (const [k, v] of Object.entries(args || {})) {- o[k] = typeof v === 'string' && v.length > 64 ? v.slice(0, 64) + '…' : v;- }- return o;-}--function esc(s) {- return String(s).replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' })[c]);-}-function attr(s) {- return String(s).replace(- /[&<>"]/g,- (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c],- );-}--// CSS inputs land inside a <style> declaration (`background: <value>`), where-// HTML attribute-escaping does not defend against `;}body{…}` breakouts. Only-// allow a strict character class that cannot terminate the declaration/rule.-function safeCssValue(s, fallback) {- if (!s) return fallback;- const str = String(s).trim();- if (!/^[a-zA-Z0-9 .,%#()\-_/+]+$/.test(str)) return fallback;- if (str.length > 120) return fallback;- return str;-}--function safeCssLength(s, fallback) {- if (!s) return fallback;- const str = String(s).trim();- if (!/^[0-9]+(?:\.[0-9]+)?(?:px|em|rem|vh|vw|%)$|^auto$|^100%$/.test(str)) return fallback;- return str;-}--// Posters are rendered as an attribute value that the browser fetches; restrict-// to https(:) to block `javascript:` and `data:` URLs that could execute code.-function safeHttpsUrl(s) {- if (!s) return undefined;- try {- const u = new URL(String(s));- return u.protocol === 'https:' ? u.toString() : undefined;- } catch {- return undefined;- }-}diff --git a/solana-agent-sdk/jest.config.ts b/solana-agent-sdk/jest.config.tsindex d93660c52..03baeb269 100644--- a/solana-agent-sdk/jest.config.ts+++ b/solana-agent-sdk/jest.config.ts@@ -8,7 +8,10 @@ const config: Config = {transform: {"^.+\\.ts$": ["ts-jest", { useESM: true, tsconfig: "./tsconfig.test.json" }],},- testMatch: ["<rootDir>/tests/wallet/**/*.test.ts"],+ testMatch: [+ "<rootDir>/tests/wallet/**/*.test.ts",+ "<rootDir>/tests/actions/**/*.test.ts",+ ],};export default config;diff --git a/solana-agent-sdk/src/solana-agent-kit/actions.ts b/solana-agent-sdk/src/solana-agent-kit/actions.tsindex 64c631978..d4552d3a8 100644--- a/solana-agent-sdk/src/solana-agent-kit/actions.ts+++ b/solana-agent-sdk/src/solana-agent-kit/actions.ts@@ -274,7 +274,7 @@ export const stakeSolAction: Action = {{input: {voteAccount: "Vote111111111111111111111111111111111111111",- amount: "1",+ amount: 1,},output: {signature: "5K4b...txSig",@@ -350,15 +350,7 @@ export const getStakeAccountsAction: Action = {{input: {},output: {- accounts: JSON.stringify([- {- address: "Ek5G...acct",- lamports: 1002282880,- state: "delegated",- voteAccount: "Vote111111111111111111111111111111111111111",- activationEpoch: 700,- },- ]),+ accounts: '[{"address":"Ek5G...acct","lamports":1002282880,"state":"delegated","voteAccount":"Vote111111111111111111111111111111111111111","activationEpoch":700}]',},explanation: "List all stake accounts for the agent wallet.",},diff --git a/solana-agent-sdk/tests/actions/ata.test.ts b/solana-agent-sdk/tests/actions/ata.test.tsindex 98a4fa840..df2db8bd6 100644--- a/solana-agent-sdk/tests/actions/ata.test.ts+++ b/solana-agent-sdk/tests/actions/ata.test.ts@@ -1,52 +1,47 @@import { jest, describe, it, expect, beforeEach } from "@jest/globals";-import {- Keypair,- PublicKey,- type Connection,-} from "@solana/web3.js";-import {- getAssociatedTokenAddressSync,- TOKEN_PROGRAM_ID,- ASSOCIATED_TOKEN_PROGRAM_ID,-} from "@solana/spl-token";-import { getOrCreateAta } from "../../src/actions/ata.js";-import { buildAndSend } from "../../src/tx/build.js";--jest.mock("../../src/tx/build.js", () => ({ buildAndSend: jest.fn() }));--const mockBuildAndSend = jest.mocked(buildAndSend);--// Deterministic test keys+import type { PublicKey, AccountInfo, Connection } from "@solana/web3.js";+import type { WalletProvider } from "../../src/wallet/types.js";++const mockBuildAndSendFn = jest.fn<(...args: any[]) => Promise<string>>();++jest.unstable_mockModule("../../src/tx/build.js", () => ({+ buildAndSend: mockBuildAndSendFn,+}));++const { getOrCreateAta } = await import("../../src/actions/ata.js");+const { Keypair } = await import("@solana/web3.js") as typeof import("@solana/web3.js");+const { getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } =+ await import("@solana/spl-token") as typeof import("@solana/spl-token");+const WALLET_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(1)).publicKey;const CUSTOM_OWNER = Keypair.fromSeed(new Uint8Array(32).fill(2)).publicKey;const MINT_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(3)).publicKey;-const FAKE_ACCOUNT = { data: Buffer.alloc(0), executable: false, lamports: 1, owner: TOKEN_PROGRAM_ID } as any;+const FAKE_ACCOUNT = { data: Buffer.alloc(0), executable: false, lamports: 1, owner: TOKEN_PROGRAM_ID };const wallet = {publicKey: WALLET_PUBKEY,signTransaction: jest.fn(),signAndSendTransaction: jest.fn(),-};+} as unknown as WalletProvider;function makeConnection(ataExists: boolean) {- return {- getAccountInfo: jest.fn().mockResolvedValue(ataExists ? FAKE_ACCOUNT : null),- } as unknown as Connection;+ const getAccountInfo = jest.fn<(pubkey: PublicKey) => Promise<AccountInfo<Buffer> | null>>();+ getAccountInfo.mockResolvedValue(ataExists ? (FAKE_ACCOUNT as unknown as AccountInfo<Buffer>) : null);+ return { getAccountInfo } as unknown as Connection;}beforeEach(() => {jest.clearAllMocks();- mockBuildAndSend.mockResolvedValue("fakeSig");+ mockBuildAndSendFn.mockResolvedValue("fakeSig");});describe("getOrCreateAta", () => {it("returns { ata, signature: undefined } when ATA already exists", async () => {- const connection = makeConnection(true);- const result = await getOrCreateAta(wallet, connection, { mint: MINT_PUBKEY });+ const result = await getOrCreateAta(wallet, makeConnection(true), { mint: MINT_PUBKEY });expect(result.signature).toBeUndefined();- expect(mockBuildAndSend).not.toHaveBeenCalled();+ expect(mockBuildAndSendFn).not.toHaveBeenCalled();const expectedAta = getAssociatedTokenAddressSync(MINT_PUBKEY, WALLET_PUBKEY, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,@@ -55,25 +50,22 @@ describe("getOrCreateAta", () => {});it("calls buildAndSend and returns signature when ATA does not exist", async () => {- const connection = makeConnection(false);- const result = await getOrCreateAta(wallet, connection, { mint: MINT_PUBKEY });+ const result = await getOrCreateAta(wallet, makeConnection(false), { mint: MINT_PUBKEY });- expect(mockBuildAndSend).toHaveBeenCalledTimes(1);+ expect(mockBuildAndSendFn).toHaveBeenCalledTimes(1);expect(result.signature).toBe("fakeSig");});it("sends createAssociatedTokenAccountInstruction when ATA does not exist", async () => {- const connection = makeConnection(false);- await getOrCreateAta(wallet, connection, { mint: MINT_PUBKEY });+ await getOrCreateAta(wallet, makeConnection(false), { mint: MINT_PUBKEY });- const [, , instructions] = mockBuildAndSend.mock.calls[0];+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;expect(instructions).toHaveLength(1);- expect(instructions[0].programId.equals(ASSOCIATED_TOKEN_PROGRAM_ID)).toBe(true);+ expect(instructions[0]!.programId.equals(ASSOCIATED_TOKEN_PROGRAM_ID)).toBe(true);});it("uses wallet.publicKey as owner by default", async () => {- const connection = makeConnection(false);- const result = await getOrCreateAta(wallet, connection, { mint: MINT_PUBKEY });+ const result = await getOrCreateAta(wallet, makeConnection(true), { mint: MINT_PUBKEY });const expectedAta = getAssociatedTokenAddressSync(MINT_PUBKEY, WALLET_PUBKEY, false, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID,@@ -82,10 +74,8 @@ describe("getOrCreateAta", () => {});it("uses provided owner instead of wallet.publicKey", async () => {- const connection = makeConnection(false);- const result = await getOrCreateAta(wallet, connection, {- mint: MINT_PUBKEY,- owner: CUSTOM_OWNER,+ const result = await getOrCreateAta(wallet, makeConnection(true), {+ mint: MINT_PUBKEY, owner: CUSTOM_OWNER,});const expectedAta = getAssociatedTokenAddressSync(diff --git a/solana-agent-sdk/tests/actions/swap.test.ts b/solana-agent-sdk/tests/actions/swap.test.tsindex 781655e5b..56ad5cfab 100644--- a/solana-agent-sdk/tests/actions/swap.test.ts+++ b/solana-agent-sdk/tests/actions/swap.test.ts@@ -1,13 +1,12 @@import { jest, describe, it, expect, beforeEach, afterEach } from "@jest/globals";-import {- Keypair,- TransactionMessage,- VersionedTransaction,- type Connection,-} from "@solana/web3.js";-import { jupiterSwap, getSwapQuote, SOL_MINT } from "../../src/actions/swap.js";-import { SwapError } from "../../src/errors.js";-import type { MetaAwareWallet, WalletProvider } from "../../src/wallet/types.js";+import type { WalletProvider, MetaAwareWallet } from "../../src/wallet/types.js";++let mockFetch = jest.fn<(url: RequestInfo | URL, init?: RequestInit) => Promise<Response>>();++const { jupiterSwap, getSwapQuote, SOL_MINT } = await import("../../src/actions/swap.js");+const { SwapError } = await import("../../src/errors.js");+const { Keypair, TransactionMessage, VersionedTransaction } =+ await import("@solana/web3.js") as typeof import("@solana/web3.js");const WALLET_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(1)).publicKey;@@ -38,18 +37,13 @@ function makeWallet(): WalletProvider {}function makeMetaWallet(): MetaAwareWallet {- return {- ...makeWallet(),- setNextMeta: jest.fn(),- } as unknown as MetaAwareWallet;+ return { ...makeWallet(), setNextMeta: jest.fn() } as unknown as MetaAwareWallet;}-const connection = {} as unknown as Connection;--let mockFetch: jest.Mock<() => Promise<Response>>;+const connection = {} as never;beforeEach(() => {- mockFetch = jest.fn<() => Promise<Response>>();+ mockFetch = jest.fn<(url: RequestInfo | URL, init?: RequestInit) => Promise<Response>>();global.fetch = mockFetch as unknown as typeof fetch;});@@ -59,10 +53,7 @@ afterEach(() => {describe("getSwapQuote", () => {it("constructs quote URL with inputMint, outputMint, amount, and slippageBps", async () => {- mockFetch.mockResolvedValue({- ok: true,- json: async () => MOCK_QUOTE,- } as unknown as Response);+ mockFetch.mockResolvedValue({ ok: true, json: async () => MOCK_QUOTE } as unknown as Response);await getSwapQuote({inputMint: SOL_MINT,@@ -72,7 +63,8 @@ describe("getSwapQuote", () => {});expect(mockFetch).toHaveBeenCalledTimes(1);- const url = new URL(mockFetch.mock.calls[0]![0] as string);+ const call = mockFetch.mock.calls[0]!;+ const url = new URL(call[0] as string);expect(url.searchParams.get("inputMint")).toBe(SOL_MINT);expect(url.searchParams.get("outputMint")).toBe("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");expect(url.searchParams.get("amount")).toBe("1000000000");@@ -104,12 +96,12 @@ describe("jupiterSwap", () => {.mockResolvedValueOnce({ ok: true, json: async () => ({ swapTransaction: MOCK_TX_BASE64 }) } as unknown as Response);await jupiterSwap(makeWallet(), connection, {- inputMint: SOL_MINT,- outputMint: SOL_MINT,- amount: 1_000_000_000n,+ inputMint: SOL_MINT, outputMint: SOL_MINT, amount: 1_000_000_000n,});- const [swapUrl, swapInit] = mockFetch.mock.calls[1]! as [string, RequestInit];+ const swapCall = mockFetch.mock.calls[1]!;+ const swapUrl = swapCall[0] as string;+ const swapInit = swapCall[1] as RequestInit;expect(swapUrl).toContain("/swap");const body = JSON.parse(swapInit.body as string) as Record<string, unknown>;expect(body["quoteResponse"]).toEqual(MOCK_QUOTE);@@ -125,32 +117,26 @@ describe("jupiterSwap", () => {await expect(jupiterSwap(makeWallet(), connection, {- inputMint: SOL_MINT,- outputMint: SOL_MINT,- amount: 1n,+ inputMint: SOL_MINT, outputMint: SOL_MINT, amount: 1n,}),).rejects.toThrow(SwapError);});- it("calls setNextMeta on MetaAwareWallet with swap metadata", async () => {+ it("calls setNextMeta on MetaAwareWallet with swap metadata including kind: 'swap'", async () => {mockFetch.mockResolvedValueOnce({ ok: true, json: async () => MOCK_QUOTE } as unknown as Response).mockResolvedValueOnce({ ok: true, json: async () => ({ swapTransaction: MOCK_TX_BASE64 }) } as unknown as Response);const w = makeMetaWallet();await jupiterSwap(w, connection, {- inputMint: SOL_MINT,- outputMint: SOL_MINT,- amount: 1_000_000_000n,- inputSymbol: "SOL",- outputSymbol: "SOL",+ inputMint: SOL_MINT, outputMint: SOL_MINT, amount: 1_000_000_000n,});expect(w.setNextMeta).toHaveBeenCalledTimes(1);- const meta = (w.setNextMeta as jest.Mock).mock.calls[0]![0] as { kind: string; amountIn: unknown; amountOut: unknown };- expect(meta.kind).toBe("swap");- expect(meta.amountIn).toBeDefined();- expect(meta.amountOut).toBeDefined();+ const meta = (w.setNextMeta as jest.Mock).mock.calls[0]![0] as Record<string, unknown>;+ expect(meta["kind"]).toBe("swap");+ expect(meta["amountIn"]).toBeDefined();+ expect(meta["amountOut"]).toBeDefined();});it("does not throw when wallet does not implement setNextMeta", async () => {@@ -160,9 +146,7 @@ describe("jupiterSwap", () => {await expect(jupiterSwap(makeWallet(), connection, {- inputMint: SOL_MINT,- outputMint: SOL_MINT,- amount: 1n,+ inputMint: SOL_MINT, outputMint: SOL_MINT, amount: 1n,}),).resolves.toBeDefined();});diff --git a/solana-agent-sdk/tests/actions/transfer-sol.test.ts b/solana-agent-sdk/tests/actions/transfer-sol.test.tsindex f22057ca3..abafc976d 100644--- a/solana-agent-sdk/tests/actions/transfer-sol.test.ts+++ b/solana-agent-sdk/tests/actions/transfer-sol.test.ts@@ -1,18 +1,15 @@import { jest, describe, it, expect, beforeEach } from "@jest/globals";-import {- Keypair,- LAMPORTS_PER_SOL,- SystemInstruction,- SystemProgram,- type Connection,-} from "@solana/web3.js";-import { transferSol } from "../../src/actions/transfer-sol.js";-import { buildAndSend } from "../../src/tx/build.js";-import type { TxMetadata, WalletProvider } from "../../src/wallet/types.js";+import type { WalletProvider, TxMetadata } from "../../src/wallet/types.js";-jest.mock("../../src/tx/build.js", () => ({ buildAndSend: jest.fn() }));+const mockBuildAndSendFn = jest.fn<(...args: any[]) => Promise<string>>();-const mockBuildAndSend = jest.mocked(buildAndSend);+jest.unstable_mockModule("../../src/tx/build.js", () => ({+ buildAndSend: mockBuildAndSendFn,+}));++const { transferSol } = await import("../../src/actions/transfer-sol.js");+const { Keypair, LAMPORTS_PER_SOL, SystemInstruction, SystemProgram } =+ await import("@solana/web3.js") as typeof import("@solana/web3.js");const WALLET_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(1)).publicKey;const RECIPIENT_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(2)).publicKey;@@ -23,18 +20,18 @@ const wallet = {signAndSendTransaction: jest.fn(),} as unknown as WalletProvider;-const connection = {} as unknown as Connection;+const connection = {} as never;beforeEach(() => {jest.clearAllMocks();- mockBuildAndSend.mockResolvedValue("mockSig");+ mockBuildAndSendFn.mockResolvedValue("mockSig");});describe("transferSol", () => {it("converts amount to correct lamports (0.5 SOL → 500_000_000)", async () => {await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY, amount: 0.5 });- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;const decoded = SystemInstruction.decodeTransfer(instructions[0]!);expect(Number(decoded.lamports)).toBe(0.5 * LAMPORTS_PER_SOL);});@@ -42,7 +39,7 @@ describe("transferSol", () => {it("builds SystemProgram.transfer with correct pubkeys", async () => {await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY, amount: 1 });- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;const ix = instructions[0]!;expect(ix.programId.equals(SystemProgram.programId)).toBe(true);@@ -54,7 +51,7 @@ describe("transferSol", () => {it("auto-generates meta label containing amount and 'SOL'", async () => {await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY, amount: 2.5 });- const [, , , opts] = mockBuildAndSend.mock.calls[0]!;+ const [, , , opts] = mockBuildAndSendFn.mock.calls[0]!;expect(opts?.meta?.label).toContain("2.5");expect(opts?.meta?.label).toContain("SOL");});@@ -62,14 +59,14 @@ describe("transferSol", () => {it("auto-generated meta has kind 'transfer'", async () => {await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY, amount: 1 });- const [, , , opts] = mockBuildAndSend.mock.calls[0]!;+ const [, , , opts] = mockBuildAndSendFn.mock.calls[0]!;expect(opts?.meta?.kind).toBe("transfer");});it("accepts to as a base58 string", async () => {await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY.toBase58(), amount: 1 });- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;const decoded = SystemInstruction.decodeTransfer(instructions[0]!);expect(decoded.toPubkey.equals(RECIPIENT_PUBKEY)).toBe(true);});@@ -78,7 +75,7 @@ describe("transferSol", () => {const customMeta: TxMetadata = { label: "Custom label", kind: "custom" };await transferSol(wallet, connection, { to: RECIPIENT_PUBKEY, amount: 1 }, { meta: customMeta });- const [, , , opts] = mockBuildAndSend.mock.calls[0]!;+ const [, , , opts] = mockBuildAndSendFn.mock.calls[0]!;expect(opts?.meta).toBe(customMeta);});});diff --git a/solana-agent-sdk/tests/actions/transfer-spl.test.ts b/solana-agent-sdk/tests/actions/transfer-spl.test.tsindex adb27dc35..6d7a43376 100644--- a/solana-agent-sdk/tests/actions/transfer-spl.test.ts+++ b/solana-agent-sdk/tests/actions/transfer-spl.test.ts@@ -1,30 +1,24 @@import { jest, describe, it, expect, beforeEach } from "@jest/globals";-import {- Keypair,- PublicKey,- type AccountInfo,- type Connection,-} from "@solana/web3.js";-import {- getAssociatedTokenAddressSync,- getMint,- TOKEN_PROGRAM_ID,- ASSOCIATED_TOKEN_PROGRAM_ID,-} from "@solana/spl-token";-import { transferSpl } from "../../src/actions/transfer-spl.js";-import { buildAndSend } from "../../src/tx/build.js";-import { MissingTokenAccountError } from "../../src/errors.js";+import type { PublicKey, AccountInfo, Connection } from "@solana/web3.js";import type { WalletProvider } from "../../src/wallet/types.js";+import { MissingTokenAccountError } from "../../src/errors.js";-jest.mock("../../src/tx/build.js", () => ({ buildAndSend: jest.fn() }));+const mockBuildAndSendFn = jest.fn<(...args: any[]) => Promise<string>>();+const mockGetMintFn = jest.fn<(...args: any[]) => Promise<any>>();-jest.mock("@solana/spl-token", () => ({- ...jest.requireActual("@solana/spl-token"),- getMint: jest.fn(),+jest.unstable_mockModule("../../src/tx/build.js", () => ({+ buildAndSend: mockBuildAndSendFn,}));-const mockBuildAndSend = jest.mocked(buildAndSend);-const mockGetMint = jest.mocked(getMint);+jest.unstable_mockModule("@solana/spl-token", async () => {+ const actual = await import("@solana/spl-token");+ return { ...(actual as object), getMint: mockGetMintFn };+});++const { transferSpl } = await import("../../src/actions/transfer-spl.js");+const { Keypair } = await import("@solana/web3.js") as typeof import("@solana/web3.js");+const { getAssociatedTokenAddressSync, TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } =+ await import("@solana/spl-token") as typeof import("@solana/spl-token");const WALLET_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(1)).publicKey;const RECIPIENT_PUBKEY = Keypair.fromSeed(new Uint8Array(32).fill(2)).publicKey;@@ -39,6 +33,10 @@ const RECEIVER_ATA = getAssociatedTokenAddressSync(const FAKE_ACCOUNT = { data: Buffer.alloc(0), executable: false, lamports: 1, owner: TOKEN_PROGRAM_ID };+const MINT_INFO = {+ decimals: 6, mintAuthority: null, supply: 0n, freezeAuthority: null, isInitialized: true,+};+function makeConnection(senderExists: boolean, receiverExists: boolean) {const getAccountInfo = jest.fn<(pubkey: PublicKey) => Promise<AccountInfo<Buffer> | null>>();getAccountInfo.mockImplementation(async (pubkey) => {@@ -49,83 +47,64 @@ function makeConnection(senderExists: boolean, receiverExists: boolean) {return { getAccountInfo } as unknown as Connection;}-function makeWallet(publicKey = WALLET_PUBKEY): WalletProvider {+function makeWallet() {return {- publicKey,+ publicKey: WALLET_PUBKEY,signTransaction: jest.fn(),signAndSendTransaction: jest.fn(),} as unknown as WalletProvider;}-const MINT_INFO = {- decimals: 6, mintAuthority: null, supply: 0n, freezeAuthority: null, isInitialized: true,-};-beforeEach(() => {jest.clearAllMocks();- mockBuildAndSend.mockResolvedValue("mockSig");- mockGetMint.mockResolvedValue(MINT_INFO as ReturnType<typeof getMint> extends Promise<infer T> ? T : never);+ mockBuildAndSendFn.mockResolvedValue("mockSig");+ mockGetMintFn.mockResolvedValue(MINT_INFO);});describe("transferSpl", () => {it("adds createAssociatedTokenAccountInstruction when receiver ATA is missing", async () => {- const connection = makeConnection(true, false);- await transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_000_000n,+ await transferSpl(makeWallet(), makeConnection(true, false), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_000_000n,});- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;expect(instructions).toHaveLength(2);expect(instructions[0]!.programId.equals(ASSOCIATED_TOKEN_PROGRAM_ID)).toBe(true);});it("skips createAssociatedTokenAccountInstruction when receiver ATA exists", async () => {- const connection = makeConnection(true, true);- await transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_000_000n,+ await transferSpl(makeWallet(), makeConnection(true, true), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_000_000n,});- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;expect(instructions).toHaveLength(1);expect(instructions[0]!.programId.equals(TOKEN_PROGRAM_ID)).toBe(true);});it("throws MissingTokenAccountError when sender ATA is missing", async () => {- const connection = makeConnection(false, true);await expect(- transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_000_000n,+ transferSpl(makeWallet(), makeConnection(false, true), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_000_000n,}),).rejects.toThrow(MissingTokenAccountError);});- it("throws error message containing 'no token account' when sender ATA is missing", async () => {- const connection = makeConnection(false, true);+ it("throws error containing 'no token account' when sender ATA is missing", async () => {await expect(- transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_000_000n,+ transferSpl(makeWallet(), makeConnection(false, true), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_000_000n,}),).rejects.toThrow(/no token account/i);});it("builds TransferChecked instruction with correct keys", async () => {- const connection = makeConnection(true, true);- await transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_000_000n,+ await transferSpl(makeWallet(), makeConnection(true, true), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_000_000n,});- const [, , instructions] = mockBuildAndSend.mock.calls[0]!;- const ix = instructions[0]!; // only instruction when receiver ATA exists+ const [, , instructions] = mockBuildAndSendFn.mock.calls[0]!;+ const ix = instructions[0]!;// TransferChecked keys: source, mint, destination, authorityexpect(ix.keys[0]!.pubkey.equals(SENDER_ATA)).toBe(true);expect(ix.keys[1]!.pubkey.equals(MINT_PUBKEY)).toBe(true);@@ -134,16 +113,12 @@ describe("transferSpl", () => {});it("computes uiAmount without BigInt precision loss for safe integers", async () => {- const connection = makeConnection(true, true);// 1_500_000 base units at 6 decimals = 1.5- await transferSpl(makeWallet(), connection, {- mint: MINT_PUBKEY,- to: RECIPIENT_PUBKEY,- amount: 1_500_000n,- symbol: "USDC",+ await transferSpl(makeWallet(), makeConnection(true, true), {+ mint: MINT_PUBKEY, to: RECIPIENT_PUBKEY, amount: 1_500_000n, symbol: "USDC",});- const [, , , opts] = mockBuildAndSend.mock.calls[0]!;+ const [, , , opts] = mockBuildAndSendFn.mock.calls[0]!;expect(opts?.meta?.amountIn?.uiAmount).toBe("1.5");});});diff --git a/solana-agent-sdk/tests/wallet/keypair.test.ts b/solana-agent-sdk/tests/wallet/keypair.test.tsindex 649f335c0..59c3e10ef 100644--- a/solana-agent-sdk/tests/wallet/keypair.test.ts+++ b/solana-agent-sdk/tests/wallet/keypair.test.ts@@ -1,18 +1,8 @@-// jest.mock() is not hoisted in ESM mode; use jest.unstable_mockModule + dynamic imports instead.import { describe, it, expect, jest } from "@jest/globals";--const mockSendAndConfirmFn = jest.fn().mockImplementation(() => Promise.resolve("fakeSig"));--jest.unstable_mockModule("@solana/web3.js", async () => {- const actual = await import("@solana/web3.js");- return { ...(actual as object), sendAndConfirmTransaction: mockSendAndConfirmFn };-});--// All imports of mocked modules must be dynamic and come AFTER unstable_mockModule.-const { Keypair, Transaction, VersionedTransaction, TransactionMessage } =- await import("@solana/web3.js") as typeof import("@solana/web3.js");-const { default: bs58 } = await import("bs58");-const { KeypairWalletProvider } = await import("../../src/wallet/keypair.js");+import { Keypair, Transaction, VersionedTransaction, TransactionMessage } from "@solana/web3.js";+import type { Connection } from "@solana/web3.js";+import bs58 from "bs58";+import { KeypairWalletProvider } from "../../src/wallet/keypair.js";describe("KeypairWalletProvider", () => {const keypair = Keypair.generate();@@ -60,18 +50,30 @@ describe("KeypairWalletProvider", () => {});it("signAndSendTransaction calls sendAndConfirmTransaction with the keypair", async () => {- mockSendAndConfirmFn.mockClear();const provider = new KeypairWalletProvider(keypair.secretKey);const tx = new Transaction();tx.recentBlockhash = "11111111111111111111111111111111";tx.feePayer = keypair.publicKey;- const connection = {} as Parameters<- typeof import("@solana/web3.js").sendAndConfirmTransaction- >[0];++ // Minimal connection mock: sendAndConfirmTransaction calls these two methods.+ const fakeSig = "fakeSig";+ const sendTransaction = jest.fn().mockImplementation(() => Promise.resolve(fakeSig));+ const confirmTransaction = jest+ .fn()+ .mockImplementation(() => Promise.resolve({ value: { err: null } }));+ const connection = { sendTransaction, confirmTransaction } as unknown as Connection;const result = await provider.signAndSendTransaction(tx, connection);- expect(result).toBe("fakeSig");- expect(mockSendAndConfirmFn).toHaveBeenCalledWith(connection, tx, expect.any(Array));+ expect(result).toBe(fakeSig);+ // Verify the keypair was included in the signers array passed to sendTransaction.+ expect(sendTransaction).toHaveBeenCalledWith(+ tx,+ expect.arrayContaining([+ expect.objectContaining({ publicKey: keypair.publicKey }),+ ]),+ // sendOptions is undefined when no ConfirmOptions are passed+ undefined,+ );});});diff --git a/tests/api/auth-email.test.js b/tests/api/auth-email.test.jsindex 346cb4146..5cc736070 100644--- a/tests/api/auth-email.test.js+++ b/tests/api/auth-email.test.js@@ -61,6 +61,7 @@ vi.mock('../../api/_lib/email.js', () => ({}));const { default: handler } = await import('../../api/auth/[action].js');+const authMod = await import('../../api/_lib/auth.js');// ── Helpers ───────────────────────────────────────────────────────────────────@@ -125,8 +126,8 @@ beforeEach(() => {rlState.success = true;authMock.verifyPassword = true;authMock.sessionToken = 'test-session-token-abc';- vi.mocked((await import('../../api/_lib/auth.js')).destroySession).mockClear();- vi.mocked((await import('../../api/_lib/auth.js')).createSession).mockClear();+ vi.mocked(authMod.destroySession).mockClear();+ vi.mocked(authMod.createSession).mockClear();});// ── login ──────────────────────────────────────────────────────────────────────diff --git a/tests/api/siwe-handler.test.js b/tests/api/siwe-handler.test.jsindex e1e4da5b4..1c72b9a92 100644--- a/tests/api/siwe-handler.test.js+++ b/tests/api/siwe-handler.test.js@@ -201,7 +201,7 @@ describe('POST /api/auth/siwe/verify', () => {const { status, body, res } = await invoke({action: 'verify',method: 'POST',- body: { message: SIWE_MESSAGE, signature: '0xfakesig' },+ body: { message: SIWE_MESSAGE, signature: `0x${'ab'.repeat(65)}` },headers: withCsrf(),});@@ -218,7 +218,7 @@ describe('POST /api/auth/siwe/verify', () => {const { status, body } = await invoke({action: 'verify',method: 'POST',- body: { message: SIWE_MESSAGE, signature: '0xfakesig' },+ body: { message: SIWE_MESSAGE, signature: `0x${'ab'.repeat(65)}` },headers: withCsrf(),});@@ -232,7 +232,7 @@ describe('POST /api/auth/siwe/verify', () => {const { status, body } = await invoke({action: 'verify',method: 'POST',- body: { message: SIWE_MESSAGE, signature: '0xfakesig' },+ body: { message: SIWE_MESSAGE, signature: `0x${'ab'.repeat(65)}` },headers: withCsrf(),});@@ -244,7 +244,7 @@ describe('POST /api/auth/siwe/verify', () => {const { status, body } = await invoke({action: 'verify',method: 'POST',- body: { message: SIWE_MESSAGE, signature: '0xfakesig' },+ body: { message: SIWE_MESSAGE, signature: `0x${'ab'.repeat(65)}` },});expect(status).toBe(403);expect(body.error).toBe('invalid_request');@@ -257,7 +257,7 @@ describe('POST /api/auth/siwe/verify', () => {const { status, body } = await invoke({action: 'verify',method: 'POST',- body: { message: SIWE_MESSAGE, signature: '0xfakesig' },+ body: { message: SIWE_MESSAGE, signature: `0x${'ab'.repeat(65)}` },headers: withCsrf({ 'x-csrf-token': 'wrong-csrf-token' }),});expect(status).toBe(403);