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

15 statements  

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

1"""Announcements MCP tool.""" 

2 

3from typing import Optional 

4 

5from data_api.mcp.server import mcp 

6from data_api.mcp.tools.result_types import AnnouncementsResult 

7from data_api.runtime import get_runtime 

8 

9 

10async def _get_announcements( 

11 department: Optional[str] = None, 

12 keyword: Optional[str] = None, 

13 limit: int = 10, 

14) -> AnnouncementsResult: 

15 """ 

16 Get campus announcements. 

17 

18 Args: 

19 department: Filter by department name (fuzzy match). 

20 keyword: Search in announcement titles. 

21 limit: Maximum number of announcements per department (default 10). 

22 

23 Returns: 

24 Dictionary with announcements grouped by department. 

25 """ 

26 _, data = await get_runtime().announcements.fuzzy_search_announcements( 

27 department=department, 

28 title=keyword, 

29 ) 

30 

31 # Format response - limit articles per department 

32 result = [] 

33 for source in data[:10]: # Limit departments 

34 articles = source.get("articles", [])[:limit] 

35 if articles: 

36 result.append( 

37 { 

38 "department": source.get("department"), 

39 "language": source.get("language"), 

40 "articles": [ 

41 { 

42 "title": a.get("title"), 

43 "link": a.get("link"), 

44 "date": a.get("date"), 

45 } 

46 for a in articles 

47 ], 

48 } 

49 ) 

50 

51 return { 

52 "count": sum(len(d["articles"]) for d in result), 

53 "sources": result, 

54 } 

55 

56 

57@mcp.tool( 

58 name="get_announcements", 

59 title="搜尋校園公告", 

60 description="Get campus announcements from NTHU departments. " 

61 "Use this to find news, notices, or updates from campus offices.", 

62) 

63async def get_announcements( 

64 department: Optional[str] = None, 

65 keyword: Optional[str] = None, 

66 limit: int = 10, 

67) -> dict: 

68 """Get campus announcements.""" 

69 return await _get_announcements(department, keyword, limit)