getNearbyEntities
Retrieve a list of all entities within a specified range in Minecraft. Ideal for tracking mobs, players, or objects nearby, enabling precise entity interaction and navigation on MCP Minecraft Remote servers.
Instructions
Get a list of all entities nearby
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| range | No | Range in blocks to search for entities |
Implementation Reference
- src/tools/entityInteraction.ts:23-77 (handler)The main handler function for the 'getNearbyEntities' tool. It filters nearby entities based on distance from the bot, groups them by type, calculates distances, and returns a formatted string listing the 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) } }
- src/tools/entityInteraction.ts:16-22 (schema)Zod schema defining the input parameter 'range' (optional number, default 10) for searching nearby entities.{ range: z .number() .optional() .default(10) .describe('Range in blocks to search for entities'), },
- src/tools/entityInteraction.ts:14-78 (registration)Registration of the 'getNearbyEntities' tool using server.tool(), including name, description, schema, and handler.'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) } } )
- src/tools/index.ts:35-35 (registration)Top-level registration call to registerEntityInteractionTools(), which includes the 'getNearbyEntities' tool, as part of registerAllTools().registerEntityInteractionTools()