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(sdk): recognize the x402 challenge the MCP transport issues over HTTP 401

nirholase439a88e2513
diff --git a/packages/glb-tools/src/http.js b/packages/glb-tools/src/http.js
index 6398a3e53..eafe53910 100644
--- a/packages/glb-tools/src/http.js
+++ b/packages/glb-tools/src/http.js
@@ -96,8 +96,17 @@ export function createHttp(opts = {}) {
const message = payload?.message || payload?.error_description || payload?.detail || `Request failed with ${res.status}`;
const retryAfter = numberOr(res.headers.get('retry-after'), payload?.retry_after);
- if (res.status === 402) {
- throw new PaymentRequiredError(message, { code, status: 402, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
+ // An x402 challenge is not always HTTP 402: the three.ws MCP transport
+ // issues it over 401 (x402Version + accepts in the body, alongside the
+ // OAuth www-authenticate header). Detect the challenge by payload so the
+ // caller always gets a PaymentRequiredError carrying `accepts`.
+ const isX402Challenge = res.status === 402 ||
+ (res.status === 401 && payload != null && payload.x402Version != null && Array.isArray(payload.accepts));
+ if (isX402Challenge) {
+ // x402 bodies put the human-readable reason under `error`, so prefer it
+ // as the message and keep the stable `payment_required` code.
+ const challengeMessage = typeof payload?.error === 'string' && payload.error ? payload.error : message;
+ throw new PaymentRequiredError(challengeMessage, { code: 'payment_required', status: res.status, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
}
throw new ThreeWsError(message, { code, status: res.status, detail: payload?.detail, retryAfter, body: payload });
};
diff --git a/packages/guardian/src/http.js b/packages/guardian/src/http.js
index 6398a3e53..eafe53910 100644
--- a/packages/guardian/src/http.js
+++ b/packages/guardian/src/http.js
@@ -96,8 +96,17 @@ export function createHttp(opts = {}) {
const message = payload?.message || payload?.error_description || payload?.detail || `Request failed with ${res.status}`;
const retryAfter = numberOr(res.headers.get('retry-after'), payload?.retry_after);
- if (res.status === 402) {
- throw new PaymentRequiredError(message, { code, status: 402, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
+ // An x402 challenge is not always HTTP 402: the three.ws MCP transport
+ // issues it over 401 (x402Version + accepts in the body, alongside the
+ // OAuth www-authenticate header). Detect the challenge by payload so the
+ // caller always gets a PaymentRequiredError carrying `accepts`.
+ const isX402Challenge = res.status === 402 ||
+ (res.status === 401 && payload != null && payload.x402Version != null && Array.isArray(payload.accepts));
+ if (isX402Challenge) {
+ // x402 bodies put the human-readable reason under `error`, so prefer it
+ // as the message and keep the stable `payment_required` code.
+ const challengeMessage = typeof payload?.error === 'string' && payload.error ? payload.error : message;
+ throw new PaymentRequiredError(challengeMessage, { code: 'payment_required', status: res.status, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
}
throw new ThreeWsError(message, { code, status: res.status, detail: payload?.detail, retryAfter, body: payload });
};
diff --git a/packages/intel/src/http.js b/packages/intel/src/http.js
index 6398a3e53..eafe53910 100644
--- a/packages/intel/src/http.js
+++ b/packages/intel/src/http.js
@@ -96,8 +96,17 @@ export function createHttp(opts = {}) {
const message = payload?.message || payload?.error_description || payload?.detail || `Request failed with ${res.status}`;
const retryAfter = numberOr(res.headers.get('retry-after'), payload?.retry_after);
- if (res.status === 402) {
- throw new PaymentRequiredError(message, { code, status: 402, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
+ // An x402 challenge is not always HTTP 402: the three.ws MCP transport
+ // issues it over 401 (x402Version + accepts in the body, alongside the
+ // OAuth www-authenticate header). Detect the challenge by payload so the
+ // caller always gets a PaymentRequiredError carrying `accepts`.
+ const isX402Challenge = res.status === 402 ||
+ (res.status === 401 && payload != null && payload.x402Version != null && Array.isArray(payload.accepts));
+ if (isX402Challenge) {
+ // x402 bodies put the human-readable reason under `error`, so prefer it
+ // as the message and keep the stable `payment_required` code.
+ const challengeMessage = typeof payload?.error === 'string' && payload.error ? payload.error : message;
+ throw new PaymentRequiredError(challengeMessage, { code: 'payment_required', status: res.status, accepts: payload?.accepts ?? null, detail: payload?.detail, body: payload });
}
throw new ThreeWsError(message, { code, status: res.status, detail: payload?.detail, retryAfter, body: payload });
};
diff --git a/packages/intel/test/intel.test.js b/packages/intel/test/intel.test.js
index 97c76aed4..a90f14d00 100644
--- a/packages/intel/test/intel.test.js
+++ b/packages/intel/test/intel.test.js
@@ -225,6 +225,24 @@ test('snapshot() surfaces a 402 as PaymentRequiredError carrying the x402 challe
});
});
+test('snapshot() surfaces the x402-over-401 challenge the live MCP transport issues', async () => {
+ // The production /api/mcp endpoint quotes its x402 challenge over HTTP 401
+ // (x402Version + accepts in the body, next to the OAuth www-authenticate
+ // header), not 402. The http core must still map it to PaymentRequiredError.
+ const accepts = [{ scheme: 'exact', network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp', amount: '1000', payTo: SYNTH }];
+ const { fetch } = stubFetch([
+ { status: 401, body: { x402Version: 2, error: 'X-PAYMENT header is required', accepts } },
+ ]);
+ await assert.rejects(() => createIntel({ fetch }).snapshot(THREE), (e) => {
+ assert.ok(e instanceof PaymentRequiredError);
+ assert.equal(e.code, 'payment_required');
+ assert.equal(e.status, 401);
+ assert.equal(e.message, 'X-PAYMENT header is required');
+ assert.deepEqual(e.accepts, accepts);
+ return true;
+ });
+});
+
test('snapshot() maps a JSON-RPC tool error to a typed ThreeWsError', async () => {
const { fetch } = stubFetch([
{ body: { jsonrpc: '2.0', id: 1, error: { code: -32602, message: 'unknown tool: pump_snapshot' } } },