zaragoza-tram-stations
Retrieve tram station locations in Zaragoza by providing coordinates to access real-time tram system information.
Instructions
Get all tram stations in Zaragoza
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| latitude | Yes | ||
| longitude | Yes |
Implementation Reference
- index.js:48-70 (handler)The handler function fetches tram station data from the API, computes the nearest stations using latitude and longitude, and returns JSON stringified nearest positions.async ({ latitude, longitude }) => { const response = await fetch("https://dndzgz.herokuapp.com/services/tram"); if (response.ok) { const data = await response.json(); const nearestPositions = getTopNearestPositions( data.locations, latitude, longitude ); return { content: [{ type: "text", text: JSON.stringify(nearestPositions) }], }; } else { return { content: [ { type: "text", text: "It was not possible to get the tram stations", }, ], }; } }
- index.js:41-71 (registration)The tool is registered using server.tool() with name, description, input schema, and handler function.server.tool( "zaragoza-tram-stations", "Get all tram stations in Zaragoza", { latitude: z.number(), longitude: z.number(), }, async ({ latitude, longitude }) => { const response = await fetch("https://dndzgz.herokuapp.com/services/tram"); if (response.ok) { const data = await response.json(); const nearestPositions = getTopNearestPositions( data.locations, latitude, longitude ); return { content: [{ type: "text", text: JSON.stringify(nearestPositions) }], }; } else { return { content: [ { type: "text", text: "It was not possible to get the tram stations", }, ], }; } } );
- index.js:44-47 (schema)Zod schema defining input parameters: latitude and longitude as numbers.{ latitude: z.number(), longitude: z.number(), },
- index.js:259-264 (helper)Helper function that gets the top N (default 10) nearest positions by calling getOrderedPositionsByDistance.function getTopNearestPositions(positions, latitude, longitude, size = 10) { return getOrderedPositionsByDistance(positions, latitude, longitude).slice( 0, size ); }
- index.js:281-297 (helper)Haversine distance calculation helper used to compute distances between coordinates for sorting nearest stations.function haversineDistanceInMeters(position1, position2) { const toRadians = (degrees) => degrees * (Math.PI / 180); const radiusOfEarth = 6371; const dLat = toRadians(position2.lat - position1.lat); const dLon = toRadians(position2.lon - position1.lon); const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(position1.lat)) * Math.cos(toRadians(position2.lat)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return radiusOfEarth * c * 1000; }