Wait for Task Completion
task_barrierWait for all submitted tasks to complete and retrieve results in batch. Submit multiple tasks first, then use this to collect all outputs. Clears completed tasks.
Instructions
Wait for ALL submitted tasks to complete and retrieve results. Essential for batch processing - submit multiple tasks first, then call task_barrier once to collect all results efficiently. Clears completed tasks.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:122-187 (handler)The main handler logic for the task_barrier tool. It validates params (empty object), calls taskManager.barrier() to wait for all submitted tasks, formats results (success/failure for image and TTS tasks), then calls clearCompletedTasks() to prevent memory leaks.
// Task barrier tool server.registerTool( "task_barrier", { title: "Wait for Task Completion", description: "Wait for ALL submitted tasks to complete and retrieve results. Essential for batch processing - submit multiple tasks first, then call task_barrier once to collect all results efficiently. Clears completed tasks.", inputSchema: taskBarrierSchema.shape }, async (params: unknown): Promise<ToolResponse> => { try { validateTaskBarrierParams(params); const { completed, results } = await taskManager.barrier(); if (completed === 0) { return { content: [{ type: "text", text: "ℹ️ No tasks were submitted before this barrier." }] }; } // Format results const resultSummaries = results.map(({ taskId, success, result, error }) => { if (!success) { return `❌ Task ${taskId}: FAILED - ${error?.message || 'Unknown error'}`; } // Format success results based on task type if (result?.files) { // Image generation result const warnings = result.warnings ? ` (${result.warnings.length} warnings)` : ''; return `✅ Task ${taskId}: Generated ${result.count} image(s)${warnings}`; } else if (result?.audioFile) { // TTS generation result const subtitles = result.subtitleFile ? ` + subtitles` : ''; const warnings = result.warnings ? ` (${result.warnings.length} warnings)` : ''; return `✅ Task ${taskId}: Generated speech${subtitles}${warnings}`; } else { // Generic success return `✅ Task ${taskId}: Completed successfully`; } }); const summary = resultSummaries.join('\n'); // Clear completed tasks to prevent memory leaks taskManager.clearCompletedTasks(); return { content: [{ type: "text", text: summary }] }; } catch (error: any) { ErrorHandler.logError(error, { tool: 'task_barrier' }); return { content: [{ type: "text", text: `❌ Task barrier failed: ${ErrorHandler.formatErrorForUser(error)}` }] }; } } ); - src/core/task-manager.ts:92-107 (handler)The barrier() method in TaskManager class. Waits for all active tasks via Promise.allSettled, then returns all completed tasks (taskId, success, result, error). This is the core blocking/waiting mechanism.
async barrier(): Promise<BarrierResult> { const activeTasks = Array.from(this.tasks.values()); // Wait for any active tasks to complete if (activeTasks.length > 0) { await Promise.allSettled(activeTasks); } // Return all completed tasks (including those completed before this barrier call) const results = Array.from(this.completedTasks.entries()).map(([taskId, taskResult]) => ({ taskId, ...taskResult })); return { completed: results.length, results }; } - src/index.ts:123-129 (registration)Registration of the task_barrier tool via server.registerTool with name 'task_barrier', title 'Wait for Task Completion', description explaining batch usage, and inputSchema from taskBarrierSchema.shape (empty object).
server.registerTool( "task_barrier", { title: "Wait for Task Completion", description: "Wait for ALL submitted tasks to complete and retrieve results. Essential for batch processing - submit multiple tasks first, then call task_barrier once to collect all results efficiently. Clears completed tasks.", inputSchema: taskBarrierSchema.shape }, - src/config/schemas.ts:143-149 (schema)Zod schema definition for task_barrier - an empty object schema (no params needed). Also includes the TaskBarrierParams type inference and the JSON schema variant taskBarrierToolSchema (lines 305-308).
// Task barrier schema export const taskBarrierSchema = z.object({}); // Type definitions for parsed schemas export type ImageGenerationParams = z.infer<typeof imageGenerationSchema>; export type TextToSpeechParams = z.infer<typeof textToSpeechSchema>; export type TaskBarrierParams = z.infer<typeof taskBarrierSchema>; - src/config/schemas.ts:350-360 (helper)Validation helper function validateTaskBarrierParams that parses input against the empty taskBarrierSchema, throwing friendly error messages on failure.
export function validateTaskBarrierParams(params: unknown): TaskBarrierParams { try { return taskBarrierSchema.parse(params); } catch (error) { if (error instanceof z.ZodError) { const messages = error.errors.map(e => `${e.path.join('.')}: ${e.message}`); throw new Error(`Validation failed: ${messages.join(', ')}`); } throw error; } }