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

17 statements  

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

1"""Library information MCP tool.""" 

2 

3from typing import Literal 

4 

5from pydantic import validate_call 

6 

7from data_api.mcp.errors import live_tool_errors 

8from data_api.mcp.server import mcp 

9from data_api.mcp.tools.result_types import LibraryItemsResult, LibrarySpacesResult 

10from data_api.runtime import get_runtime 

11 

12 

13@validate_call 

14async def _get_library_info( 

15 info_type: Literal["space", "lost_and_found"] = "space", 

16) -> LibraryItemsResult | LibrarySpacesResult: 

17 """ 

18 Get library information. 

19 

20 Args: 

21 info_type: Type of information - 'space' for study space availability, 'lost_and_found' for lost items. 

22 

23 Returns: 

24 Dictionary with library information. 

25 """ 

26 with live_tool_errors(): 

27 if info_type == "space": 

28 spaces = await get_runtime().libraries.get_space_availability() 

29 return { 

30 "spaces": [ 

31 { 

32 "zone": s.get("zonename"), 

33 "type": s.get("spacetypename"), 

34 "available": s.get("count"), 

35 } 

36 for s in spaces 

37 ] 

38 } 

39 items = await get_runtime().libraries.get_lost_and_found_items() 

40 return {"items": items[:10]} 

41 

42 

43@mcp.tool( 

44 name="get_library_info", 

45 title="查詢圖書館資訊", 

46 description="Get library information including space availability and lost items. " 

47 "Use this to check if study spaces are available or to find lost items.", 

48) 

49async def get_library_info( 

50 info_type: Literal["space", "lost_and_found"] = "space", 

51) -> dict: 

52 """Get library information.""" 

53 return await _get_library_info(info_type)