Files
librechat-custom/mcp/server.py
T
inference-admin 944410560c docs MCP: index every doc and emit absolute docs.inference.coop links
The docs MCP served raw repo-relative markdown, so chat answers carried
hrefs like guides/what-happens-with-your-data.md. The chat client resolved
them against chat.inference.coop, which answers 200 with its SPA shell for
any path — a link that looks live and is not the document.

- list docs recursively (git trees API) instead of repo-root contents; the
  old call indexed only README.md and background.md (2 docs, 7 chunks)
- rewrite repo-relative links to absolute published URLs, using MkDocs
  directory URLs (/guides/models/)
- index only pages the published site serves, per its sitemap
- name docs.inference.coop in the tool description
2026-11-22 14:06:48 +00:00

343 lines
11 KiB
Python

#!/usr/bin/env python3
"""Inference Cooperative docs MCP server (stdio, stdlib-only).
Exposes a `search_docs` tool over the co-op documentation (co-op/docs on
git.inference.coop). Members opt in per conversation via the tools palette.
Design notes:
- stdio transport: newline-delimited JSON-RPC 2.0 on stdin/stdout (the MCP
stdio protocol). All logging goes to stderr — stdout is protocol-only.
- No third-party dependencies: the corpus is fetched from the public Gitea
raw API, chunked by heading, and ranked with a compact BM25.
- The index refreshes lazily if older than DOCS_TTL seconds (default 30 min).
- Every repo-relative markdown link is rewritten to its ABSOLUTE published URL
on https://docs.inference.coop/ before it reaches the model. Raw source links
(``guides/models.md``) leak into chat answers as relative hrefs, which the
chat client then resolves against chat.inference.coop — a 200 that serves the
chat app shell instead of the document.
- Only documents the published site actually serves are indexed; the sitemap is
the authority, so unpublished source files never become citations.
"""
import json
import math
import posixpath
import re
import sys
import time
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor
DOCS_REPO = "co-op/docs"
DOCS_BRANCH = "main"
DOCS_SITE = "https://docs.inference.coop"
GITEA_API = "https://git.inference.coop/api/v1"
DOCS_TTL = 30 * 60
MAX_CHUNKS_RETURNED = 5
SERVER_NAME = "inference-cooperative-docs"
SERVER_VERSION = "1.0.0"
_index = {"chunks": [], "built_at": 0.0}
_lock_held = False
TOKEN_RE = re.compile(r"[a-z0-9]+")
def log(msg):
sys.stderr.write(f"[inference-docs-mcp] {msg}\n")
sys.stderr.flush()
def fetch_json(url):
with urllib.request.urlopen(url, timeout=30) as r:
return json.loads(r.read().decode("utf-8"))
def fetch_text(url):
with urllib.request.urlopen(url, timeout=30) as r:
return r.read().decode("utf-8")
def list_doc_files():
"""Every markdown file in the repo, at any depth (not just repo root)."""
tree = fetch_json(
f"{GITEA_API}/repos/{DOCS_REPO}/git/trees/{DOCS_BRANCH}"
"?recursive=true&per_page=1000"
)
if tree.get("truncated"):
log("warning: git tree truncated; some docs are missing from the index")
return [
{"path": f["path"]}
for f in tree.get("tree", [])
if f.get("type") == "blob"
and f["path"].endswith(".md")
and not f["path"].startswith(("publishing/", "."))
]
def doc_url(path):
"""Published URL for a source file, using MkDocs' directory URLs."""
if path == "README.md":
return DOCS_SITE + "/"
stem = path[:-3] # strip ".md"
if stem == "index":
stem = ""
elif stem.endswith("/index"):
stem = stem[: -len("/index")]
return f"{DOCS_SITE}/{stem}/" if stem else DOCS_SITE + "/"
def published_paths():
"""URL paths the docs site actually serves, according to its sitemap."""
try:
xml = fetch_text(f"{DOCS_SITE}/sitemap.xml")
except Exception as exc: # noqa: BLE001 - fail open, log the gap
log(f"sitemap unavailable ({exc}); indexing every source doc")
return None
locs = re.findall(r"<loc>\s*([^<\s]+)\s*</loc>", xml)
return {urllib.parse.urlparse(u).path for u in locs}
LINK_RE = re.compile(r"\]\(\s*([^)\s]+)\s*\)")
def rewrite_links(text, path):
"""Turn repo-relative links into absolute docs.inference.coop URLs.
A relative href survives into the model's answer and is resolved by the
chat client against chat.inference.coop, which serves its SPA shell with
status 200 for any path — a link that looks live and is not the document.
"""
base_dir = posixpath.dirname(path)
def repl(match):
target = match.group(1)
if "://" in target or target.startswith(("#", "//", "mailto:")):
return match.group(0)
fragment = ""
if "#" in target:
target, fragment = target.split("#", 1)
fragment = "#" + fragment
resolved = posixpath.normpath(posixpath.join(base_dir, target))
if resolved in (".", "..") or resolved.startswith("../"):
return match.group(0)
if resolved.endswith(".md"):
url = doc_url(resolved)
else:
url = f"{DOCS_SITE}/{resolved}"
return f"]({url}{fragment})"
return LINK_RE.sub(repl, text)
def chunk_markdown(path, text):
"""Split a doc into section chunks keyed by its top title + heading."""
lines = text.splitlines()
title = lines[0].lstrip("# ").strip() if lines and lines[0].startswith("#") else path
sections = []
current_head = "Overview"
current = []
def flush():
body = "\n".join(current).strip()
if len(body) < 40:
return
sections.append({"doc": title, "path": path, "section": current_head, "text": body})
for line in lines:
if line.startswith("## "):
flush()
current_head = line.lstrip("# ").strip()
current = []
else:
current.append(line)
flush()
return sections
def tokenize(text):
return TOKEN_RE.findall(text.lower())
def build_index():
chunks = []
docs = list_doc_files()
published = published_paths()
if published is not None:
kept = []
for doc in docs:
url_path = urllib.parse.urlparse(doc_url(doc["path"])).path
if url_path in published:
kept.append(doc)
else:
log(f"skipping unpublished doc: {doc['path']}")
docs = kept
def load(doc):
try:
raw = fetch_text(f"{GITEA_API}/repos/{DOCS_REPO}/raw/{doc['path']}")
return chunk_markdown(doc["path"], rewrite_links(raw, doc["path"]))
except Exception as exc: # noqa: BLE001 - one bad doc shouldn't kill the index
log(f"skipping {doc['path']}: {exc}")
return []
with ThreadPoolExecutor(max_workers=6) as pool:
for parts in pool.map(load, docs):
chunks.extend(parts)
# BM25 statistics
doc_terms = [tokenize(c["text"]) for c in chunks]
n = len(chunks)
avg_len = sum(len(t) for t in doc_terms) / max(n, 1)
df = {}
for terms in doc_terms:
for term in set(terms):
df[term] = df.get(term, 0) + 1
for c, terms in zip(chunks, doc_terms):
c["_tf"] = {}
for t in terms:
c["_tf"][t] = c["_tf"].get(t, 0) + 1
c["_len"] = len(terms)
_index["chunks"] = chunks
_index["df"] = df
_index["n"] = n
_index["avg_len"] = avg_len
_index["built_at"] = time.time()
log(f"index built: {n} chunks from {len(docs)} docs")
def ensure_index():
if not _index["chunks"] or time.time() - _index["built_at"] > DOCS_TTL:
build_index()
def search(query, limit=MAX_CHUNKS_RETURNED):
ensure_index()
k1, b = 1.5, 0.75
q_terms = tokenize(query)
scored = []
for c in _index["chunks"]:
score = 0.0
for t in q_terms:
tf = c["_tf"].get(t, 0)
if not tf:
continue
idf = math.log(1 + (_index["n"] - _index["df"].get(t, 0) + 0.5) / (_index["df"].get(t, 0) + 0.5))
score += idf * tf * (k1 + 1) / (tf + k1 * (1 - b + b * c["_len"] / _index["avg_len"]))
if score > 0:
scored.append((score, c))
scored.sort(key=lambda pair: -pair[0])
return scored[:limit]
def tool_search_docs(args):
query = str(args.get("query") or "").strip()
if not query:
return {"content": [{"type": "text", "text": "Provide a query."}]}
limit = min(int(args.get("limit") or MAX_CHUNKS_RETURNED), 10)
results = search(query, limit)
if not results:
return {"content": [{"type": "text", "text": "No matching documentation found."}]}
parts = []
for score, c in results:
parts.append(f"[{c['doc']} — {c['section']}] (relevance {score:.2f})\n{c['text']}")
return {"content": [{"type": "text", "text": "\n\n---\n\n".join(parts)}]}
TOOLS = [
{
"name": "search_docs",
"description": (
"Search the Inference Cooperative documentation (getting started, models, "
"API access, charter, policies). Returns the most relevant doc sections. "
"The docs are published at https://docs.inference.coop/ — cite links "
"exactly as they appear in the results."
),
"inputSchema": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "What to look for in the docs"},
"limit": {"type": "integer", "description": "Max sections to return (default 5)"},
},
"required": ["query"],
},
}
]
TOOLS_HANDLERS = {"search_docs": tool_search_docs}
def handle(msg):
method = msg.get("method")
msg_id = msg.get("id")
if method == "initialize":
return {
"jsonrpc": "2.0",
"id": msg_id,
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"serverInfo": {"name": SERVER_NAME, "version": SERVER_VERSION},
},
}
if method == "ping":
return {"jsonrpc": "2.0", "id": msg_id, "result": {}}
if method == "tools/list":
return {"jsonrpc": "2.0", "id": msg_id, "result": {"tools": TOOLS}}
if method == "tools/call":
params = msg.get("params") or {}
name = params.get("name")
handler = TOOLS_HANDLERS.get(name)
if not handler:
return {
"jsonrpc": "2.0",
"id": msg_id,
"error": {"code": -32602, "message": f"Unknown tool: {name}"},
}
try:
result = handler(params.get("arguments") or {})
return {"jsonrpc": "2.0", "id": msg_id, "result": result}
except Exception as exc: # noqa: BLE001 - report tool errors in-band
log(f"tool error: {exc}")
return {
"jsonrpc": "2.0",
"id": msg_id,
"result": {"content": [{"type": "text", "text": f"Search failed: {exc}"}], "isError": True},
}
if method and method.startswith("notifications/"):
return None
if msg_id is not None:
return {"jsonrpc": "2.0", "id": msg_id, "error": {"code": -32601, "message": "Method not found"}}
return None
def main():
log("starting (stdio)")
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
msg = json.loads(line)
except json.JSONDecodeError as exc:
log(f"bad json: {exc}")
continue
try:
resp = handle(msg)
except Exception as exc: # noqa: BLE001 - never crash the protocol loop
log(f"handler crash: {exc}")
if msg.get("id") is not None:
resp = {"jsonrpc": "2.0", "id": msg["id"], "error": {"code": -32603, "message": "Internal error"}}
else:
resp = None
if resp is not None:
sys.stdout.write(json.dumps(resp) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()