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): answer an unpaid OKX tool call with 402 for protocol clients too

nirholas314cfb55b796
diff --git a/api/_mcp-studio/gpt-forge-client.js b/api/_mcp-studio/gpt-forge-client.js
index 5373b5232..1c0182000 100644
--- a/api/_mcp-studio/gpt-forge-client.js
+++ b/api/_mcp-studio/gpt-forge-client.js
@@ -94,7 +94,13 @@ function internalHeaders() {
// self-hosted Hunyuan3D worker that serves high does return a poll handle, so
// the usual cause of the second case is its scale-to-zero cold start rather
// than a blocking lane; either way, standard is better than a dead end.
-export async function startForge(base, { prompt, imageUrls, aspect, backend, path, tier, internal }) {
+//
+// `strictTier: true` disables that degrade and throws `tier_unavailable`
+// instead. A PAID high-tier surface must use it: a buyer who paid the HD price
+// and silently received standard has been overcharged, and the seller side
+// settles on success, so refusing here is what keeps their money in their
+// wallet.
+export async function startForge(base, { prompt, imageUrls, aspect, backend, path, tier, internal, strictTier = false }) {
const attempt = async (tierId, withInternal) => {
const payload = {
...(prompt ? { prompt } : {}),
@@ -131,9 +137,13 @@ export async function startForge(base, { prompt, imageUrls, aspect, backend, pat
// in milliseconds, so a submit that blocked to the deadline almost always
// hit a cold start or a blocking fallback lane. High tier degrades to the
// async standard router; other tiers retry as submitted.
+ if (tier === 'high' && strictTier)
+ throw failure('tier_unavailable', 'the high-detail lane did not accept the job in time; try again shortly', { retryAfter: 30 });
if (tier === 'high') ({ res, data } = await attempt('standard', false));
else ({ res, data } = await attempt(tier, !!internal));
}
+ if (res.status === 402 && tier === 'high' && strictTier)
+ throw failure('tier_unavailable', 'the high-detail lane is not accepting operator-funded jobs right now; try again shortly', { retryAfter: 60 });
if (res.status === 402 && tier === 'high') ({ res, data } = await attempt('standard', false));
if (res.status === 503) throw failure('not_configured', data?.message || '3D generation is not configured on this deployment');
if (res.status === 429) throw failure('busy', data?.message || 'the 3D generator is busy; try again shortly', { retryAfter: data?.retry_after });
diff --git a/api/_mcp/auth.js b/api/_mcp/auth.js
index c81541d77..0cfde74a0 100644
--- a/api/_mcp/auth.js
+++ b/api/_mcp/auth.js
@@ -56,9 +56,18 @@ export function isMcpProtocolClient(req) {
// treats any WWW-Authenticate on a 402 as an MPP header and flags the missing
// Payment challenge. A plain client that actually wants OAuth can still find
// the metadata at /.well-known/oauth-protected-resource.
-export async function sendAuthChallenge(res, { req, resourceUrl, requirements, challenge }) {
+// `paymentStatus: 402` forces a Payment Required answer for EVERY caller, MCP
+// protocol clients included, and drops the OAuth WWW-Authenticate hint. The
+// OKX.AI surfaces need it: a spec-compliant MCP client sends
+// `Accept: application/json, text/event-stream`, which reads as a protocol
+// client here and used to earn a 401. The OKX buyer flow keys strictly on 402
+// ("if it is not 402, return the body directly"), so a real MCP client calling
+// a paid tool unpaid was handed a 401 it could not pay, and a reviewer probing
+// with one saw a service with no payment integration. curl never showed it.
+export async function sendAuthChallenge(res, { req, resourceUrl, requirements, challenge, paymentStatus = null }) {
const resource = env.MCP_RESOURCE;
- const isProtocolClient = isMcpProtocolClient(req);
+ const forced402 = paymentStatus === 402;
+ const isProtocolClient = !forced402 && isMcpProtocolClient(req);
res.statusCode = isProtocolClient ? 401 : 402;
if (isProtocolClient) {
res.setHeader(
@@ -116,7 +125,7 @@ function mergeAccepts(extra, base) {
export async function authenticateRequest(
req,
res,
- { x402Amount, resourcePath = '/api/mcp', challenge, allowFree = false, extraAccepts = [] } = {},
+ { x402Amount, resourcePath = '/api/mcp', challenge, allowFree = false, extraAccepts = [], paymentStatus = null } = {},
) {
const bearer = extractBearer(req);
// OKX Agent Payments Protocol buyers replay with PAYMENT-SIGNATURE (x402 v2
@@ -197,7 +206,7 @@ export async function authenticateRequest(
}
}
- await sendAuthChallenge(res, { req, resourceUrl, requirements, challenge });
+ await sendAuthChallenge(res, { req, resourceUrl, requirements, challenge, paymentStatus });
return null;
}
@@ -209,7 +218,7 @@ export async function authenticateRequest(
export async function handleSse(
req,
res,
- { resourcePath = '/api/mcp', challenge, extraAccepts = [], x402Amount = null } = {},
+ { resourcePath = '/api/mcp', challenge, extraAccepts = [], x402Amount = null, paymentStatus = null } = {},
) {
// We don't hold long-lived server→client subscriptions yet; respond politely.
const bearer = extractBearer(req);
@@ -227,6 +236,7 @@ export async function handleSse(
paymentRequirements(sseResourceUrl, x402Amount != null ? { amount: x402Amount } : {}),
),
challenge,
+ paymentStatus,
});
}
const auth = await authenticateBearer(bearer, { audience: env.MCP_RESOURCE });
diff --git a/api/okx/3d/[service].js b/api/okx/3d/[service].js
index b3d21a312..230965e69 100644
--- a/api/okx/3d/[service].js
+++ b/api/okx/3d/[service].js
@@ -378,6 +378,7 @@ async function handleA2mcp(req, res, cfg) {
challenge,
extraAccepts: xlayerAcceptsFor(listPrice),
x402Amount: listPrice,
+ paymentStatus: 402,
});
if (req.method === 'DELETE') return handleTerminate(req, res);
if (req.method !== 'POST') return send401(res, 'method not supported');
@@ -403,6 +404,9 @@ async function handleA2mcp(req, res, cfg) {
challenge,
allowFree: allFree || (isDiscoveryOnlyBatch(body) && !isMcpProtocolClient(req)),
extraAccepts,
+ // OKX buyers pay a 402 and nothing else; there is no OAuth story on this
+ // surface, so an MCP client must never be diverted to a 401.
+ paymentStatus: 402,
});
if (!result) return;
const { auth, x402Ctx } = result;