Skip to main content
Glama

getNearbyEntities

Retrieve a list of all entities within a specified block range in Minecraft, enabling detection of nearby mobs, players, and items for interaction or monitoring purposes.

Instructions

Get a list of all entities nearby

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
rangeNoRange in blocks to search for entities

Implementation Reference

  • The handler function that checks connection, filters nearby entities by distance, groups them by type, formats a detailed list with distances and IDs, and returns a success response.
    async ({ range }) => {
      if (!botState.isConnected || !botState.bot) {
        return createNotConnectedResponse()
      }
    
      try {
        // Get all entities within range
        const nearbyEntities = Object.values(botState.bot.entities).filter(
          (entity) => {
            if (!entity || !entity.position || !botState.bot) return false
            const distance = entity.position.distanceTo(
              botState.bot.entity.position
            )
            return distance <= range && entity.id !== botState.bot.entity.id
          }
        )
    
        if (nearbyEntities.length === 0) {
          return createSuccessResponse('No entities found nearby.')
        }
    
        // Group entities by type
        const groupedEntities: Record<string, any[]> = {}
        nearbyEntities.forEach((entity) => {
          const type = String(entity.type || 'unknown')
          if (!groupedEntities[type]) {
            groupedEntities[type] = []
          }
          groupedEntities[type].push(entity)
        })
    
        // Format the response
        let response = `Entities within ${range} blocks:\n\n`
    
        for (const [type, entities] of Object.entries(groupedEntities)) {
          response += `${type.toUpperCase()} (${entities.length}):\n`
          entities.forEach((entity: any) => {
            const distance = entity.position
              .distanceTo(botState.bot!.entity.position)
              .toFixed(1)
            const name =
              entity.name ||
              entity.username ||
              entity.displayName ||
              `Entity #${entity.id}`
            response += `- ${name} (${distance} blocks away, ID: ${entity.id})\n`
          })
          response += '\n'
        }
    
        return createSuccessResponse(response)
      } catch (error) {
        return createErrorResponse(error)
      }
    }
  • Zod schema defining the input parameters for the tool, specifically the optional 'range' parameter defaulting to 10 blocks.
    {
      range: z
        .number()
        .optional()
        .default(10)
        .describe('Range in blocks to search for entities'),
    },
  • The server.tool call that registers the 'getNearbyEntities' tool with its description, input schema, and handler function.
    server.tool(
      'getNearbyEntities',
      'Get a list of all entities nearby',
      {
        range: z
          .number()
          .optional()
          .default(10)
          .describe('Range in blocks to search for entities'),
      },
      async ({ range }) => {
        if (!botState.isConnected || !botState.bot) {
          return createNotConnectedResponse()
        }
    
        try {
          // Get all entities within range
          const nearbyEntities = Object.values(botState.bot.entities).filter(
            (entity) => {
              if (!entity || !entity.position || !botState.bot) return false
              const distance = entity.position.distanceTo(
                botState.bot.entity.position
              )
              return distance <= range && entity.id !== botState.bot.entity.id
            }
          )
    
          if (nearbyEntities.length === 0) {
            return createSuccessResponse('No entities found nearby.')
          }
    
          // Group entities by type
          const groupedEntities: Record<string, any[]> = {}
          nearbyEntities.forEach((entity) => {
            const type = String(entity.type || 'unknown')
            if (!groupedEntities[type]) {
              groupedEntities[type] = []
            }
            groupedEntities[type].push(entity)
          })
    
          // Format the response
          let response = `Entities within ${range} blocks:\n\n`
    
          for (const [type, entities] of Object.entries(groupedEntities)) {
            response += `${type.toUpperCase()} (${entities.length}):\n`
            entities.forEach((entity: any) => {
              const distance = entity.position
                .distanceTo(botState.bot!.entity.position)
                .toFixed(1)
              const name =
                entity.name ||
                entity.username ||
                entity.displayName ||
                `Entity #${entity.id}`
              response += `- ${name} (${distance} blocks away, ID: ${entity.id})\n`
            })
            response += '\n'
          }
    
          return createSuccessResponse(response)
        } catch (error) {
          return createErrorResponse(error)
        }
      }
    )

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/nacal/mcp-minecraft-remote'

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