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-chatgpt): answer every ChatGPT tool call inside its 60 s limit and rate-limit per ChatGPT user

nirholas24b862cc6b89
diff --git a/api/_lib/rate-limit.js b/api/_lib/rate-limit.js
index 4c2254d6d..cf5590f23 100644
--- a/api/_lib/rate-limit.js
+++ b/api/_lib/rate-limit.js
@@ -865,6 +865,13 @@ export const limits = {
getLimiter('studio:gen:burst', { limit: 4, window: '1 m' }).limit(ip),
studioGenHourly: (ip) =>
getLimiter('studio:gen:hourly', { limit: 30, window: '1 h' }).limit(ip),
+ // Ceiling on one source IP when the burst/hourly caps above are keyed per
+ // ChatGPT user (openai/subject) instead of per IP. Every ChatGPT user reaches
+ // us from OpenAI's shared egress pool, so a per-IP 30/h would ration the whole
+ // directory as one caller; the subject is client-supplied, so this pool cap is
+ // what stops a forged subject per call from escaping the limits entirely.
+ studioGenPoolHourly: (ip) =>
+ getLimiter('studio:gen:pool:hourly', { limit: 300, window: '1 h' }).limit(ip),
// Cheap per-IP cap on studio transport/discovery (initialize, tools/list,
// ping, resources). Bounds discovery floods without touching the generation
// budget. Non-critical: a missing-Redis misconfig degrades gracefully.
diff --git a/api/_mcp-studio/component.js b/api/_mcp-studio/component.js
index 92de0f9b2..5a6a58491 100644
--- a/api/_mcp-studio/component.js
+++ b/api/_mcp-studio/component.js
@@ -9,7 +9,12 @@
//
// Reads structuredContent shape: { glbUrl, viewerUrl, kind, prompt, rigged?,
// lineage?: [{ index, parentIndex, glbUrl, viewerUrl, label, instruction, active }],
-// activeIndex? }. When a lineage is present (a conversational refinement) the
+// activeIndex? }, look_at_model's { model_url, viewer_url, ar_url }, and the
+// pending envelope { status: 'pending', jobId, stage, next? }. A pending job is
+// collected by the widget itself: it polls check_job through
+// window.openai.callTool (every tool here is widgetAccessible) until the model
+// lands, runs rig_mesh when the envelope says next: 'rig', and saves the result
+// to widget state so a re-render shows the model instead of polling again. When a lineage is present (a conversational refinement) the
// widget shows a version strip: click any version to swap it in with a cross-fade
// (a client-side revert view — every version's GLB is already in the lineage).
// No identifiers, no payment, no crypto — only what is needed to show the model.
@@ -133,8 +138,9 @@ export const COMPONENT_HTML = `<!doctype html>
</div>
<div id="error" class="overlay hidden">
- <div class="title">Couldn't load the model</div>
+ <div id="errtitle" class="title">Couldn't load the model</div>
<div id="errmsg" class="muted">Something went wrong generating or displaying this model.</div>
+ <button id="retry" class="btn hidden" type="button">Keep waiting</button>
</div>
</div>
@@ -158,6 +164,8 @@ export const COMPONENT_HTML = `<!doctype html>
var empty = document.getElementById('empty');
var errEl = document.getElementById('error');
var errMsg = document.getElementById('errmsg');
+ var errTitle = document.getElementById('errtitle');
+ var retryEl = document.getElementById('retry');
var bar = document.getElementById('bar');
var promptEl = document.getElementById('prompt');
var kindEl = document.getElementById('kind');
@@ -182,12 +190,15 @@ export const COMPONENT_HTML = `<!doctype html>
function show(el) { [loading, empty, errEl].forEach(function (n) { n.classList.add('hidden'); }); if (el) el.classList.remove('hidden'); }
- function fail(msg) {
+ function fail(msg, opts) {
clearTimeout(loadWatchdog);
mv.classList.add('veiled');
bar.classList.add('hidden');
versionsEl.classList.add('hidden');
+ errTitle.textContent = (opts && opts.title) || "Couldn't load the model";
errMsg.textContent = msg || 'Something went wrong displaying this model.';
+ retryEl.classList.toggle('hidden', !(opts && opts.retry));
+ retryEl.onclick = (opts && opts.retry) || null;
show(errEl);
}
@@ -261,22 +272,161 @@ export const COMPONENT_HTML = `<!doctype html>
var defaultLoadingTitle = loadingTitle.textContent;
var defaultLoadingMuted = loadingMuted.textContent;
- function render(out) {
- if (!out) { mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
- // A pending envelope means the job is real and still running server-side.
- // Rendering it as "No model yet" (the old fallthrough) read as a failure;
- // show the designed in-progress state with the live timing instead.
- if (out.status === 'pending') {
- clearTimeout(loadWatchdog);
- mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden');
- loadingTitle.textContent = 'Still rendering your 3D model…';
- var eta = Number(out.etaRemainingSeconds);
- loadingMuted.textContent =
- (isFinite(eta) && eta > 0 ? 'Roughly ' + Math.round(eta) + 's to go. ' : '') +
- 'It keeps running in the background. Ask to check the job to collect the finished model.';
- show(loading);
+ // ── pending jobs: the widget collects them itself ──────────────────────────
+ // ChatGPT ends a tool call at 60 s and a model takes one to four minutes, so
+ // the tool hands back a pending job and this widget finishes the job in place.
+ // Every wait below is a real check_job round trip; the clock shows real time.
+ var POLL_FIRST_MS = 5000;
+ var POLL_MAX_MS = 12000;
+ var POLL_GIVE_UP_MS = 12 * 60 * 1000;
+ var MAX_POLL_FAILURES = 6;
+ var poll = null;
+
+ function canCallTools() {
+ try { return !!(window.openai && typeof window.openai.callTool === 'function'); } catch (e) { return false; }
+ }
+
+ function structuredOf(r) {
+ if (!r) return null;
+ if (r.structuredContent) return r.structuredContent;
+ if (r.result && r.result.structuredContent) return r.result.structuredContent;
+ return null;
+ }
+
+ function clock(sec) {
+ var m = Math.floor(sec / 60), s = sec % 60;
+ return m + ':' + (s < 10 ? '0' : '') + s;
+ }
+
+ function stopPolling() {
+ if (!poll) return;
+ clearTimeout(poll.timer);
+ clearInterval(poll.ticker);
+ poll = null;
+ }
+
+ function paintPending() {
+ if (!poll) return;
+ var secs = Math.max(0, Math.round((Date.now() - poll.since) / 1000));
+ loadingTitle.textContent = poll.stage === 'rig' ? 'Rigging your avatar…' : 'Generating your 3D model…';
+ loadingMuted.textContent =
+ (poll.stage === 'rig' ? 'Adding a skeleton so it can be posed and animated. ' : 'Most models take one to four minutes. ') +
+ 'It will appear here when it is ready. ' + clock(secs) + ' elapsed.';
+ }
+
+ function persist(result) {
+ try { if (window.openai && window.openai.setWidgetState) window.openai.setWidgetState({ jobId: poll && poll.origin, result: result }); } catch (e) {}
+ }
+
+ function finish(result) {
+ persist(result);
+ stopPolling();
+ render(result, true);
+ }
+
+ function schedule() {
+ if (!poll) return;
+ if (Date.now() - poll.since > POLL_GIVE_UP_MS) {
+ var resume = poll;
+ stopPolling();
+ fail('This model is taking much longer than usual. It is still running, and you can keep waiting for it.', {
+ title: 'Still rendering',
+ retry: function () { beginPolling(resume.jobId, resume.stage, resume.next, resume.origin, Date.now()); },
+ });
return;
}
+ poll.timer = setTimeout(tick, poll.delay);
+ poll.delay = Math.min(POLL_MAX_MS, Math.round(poll.delay * 1.3));
+ }
+
+ function rigThenFinish(mesh) {
+ var p = poll;
+ p.stage = 'rig';
+ p.next = null;
+ paintPending();
+ window.openai.callTool('rig_mesh', { glb_url: mesh.glbUrl }).then(function (r) {
+ if (poll !== p) return;
+ var out = structuredOf(r);
+ if (out && out.status === 'pending' && out.jobId) { p.jobId = out.jobId; p.delay = POLL_FIRST_MS; schedule(); return; }
+ if (out && out.glbUrl) { finish(out); return; }
+ // The mesh is real and saved; a failed rig must not throw it away.
+ finish(mesh);
+ }, function () { if (poll === p) finish(mesh); });
+ }
+
+ function tick() {
+ var p = poll;
+ if (!p) return;
+ window.openai.callTool('check_job', { job_id: p.jobId }).then(function (r) {
+ if (poll !== p) return;
+ p.failures = 0;
+ var out = structuredOf(r);
+ if (out && out.status === 'pending') { paintPending(); schedule(); return; }
+ if (out && out.glbUrl) {
+ if (p.next === 'rig' && !out.rigged) { rigThenFinish(out); return; }
+ finish(p.stage === 'rig' ? Object.assign({}, out, { rigged: true }) : out);
+ return;
+ }
+ stopPolling();
+ fail((out && out.message) || 'Generation did not return a model. Try generating it again.');
+ }, function () {
+ if (poll !== p) return;
+ if (++p.failures >= MAX_POLL_FAILURES) {
+ var resume = p;
+ stopPolling();
+ fail('Lost contact with the job while it was rendering. It keeps running, so checking again usually finds it.', {
+ title: 'Connection interrupted',
+ retry: function () { beginPolling(resume.jobId, resume.stage, resume.next, resume.origin, resume.since); },
+ });
+ return;
+ }
+ schedule();
+ });
+ }
+
+ function beginPolling(jobId, stage, next, origin, since) {
+ stopPolling();
+ poll = { jobId: jobId, origin: origin || jobId, stage: stage, next: next, since: since, delay: POLL_FIRST_MS, failures: 0, timer: null, ticker: null };
+ mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden');
+ paintPending();
+ show(loading);
+ poll.ticker = setInterval(paintPending, 1000);
+ schedule();
+ }
+
+ function renderPending(out) {
+ clearTimeout(loadWatchdog);
+ if (poll && poll.origin === out.jobId) return;
+ var elapsed = Number(out.elapsedSeconds);
+ var since = Date.now() - (isFinite(elapsed) && elapsed > 0 ? elapsed * 1000 : 0);
+ if (canCallTools()) { beginPolling(out.jobId, out.stage, out.next, out.jobId, since); return; }
+ // A host without widget tool calls: say what is happening and how to collect it.
+ mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden');
+ loadingTitle.textContent = 'Still rendering your 3D model…';
+ var eta = Number(out.etaRemainingSeconds);
+ loadingMuted.textContent =
+ (isFinite(eta) && eta > 0 ? 'Roughly ' + Math.round(eta) + 's to go. ' : '') +
+ 'It keeps running in the background. Ask to check the job to collect the finished model.';
+ show(loading);
+ }
+
+ // look_at_model reports the model it inspected under snake_case keys.
+ function normalize(out) {
+ if (!out || out.glbUrl || !isHttps(out.model_url)) return out;
+ return Object.assign({}, out, { glbUrl: out.model_url, viewerUrl: out.viewer_url, arUrl: out.ar_url, kind: 'inspected model' });
+ }
+
+ var lastKey = null;
+ function render(out, force) {
+ out = normalize(out);
+ // The host re-sends globals for unrelated reasons (theme, layout, widget
+ // state). Re-rendering the same output would re-veil a loaded model.
+ var key = out ? JSON.stringify(out) : '';
+ if (!force && key === lastKey) return;
+ lastKey = key;
+ if (!out) { stopPolling(); mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
+ if (out.status === 'pending' && out.jobId) { renderPending(out); return; }
+ stopPolling();
if (out.error || out.message && !out.glbUrl) { fail(out.message || 'Generation did not return a model.'); return; }
var glb = out.glbUrl;
if (!isHttps(glb)) { mv.classList.add('veiled'); bar.classList.add('hidden'); versionsEl.classList.add('hidden'); show(empty); return; }
@@ -324,8 +474,14 @@ export const COMPONENT_HTML = `<!doctype html>
});
mv.addEventListener('error', function () { fail('The 3D model could not be displayed. You can still download the GLB file.'); });
+ // A pending tool output whose job this widget already finished shows the saved
+ // model, so reopening the conversation never starts the wait over.
function current() {
- try { return (window.openai && window.openai.toolOutput) || null; } catch (e) { return null; }
+ var out = null, saved = null;
+ try { out = (window.openai && window.openai.toolOutput) || null; } catch (e) { out = null; }
+ try { saved = (window.openai && window.openai.widgetState) || null; } catch (e) { saved = null; }
+ if (out && out.status === 'pending' && saved && saved.result && saved.jobId === out.jobId) return saved.result;
+ return out;
}
// Initial paint (toolOutput may already be present) + live updates from the host.
diff --git a/api/_mcp-studio/dispatch.js b/api/_mcp-studio/dispatch.js
index 5a9d1d161..420544321 100644
--- a/api/_mcp-studio/dispatch.js
+++ b/api/_mcp-studio/dispatch.js
@@ -39,6 +39,19 @@ const BASE_INSTRUCTIONS = [
'rendering: call check_job(job_id) after the suggested wait to collect it.',
];
+// ChatGPT drops a tool call still open at 60 s, and a generation takes one to
+// four minutes, so on that surface every call answers inside CHATGPT_CALL_BUDGET_MS
+// with either the model or a pending job the viewer widget keeps polling.
+const CHATGPT_INSTRUCTIONS = [
+ 'In ChatGPT the inline viewer collects a pending job by itself and shows the model when it lands, so tell the',
+ 'user it is rendering and do not loop on check_job; call check_job only when the user asks about the job.',
+];
+
+// 40 s of work, leaving headroom under the host's 60 s limit for a submit that
+// needs its guaranteed floor (gpt-forge-client.js SUBMIT_FLOOR_MS) and the
+// response's own trip back through ChatGPT.
+export const CHATGPT_CALL_BUDGET_MS = 40_000;
+
const PERSONA_INSTRUCTIONS = [
'To give the assistant a LIVING body: create_agent_persona(glb_url, name) saves a rigged model as a named,',
'persistent persona and returns a persona_id; persona_say(persona_id, text) makes that body lip-sync the reply and',
@@ -74,7 +87,8 @@ const SURFACES = {
catalog: [...TOOL_CATALOG],
tools: { ...TOOLS },
personas: false,
- instructions: BASE_INSTRUCTIONS.join(' '),
+ instructions: [...BASE_INSTRUCTIONS, ...CHATGPT_INSTRUCTIONS].join(' '),
+ callBudgetMs: CHATGPT_CALL_BUDGET_MS,
},
};
@@ -163,7 +177,8 @@ async function onToolCall(params, auth, started, req, surface) {
throw rpcError(-32602, `invalid params for ${name}: ${detail}`);
}
try {
- const result = await tool.handler(args, auth, req);
+ const ctx = surface.callBudgetMs ? { deadline: started + surface.callBudgetMs } : {};
+ const result = await tool.handler(args, auth, req, ctx);
recordEvent({ kind: 'tool_call', tool: name, latencyMs: Date.now() - started, meta: { args_summary: summarize(args), server: surface.server } });
return await finishCall(POLICY_SERVER, name, sentArgs, auth, result, gate.preview);
} catch (err) {
diff --git a/api/_mcp-studio/gpt-forge-client.js b/api/_mcp-studio/gpt-forge-client.js
index b36f63423..1cbd682a6 100644
--- a/api/_mcp-studio/gpt-forge-client.js
+++ b/api/_mcp-studio/gpt-forge-client.js
@@ -21,6 +21,20 @@ import { llmComplete } from '../_lib/llm.js';
const DEFAULT_TIMEOUT_MS = 180_000;
const DEFAULT_POLL_MS = 3_000;
+const SUBMIT_TIMEOUT_MS = 90_000;
+const RIG_SUBMIT_TIMEOUT_MS = 30_000;
+
+// A caller may bound a whole tool call with an absolute `deadline` (epoch ms).
+// ChatGPT drops any tool call still open at 60 s, so its surface passes one and
+// every wait below shrinks to fit it. A submit still gets this floor even past
+// the deadline: without an accepted job there is nothing to hand back, and the
+// surface's budget leaves room for it under the host's limit.
+const SUBMIT_FLOOR_MS = 8_000;
+
+function submitWindow(deadline, capMs) {
+ if (!deadline) return capMs;
+ return Math.min(capMs, Math.max(SUBMIT_FLOOR_MS, deadline - Date.now()));
+}
function envNum(key, def) {
const v = Number(process.env[key]);
@@ -100,7 +114,7 @@ function internalHeaders() {
// 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 }) {
+export async function startForge(base, { prompt, imageUrls, aspect, backend, path, tier, internal, strictTier = false }, { deadline } = {}) {
const attempt = async (tierId, withInternal) => {
const payload = {
...(prompt ? { prompt } : {}),
@@ -116,7 +130,7 @@ export async function startForge(base, { prompt, imageUrls, aspect, backend, pat
method: 'POST',
headers: { 'content-type': 'application/json', ...(withInternal ? internalHeaders() : {}) },
body: JSON.stringify(payload),
- signal: AbortSignal.timeout(90_000),
+ signal: AbortSignal.timeout(submitWindow(deadline, SUBMIT_TIMEOUT_MS)),
});
} catch (err) {
if (err?.name === 'TimeoutError' || err?.name === 'AbortError')
@@ -133,6 +147,9 @@ export async function startForge(base, { prompt, imageUrls, aspect, backend, pat
({ res, data } = await attempt(tier, !!internal));
} catch (err) {
if (err?.code !== 'timeout') throw err;
+ // A bounded call that spent its budget on the first submit has no time
+ // left for a second one; the caller reports the timeout instead.
+ if (deadline && deadline - Date.now() < SUBMIT_FLOOR_MS) throw err;
// One more shot at the accept path before giving up: the async lanes 202
// 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
@@ -152,14 +169,14 @@ export async function startForge(base, { prompt, imageUrls, aspect, backend, pat
return data;
}
-export async function startRig(base, glbUrl) {
+export async function startRig(base, glbUrl, { deadline } = {}) {
let res;
try {
res = await fetch(`${base}/api/gpt-forge?action=rig`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ glb_url: glbUrl }),
- signal: AbortSignal.timeout(30_000),
+ signal: AbortSignal.timeout(submitWindow(deadline, RIG_SUBMIT_TIMEOUT_MS)),
});
} catch (err) {
if (err?.name === 'TimeoutError' || err?.name === 'AbortError')
@@ -175,9 +192,12 @@ export async function startRig(base, glbUrl) {
// Poll a /api/gpt-forge job to a terminal state. Returns the done payload, throws a
// coded failure on a failed job, or returns { _timedOut: true } at the deadline.
-export async function pollJob(base, jobId, { timeoutMs, intervalMs } = {}) {
+// `deadline` (epoch ms) caps the wait below timeoutMs when the caller's own call
+// budget ends sooner; no probe or sleep is allowed to run past it.
+export async function pollJob(base, jobId, { timeoutMs, intervalMs, deadline: callDeadline } = {}) {
const tMs = timeoutMs || DEFAULT_TIMEOUT_MS;
- const deadline = Date.now() + tMs;
+ const deadline = Math.min(Date.now() + tMs, callDeadline || Infinity);
+ const left = () => deadline - Date.now();
// Gentle backoff: start at the configured cadence and stretch toward a cap,
// so a minutes-long self-host job costs ~a third of the self-calls a fixed
// 3s cadence would fire at the shared mcp3dStatus rate bucket.
@@ -191,7 +211,7 @@ export async function pollJob(base, jobId, { timeoutMs, intervalMs } = {}) {
try {
res = await fetch(`${base}/api/gpt-forge?job=${encodeURIComponent(jobId)}`, {
headers: { accept: 'application/json' },
- signal: AbortSignal.timeout(Math.max(iMs * 3, 15_000)),
+ signal: AbortSignal.timeout(Math.max(1_000, Math.min(Math.max(iMs * 3, 15_000), left()))),
});
data = await res.json().catch(() => ({}));
} catch {
@@ -207,7 +227,7 @@ export async function pollJob(base, jobId, { timeoutMs, intervalMs } = {}) {
// caller returns a pollable handle, never a dead error.
return { ...(last || {}), _timedOut: true };
}
- await sleep(iMs);
+ await sleep(Math.max(0, Math.min(iMs, left())));
iMs = Math.min(maxIMs, Math.round(iMs * 1.35));
continue;
}
@@ -220,7 +240,7 @@ export async function pollJob(base, jobId, { timeoutMs, intervalMs } = {}) {
retryBackends: Array.isArray(data.retry_backends) ? data.retry_backends : undefined,
});
}
- await sleep(iMs);
+ await sleep(Math.max(0, Math.min(iMs, left())));
iMs = Math.min(maxIMs, Math.round(iMs * 1.35));
}
return { ...(last || {}), _timedOut: true };
@@ -261,21 +281,23 @@ export async function pollOnce(base, jobId) {
// Run a submit→poll cycle end to end, returning the terminal job payload.
// A timed-out payload keeps `job_id` so the caller can hand the (still
// running) job back to the client as a pollable handle instead of an error.
-export async function generate(base, submitArgs, { timeoutEnv } = {}) {
- const job = await startForge(base, submitArgs);
+export async function generate(base, submitArgs, { timeoutEnv, deadline } = {}) {
+ const job = await startForge(base, submitArgs, { deadline });
if (job.status === 'done' && job.glb_url) return job;
const out = await pollJob(base, job.job_id, {
timeoutMs: timeoutEnv ? envNum(timeoutEnv, DEFAULT_TIMEOUT_MS) : DEFAULT_TIMEOUT_MS,
intervalMs: envNum('STUDIO_POLL_MS', DEFAULT_POLL_MS),
+ deadline,
});
return out._timedOut ? { ...out, job_id: job.job_id } : out;
}
-export async function rig(base, glbUrl, { timeoutEnv } = {}) {
- const job = await startRig(base, glbUrl);
+export async function rig(base, glbUrl, { timeoutEnv, deadline } = {}) {
+ const job = await startRig(base, glbUrl, { deadline });
const out = await pollJob(base, job.job_id, {
timeoutMs: timeoutEnv ? envNum(timeoutEnv, DEFAULT_TIMEOUT_MS) : DEFAULT_TIMEOUT_MS,
intervalMs: envNum('STUDIO_POLL_MS', DEFAULT_POLL_MS),
+ deadline,
});
return out._timedOut ? { ...out, job_id: job.job_id } : out;
}
diff --git a/api/_mcp-studio/handler.js b/api/_mcp-studio/handler.js
index 07e65bf5f..76f918342 100644
--- a/api/_mcp-studio/handler.js
+++ b/api/_mcp-studio/handler.js
@@ -48,6 +48,20 @@ function callsGenerationTool(body) {
return batch.some((m) => m && m.method === 'tools/call' && isGenerationTool(m?.params?.name));
}
+// ChatGPT sends an anonymized, per-user `openai/subject` on every tool call and
+// documents it for rate limiting. All ChatGPT traffic shares OpenAI's egress
+// IPs, so on that surface the per-caller caps key on it. Bounded to a sane
+// token shape; anything else falls back to the IP.
+const SUBJECT_RE = /^[A-Za-z0-9_.:/+=-]{8,256}$/;
+export function chatgptSubject(body) {
+ const batch = Array.isArray(body) ? body : [body];
+ for (const m of batch) {
+ const sub = m?.params?._meta?.['openai/subject'];
+ if (typeof sub === 'string' && SUBJECT_RE.test(sub)) return sub;
+ }
+ return null;
+}
+
export function studioHandler({ surface = 'full' } = {}) {
return wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,HEAD,POST,OPTIONS', origins: '*', payments: false })) return;
@@ -81,9 +95,15 @@ export function studioHandler({ surface = 'full' } = {}) {
// Generation quota, burst then hourly, per IP. Applied only when the request
// actually calls a generation tool, so discovery is never throttled by it.
if (callsGenerationTool(body)) {
- const burst = await limits.studioGenBurst(ip);
+ const subject = surface === 'chatgpt' ? chatgptSubject(body) : null;
+ if (subject) {
+ const pool = await limits.studioGenPoolHourly(ip);
+ if (!pool.success) return rateLimited(res, pool, 'the free 3D studio is at capacity right now, please try again later');
+ }
+ const caller = subject ? `oai:${subject}` : ip;
+ const burst = await limits.studioGenBurst(caller);
if (!burst.success) return rateLimited(res, burst, 'generation rate limit, slow down and try again shortly');
- const hourly = await limits.studioGenHourly(ip);
+ const hourly = await limits.studioGenHourly(caller);
if (!hourly.success) return rateLimited(res, hourly, 'hourly generation limit reached, try again later');
// Platform-wide circuit breaker across ALL free-studio callers, backstops
// the shared GPU/provider budget when many distinct IPs, each under their
diff --git a/api/_mcp-studio/tools.js b/api/_mcp-studio/tools.js
index c37451a7f..77779b1f5 100644
--- a/api/_mcp-studio/tools.js
+++ b/api/_mcp-studio/tools.js
@@ -167,7 +167,12 @@ function pendingTiming(job) {
// quietly finished minutes later and the caller never learned. The job handle
// is public (the free /api/forge poll endpoint takes it with no auth), so hand
// it over and let the caller collect the result.
-function pendingResult({ base, jobId, what, prompt, etaRemainingSeconds, stage = 'mesh', cold = null }) {
+//
+// `next: 'rig'` marks a mesh that the caller asked to have rigged (forge_avatar
+// ran out of budget before the rig stage). The viewer widget reads it and runs
+// rig_mesh itself once the mesh lands, so the user still ends with the rigged
+// avatar they asked for rather than a bare mesh.
+function pendingResult({ base, jobId, what, prompt, etaRemainingSeconds, stage = 'mesh', cold = null, next = null }) {
// The ChatGPT pipeline's own endpoint, not /api/forge: the whole point of
// the clone is that this surface can evolve independently.
const pollUrl = `${base}/api/gpt-forge?job=${encodeURIComponent(jobId)}`;
@@ -216,6 +221,7 @@ function pendingResult({ base, jobId, what, prompt, etaRemainingSeconds, stage =
// mesh (rig it) or the finished rig (use it). Identifier-free, so it
// costs the data-minimization rule nothing.
stage,
+ ...(next ? { next } : {}),
...(eta ? { etaRemainingSeconds: eta } : {}),
// Machine-readable twin of the sentence above, so a client can render
// its own "waking up" state instead of parsing prose.
@@ -351,7 +357,7 @@ const AVATAR_TIER = 'high';
// ── handlers ────────────────────────────────────────────────────────────────
-async function handleForgeFree(args, _auth, req) {
+async function handleForgeFree(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const prompt = String(args.prompt || '').trim();
if (prompt.length < 3) return toolError('Provide a text prompt of at least 3 characters.');
@@ -391,7 +397,7 @@ async function handleForgeFree(args, _auth, req) {
markImageUrls
? { prompt: effective, imageUrls: markImageUrls, tier, internal: true }
: { prompt: effective, path: 'image', tier, internal: true },
- { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS' },
+ { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS', deadline: ctx.deadline },
);
} catch (err) {
return toolError(failureMessage(err));
@@ -401,7 +407,7 @@ async function handleForgeFree(args, _auth, req) {
return ok({ glbUrl: job.glb_url, base, kind: 'model', prompt, referenceImageUrl: job.preview_image_url });
}
-async function handleTextToAvatar(args, _auth, req) {
+async function handleTextToAvatar(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const prompt = String(args.prompt || '').trim();
const imageUrl = args.image_url ? String(args.image_url).trim() : '';
@@ -429,7 +435,7 @@ async function handleTextToAvatar(args, _auth, req) {
job = await generate(
base,
{ prompt: effective || undefined, imageUrls: imageUrl ? [imageUrl] : undefined, aspect: '1:1', tier: AVATAR_TIER, internal: true },
- { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS' },
+ { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS', deadline: ctx.deadline },
);
} catch (err) {
return toolError(failureMessage(err));
@@ -439,7 +445,7 @@ async function handleTextToAvatar(args, _auth, req) {
return ok({ glbUrl: job.glb_url, base, kind: 'avatar', prompt: prompt || undefined, referenceImageUrl: job.preview_image_url });
}
-async function handleMeshForge(args, _auth, req) {
+async function handleMeshForge(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const prompt = String(args.prompt || '').trim();
const imageUrl = args.image_url ? String(args.image_url).trim() : '';
@@ -481,7 +487,7 @@ async function handleMeshForge(args, _auth, req) {
tier: 'standard',
internal: true,
},
- { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS' },
+ { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS', deadline: ctx.deadline },
);
} catch (err) {
return toolError(failureMessage(err));
@@ -491,7 +497,7 @@ async function handleMeshForge(args, _auth, req) {
return ok({ glbUrl: job.glb_url, base, kind: 'mesh', prompt: prompt || undefined, referenceImageUrl: job.preview_image_url });
}
-async function handleRigMesh(args, _auth, req) {
+async function handleRigMesh(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const glbUrl = String(args.glb_url || '').trim();
if (!/^https?:\/\//i.test(glbUrl)) return toolError('Provide an http(s) URL to a GLB mesh to rig.');
@@ -502,7 +508,7 @@ async function handleRigMesh(args, _auth, req) {
}
let job;
try {
- job = await rig(base, glbUrl, { timeoutEnv: 'STUDIO_RIG_TIMEOUT_MS' });
+ job = await rig(base, glbUrl, { timeoutEnv: 'STUDIO_RIG_TIMEOUT_MS', deadline: ctx.deadline });
} catch (err) {
return toolError(failureMessage(err));
}
@@ -511,7 +517,7 @@ async function handleRigMesh(args, _auth, req) {
return ok({ glbUrl: job.glb_url, base, kind: 'rigged model', rigged: true });
}
-async function handleForgeAvatar(args, _auth, req) {
+async function handleForgeAvatar(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const prompt = String(args.prompt || '').trim();
const imageUrl = args.image_url ? String(args.image_url).trim() : '';
@@ -542,18 +548,18 @@ async function handleForgeAvatar(args, _auth, req) {
gen = await generate(
base,
{ prompt: effective || undefined, imageUrls: imageUrl ? [imageUrl] : undefined, aspect: '1:1', tier: AVATAR_TIER, internal: true },
- { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS' },
+ { timeoutEnv: 'STUDIO_FORGE_TIMEOUT_MS', deadline: ctx.deadline },
);
} catch (err) {
return toolError(failureMessage(err));
}
- if (gen._timedOut && gen.job_id) return pendingResult({ base, jobId: gen.job_id, what: 'avatar mesh (rig it with rig_mesh once done)', prompt: prompt || undefined, ...pendingTiming(gen), stage: 'mesh' });
+ if (gen._timedOut && gen.job_id) return pendingResult({ base, jobId: gen.job_id, what: 'avatar mesh (rig it with rig_mesh once done)', prompt: prompt || undefined, ...pendingTiming(gen), stage: 'mesh', next: 'rig' });
if (gen._timedOut || !gen.glb_url) return toolError('Generation is taking longer than expected. Please try again.');
// Stage 2 — auto-rig the generated mesh.
let rigged;
try {
- rigged = await rig(base, gen.glb_url, { timeoutEnv: 'STUDIO_RIG_TIMEOUT_MS' });
+ rigged = await rig(base, gen.glb_url, { timeoutEnv: 'STUDIO_RIG_TIMEOUT_MS', deadline: ctx.deadline });
} catch (err) {
// Generation succeeded but rigging failed: hand back the (unrigged) mesh so
// the work isn't lost, and say so plainly.
@@ -588,7 +594,7 @@ async function handleForgeAvatar(args, _auth, req) {
// generation as image→3D. No faked diffing — the composed prompt is what the
// generator actually runs. Every version is recorded in an immutable lineage the
// client passes back to branch/revert. Free, stateless, zero payment surface.
-async function handleRefineModel(args, _auth, req) {
+async function handleRefineModel(args, _auth, req, ctx = {}) {
const base = originFromReq(req);
const glbUrl = String(args.glb_url || '').trim();
if (!/^https?:\/\//i.test(glbUrl)) return toolError('Provide the http(s) glb_url of the model to refine.');
@@ -655,7 +661,7 @@ async function handleRefineModel(args, _auth, req) {
refImageUrl
? { prompt: composed, imageUrls: [refImageUrl], aspect: '1:1', tier: 'standard', internal: true }
: { prompt: composed, tier: 'standard', internal: true },
- { timeoutEnv: 'STUDIO_REFINE_TIMEOUT_MS' },
+ { timeoutEnv: 'STUDIO_REFINE_TIMEOUT_MS', deadline: ctx.deadline },
);
} catch (err) {
return toolError(failureMessage(err));
diff --git a/tests/mcp-chatgpt-budget.test.js b/tests/mcp-chatgpt-budget.test.js
new file mode 100644
index 000000000..f1c5e40cf
--- /dev/null
+++ b/tests/mcp-chatgpt-budget.test.js
@@ -0,0 +1,134 @@
+// The ChatGPT plugin surface answers every tool call inside ChatGPT's 60 s
+// limit. A generation takes one to four minutes, so /api/mcp-chatgpt bounds each
+// call with CHATGPT_CALL_BUDGET_MS and hands back a pending job the viewer widget
+// collects by itself. These tests pin each link of that chain: the budget reaches
+// the generator, the client's waits stop at it, a spent budget never buys a
+// second submit, the widget polls and rigs through window.openai.callTool, and
+// generation limits key on ChatGPT's per-user subject rather than OpenAI's shared
+// egress IP.
+
+import { describe, it, expect, vi, afterEach } from 'vitest';
+
+vi.mock('../api/_mcp-studio/gpt-forge-client.js', async (importOriginal) => {
+ const real = await importOriginal();
+ return {
+ ...real,
+ generate: vi.fn(),
+ rig: vi.fn(),
+ directPrompt: vi.fn(async () => null),
+ };
+});
+
+import { generate } from '../api/_mcp-studio/gpt-forge-client.js';
+import { dispatch, CHATGPT_CALL_BUDGET_MS } from '../api/_mcp-studio/dispatch.js';
+import { chatgptSubject } from '../api/_mcp-studio/handler.js';
+import { COMPONENT_HTML } from '../api/_mcp-studio/component.js';
+
+const req = { headers: { host: 'three.ws', 'x-forwarded-proto': 'https' } };
+const auth = { userId: null, rateKey: '127.0.0.1', scope: '' };
+
+function callMsg(name, args) {
+ return { jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } };
+}
+
+const realFetch = globalThis.fetch;
+afterEach(() => {
+ globalThis.fetch = realFetch;
+ vi.mocked(generate).mockReset();
+});
+
+describe('ChatGPT call budget', () => {
+ it('stays under the 60 s host limit with room for a floored submit', () => {
+ expect(CHATGPT_CALL_BUDGET_MS).toBeLessThanOrEqual(45_000);
+ });
+
+ it('passes a deadline inside the budget to the generator on the chatgpt surface', async () => {
+ vi.mocked(generate).mockResolvedValue({ status: 'done', glb_url: 'https://three.ws/cdn/a.glb' });
+ const before = Date.now();
+ await dispatch(callMsg('forge_free', { prompt: 'a brass telescope' }), auth, req, { surface: 'chatgpt' });
+ const opts = vi.mocked(generate).mock.calls[0][2];
+ expect(opts.deadline).toBeGreaterThan(before);
+ expect(opts.deadline).toBeLessThanOrEqual(Date.now() + CHATGPT_CALL_BUDGET_MS);
+ });
+
+ it('leaves the full MCP surface unbounded, where hosts wait minutes', async () => {
+ vi.mocked(generate).mockResolvedValue({ status: 'done', glb_url: 'https://three.ws/cdn/a.glb' });
+ await dispatch(callMsg('forge_free', { prompt: 'a brass telescope' }), auth, req, { surface: 'full' });
+ expect(vi.mocked(generate).mock.calls[0][2].deadline).toBeUndefined();
+ });
+
+ it('marks an avatar mesh that ran out of budget so the widget rigs it', async () => {
+ vi.mocked(generate).mockResolvedValue({ _timedOut: true, job_id: 'job-mesh-1', status: 'running' });
+ const r = await dispatch(callMsg('forge_avatar', { prompt: 'a knight in plate armor' }), auth, req, { surface: 'chatgpt' });
+ const sc = r.result.structuredContent;
+ expect(sc.status).toBe('pending');
+ expect(sc.jobId).toBe('job-mesh-1');
+ expect(sc.stage).toBe('mesh');
+ expect(sc.next).toBe('rig');
+ });
+
+ it('tells the ChatGPT model the viewer collects pending jobs', async () => {
+ const r = await dispatch({ jsonrpc: '2.0', id: 2, method: 'initialize' }, auth, req, { surface: 'chatgpt' });
+ expect(r.result.instructions).toMatch(/viewer collects a pending job by itself/);
+ });
+});
+
+describe('gpt-forge client deadline', () => {
+ it('pollJob returns a pending handle at the deadline instead of its own timeout', async () => {
+ const { pollJob } = await vi.importActual('../api/_mcp-studio/gpt-forge-client.js');
+ globalThis.fetch = vi.fn(async () => ({ ok: true, status: 200, json: async () => ({ status: 'running' }) }));
+ const started = Date.now();
+ const out = await pollJob('https://three.ws', 'job-x', { timeoutMs: 180_000, intervalMs: 50, deadline: started + 400 });
+ expect(out._timedOut).toBe(true);
+ expect(Date.now() - started).toBeLessThan(1_500);
+ });
+
+ it('startForge does not spend a second submit once the budget is gone', async () => {
+ const { startForge } = await vi.importActual('../api/_mcp-studio/gpt-forge-client.js');
+ const timeout = Object.assign(new Error('timed out'), { name: 'TimeoutError' });
+ globalThis.fetch = vi.fn(async () => {
+ throw timeout;
+ });
+ await expect(
+ startForge('https://three.ws', { prompt: 'a lamp', tier: 'standard' }, { deadline: Date.now() + 1_000 }),
+ ).rejects.toMatchObject({ code: 'timeout' });
+ expect(globalThis.fetch).toHaveBeenCalledTimes(1);
+ });
+});
+
+describe('ChatGPT per-user generation limits', () => {
+ it('reads the openai/subject ChatGPT sends on a tool call', () => {
+ const body = { ...callMsg('forge_free', { prompt: 'x' }), params: { name: 'forge_free', arguments: {}, _meta: { 'openai/subject': 'v1/abcDEF123456' } } };
+ expect(chatgptSubject(body)).toBe('v1/abcDEF123456');
+ });
+
+ it('accepts a well-formed subject and rejects anything else', () => {
+ const withSub = (sub) => ({ jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'forge_free', arguments: {}, _meta: { 'openai/subject': sub } } });
+ expect(chatgptSubject(withSub('user_8f3kQ2abZx'))).toBe('user_8f3kQ2abZx');
+ expect(chatgptSubject(withSub('short'))).toBeNull();
+ expect(chatgptSubject(withSub('has spaces in it'))).toBeNull();
+ expect(chatgptSubject(withSub(42))).toBeNull();
+ expect(chatgptSubject(callMsg('forge_free', {}))).toBeNull();
+ });
+});
+
+describe('viewer widget collects pending jobs', () => {
+ it('polls check_job and rigs through the host tool bridge', () => {
+ expect(COMPONENT_HTML).toContain("callTool('check_job'");
+ expect(COMPONENT_HTML).toContain("callTool('rig_mesh'");
+ expect(COMPONENT_HTML).toContain("p.next === 'rig'");
+ });
+
+ it('saves the finished model so a re-render does not start the wait over', () => {
+ expect(COMPONENT_HTML).toContain('setWidgetState');
+ expect(COMPONENT_HTML).toContain('saved.jobId === out.jobId');
+ });
+
+ it('renders the model look_at_model inspected instead of the empty state', () => {
+ expect(COMPONENT_HTML).toContain('out.model_url');
+ });
+
+ it('no longer dead-ends a pending job on "ask to check the job" when tool calls are available', () => {
+ expect(COMPONENT_HTML).toContain('canCallTools()');
+ });
+});