MCP: Retrieve Mesh Generation job result for external APIs
This commit is contained in:
parent
3dd664d95c
commit
4868a97a32
2 changed files with 143 additions and 36 deletions
|
|
@ -67,7 +67,7 @@ Connect with transport "Streamable HTTP" to `http://localhost:3001/mcp`.
|
|||
| Kanban cards | `list_cards`, `move_card`, `delete_card`, `list_card_attributes`, `create_card_attribute`, `update_card_attribute`, `delete_card_attribute` |
|
||||
| Graph | `get_graph`, `create_node`, `update_node`, `move_node`, `delete_node`, `connect_nodes`, `disconnect_nodes` |
|
||||
| ComfyUI workflows | `list_workflows`, `inspect_workflow`, `import_workflow`, `update_workflow`, `run_workflow`, `get_run_status` |
|
||||
| AI actions | `generate_image`, `edit_image`, `generate_mesh`, `generate_mesh_tencent`, `generate_mesh_tripo`, `generate_mesh_hitem`, `edit_mesh`, `texture_mesh`, `rig_mesh_api` |
|
||||
| AI actions | `generate_image`, `edit_image`, `generate_mesh`, `generate_mesh_tencent`, `generate_mesh_tripo`, `generate_mesh_hitem`, `get_mesh_result`, `edit_mesh`, `texture_mesh`, `rig_mesh_api` |
|
||||
| Mesh tools | `auto_uv_mesh`, `auto_retopo_mesh` (fully-typed parameters), `run_mesh_tool` (auto_uv / auto_retopo / repair / auto_rig / optimize / convert_fbx), `export_mesh` |
|
||||
| Assets | `list_assets`, `list_library_assets`, `view_asset`, `download_asset`, `upload_asset`, `link_asset`, `delete_asset` |
|
||||
| System | `get_settings` (secrets redacted), `get_system_stats` |
|
||||
|
|
@ -84,6 +84,8 @@ Auto UV and Auto Retopo have many tuning parameters (14 and 20 respectively) tha
|
|||
|
||||
Tencent Hunyuan3D, Tripo AI, and Hitem3D each take a different, parameter-heavy option set. Use the dedicated `generate_mesh_tencent`, `generate_mesh_tripo`, and `generate_mesh_hitem` tools: each hardwires its provider and declares every parameter in its `options` schema with type, enum/range, and default (mirroring the backend 1:1), so a client can set exactly what it needs. Provider notes: Tencent `region` is required and `LowPoly` needs model `3.0`; Tripo's `P1` model ignores several options and `generateParts` is incompatible with `texture`/`pbr`/`quad`; Hitem3D requires an `imageSource`. Tencent and Tripo accept either a `prompt` (text-to-3D) or an `imageSource` (image-to-3D). The generic `generate_mesh` still accepts these providers with a free-form `options` object for backward compatibility.
|
||||
|
||||
These provider jobs are asynchronous. Unlike the app UI (where you click **Get Result** to poll), the MCP tools poll for you automatically: `generate_mesh*` submits the job and then polls the provider until the mesh is ready, streaming progress, and returns the saved assets — the AI just awaits the call and gets `{status:"completed", assets}`. The mesh is only saved once a poll sees completion, so if a job outlives `timeoutSeconds` (default 1200s) the tool returns `{status:"running", provider, taskId/jobId, region}`; pass those ids to **`get_mesh_result`** to finish and save the job (safe to call repeatedly). Do not re-run generation on timeout — that starts a new job.
|
||||
|
||||
Image generation (`generate_image`) is prompt-only by design: OpenAI/Google image parameters (size, quality, aspect ratio) are fixed in each provider's payload template in Settings, not passed per request, so there is nothing extra to set over MCP.
|
||||
|
||||
### Long-running operations
|
||||
|
|
|
|||
|
|
@ -71,6 +71,49 @@ const HITEM_MESH_OPTIONS = {
|
|||
hitemPbr: z.boolean().default(false).describe('Generate a PBR-ready mesh.')
|
||||
};
|
||||
|
||||
// Poll a provider's mesh-result endpoint until the job completes, errors, or the
|
||||
// timeout elapses — streaming MCP progress and attaching results to a graph node
|
||||
// on completion. The mesh is only persisted server-side once a poll sees the job
|
||||
// complete, so this loop IS what finishes an async job. Returns the completed
|
||||
// result, throws on provider error, or returns {status:'running'} on timeout.
|
||||
async function pollMeshResult(api, notifyMutation, {
|
||||
pollRequest, providerLabel, projectId, nodeId, selectedApi, timeoutSeconds, pollIntervalSeconds, pollFirst = false
|
||||
}, reportProgress) {
|
||||
const deadline = Date.now() + timeoutSeconds * 1000;
|
||||
let lastStatus = null;
|
||||
let first = true;
|
||||
while (Date.now() < deadline) {
|
||||
if (!(first && pollFirst)) await sleep(pollIntervalSeconds * 1000);
|
||||
first = false;
|
||||
lastStatus = await api.apiJson('POST', pollRequest.path, { body: pollRequest.body });
|
||||
|
||||
if (lastStatus?.status === 'completed') {
|
||||
await reportProgress(100, 100, 'Mesh generation completed');
|
||||
let nodeAttachment = null;
|
||||
if (nodeId && Array.isArray(lastStatus.assets) && lastStatus.assets.length > 0) {
|
||||
nodeAttachment = await attachResultsToNode(api, {
|
||||
projectId,
|
||||
nodeId,
|
||||
assets: lastStatus.assets.map(asset => ({ ...asset, type: asset?.type || 'mesh' })),
|
||||
metadata: { lastAction: 'mesh-generation', selectedApi }
|
||||
});
|
||||
}
|
||||
notifyMutation(projectId);
|
||||
return nodeAttachment ? { ...lastStatus, nodeAttachment } : lastStatus;
|
||||
}
|
||||
if (lastStatus?.status === 'error') {
|
||||
throw new Error(lastStatus.error || 'Mesh generation failed');
|
||||
}
|
||||
const percent = Number(lastStatus?.progressPercent ?? lastStatus?.progress);
|
||||
await reportProgress(
|
||||
Number.isFinite(percent) ? percent : 50,
|
||||
100,
|
||||
`${providerLabel} status: ${lastStatus?.jobStatus || lastStatus?.taskStatus || lastStatus?.status || 'processing'}`
|
||||
);
|
||||
}
|
||||
return { status: 'running', lastStatus };
|
||||
}
|
||||
|
||||
// Core submit-and-poll runner shared by generate_mesh and the typed
|
||||
// generate_mesh_{tencent,tripo,hitem} tools: submit the provider job, poll to
|
||||
// completion (streaming MCP progress), attach results to a graph node when
|
||||
|
|
@ -108,43 +151,34 @@ async function runMeshGeneration(api, notifyMutation, args, extra) {
|
|||
|
||||
await reportProgress(0, 100, `${submit.provider} job submitted (${submit.jobId || submit.taskId})`);
|
||||
|
||||
const deadline = Date.now() + timeoutSeconds * 1000;
|
||||
let lastStatus = submit;
|
||||
while (Date.now() < deadline) {
|
||||
await sleep(pollIntervalSeconds * 1000);
|
||||
lastStatus = await api.apiJson('POST', pollRequest.path, { body: pollRequest.body });
|
||||
const outcome = await pollMeshResult(api, notifyMutation, {
|
||||
pollRequest,
|
||||
providerLabel: submit.provider,
|
||||
projectId,
|
||||
nodeId,
|
||||
selectedApi,
|
||||
timeoutSeconds,
|
||||
pollIntervalSeconds
|
||||
}, reportProgress);
|
||||
|
||||
if (lastStatus?.status === 'completed') {
|
||||
await reportProgress(100, 100, 'Mesh generation completed');
|
||||
let nodeAttachment = null;
|
||||
if (nodeId && Array.isArray(lastStatus.assets) && lastStatus.assets.length > 0) {
|
||||
nodeAttachment = await attachResultsToNode(api, {
|
||||
projectId,
|
||||
nodeId,
|
||||
assets: lastStatus.assets.map(asset => ({ ...asset, type: asset?.type || 'mesh' })),
|
||||
metadata: { lastAction: 'mesh-generation', selectedApi }
|
||||
});
|
||||
}
|
||||
notifyMutation(projectId);
|
||||
return nodeAttachment ? { ...lastStatus, nodeAttachment } : lastStatus;
|
||||
}
|
||||
if (lastStatus?.status === 'error') {
|
||||
throw new Error(lastStatus.error || 'Mesh generation failed');
|
||||
}
|
||||
const percent = Number(lastStatus?.progressPercent);
|
||||
await reportProgress(
|
||||
Number.isFinite(percent) ? percent : 50,
|
||||
100,
|
||||
`${submit.provider} status: ${lastStatus?.jobStatus || lastStatus?.taskStatus || lastStatus?.status || 'processing'}`
|
||||
);
|
||||
if (outcome.status === 'running') {
|
||||
// Timed out with the job still on the provider. The mesh is NOT saved until a
|
||||
// result poll sees completion, so hand back the job ids for get_mesh_result.
|
||||
return {
|
||||
status: 'running',
|
||||
note: `Still processing after ${timeoutSeconds}s. The provider keeps working, but the mesh is only saved once a result poll sees it finish — call get_mesh_result with the ids below to retrieve it when ready. Do NOT re-run generation (that starts a new job).`,
|
||||
provider: submit.provider,
|
||||
selectedApi,
|
||||
projectId,
|
||||
name,
|
||||
...(submit.jobId ? { jobId: submit.jobId } : {}),
|
||||
...(submit.taskId ? { taskId: submit.taskId } : {}),
|
||||
...(submit.region ? { region: submit.region } : {}),
|
||||
...(submit.cardId ? { cardId: submit.cardId } : {}),
|
||||
lastStatus: outcome.lastStatus
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'running',
|
||||
note: `Still processing after ${timeoutSeconds}s. The provider keeps working — re-running the tool is NOT needed; check the project with list_assets, or the card status with list_cards.`,
|
||||
submit,
|
||||
lastStatus
|
||||
};
|
||||
return outcome;
|
||||
}
|
||||
|
||||
export function registerActionTools(server, { api, notifyMutation }) {
|
||||
|
|
@ -270,6 +304,77 @@ export function registerActionTools(server, { api, notifyMutation }) {
|
|||
}
|
||||
}, toolHandler((args, extra) => runMeshGeneration(api, notifyMutation, { ...args, selectedApi: HITEM_MESH_API_ID }, extra)));
|
||||
|
||||
server.registerTool('get_mesh_result', {
|
||||
title: 'Get mesh generation result',
|
||||
description: 'Retrieve and finish an async mesh-generation job (Tencent / Tripo / Hitem3D) — use this when the original generate_mesh* call returned {status:"running"} after timing out. The generated mesh is NOT saved to the project until a result poll sees the job complete, so this tool is required to recover a timed-out job (list_assets will not show it before then). It polls the provider until the mesh is ready, saves it, and returns the assets. Pass the ids from the timeout payload: Tripo/Hitem3D use taskId; Tencent uses jobId + region. It is safe to call repeatedly.',
|
||||
inputSchema: {
|
||||
projectId: z.number().int(),
|
||||
name: z.string().min(1).describe('The same name the job was submitted with (used to save the asset)'),
|
||||
provider: z.enum(['tencent', 'tripo', 'hitem']).describe('Provider the job was submitted to'),
|
||||
taskId: z.string().optional().describe('Tripo AI / Hitem3D job id (from the timeout payload)'),
|
||||
jobId: z.string().optional().describe('Tencent Cloud job id (from the timeout payload)'),
|
||||
region: z.enum(['ap-singapore', 'eu-frankfurt', 'na-siliconvalley']).optional().describe('Tencent Cloud region the job was submitted in (required for Tencent)'),
|
||||
selectedApi: z.string().optional().describe('Provider id override (defaults to the standard id for the provider)'),
|
||||
prompt: z.string().optional().describe('Original prompt (stored in asset metadata; optional)'),
|
||||
cardId: z.union([z.number().int(), z.string()]).optional().describe('Kanban card the job is attached to (from the timeout payload)'),
|
||||
parentAssetId: z.number().int().optional().describe('Save the result as a version of this asset'),
|
||||
nodeId: z.number().int().optional().describe('Graph node to attach the result to (graph projects)'),
|
||||
timeoutSeconds: z.number().int().min(5).max(3600).default(600),
|
||||
pollIntervalSeconds: z.number().int().min(3).max(120).default(10)
|
||||
}
|
||||
}, toolHandler(async (args, extra) => {
|
||||
const {
|
||||
projectId, name, provider, taskId, jobId, region, selectedApi, prompt,
|
||||
cardId, parentAssetId, nodeId, timeoutSeconds = 600, pollIntervalSeconds = 10
|
||||
} = args;
|
||||
const reportProgress = createProgressReporter(extra);
|
||||
|
||||
const providerIds = { tencent: TENCENT_MESH_API_ID, tripo: TRIPO_MESH_API_ID, hitem: HITEM_MESH_API_ID };
|
||||
const providerLabels = { tencent: 'Tencent Cloud', tripo: 'Tripo AI', hitem: 'Hitem3D' };
|
||||
const effectiveSelectedApi = selectedApi || providerIds[provider];
|
||||
const providerLabel = providerLabels[provider];
|
||||
|
||||
if (provider === 'tencent' && (!jobId || !region)) {
|
||||
throw new Error('Tencent result lookup requires both jobId and region (from the timeout payload).');
|
||||
}
|
||||
if ((provider === 'tripo' || provider === 'hitem') && !taskId) {
|
||||
throw new Error(`${providerLabel} result lookup requires taskId (from the timeout payload).`);
|
||||
}
|
||||
|
||||
// buildMeshPollRequest keys off the provider label substring and the ids.
|
||||
const pollRequest = buildMeshPollRequest(
|
||||
{ provider: providerLabel, jobId, taskId, region },
|
||||
{ projectId, name, prompt: prompt || '', cardId: cardId ?? null, selectedApi: effectiveSelectedApi, parentAssetId: parentAssetId ?? null }
|
||||
);
|
||||
if (!pollRequest) throw new Error(`Unknown mesh provider: ${provider}`);
|
||||
|
||||
const outcome = await pollMeshResult(api, notifyMutation, {
|
||||
pollRequest,
|
||||
providerLabel,
|
||||
projectId,
|
||||
nodeId,
|
||||
selectedApi: effectiveSelectedApi,
|
||||
timeoutSeconds,
|
||||
pollIntervalSeconds,
|
||||
pollFirst: true
|
||||
}, reportProgress);
|
||||
|
||||
if (outcome.status === 'running') {
|
||||
return {
|
||||
status: 'running',
|
||||
note: `Still processing after ${timeoutSeconds}s. Call get_mesh_result again with the same ids to keep waiting.`,
|
||||
provider: providerLabel,
|
||||
projectId,
|
||||
name,
|
||||
...(jobId ? { jobId } : {}),
|
||||
...(taskId ? { taskId } : {}),
|
||||
...(region ? { region } : {}),
|
||||
lastStatus: outcome.lastStatus
|
||||
};
|
||||
}
|
||||
return outcome;
|
||||
}));
|
||||
|
||||
server.registerTool('edit_mesh', {
|
||||
title: 'Edit mesh (AI)',
|
||||
description: 'Edit an existing project mesh through a configured custom mesh-edit API (selectedApi must be a custom_* id from Settings).',
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue