findNearbySupermarkets.tsβ’3.93 kB
/**
* MCP Tool: Find Nearby Supermarkets
* Find supermarkets near a given location using GPS coordinates
*/
import type { SuperPrecioApiClient } from '../client/superPrecioApi.js';
export const findNearbySupermarketsTool = {
name: 'find_nearby_supermarkets',
description: `Find supermarkets near your current location using GPS coordinates.
Discover which supermarkets are closest to you!
Features:
- Search by latitude and longitude
- Customizable search radius (default: 5km)
- Get distance in km and meters
- See full addresses and contact info
- Opening hours when available
Perfect for:
- Finding nearby stores after optimizing your shopping list
- Planning your shopping route
- Discovering new supermarkets in your area
- Getting directions to the best-priced store
Example coordinates for Buenos Aires city center:
- Latitude: -34.6037
- Longitude: -58.3816
The tool uses the Haversine formula to calculate accurate distances.`,
inputSchema: {
type: 'object',
properties: {
latitude: {
type: 'number',
description: 'Latitude coordinate (-90 to 90)',
minimum: -90,
maximum: 90,
},
longitude: {
type: 'number',
description: 'Longitude coordinate (-180 to 180)',
minimum: -180,
maximum: 180,
},
radiusKm: {
type: 'number',
description: 'Search radius in kilometers (default: 5km)',
minimum: 0.1,
maximum: 50,
default: 5,
},
},
required: ['latitude', 'longitude'],
},
};
export async function executeFindNearbySupermarkets(
client: SuperPrecioApiClient,
args: { latitude: number; longitude: number; radiusKm?: number }
) {
const response = await client.findNearbySupermarkets({
lat: args.latitude,
lng: args.longitude,
radius: args.radiusKm || 5,
});
if (!response.success) {
return {
content: [
{
type: 'text',
text: `Failed to find nearby supermarkets: ${response.message || 'Unknown error'}`,
},
],
isError: true,
};
}
const locations = response.locations || [];
const count = response.found || 0;
if (count === 0) {
return {
content: [
{
type: 'text',
text: `
π No supermarkets found within ${response.radiusKm}km of your location.
Search location:
Latitude: ${response.searchLocation.latitude}
Longitude: ${response.searchLocation.longitude}
Try:
- Increasing the search radius
- Checking if your coordinates are correct
- Moving to a different location
`,
},
],
};
}
const summary = `
π Nearby Supermarkets (${count} found)
Search radius: ${response.radiusKm}km
Location: ${response.searchLocation.latitude.toFixed(4)}, ${response.searchLocation.longitude.toFixed(4)}
βββββββββββββββββββββββββββββββββββββββββββ
${locations
.map((loc: any, i: number) => {
return `
${i + 1}. ${loc.supermarketName} - ${loc.branchName}
π Distance: ${loc.distance}km (${loc.distanceMeters}m)
π Address: ${loc.address}, ${loc.city}
${loc.phone ? `π Phone: ${loc.phone}` : ''}
${loc.openingHours ? `π Hours: ${loc.openingHours}` : ''}
πΊοΈ Coordinates: ${loc.coordinates.latitude}, ${loc.coordinates.longitude}
`;
})
.join('\n')}
βββββββββββββββββββββββββββββββββββββββββββ
π Closest supermarket: ${locations[0].supermarketName} (${locations[0].distance}km away)
π‘ Tip: After optimizing your shopping list, use this to find the nearest
location of the best-priced supermarket!
`;
return {
content: [
{
type: 'text',
text: summary,
},
{
type: 'text',
text: JSON.stringify(response, null, 2),
},
],
};
}