The nearest station can be the wrong place to start a trip. Walk a little farther and you might reach a direct connection; choose the closest stop automatically and you may spend the time you saved changing lines. A route planner can calculate a perfectly valid path while missing the journey you would actually prefer.
That problem shaped the assistant I added to my Mexico City transit map. Gemini handles the conversation and chooses tools; PostgreSQL calculates the route. The interesting work happens between "near the museum" and the pair of stations passed to the route engine. Which museum? Which stations? And what does "best" mean for this traveler?
The source is oorona/metro. Its saved data and inspectable tool calls let us separate those questions instead of trusting an answer because it sounds like directions.
Start with the place, then choose the station
Consider an illustrative request: "I'm near the museum. How do I get to the stadium?" This is a worked example of the decisions the assistant faces, not a recorded conversation or a measured trip.
The first useful response may be a question. "The museum" needs to resolve to a particular place, and the origin may still be missing. Once the locations are clear, geocoding turns each landmark into coordinates. A proximity lookup then finds nearby stations. Only now does the assistant have candidates for a transit journey.
I prefer comparing those candidates to blindly picking the nearest stop. A short walk to a useful rail connection can beat a slightly closer bus stop. For our museum request, the comparison needs the walk to the departure station, the transit route, and the walk from the arrival station to the stadium. Optimizing only the middle leaves part of the passenger's trip out of the decision.
The prompt asks the agent to compare total journey time, including walking. Whether it follows that instruction consistently needs an evaluation set; a prompt instruction is not a measured success rate. It also needs enough place information to distinguish similar names. If the lookup is ambiguous, asking for clarification is a useful result. Choosing the wrong museum makes every later calculation precise about the wrong trip.
The MCP server exposes the backend operations as callable tools. The Gemini agent discovers them and runs a bounded loop: in the inspected implementation, it allows eight iterations and uses a 30-second tool timeout. A station-to-station question can go directly from resolved station identifiers to routing. A landmark question requires the extra lookups and candidate comparisons.
What the route engine can know
Before comparing journeys, we need to know what the map represents. The backend ingests GTFS, a transit feed format, into PostGIS tables for stops, trips, stop times, and shapes. It builds a graph for pgRouting: stations become nodes, and rides or walking transfers become connections between them, called edges.
GTFS separates a route, a particular trip, and that trip's ordered stops. Those identifiers do different jobs. A route name helps a passenger recognize the service; the stop sequence tells the database which neighboring stops can be connected. Joining every stop on a line to every other stop would create connections the feed never described.
The repository documents an October 2022 GTFS snapshot and ridership files through March 2026. A calculated path is an estimate under the stored graph's assumptions. It cannot establish that every service is operating today.
The ridership data has another boundary worth respecting. Different transport modes provide different levels of detail. A line-level count cannot support a station-level heatmap, so the application keeps that heatmap specific to Metro, where station-level records exist. Distributing a line's total across its stops would produce a tidy picture, but the station counts would be assumptions presented as measurements.
The saved stop sample lets you inspect one trip from the stored GTFS feed without calling the assistant. It is a stop sequence, not a live service advisory or a recorded model session.
The fastest path depends on what you count
Suppose our illustrative trip has two options. A direct ride has a modeled cost of 900 seconds. The alternative uses 300 seconds of riding, a 240-second transfer, and another 300 seconds of riding. That adds up to 840 seconds, so the route engine prefers the second option. A passenger who dislikes transfers may happily spend the extra minute on the direct ride. These are illustrative costs, not measured journey times from Mexico City.
Dijkstra's algorithm minimizes the sum of edge costs. When those costs are seconds, "best" means the least modeled travel time. Fewer stairs, a lower fare, or less crowded platforms are separate preferences. The calculation needs costs, constraints, or additional search criteria to account for them. And the assistant still has to include walking at both ends when comparing candidate stations.
The service maps external stop IDs to integer nodes in transit_nodes, then queries v_routing_graph for edge IDs, source and target nodes, cost, and reverse cost. Its call to pgr_dijkstra uses directed := false, treating the graph as undirected. A path through that representation does not establish that a service operates in both directions at every time.
Language can change the available paths before the algorithm starts. "Take the light rail" can mean including it in a journey with other modes. "Use only light rail" restricts the entire trip. The prompt explicitly distinguishes those requests because applying the second interpretation to the first can remove the transfer that makes the journey possible.
When the user does restrict modes, the service filters ride edges by agency while retaining edges whose edge_type is transfer. A rail restriction should still allow the walk connecting two rail stations. Otherwise the filter itself could break the journey.
Make the answer match the trip
For our two-option example, the assistant needs to explain both the time and the transfer. The routing tool returns ordered nodes, edges, total seconds, total meters, transfers, and modes used, along with the route geometry. Those fields give the answer something concrete to describe. Inventing a more plausible-sounding duration would throw away the benefit of calculating it.
Even a transfer count needs a careful definition. Riding past ten stations does not mean boarding ten times. The implementation counts contiguous ride segments whose route-ID sets overlap as one boarding. A walking transfer ends the segment, and the next ride starts another. Transfers are then max(0, ride_segments - 1). Counting every edge would inflate the number; counting only agency changes would miss a transfer between lines of the same system.
The map must agree with that sequence too. An edge stored from A to B can be traversed from B to A in this undirected search. The service checks the traversal direction and reverses the stored LineString coordinates when necessary before joining segments. Without that step, the drawn route can double back even when the station sequence is correct.
Coordinates are emitted as longitude, latitude. The service calculates distance with ST_Length(e.geom::geography), requesting geographic distance instead of treating angular coordinates as planar meters, and adds those distances over the returned edges. A line appearing on the map is a weak test of all this work. Reversed traversals, same-line trips, transfers between lines, unknown stop IDs, and disconnected endpoints need checks independent of the assistant's explanation.
Follow the mistake back to its source
The backend streams typed events for loop starts, model output, actions, and tool completion. A trace panel sits alongside the map. For the museum request, it lets me distinguish a wrong place lookup from a poor station choice, an incorrect routing result, or a final answer that misdescribes a valid route.
That separation makes an evaluation useful. I would include duplicate station names, abbreviated landmarks, trips requiring a transfer, missing origins, and places that cannot be resolved. Each case should retain the resolved place, chosen stations, returned route, and final description. I want to see a failed lookup followed by clarification when the place cannot be found. A plausible route assembled from memory would conceal the failure.
The source manifest identifies the inspected agent, MCP server, and routing engine, with revision and file hashes. The browser sample contains real saved stop data; it cannot substitute for a complete tool-call trace from a newly requested trip. A recorded conversation needs to be reviewed with its model configuration and returned route.
To reproduce the project, start with the repository instructions and an isolated PostgreSQL instance with PostGIS and pgRouting. The existing deployment uses a shared database host; reproduction does not require restarting it or touching its other databases. Load the feed, verify the stop and trip joins, and exercise routing directly. Add MCP next and compare its returned objects with the direct service result. Then evaluate natural-language questions. Debugging all three layers through a chat window is a slow way to find a bad join.
A useful journey needs more than a valid path
This build connects spatial data, route calculation, and conversation in a way I can inspect. PostgreSQL supplies the route object, and the trace lets me check how the assistant selected and described it. That makes the assistant's work testable at the points where an answer can go wrong.
The nearest-station problem explains why those checks matter. A correct shortest-path calculation can only compare the choices it receives, using the costs it was given. A useful recommendation also needs the right place, reasonable candidate stations, walking at both ends, and a clear account of the traveler's preferences. The stored feed adds a further limit: a calculated route still needs current service information before it can be treated as advice about today's trip.
I would judge the assistant by whether it gets those decisions right and explains their consequences. A traveler should understand why walking farther might improve the journey, what a transfer saves, and which parts of the answer remain estimates. That is a more useful result than a confident set of directions whose assumptions stay hidden.
