wiki_search
Search Wikipedia articles to find information and answers for research or general knowledge queries, returning relevant results based on your search terms.
Instructions
Search Wikipedia for articles.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| limit | No |
Implementation Reference
- server.py:60-88 (handler)The main handler function for the 'wiki_search' tool. It uses the Wikipedia API to search for articles matching the query, up to the specified limit, and returns formatted results with titles, summaries, and URLs. Includes error handling.
@mcp.tool() async def wiki_search(query: str, limit: int = 3) -> str: """Search Wikipedia for articles.""" try: params = { "action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": limit } data = await make_api_request(WIKI_API_BASE, params=params) if not data or "query" not in data or not data["query"]["search"]: return "No Wikipedia articles found for your query." results = [] for item in data["query"]["search"]: title = item["title"] snippet = item["snippet"].replace("<span class=\"searchmatch\">", "").replace("</span>", "") url = f"https://en.wikipedia.org/wiki/{urllib.parse.quote(title.replace(' ', '_'))}" results.append(f"Title: {title}\nSummary: {snippet}\nURL: {url}") return "\n\n".join(results) except Exception as e: logger.error(f"Error in wiki_search: {e}") return "Failed to search Wikipedia due to an internal error."