Skip to content

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.

fix(mcp): bind domain registrant contact to the quote and cap persona spend per day across sessions

nirholas82a1eae32306
diff --git a/api/_lib/persona-spend-ledger.js b/api/_lib/persona-spend-ledger.js
index edf1488a8..7bffab504 100644
--- a/api/_lib/persona-spend-ledger.js
+++ b/api/_lib/persona-spend-ledger.js
@@ -162,7 +162,11 @@ export async function checkPersonaSpend({ personaId, sessionId, usdc }) {
cap_usdc: PERSONA_SPEND_CAPS.maxPerCallUsdc,
};
}
- const spent = await sessionSpentUsdc(personaId, sessionId);
+ // The session id is caller-chosen, so a fresh one must not reset the cap:
+ // every spend is also booked to the persona's UTC-day bucket, and that
+ // bucket is held to the same ceiling whatever session_id the caller sends.
+ const daily = await sessionSpentUsdc(personaId, defaultSessionId(personaId));
+ const spent = Math.max(await sessionSpentUsdc(personaId, sessionId), daily);
if (spent + amount > PERSONA_SPEND_CAPS.maxPerSessionUsdc) {
return {
ok: false,
@@ -178,6 +182,8 @@ export async function checkPersonaSpend({ personaId, sessionId, usdc }) {
/** Record a SETTLED spend (call only after the on-chain transfer confirms). */
export async function recordPersonaSpend({ personaId, sessionId, usdc, tool, toAddress, signature }) {
const row = { personaId, sessionId, usdc: Number(usdc), tool, toAddress, signature };
- if (dbConfigured()) return dbRecordSpend(row);
- return fsRecordSpend(row);
+ const record = dbConfigured() ? dbRecordSpend : fsRecordSpend;
+ await record(row);
+ const dailyId = defaultSessionId(personaId);
+ if (sessionId !== dailyId) await record({ ...row, sessionId: dailyId });
}
diff --git a/packages/mcp-policy/src/table.js b/packages/mcp-policy/src/table.js
index 37757d6c2..5a36bc591 100644
--- a/packages/mcp-policy/src/table.js
+++ b/packages/mcp-policy/src/table.js
@@ -166,7 +166,7 @@ export const POLICY = {
domain_check: r('domains'),
domain_pricing: r('domains'),
domain_register_quote: w('domains'),
- domain_register: f('domains', 'confirm_spend', 'domain_register_quote', ['domain', 'auto_renew', 'privacy']),
+ domain_register: f('domains', 'confirm_spend', 'domain_register_quote', ['domain', 'contact', 'auto_renew', 'privacy']),
domain_status: r('domains'),
domain_connect: w('domains'),
domain_connect_status: r('domains'),
diff --git a/tests/mcp-domain-register-contact-bind.test.js b/tests/mcp-domain-register-contact-bind.test.js
new file mode 100644
index 000000000..6a582bad5
--- /dev/null
+++ b/tests/mcp-domain-register-contact-bind.test.js
@@ -0,0 +1,34 @@
+import { describe, it, expect } from 'vitest';
+import { createPolicy, allowListEnablement } from '@three-ws/mcp-policy';
+
+const contact = (email) => ({ name: 'A Owner', email, phone: '+14155550100', address: '1 Main', city: 'SF', postal_code: '94105', country: 'US' });
+const quoteArgs = { domain: 'orbit.app', contact: contact('[email protected]'), auto_renew: false, privacy: 'REDACTED_CONTACT_DATA' };
+
+async function quoted() {
+ const policy = createPolicy({ serverId: 'three.ws' });
+ const en = allowListEnablement(['domain_register_quote', 'domain_register']);
+ const done = await policy.afterCall({ name: 'domain_register_quote', args: quoteArgs, principal: 'u1', result: { content: [] } });
+ const id = done._meta['three.ws/preview'].quote_id;
+ return { policy, en, id };
+}
+
+describe('domain_register binds the registrant contact to the approved quote', () => {
+ it('refuses a call whose contact differs from the quoted one', async () => {
+ const { policy, en, id } = await quoted();
+ const gate = await policy.beforeCall({
+ name: 'domain_register', en, principal: 'u1',
+ args: { ...quoteArgs, contact: contact('[email protected]'), quote_id: id, confirm_spend: true },
+ });
+ expect(gate.ok).toBe(false);
+ expect(gate.result.structuredContent.reason).toBe('preview_mismatch');
+ });
+
+ it('accepts the same contact', async () => {
+ const { policy, en, id } = await quoted();
+ const gate = await policy.beforeCall({
+ name: 'domain_register', en, principal: 'u1',
+ args: { ...quoteArgs, quote_id: id, confirm_spend: true },
+ });
+ expect(gate.ok).toBe(true);
+ });
+});
diff --git a/tests/persona-spend-ledger.test.js b/tests/persona-spend-ledger.test.js
index bcb48d0f1..a3ff8827b 100644
--- a/tests/persona-spend-ledger.test.js
+++ b/tests/persona-spend-ledger.test.js
@@ -61,14 +61,14 @@ describe('persona spend ledger — cumulative per-session cap', () => {
expect(over.code).toBe('over_session_cap');
});
- it('a DIFFERENT session for the same persona has its own independent cap', async () => {
+ it('a DIFFERENT persona has its own independent cap', async () => {
const personaId = 'persona_sessionisolate01';
await recordPersonaSpend({ personaId, sessionId: 'session-x', usdc: PERSONA_SPEND_CAPS.maxPerSessionUsdc, tool: 'persona_send', toAddress: 'A', signature: 'sig-x' });
const exhausted = await checkPersonaSpend({ personaId, sessionId: 'session-x', usdc: 0.01 });
expect(exhausted.ok).toBe(false);
- const freshSession = await checkPersonaSpend({ personaId, sessionId: 'session-y', usdc: 0.01 });
- expect(freshSession.ok).toBe(true);
+ const otherPersona = await checkPersonaSpend({ personaId: 'persona_sessionisolate02', sessionId: 'session-x', usdc: 0.01 });
+ expect(otherPersona.ok).toBe(true);
});
it('sessionSpentUsdc sums exactly what was recorded', async () => {
@@ -86,3 +86,19 @@ describe('persona spend ledger — default session bucketing', () => {
expect(id).toMatch(/^persona_defaultbucket01:\d{4}-\d{2}-\d{2}$/);
});
});
+
+describe('persona spend ledger - cap cannot be reset with a fresh session_id', () => {
+ it('spend recorded under one session counts against another session of the same persona', async () => {
+ const personaId = 'persona_freshsession001';
+ const cap = PERSONA_SPEND_CAPS.maxPerSessionUsdc;
+ const perCall = PERSONA_SPEND_CAPS.maxPerCallUsdc;
+ let spent = 0;
+ while (spent + perCall <= cap + 1e-9) {
+ await recordPersonaSpend({ personaId, sessionId: 'session-a', usdc: perCall, tool: 'persona_send' });
+ spent += perCall;
+ }
+ const gate = await checkPersonaSpend({ personaId, sessionId: 'session-b', usdc: perCall });
+ expect(gate.ok).toBe(false);
+ expect(gate.code).toBe('over_session_cap');
+ });
+});