execute_map_reduce_mcp_client
Execute parallel processing of multiple items using a map prompt, then sequentially reduce the results to a single output with a reduce prompt. Designed for efficient task delegation and context offloading within the MCP Inception MCP Server.
Instructions
Process multiple items in parallel then sequentially reduce the results to a single output.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| initialValue | No | Initial value for the accumulator (optional). | |
| items | Yes | Array of items to process. | |
| mapPrompt | Yes | Template prompt for processing each individual item. Use {item} as placeholder for the current item. | |
| reducePrompt | Yes | Template prompt for reducing results. Use {accumulator} and {result} as placeholders. |
Implementation Reference
- src/index.ts:119-175 (handler)Core implementation of the map-reduce tool: processes items in parallel (map phase with concurrency control using mapPrompt replacing {item}), collects results, then sequentially reduces using reducePrompt (replacing {accumulator} and {result}), handling errors.private async executeMapReduce( mapPrompt: string, reducePrompt: string, items: string[], initialValue?: string ): Promise<{result: string, errors: string[]}> { const errors: string[] = []; let accumulator = initialValue || ''; try { // Step 1: Process all items in parallel (map phase) const mapResults: string[] = []; // Process items in chunks based on maxConcurrent (similar to executeParallel) for (let i = 0; i < items.length; i += this.maxConcurrent) { const chunk = items.slice(i, i + this.maxConcurrent); const promises = chunk.map(async (item) => { try { // Format the map prompt by replacing {item} with the current item const formattedMapPrompt = mapPrompt.replace(/{item}/g, item); const { stdout, stderr } = await this.safeCommandPipe(formattedMapPrompt, this.executable, true); if (stdout) { return stdout; } else if (stderr) { errors.push(`Error processing item "${item}": ${stderr}`); return null; } } catch (error: any) { errors.push(`Failed to process item "${item}": ${error.message}`); return null; } }); // Wait for current chunk to complete before processing next chunk const results = await Promise.all(promises); mapResults.push(...results.filter(Boolean) as string[]); } // Step 2: Sequentially reduce the results for (const result of mapResults) { // Format the reduce prompt by replacing {accumulator} and {result} placeholders const formattedReducePrompt = reducePrompt .replace(/{accumulator}/g, accumulator) .replace(/{result}/g, result); const { stdout, stderr } = await this.safeCommandPipe(formattedReducePrompt, this.executable, true); if (stdout) { accumulator = stdout; } } return { result: accumulator, errors }; } catch (error: any) { errors.push(`Map-reduce operation failed: ${error.message}`); return { result: accumulator, errors }; } }
- src/index.ts:245-267 (schema)Input schema for the execute_map_reduce_mcp_client tool, defining properties and requirements.inputSchema: { type: 'object', properties: { mapPrompt: { type: 'string', description: 'Template prompt for processing each individual item. Use {item} as placeholder for the current item.', }, reducePrompt: { type: 'string', description: 'Template prompt for reducing results. Use {accumulator} and {result} as placeholders.', }, initialValue: { type: 'string', description: 'Initial value for the accumulator (optional).', }, items: { type: 'array', items: { type: 'string' }, description: 'Array of items to process.', }, }, required: ['mapPrompt', 'reducePrompt', 'items'], },
- src/index.ts:242-268 (registration)Registration of the tool in the ListTools response, including name, description, and full input schema.{ name: 'execute_map_reduce_mcp_client', description: 'Process multiple items in parallel then sequentially reduce the results to a single output.', inputSchema: { type: 'object', properties: { mapPrompt: { type: 'string', description: 'Template prompt for processing each individual item. Use {item} as placeholder for the current item.', }, reducePrompt: { type: 'string', description: 'Template prompt for reducing results. Use {accumulator} and {result} as placeholders.', }, initialValue: { type: 'string', description: 'Initial value for the accumulator (optional).', }, items: { type: 'array', items: { type: 'string' }, description: 'Array of items to process.', }, }, required: ['mapPrompt', 'reducePrompt', 'items'], }, },
- src/index.ts:326-361 (registration)Handler registration in the CallToolRequest switch statement: parses arguments, invokes the executeMapReduce method, and returns result/errors as JSON or error response.case 'execute_map_reduce_mcp_client': { const args = request.params.arguments as { mapPrompt: string; reducePrompt: string; items: string[]; initialValue?: string; }; try { const { result, errors } = await this.executeMapReduce( args.mapPrompt, args.reducePrompt, args.items, args.initialValue ); return { content: [ { type: 'text', text: JSON.stringify({ result, errors }, null, 2), }, ], isError: errors.length > 0, }; } catch (error: any) { return { content: [ { type: 'text', text: `Error executing map-reduce operation: ${error?.message || 'Unknown error'}`, }, ], isError: true, }; } }