Skip to main content
Glama
leo4life2

Minecraft MCP Server

by leo4life2

sleepInNearbyBed

Locate and use the nearest bed within a specified distance to restore player health and reset spawn points in Minecraft, enabling efficient gameplay progress.

Instructions

Find and sleep in a nearby bed

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
maxDistanceNoMaximum distance to search for bed (default: 30)

Implementation Reference

  • Main handler function that implements the logic for finding a nearby bed, navigating to it, checking if it's nighttime, sleeping in the bed, and handling wake-up conditions including interruptions and other players' sleep status.
    export const sleepInNearbyBed = async (
      bot: Bot,
      params: ISkillParams,
      serviceParams: ISkillServiceParams,
    ): Promise<boolean> => {
      const skillName = 'sleepInNearbyBed';
      const requiredParams: string[] = [];
      const isParamsValid = validateSkillParams(
        { ...serviceParams },
        requiredParams,
        skillName,
      );
      if (!isParamsValid) {
        serviceParams.cancelExecution?.();
        bot.emit(
          'alteraBotEndObservation',
          `Mistake: You didn't provide all of the required parameters ${requiredParams.join(', ')} for the ${skillName} skill.`,
        );
        return false;
      }
    
      const defaultParams = {
        maxDistance: 10,
      };
      const unpackedParams = {
        maxDistance: params.maxDistance ?? defaultParams.maxDistance,
        signal: serviceParams.signal,
      };
      const { maxDistance, signal } = unpackedParams;
      const TIME_TO_FALL_ASLEEP_S = 10;
      const SLEEP_IN_TICKS = TIME_TO_FALL_ASLEEP_S * 20;
    
      // Find the nearest bed within the specified range
      const bed = bot.findBlock({
        matching: (block) => bot.isABed(block),
        maxDistance: maxDistance,
      });
    
      // No bed is found
      if (!bed) {
        bot.emit(
          'alteraBotEndObservation',
          `Mistake: you tried to sleep in a bed nearby but found no bed around.`,
        );
        return false;
      }
      console.log(
        `Found a bed at {${bed.position.x},${bed.position.y},${bed.position.z}}.`,
      );
    
      // Get to bed
      try {
        navigateToLocation(bot, {
          x: bed.position.x,
          y: bed.position.y,
          z: bed.position.z,
          signal,
          range: 1,
        });
      } catch (error) {
        bot.emit(
          'alteraBotEndObservation',
          `You tried to navigate to bed at {${bed.position.x},${bed.position.y},${bed.position.z}} but could not.`,
        );
      }
    
      console.log(
        `Searching for a bed within ${maxDistance} blocks to sleep in.`,
      );
      // It's day time, can't sleep
      if (!(bot.time.timeOfDay >= 13000 && bot.time.timeOfDay <= 23000)) {
        bot.emit(
          'alteraBotEndObservation',
          `Mistake: you tried to sleep but it is daytime.`,
        );
        return false;
      }
    
      // Attempt to go to the bed and sleep
      try {
        await bot.sleep(bed);
        bot.emit('alteraBotTextObservation', `You have started sleeping.`);
        let cur_sleep_ticks = 0;
        let woke_up_naturally = false;
    
        const checkForWake = () => {
          console.log(`Woke up naturally.`);
          woke_up_naturally = true;
        };
    
        // Set up a promise that resolves when the bot wakes up
        new Promise((resolve) => {
          bot.once('wake', checkForWake);
          resolve(true);
        });
    
        while (cur_sleep_ticks < SLEEP_IN_TICKS && !woke_up_naturally) {
          if (isSignalAborted(signal)) {
            // You interrupted your own sleep
            bot.removeListener('wake', checkForWake);
            return bot.emit(
              'alteraBotEndObservation',
              `You decided to do something else instead of sleeping.`,
            );
          }
          await bot.waitForTicks(1);
          cur_sleep_ticks++;
        }
        bot.removeListener('wake', checkForWake);
    
        // Have to wait >20 ticks for game time to update
        await bot.waitForTicks(40);
        console.log(
          `BOT DONE SLEEPING, TIME: ${bot.time.timeOfDay} SLEEP TICKS: ${cur_sleep_ticks}, WOKE UP NATURALLY: ${woke_up_naturally}`,
        );
        // At this point, you finished sleeping without getting interrupted, but you don't know if you've slept through the whole night
        if (bot.time.timeOfDay < 13000 || bot.time.timeOfDay > 23000) {
          bot.emit(
            'alteraBotEndObservation',
            `You have slept through the entire night and have woken up naturally.`,
          );
          return true;
        } else {
          const msg = checkOtherPlayersSleeping(bot);
          bot.emit(
            'alteraBotEndObservation',
            `You woke up before the night was skipped. ${msg}`,
          );
          bot.wake();
          return false;
        }
      } catch (error) {
        const err_str = `ERROR: You tried to sleep in the nearby bed but failed because ${error}`;
        console.error(err_str);
        bot.emit('alteraBotEndObservation', err_str);
        return false;
      }
    };
  • Registration entry in SKILL_METADATA for the sleepInNearbyBed skill, including description, input parameters schema (maxDistance), and required fields.
    sleepInNearbyBed: {
        description: "Find and sleep in a nearby bed",
        params: {
            maxDistance: { type: "number", description: "Maximum distance to search for bed (default: 30)" }
        },
        required: []
    },
  • Helper function called by the handler to determine why the bot woke up prematurely by checking if other players are sleeping.
    const checkOtherPlayersSleeping = (bot: Bot): string => {
      // If the bot woke up and it is still nighttime, it suggests other players didn't sleep
      const playerNames = Object.keys(bot.players);
      const sleepingPlayers = [];
      const awakePlayers: string[] = [];
    
      playerNames.forEach((name) => {
        const playerEntity = bot.players[name].entity;
        if (!(name == bot.username)) {
          if (playerEntity && (playerEntity as any).isSleeping) {
            sleepingPlayers.push(name);
          } else {
            awakePlayers.push(name);
          }
        }
      });
    
      if (awakePlayers.length > 0) {
        return `Players that were not sleeping: ${awakePlayers.join(', ')}.`;
      } else {
        return `ERROR: You don't know why you did not sleep, because it is neither you interrupting your own sleep or other players.`;
      }
    };
  • Construction of the inputSchema for skills from SKILL_METADATA during registration in loadSkills function.
        type: "object",
        properties: metadata.params,
        required: metadata.required
    },
    execute: createSkillExecutor(skillName)
Behavior2/5

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

With no annotations, the description carries full burden but only states the action without behavioral details. It doesn't disclose what 'sleep' does (e.g., health restoration, time passage, side effects), success/failure conditions, or environmental requirements (e.g., bed availability, safety).

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 with zero wasted words. It's front-loaded with the core action ('find and sleep'), making it immediately understandable and appropriately sized for the tool's apparent simplicity.

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 no annotations or output schema, the description is incomplete for a tool that likely involves game mechanics like health effects or time changes. It lacks details on outcomes, failure modes, or context needed for reliable agent use beyond the basic action stated.

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 parameter 'maxDistance' is fully documented in the schema. The description adds no additional parameter semantics beyond implying 'nearby' relates to distance, matching the baseline for high schema coverage.

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 action ('find and sleep') and target resource ('bed'), making the purpose understandable. However, it doesn't differentiate from sibling tools like 'rest' or specify what 'sleep' entails in this context, preventing a perfect score.

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?

No guidance is provided on when to use this tool versus alternatives like 'rest' or 'goToKnownLocation'. The description implies proximity-based usage ('nearby'), but lacks explicit conditions, prerequisites, or exclusions for effective tool selection.

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

Related 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/leo4life2/minecraft-mcp-http'

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