Skip to main content
Glama

Prevent Sleep

prevent_sleep

Prevent your Mac from sleeping for a set duration. Use this to avoid interruptions during downloads, presentations, or extended workflows.

Instructions

Prevent the Mac from sleeping for a specified duration using caffeinate.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
secondsNoDuration in seconds (default: 3600 = 1 hour, max: 86400 = 24 hours)

Implementation Reference

  • Handler function for the 'prevent_sleep' tool. Kills any previous caffeinate processes, runs the JXA script to start a new caffeinate process, tracks its PID, and returns the result.
      async ({ seconds }) => {
        try {
          // Kill any previous caffeinate processes before starting a new one
          for (const pid of caffeinatePids) {
            try {
              process.kill(pid);
            } catch {
              /* already exited */
            }
          }
          caffeinatePids.clear();
          const result = await runJxa<{ action: string; pid: number; seconds: number }>(preventSleepScript(seconds));
          caffeinatePids.add(result.pid);
          return ok(result);
        } catch (e) {
          return errJxaFor("prevent sleep", e);
        }
      },
    );
  • Registration of the 'prevent_sleep' tool with title, description, input schema (seconds: number, optional, default 3600, max 86400), and annotations.
    server.registerTool(
      "prevent_sleep",
      {
        title: "Prevent Sleep",
        description:
          "Prevent the Mac from sleeping for a specified duration using caffeinate. Returns the process PID for cancellation.",
        inputSchema: {
          seconds: z
            .number()
            .int()
            .min(1)
            .max(86400)
            .optional()
            .default(3600)
            .describe("Duration in seconds (default: 3600 = 1 hour, max: 86400 = 24 hours)"),
        },
        annotations: {
          readOnlyHint: false,
          destructiveHint: false,
          idempotentHint: false,
          openWorldHint: false,
        },
  • Registration call for 'prevent_sleep' tool on the MCP server via server.registerTool().
    server.registerTool(
      "prevent_sleep",
      {
        title: "Prevent Sleep",
        description:
          "Prevent the Mac from sleeping for a specified duration using caffeinate. Returns the process PID for cancellation.",
        inputSchema: {
          seconds: z
            .number()
            .int()
            .min(1)
            .max(86400)
            .optional()
            .default(3600)
            .describe("Duration in seconds (default: 3600 = 1 hour, max: 86400 = 24 hours)"),
        },
        annotations: {
          readOnlyHint: false,
          destructiveHint: false,
          idempotentHint: false,
          openWorldHint: false,
        },
      },
      async ({ seconds }) => {
        try {
          // Kill any previous caffeinate processes before starting a new one
          for (const pid of caffeinatePids) {
            try {
              process.kill(pid);
            } catch {
              /* already exited */
            }
          }
          caffeinatePids.clear();
          const result = await runJxa<{ action: string; pid: number; seconds: number }>(preventSleepScript(seconds));
          caffeinatePids.add(result.pid);
          return ok(result);
        } catch (e) {
          return errJxaFor("prevent sleep", e);
        }
      },
    );
  • Helper function 'preventSleepScript' that generates a JXA script string. The script runs 'caffeinate -t <seconds>' in the shell and returns the PID and action info via JSON.
    export function preventSleepScript(seconds: number): string {
      const secs = Math.max(1, Math.round(seconds));
      return `
        const app = Application.currentApplication();
        app.includeStandardAdditions = true;
        const pid = app.doShellScript('caffeinate -t ${secs} & echo $!');
        JSON.stringify({action: 'prevent_sleep', pid: parseInt(pid.trim()), seconds: ${secs}});
      `;
    }
  • Module-level Set<number> 'caffeinatePids' to track active caffeinate PIDs, with cleanup on server exit via process.on('exit') handler.
    const caffeinatePids = new Set<number>();
    
    // Clean up ALL orphaned caffeinate processes on server exit
    process.on("exit", () => {
      for (const pid of caffeinatePids) {
        try {
          process.kill(pid);
        } catch {
          /* already exited */
        }
      }
      caffeinatePids.clear();
    });
Behavior2/5

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

Annotations provide minimal behavior cues (not read-only, not destructive, not idempotent). The description mentions 'using caffeinate' but does not disclose important details such as whether it prevents display sleep or system sleep, whether it requires permissions, or what happens after the duration expires. More transparency is needed given sparse annotations.

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 sentence that conveys the essential information without unnecessary words. It is front-loaded and efficient.

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 the simplicity of the tool and that the schema and annotations are present, the description covers the basic purpose. However, it could be more complete by explaining what 'using caffeinate' entails (e.g., permissions, behavior when lid is closed). It is adequate but not thorough.

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?

The input schema fully documents the single parameter (seconds) with description, min, max, and default. The description adds no additional meaning beyond the schema, so baseline score of 3 is appropriate.

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 prevents Mac from sleeping for a specified duration using caffeinate. It identifies the specific verb (prevent), resource (Mac sleep), and distinguishes from sibling tools like system_sleep (which puts Mac to sleep) and system_power (power management).

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?

The description describes what the tool does but does not explicitly state when to use it versus alternatives. While siblings like system_sleep suggest opposite actions, no direct guidance is given on when not to use this tool or which alternative to choose for other sleep-related tasks.

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/heznpc/AirMCP'

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