Coverage for src/romfarmer/mcp/wiki.py: 0%
50 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-01 23:08 -0700
« prev ^ index » next coverage.py v7.15.4, created at 2026-09-01 23:08 -0700
1"""MCP tools for Wikipedia game search and info."""
3from typing import Any
6async def tool_wiki_search(
7 query: str,
8 limit: int = 10,
9 section_filter: str | None = None,
10) -> dict[str, Any]:
11 """Semantic search over Wikipedia game articles."""
12 try:
13 from romfarmer.metadata.wiki_search import WikiSearch
15 search = WikiSearch()
16 results = search.search(query=query, limit=limit, section_filter=section_filter)
17 return {
18 "query": query,
19 "count": len(results),
20 "results": [
21 {
22 "game": r["article_title"],
23 "section": r["section"],
24 "score": round(r["score"], 3),
25 "content": r["content"][:500] + "..."
26 if len(r["content"]) > 500
27 else r["content"],
28 "url": r["article_url"],
29 }
30 for r in results
31 ],
32 }
33 except Exception as e:
34 return {"error": str(e)}
37async def tool_wiki_game_info(game_title: str) -> dict[str, Any]:
38 """Get Wikipedia information about a specific game."""
39 try:
40 from romfarmer.metadata.wiki_search import WikiSearch
42 search = WikiSearch()
43 info = search.get_game_info(game_title)
45 if not info:
46 return {
47 "game_title": game_title,
48 "found": False,
49 "message": f"No Wikipedia article found for '{game_title}'",
50 }
52 sections_preview = {}
53 for name, content in info["sections"].items():
54 if len(content) > 1000:
55 sections_preview[name] = content[:1000] + f"... ({len(content)} chars total)"
56 else:
57 sections_preview[name] = content
59 return {
60 "game_title": game_title,
61 "found": True,
62 "article_title": info["title"],
63 "url": info["url"],
64 "section_names": list(info["sections"].keys()),
65 "sections": sections_preview,
66 }
67 except Exception as e:
68 return {"error": str(e)}
71async def tool_wiki_get_section(
72 game_title: str,
73 section_name: str,
74) -> dict[str, Any]:
75 """Get a specific section from a game's Wikipedia article."""
76 try:
77 from romfarmer.metadata.wiki_search import WikiSearch
79 search = WikiSearch()
80 content = search.get_section(game_title, section_name)
82 if not content:
83 return {"game_title": game_title, "section_name": section_name, "found": False}
85 return {
86 "game_title": game_title,
87 "section_name": section_name,
88 "found": True,
89 "content": content,
90 "length": len(content),
91 }
92 except Exception as e:
93 return {"error": str(e)}
96async def tool_wiki_stats() -> dict[str, Any]:
97 """Get statistics about the Wikipedia game database."""
98 try:
99 from romfarmer.metadata.wiki_search import WikiSearch
101 search = WikiSearch()
102 stats = search.stats()
103 return {
104 "total_chunks": stats["total_chunks"],
105 "total_articles": stats["total_articles"],
106 "unique_games": stats["unique_games"],
107 "avg_tokens_per_chunk": stats["avg_tokens_per_chunk"],
108 "total_tokens": stats["total_tokens"],
109 "estimated_words": stats["total_tokens"] // 1.3,
110 }
111 except Exception as e:
112 return {"error": str(e)}
115async def tool_wiki_find_game(
116 query: str,
117 limit: int = 10,
118 threshold: float = 0.5,
119) -> dict[str, Any]:
120 """Fuzzy search for game titles by name."""
121 try:
122 from romfarmer.metadata.wiki_search import WikiSearch
124 search = WikiSearch()
125 results = search.find_games(query=query, limit=limit, threshold=threshold)
126 return {
127 "query": query,
128 "count": len(results),
129 "matches": results,
130 "best_match": results[0] if results else None,
131 }
132 except Exception as e:
133 return {"error": str(e)}