|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""An MCP server that turns MFS into searchable context for any MCP client. |
| 3 | +
|
| 4 | +In the spirit of zilliztech/claude-context — "make the codebase the context for |
| 5 | +any coding agent" — but the index is MFS, so a single server covers every source |
| 6 | +you've indexed (code, docs, issues, chat, databases), not just one codebase. |
| 7 | +
|
| 8 | +It exposes two tools over the Model Context Protocol: |
| 9 | +
|
| 10 | +- ``search`` — hybrid (semantic + keyword) search across MFS-indexed sources; |
| 11 | +- ``read`` — read a hit in full (or a line range) by its source URI. |
| 12 | +
|
| 13 | +Point it at a running MFS server with ``MFS_URL`` / ``MFS_TOKEN`` (defaults to |
| 14 | +``http://127.0.0.1:13619`` and ``~/.mfs/server.token``). Register it with any MCP |
| 15 | +client, e.g. Claude Code: |
| 16 | +
|
| 17 | + claude mcp add mfs-context -- python /abs/path/to/server.py |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | +import mfs_sdk |
| 26 | +from mcp.server.fastmcp import FastMCP |
| 27 | + |
| 28 | +mcp = FastMCP("mfs-context") |
| 29 | + |
| 30 | + |
| 31 | +def _token() -> str | None: |
| 32 | + if os.getenv("MFS_TOKEN"): |
| 33 | + return os.environ["MFS_TOKEN"] |
| 34 | + tok = Path.home() / ".mfs" / "server.token" |
| 35 | + return tok.read_text().strip() if tok.exists() else None |
| 36 | + |
| 37 | + |
| 38 | +def _api(api_cls): |
| 39 | + client = mfs_sdk.ApiClient( |
| 40 | + mfs_sdk.Configuration(host=os.getenv("MFS_URL", "http://127.0.0.1:13619")) |
| 41 | + ) |
| 42 | + token = _token() |
| 43 | + if token: |
| 44 | + client.set_default_header("Authorization", f"Bearer {token}") |
| 45 | + return api_cls(client) |
| 46 | + |
| 47 | + |
| 48 | +@mcp.tool() |
| 49 | +def search(query: str, scope: str = "", top_k: int = 8) -> str: |
| 50 | + """Search MFS-indexed sources (code, docs, issues, chat, databases) by meaning |
| 51 | + or keyword. Leave ``scope`` empty to search everything, or pass a path / URI |
| 52 | + prefix to narrow it (e.g. ``github://org/repo`` or a local path). Returns |
| 53 | + ranked hits, each with a snippet and the ``source`` URI to pass to ``read``. |
| 54 | + """ |
| 55 | + resp = _api(mfs_sdk.RetrievalApi).search(q=query, path=scope or None, top_k=top_k) |
| 56 | + if not resp.results: |
| 57 | + return "No matches." |
| 58 | + blocks = [] |
| 59 | + for h in resp.results: |
| 60 | + score = f"{h.score:.2f}" if h.score is not None else "n/a" |
| 61 | + blocks.append(f"## {h.source} (score={score})\n{h.content.strip()}") |
| 62 | + return "\n\n".join(blocks) |
| 63 | + |
| 64 | + |
| 65 | +@mcp.tool() |
| 66 | +def read(source: str, lines: str = "") -> str: |
| 67 | + """Read a source in full, or a line range like ``"40:80"``. ``source`` is the |
| 68 | + URI from a ``search`` hit. Use this to pull the exact code or text into context |
| 69 | + after ``search`` locates it.""" |
| 70 | + resp = _api(mfs_sdk.BrowseApi).cat(source, range=lines or None) |
| 71 | + return resp.content |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + mcp.run() |
0 commit comments