Skip to main content
Glama

wait_for

Blocks execution until an agent reaches a specified state (ready, done, error) with retroactive checking and timeout control.

Instructions

Block until an agent reaches a target state (ready, done, error). Checks retroactively first.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
agent_idYesAgent ID from spawn_agent
target_stateYesState to wait for
timeout_msNoTimeout in milliseconds (default: 5 minutes)

Implementation Reference

  • The core logic for waitFor which checks agent states and manages waiting/timeouts.
    async waitFor(
      agentId: string,
      targetState: AgentState,
      timeoutMs: number,
    ): Promise<WaitResult> {
      const start = Date.now();
    
      // Check if agent exists
      const initial = this.registry.get(agentId);
      if (!initial) {
        throw new Error(`Agent not found: ${agentId}`);
      }
    
      // Retroactive check — already in target state?
      if (initial.state === targetState) {
        return {
          matched: true,
          state: initial.state,
          elapsed: Date.now() - start,
          source: "immediate",
        };
      }
    
      // Already in terminal error state and target isn't error?
      if (initial.state === "error" && targetState !== "error") {
        return {
          matched: false,
          state: initial.state,
          elapsed: Date.now() - start,
          source: "immediate",
          error: initial.error ?? "Agent is in error state",
        };
      }
    
      // Already in terminal done state and target isn't done?
      if (initial.state === "done" && targetState !== "done") {
        return {
          matched: false,
          state: initial.state,
          elapsed: Date.now() - start,
          source: "immediate",
          error: "Agent has already completed",
        };
      }
    
      // Polling sweep loop
      return new Promise<WaitResult>((resolve) => {
        const checkInterval = setInterval(async () => {
          const elapsed = Date.now() - start;
          if (elapsed >= timeoutMs) {
            clearInterval(checkInterval);
            const current = this.registry.get(agentId);
            resolve({
              matched: false,
              state: current?.state ?? "error",
              elapsed,
              source: "timeout",
              error: `Timed out after ${timeoutMs}ms waiting for state "${targetState}"`,
            });
            return;
          }
    
          // Re-read from disk (another process may have updated)
          await this.registry.reconcile();
          const current = this.registry.get(agentId);
          if (!current) {
            clearInterval(checkInterval);
            resolve({
              matched: false,
              state: "error",
              elapsed,
              source: "sweep",
              error: "Agent disappeared during wait",
  • src/server.ts:716-745 (registration)
    MCP tool registration for the 'wait_for' tool in src/server.ts.
    // 12. wait_for
    server.tool(
      "wait_for",
      "Block until an agent reaches a target state (ready, done, error). Checks retroactively first.",
      {
        agent_id: z.string().describe("Agent ID from spawn_agent"),
        target_state: z
          .enum(["ready", "working", "idle", "done", "error"])
          .describe("State to wait for"),
        timeout_ms: z
          .number()
          .int()
          .positive()
          .optional()
          .default(300000)
          .describe("Timeout in milliseconds (default: 5 minutes)"),
      },
      async (args) => {
        try {
          const result = await engine.waitFor(
            args.agent_id,
            args.target_state,
            args.timeout_ms,
          );
          return ok({ ...result });
        } catch (e) {
          return err(e);
        }
      },
    );
Behavior3/5

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

With no annotations provided, the description carries the full burden. It discloses key behavioral traits: it's a blocking operation, it checks retroactively first (implying it might return immediately if the agent is already in the target state), and it waits for state transitions. However, it doesn't mention error handling, what happens on timeout, or the return format (e.g., success/failure indicators).

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 purpose ('Block until an agent reaches a target state') and adds a key behavioral detail ('Checks retroactively first'). Every word earns its place with zero waste.

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

Completeness3/5

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

Given no annotations and no output schema, the description is adequate but has gaps. It covers the purpose and a key behavior, but doesn't explain what the tool returns (e.g., success/failure, timeout handling) or error conditions. For a blocking tool with state dependencies, more context on outcomes would be helpful.

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 additional parameter semantics beyond what's in the schema (e.g., it doesn't explain the implications of 'checks retroactively first' on parameters). Baseline 3 is appropriate when the schema does the heavy lifting.

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

Purpose5/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 with specific verbs ('block until', 'checks retroactively') and identifies the resource ('agent') and target ('target state'). It distinguishes from siblings like get_agent_state (which checks current state without waiting) and wait_for_all (which waits for multiple agents).

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

Usage Guidelines4/5

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

The description implies usage context by mentioning 'agent reaches a target state' and 'checks retroactively first', suggesting it's for monitoring agent state transitions. However, it doesn't explicitly state when to use this versus alternatives like get_agent_state (for immediate checks) or wait_for_all (for multiple agents), nor does it mention prerequisites like needing a spawned agent first.

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/EtanHey/cmuxlayer'

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