Skip to main content
Glama

execute_map_reduce_mcp_client

Process multiple items in parallel using a map prompt, then sequentially combine results into a single output with a reduce prompt for task delegation and context management.

Instructions

Process multiple items in parallel then sequentially reduce the results to a single output.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mapPromptYesTemplate prompt for processing each individual item. Use {item} as placeholder for the current item.
reducePromptYesTemplate prompt for reducing results. Use {accumulator} and {result} as placeholders.
initialValueNoInitial value for the accumulator (optional).
itemsYesArray of items to process.

Implementation Reference

  • Core handler function implementing the map-reduce logic: processes items in parallel (map phase) using the LLM executable via safeCommandPipe, collects results, then sequentially reduces them using reducePrompt.
    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 };
      }
    }
  • Tool dispatch handler in the CallToolRequestSchema switch statement: parses input arguments, calls executeMapReduce, and formats the response as JSON.
    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,
        };
      }
    }
  • Input schema defining the structure for mapPrompt, reducePrompt, optional initialValue, and items array.
    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 execute_map_reduce_mcp_client tool in the ListToolsRequestSchema handler, including name, description, and 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'],
      },
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions parallel processing and sequential reduction, but lacks details on execution limits (e.g., rate limits, concurrency), error handling, or output format. For a tool with 4 parameters and no output schema, this is insufficient to inform safe or effective use.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core functionality. Every word earns its place by concisely explaining the two-phase process, with no redundant or vague language.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (map-reduce with 4 parameters), lack of annotations, and no output schema, the description is incomplete. It doesn't address behavioral aspects like performance, errors, or result format, which are critical for an agent to use this tool correctly in context with siblings.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all parameters. The description adds no parameter-specific semantics beyond implying a map-reduce workflow, which is already suggested by parameter names like 'mapPrompt' and 'reducePrompt'. Thus, it meets the baseline of 3 without compensating for gaps.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Process multiple items in parallel then sequentially reduce the results to a single output.' This specifies the verb ('process' and 'reduce') and resource ('multiple items'), but doesn't explicitly distinguish it from sibling tools like 'execute_parallel_mcp_client' or 'execute_mcp_client', which likely have different processing patterns.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus its siblings ('execute_mcp_client' and 'execute_parallel_mcp_client'). It implies usage for parallel processing followed by reduction, but doesn't specify alternatives, prerequisites, or exclusions, leaving the agent to guess based on tool names alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/tanevanwifferen/mcp-inception'

If you have feedback or need assistance with the MCP directory API, please join our Discord server