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
// three.ws Agent — MCP server (Streamable HTTP transport, MCP 2025-06-18).
// "Add a wallet to Claude": discover, pay for, and call x402 services in USDC
// from the signed-in user's own three.ws agent wallet, bounded by spend caps.
// POST /api/mcp-agent — tool calls GET — SSE DELETE — terminate.
// Registered with the MCP Registry as io.github.nirholas/threews-agent.
import { cors, readJson, wrap } from './_lib/http.js';
import { issueSession, trackMcp } from './_lib/mcp-clients.js';
import { limits, clientIp } from './_lib/rate-limit.js';
import { settlePayment, encodePaymentResponseHeader } from './_lib/x402-spec.js';
import { peekCalledTool } from './_lib/mcp-dispatch.js';
import { isDiscoveryOnlyBatch } from './_lib/mcp-batch-price.js';
import { PROTOCOL_VERSION, dispatch, isPublicTool } from './_mcpagent/dispatch.js';
import { AGENT_CHALLENGE } from './_mcpagent/discovery.js';
import {
send401,
sendJsonRpcError,
authenticateRequest,
handleSse,
handleTerminate,
isMcpProtocolClient,
} from './_mcp/auth.js';
import { sendX402Error, reservePaymentProof } from './_mcp/payments.js';
// Every 402/401 challenge this server issues is scoped to its OWN resource.
// Passing neither resourcePath nor challenge left the Agent wallet advertising
// https://three.ws/api/mcp and the main server's description to facilitators,
// and quoted that same wrong resource inside every accepts[] entry, so a paying
// agent signed a payment scoped to an endpoint it never called.
const RESOURCE_PATH = '/api/mcp-agent';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,HEAD,POST,DELETE,OPTIONS', origins: '*' })) return;
if (req.method === 'GET' || req.method === 'HEAD')
return handleSse(req, res, { resourceServer: 'mcp-agent', resourcePath: RESOURCE_PATH, challenge: AGENT_CHALLENGE });
if (req.method === 'DELETE') return handleTerminate(req, res, { resourceServer: 'mcp-agent' });
if (req.method !== 'POST') return send401(res, 'method not supported');
// Read + parse the body before auth so a call to the free public
// getting_started tool can be served without an OAuth token or x402 payment.
// Discovery-only batches (initialize / tools/list / ping) from plain x402
// agents and crawlers are likewise free — but NOT from MCP protocol clients,
// which need the 401 to start their OAuth flow (same gate as /api/mcp).
const body = await readJson(req, 1_000_000);
const { toolName } = peekCalledTool(body);
const result = await authenticateRequest(req, res, {
resourcePath: RESOURCE_PATH,
challenge: AGENT_CHALLENGE,
allowFree:
Boolean(toolName && isPublicTool(toolName)) ||
(isDiscoveryOnlyBatch(body) && !isMcpProtocolClient(req)),
});
if (!result) return;
const { auth, x402Ctx } = result;
const ipRl = await limits.mcpIp(clientIp(req));
if (!ipRl.success)
return sendJsonRpcError(res, null, -32000, 'rate_limited', {
retry_after: Math.ceil((ipRl.reset - Date.now()) / 1000),
});
const userRl = await limits.mcpUser(auth.userId || auth.rateKey || clientIp(req));
if (!userRl.success)
return sendJsonRpcError(res, null, -32000, 'rate_limited', {
retry_after: Math.ceil((userRl.reset - Date.now()) / 1000),
});
const batch = Array.isArray(body) ? body : [body];
if (batch.length > 16) return sendJsonRpcError(res, null, -32600, 'batch too large (max 16)');
// Replay guard (parity with paidEndpoint): hold a single-use lock on the
// signed payment proof across dispatch+settle so a captured/retried X-PAYMENT
// can't re-run the priced tools before the first settle lands. Released in the
// finally; the consumed on-chain nonce blocks any replay thereafter. Fails
// open — see reservePaymentProof.
let releaseProof = async () => {};
if (x402Ctx) {
const guard = await reservePaymentProof(
RESOURCE_PATH,
// authenticateRequest accepts BOTH payment headers (v1 X-PAYMENT and
// the v2 payment-signature dialect); the guard must key on whichever
// one carried the proof or a v2 replay slips past it unclaimed.
req.headers['x-payment'] || req.headers['payment-signature'],
);
if (!guard.ok) {
return sendJsonRpcError(res, null, -32000, 'payment_in_flight', { retry_after: 1 });
}
releaseProof = guard.release;
}
try {
const responses = [];
for (const msg of batch) {
const r = await dispatch(msg, auth, req);
if (r !== null) responses.push(r);
}
// When the caller presented an x402 payment, settle it AFTER the work
// succeeded — same atomic verify → work → settle sequence as /api/mcp. The
// advertised 402 price is only ever charged once useful work was delivered;
// a wholesale failure leaves the payer's signed payload un-broadcast.
if (x402Ctx) {
const anySuccess = responses.some(
(r) => r && !r.error && !(r.result && r.result.isError),
);
if (anySuccess) {
try {
const settled = await settlePayment({ verified: x402Ctx.verified });
res.setHeader('x-payment-response', encodePaymentResponseHeader(settled));
} catch (err) {
return sendX402Error(
res,
{
resourceUrl: x402Ctx.resourceUrl,
accepts: x402Ctx.requirements,
challenge: AGENT_CHALLENGE,
},
err,
);
}
}
}
res.statusCode = 200;
res.setHeader('content-type', 'application/json; charset=utf-8');
res.setHeader('mcp-protocol-version', PROTOCOL_VERSION);
const issuedSessionId = issueSession(res, body);
res.end(JSON.stringify(Array.isArray(body) ? responses : (responses[0] ?? null)));
trackMcp({ surface: 'mcp-agent', req, body, responses, auth, issuedSessionId });
} finally {
await releaseProof();
}
});