Skip to main content
Glama

Wikipedia Agent MCP Server

by rvkrishna13
mcp_server.py2.57 kB
import wikipedia from mcp.server.fastmcp import FastMCP from pathlib import Path mcp = FastMCP("WikipediaSearch") @mcp.tool() def fetch_wikipedia_info(query: str) -> dict: """ Search Wikipedia for a topic and return title, summary, and URL of the best match. """ try: search_results = wikipedia.search(query) if not search_results: return {"error": "No results found for your query."} best_match = search_results[0] page = wikipedia.page(best_match) return { "title": page.title, "summary": page.summary, "url": page.url } except wikipedia.DisambiguationError as e: return { "error": f"Ambiguous topic. Try one of these: {', '.join(e.options[:5])}" } except wikipedia.PageError: return { "error": "No Wikipedia page could be loaded for this query." } @mcp.tool() def list_wikipedia_sections(topic: str) -> dict: """ Return a list of section titles from the Wikipedia page of a given topic. """ try: page = wikipedia.page(topic) sections = page.sections return {"sections": sections} except Exception as e: return {"error": str(e)} @mcp.tool() def get_section_content(topic: str, section_name: str) -> dict: """ Return section content """ try: page = wikipedia.page(topic) content = page.section(section_name) if content: return {"content": content} else: return {"error": f"Section '{section_title}' not found in article '{topic}'."} except Exception as e: return {"error": str(e)} @mcp.prompt() def highlight_sections_prompt(topic: str) -> str: """ Identifies the most important sections from a Wikipedia article on the given topic. """ return f""" The user is exploring the Wikipedia article on "{topic}". Given the list of section titles from the article, choose the 3–5 most important or interesting sections that are likely to help someone learn about the topic. Return a bullet list of these section titles, along with 1-line explanations of why each one matters. """ @mcp.resource('file://suggested_topics') def suggested_titles() -> list[str]: """ Read and return suggested Wikipedia topics from a local file. """ try: path = Path("suggested_topics.txt") if not path.exists(): return ["File Not Found"] return path.read_text(encoding='utf-8').strip().splitlines() except Exception as e: return [f"Error reading file: {str(e)}"] if __name__=="__main__": print("Starting MCP Wikipedia Server....") mcp.run(transport="stdio")

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rvkrishna13/MCP_Wikipedia_Search'

If you have feedback or need assistance with the MCP directory API, please join our Discord server