Coverage for src/data_api/mcp/tools/locations.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 19:28 +0000

1"""Location search MCP tool.""" 

2 

3from data_api.mcp.server import mcp 

4from data_api.mcp.tools.result_types import LocationsResult 

5from data_api.runtime import get_runtime 

6 

7 

8async def _search_locations(query: str, fuzzy: bool = True) -> LocationsResult: 

9 """Search location names before applying the result cap.""" 

10 service = get_runtime().locations 

11 if fuzzy: 

12 _, locations = await service.fuzzy_search_locations(query=query) 

13 else: 

14 _, locations = await service.get_locations(name=query) 

15 # The REST-oriented service treats an empty name as an unfiltered listing. 

16 locations = [location for location in locations if location["name"] == query] 

17 return {"locations": locations[:5]} 

18 

19 

20@mcp.tool( 

21 name="search_locations", 

22 title="搜尋校園地點", 

23 description="Search NTHU campus locations and building coordinates. " 

24 "Returns up to 5 locations. Fuzzy matching is enabled by default; " 

25 "set fuzzy=false for case-sensitive full-string equality. " 

26 "Use search_directory instead for department or staff contact information.", 

27) 

28async def search_locations(query: str, fuzzy: bool = True) -> dict: 

29 """Search campus locations using fuzzy or exact matching.""" 

30 return await _search_locations(query, fuzzy=fuzzy)