File size: 1,971 Bytes
9b119f9
70ae91b
9b119f9
b5ee702
dc8e24f
b5ee702
 
9b119f9
 
 
b5ee702
9b119f9
b5ee702
 
9b119f9
b5ee702
9b119f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5ee702
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from urllib.parse import urlencode, quote_plus
import gradio as gr
from src.common.logger import get_logger
from src.common.utils import format_docstring
from src.tools.schemas.map_tools_schema import (
    MapWalkingDirectionResult,
)

logger = get_logger(__name__)

##### get_google_maps_walking_direction #####


def get_google_maps_walking_direction(
    origin: str, destination: str, waypoints: list[str] | None = None
) -> MapWalkingDirectionResult:
    """
    Generates a Google Maps walking direction URL.
    """

    params = {
        "api": "1",
        "origin": origin,
        "destination": destination,
        "travelmode": "walking",
    }
    if waypoints:
        # Google Maps expects pipe-separated waypoints
        params["waypoints"] = "|".join(waypoints)

    params["dir_action"] = "navigate"

    # urlencode will properly escape commas, pipes, spaces, etc.
    query = urlencode(params, quote_via=quote_plus, safe="|,")
    return MapWalkingDirectionResult(
        map_url=f"https://www.google.com/maps/dir/?{query}"
    )


@format_docstring(
    template_args={"return_schema": MapWalkingDirectionResult._model_json_schema}
)
@gr.mcp.tool(
    name="gm_walking_dir_tool",
)
async def gm_walking_dir_tool(
    origin: str, destination: str, waypoints: list[str] | None = None
) -> str:
    """
    Generates a Maps walking direction route.

    Args:
        origin: The origin of the route, it can be a string like "40.1777,44.5126" or "Republic Square, Yerevan"
        destination: The destination of the route, it can be a string like "40.1777,44.5126" or "Republic Square, Yerevan"
        waypoints: The waypoints of the route, it can be a list of strings like ["40.1810,44.5140", "40.1850,44.5160"] or ["Republic Square, Yerevan", "40.1850,44.5160"], by default None

    Returns:
        {return_schema}
    """

    result = get_google_maps_walking_direction(origin, destination, waypoints)

    return result.model_dump_json()