Project Files
src / tools / ingest_tool.py
"""
tools/ingest_tool.py — MCP tool to trigger live re-ingestion at runtime.
This lets the LLM (or an operator) add new documents to the docs folder and
re-index without restarting the server.
"""
from __future__ import annotations
from mcp import types
from src.retrieval.engine import RAGEngine
from src.utils.logging import get_logger
log = get_logger("tools.ingest")
def build_ingest_tool_schema() -> types.Tool:
return types.Tool(
name="reindex_documents",
description=(
"Re-scan the documents folder and index any new or changed files. "
"Use this after adding new documents to the watched folder. "
"Already-indexed unchanged files are skipped automatically."
),
inputSchema={
"type": "object",
"properties": {},
"required": [],
},
)
async def handle_reindex(engine: RAGEngine) -> list[types.TextContent]:
"""Trigger a live re-ingest and return a summary."""
log.info("Live re-ingest triggered via MCP tool.")
try:
stats = engine.ingest()
msg = (
f"Re-indexing complete.\n"
f"• New files indexed : {stats['new']}\n"
f"• Unchanged skipped : {stats['skipped']}\n"
f"• Total chunks : {stats['total_chunks']}"
)
except Exception as exc:
log.error("Re-ingest failed: %s", exc)
msg = f"Re-indexing failed: {exc}"
return [types.TextContent(type="text", text=msg)]