Skip to main content
Glama
wu-yu-pei
by wu-yu-pei

watch_messages

Wait for a message to arrive for your terminal. Returns immediately upon receipt or when the timeout expires.

Instructions

Block and wait for a message to arrive for this terminal. Returns as soon as a message is received or timeout is reached.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
terminal_nameYesYour terminal name
timeoutNoMax seconds to wait
intervalNoPoll interval in seconds

Implementation Reference

  • The handler function for watch_messages. Polls for messages using list() from messages.js. Deletes messages after reading (deleteAfterRead: true). Loops until a message arrives or timeout is reached, sleeping interval seconds between polls.
      async ({ terminal_name, timeout, interval }) => {
        const deadline = Date.now() + timeout * 1000;
        while (Date.now() < deadline) {
          const messages = list(terminal_name, { deleteAfterRead: true });
          if (messages.length > 0) {
            return {
              content: [
                { type: "text", text: renderMessages(terminal_name, messages) },
              ],
            };
          }
          await sleep(interval * 1000);
        }
        return {
          content: [
            {
              type: "text",
              text: `Watch timed out after ${timeout}s. No messages received for "${terminal_name}".`,
            },
          ],
        };
      },
    );
  • Input schema for watch_messages: terminal_name (required), timeout (default 300s, max 3600s), interval (default 2s, max 60s). Uses Zod for validation.
    {
      terminal_name: nameSchema.describe("Your terminal name"),
      timeout: z
        .number()
        .int()
        .min(1)
        .max(3600)
        .default(300)
        .describe("Max seconds to wait"),
      interval: z
        .number()
        .int()
        .min(1)
        .max(60)
        .default(2)
        .describe("Poll interval in seconds"),
    },
  • src/tools.js:121-163 (registration)
    The watch_messages tool is registered via server.tool() in the registerTools function (src/tools.js). It is registered alongside other tools (register, list_terminals, send_message, get_messages).
    server.tool(
      "watch_messages",
      "Block and wait for a message to arrive for this terminal. Returns as soon as a message is received or timeout is reached.",
      {
        terminal_name: nameSchema.describe("Your terminal name"),
        timeout: z
          .number()
          .int()
          .min(1)
          .max(3600)
          .default(300)
          .describe("Max seconds to wait"),
        interval: z
          .number()
          .int()
          .min(1)
          .max(60)
          .default(2)
          .describe("Poll interval in seconds"),
      },
      async ({ terminal_name, timeout, interval }) => {
        const deadline = Date.now() + timeout * 1000;
        while (Date.now() < deadline) {
          const messages = list(terminal_name, { deleteAfterRead: true });
          if (messages.length > 0) {
            return {
              content: [
                { type: "text", text: renderMessages(terminal_name, messages) },
              ],
            };
          }
          await sleep(interval * 1000);
        }
        return {
          content: [
            {
              type: "text",
              text: `Watch timed out after ${timeout}s. No messages received for "${terminal_name}".`,
            },
          ],
        };
      },
    );
  • The list() function from messages.js, called by watch_messages to poll for messages. It reads JSON message files for the given recipient, filters stale ones, and optionally deletes them after reading.
    export function list(to, { deleteAfterRead = false } = {}, now = Date.now) {
      assertValidName(to);
      ensureDirs();
    
      const prefix = filePrefix(to);
      let entries;
      try {
        entries = readdirSync(messagesDir()).filter(
          (f) => f.startsWith(prefix) && f.endsWith(".json"),
        );
      } catch {
        return [];
      }
      entries.sort();
    
      const out = [];
      const cutoff = now() - MESSAGE_TTL_MS;
    
      for (const file of entries) {
        const path = join(messagesDir(), file);
        const data = /** @type {{from?: string, sentAt?: string, summary?: string, content?: string} | null} */ (
          readJsonSafe(path)
        );
        if (!data) {
          tryUnlink(path);
          continue;
        }
        const sentMs = data.sentAt ? Date.parse(data.sentAt) : NaN;
        if (Number.isFinite(sentMs) && sentMs < cutoff) {
          tryUnlink(path);
          continue;
        }
        out.push({ file, ...data });
      }
    
      if (deleteAfterRead) {
        for (const m of out) {
          tryUnlink(join(messagesDir(), m.file));
        }
      }
    
      return out;
    }
  • Helper sleep function used by watch_messages to wait between poll intervals.
    function sleep(ms) {
      return new Promise((resolve) => setTimeout(resolve, ms));
    }
Behavior3/5

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

Describes blocking behavior and timeout, but omits what is returned (message content? confirmation?) and behavior on timeout. No annotations to supplement.

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?

Two sentences, front-loaded, no extraneous words.

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?

Missing critical info about return value and whether it is a synthetic or real-time wait. With no output schema, description should explain what the tool returns.

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 covers all parameters with descriptions. Description adds no extra meaning beyond schema.

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?

Clearly states verb ('Block and wait') and resource ('message for this terminal'). Distinguishes from siblings like get_messages (non-blocking retrieval) and send_message.

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

Usage Guidelines3/5

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

Implies a blocking use case for real-time waiting, but no explicit guidance on when to choose this over get_messages (polling) or alternatives.

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/wu-yu-pei/mcp-terminal-share'

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