Last active
May 13, 2025 05:48
-
-
Save g-fukurowl/6bd1d0956395c4dcaca4ce9e6d5c4f08 to your computer and use it in GitHub Desktop.
オープンソースの全文検索エンジンFESSで検索を行うMCPサーバの実装。Claude for Desktopで動作確認済み。FESSはローカルに立てた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Any | |
| import asyncio | |
| import httpx | |
| from mcp.server.fastmcp import FastMCP | |
| # Initialize FastMCP server | |
| mcp = FastMCP("fess-mcp") | |
| # Constants | |
| FESS_API_BASE = "http://localhost:8080/api/v1" | |
| USER_AGENT = "fess-search-app/1.0" | |
| async def make_search_request(query: str) -> dict[str, Any] | None: | |
| """Make a request to the FESS API with proper error handling.""" | |
| headers = { | |
| "User-Agent": USER_AGENT, | |
| "Accept": "application/geo+json" | |
| } | |
| url: str = f"{FESS_API_BASE}/documents?q=" + query | |
| async with httpx.AsyncClient() as client: | |
| try: | |
| response = await client.get(url, headers=headers, timeout=30.0) | |
| response.raise_for_status() | |
| return response.json() | |
| except Exception: | |
| return None | |
| @mcp.tool() | |
| async def get_fess_search_results(query: str) -> str: | |
| """オープンソース検索エンジンFESSを用いて、webに一般公開されていない情報を検索します。 | |
| Args: | |
| query: 検索クエリ。調べたい事に関する単語や用語など | |
| """ | |
| results = await make_search_request(query) | |
| if not results or "data" not in results: | |
| return "検索結果が見つかりませんでした。" | |
| formatted_results = [] | |
| references = [] | |
| for i, result in enumerate(results["data"], 1): | |
| content = result["content_description"].replace("<strong>", "").replace("</strong>", "") | |
| title = result.get("title", "タイトルなし") | |
| url_link = result.get("url_link", "URLなし") | |
| last_modified = result.get("last_modified", "最終更新日不明") | |
| formatted_results.append(f"[{i}] {content}") | |
| references.append(f"[{i}] \"{title}\", {url_link}, {last_modified}") | |
| # 検索結果と参考文献を結合 | |
| output = "\n\n".join(formatted_results) | |
| output += "\n\n【参考文献】\n" + "\n".join(references) | |
| print(output) | |
| return output | |
| if __name__ == "__main__": | |
| # Initialize and run the server | |
| mcp.run(transport='stdio') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考文献も付けるようにした