Skip to content

Hypergraph is more powerful. Transform unstructured text into structured knowledge with LLMs. Graphs, hypergraphs, and spatio-temporal extractions — with one command.

15.0 KB
Raw Blame
"""MCP server exposing Hyper-Extract knowledge abstracts.

Lets MCP-capable assistants query and export existing Knowledge Abstracts over
the Model Context Protocol. Read + export only — it never creates, mutates, or
deletes a KA.

Tools:
    - list_templates  : list available extraction templates
    - info            : stats for a knowledge abstract (chunks, timestamps,
                        optional sources; no LLM needed)
    - search          : semantic retrieval over a KA (needs an index)
    - ask             : RAG question-answering over a KA (needs an index)
    - export_obsidian : export a KA to an Obsidian vault
    - export_graphml  : export a KA to GraphML (same as `he export graphml`)
    - export_csv      : export a KA to CSV tables (same as `he export csv`)
    - export_jsonld   : export a KA to JSON-LD (same as `he export jsonld`)
    - export_cypher   : export a KA to Cypher (same as `he export cypher`)

Run it (stdio transport):

    he-mcp
    # or
    python -m hyperextract.mcp_server

LLM/embedder come from ``~/.he/config.toml`` (same as the CLI). Requires the
optional extra: ``pip install 'hyperextract[mcp]'``.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Any

from langchain_core.embeddings import Embeddings
from langchain_core.language_models.chat_models import BaseChatModel

SERVER_NAME = "hyper-extract"


# ---------------------------------------------------------------------------
# Internal helpers (kept module-level so they can be monkeypatched in tests)
# ---------------------------------------------------------------------------


def _get_clients() -> tuple[BaseChatModel, Embeddings]:
    """Return (llm, embedder) from the user's ~/.he/config.toml."""
    from hyperextract.utils.client import get_client

    return get_client()


def _template_and_lang(path: Path) -> tuple[str, str]:
    """Read the template name and language from a KA's metadata.json."""
    meta_path = path / "metadata.json"
    if not meta_path.exists():
        raise FileNotFoundError(f"No metadata.json in {path}")
    meta = json.loads(meta_path.read_text(encoding="utf-8"))
    template = meta.get("template")
    if not template:
        raise ValueError(f"No template recorded in {meta_path}")
    return template, meta.get("lang", "en")


def _load_ka(ka_path: str):
    """Load a Knowledge Abstract from disk (needs LLM/embedder clients)."""
    from hyperextract.utils.template_engine import Template

    path = Path(ka_path)
    if not (path / "data.json").exists():
        raise FileNotFoundError(f"Not a knowledge abstract (no data.json): {ka_path}")
    template, lang = _template_and_lang(path)
    llm, emb = _get_clients()
    ka = Template.create(template, lang, llm_client=llm, embedder=emb)
    ka.load(path)
    return ka


_LOAD_ERRORS = (FileNotFoundError, ValueError, OSError, ImportError)


def _safe_load_ka(ka_path: str):
    """Load a KA, or return an explanation string (never raise)."""
    try:
        return _load_ka(ka_path)
    except _LOAD_ERRORS as exc:
        return f"Cannot load KA: {exc}"


def _dump(obj: Any) -> str:
    return json.dumps(obj, ensure_ascii=False, indent=2, default=str)


# ---------------------------------------------------------------------------
# Tool implementations (registered with FastMCP in build_server)
# ---------------------------------------------------------------------------


def list_templates(include_methods: bool = True) -> str:
    """List the available knowledge-extraction templates.

    Args:
        include_methods: When true (default), include ``method/<name>``
            entries the CLI lists and ``he parse -t`` accepts.

    Returns a JSON array of {name, type, description}.
    """
    from hyperextract.utils.template_engine import Template

    templates = Template.list(include_methods=include_methods)
    out = []
    for name, cfg in sorted(templates.items()):
        desc = getattr(cfg, "description", "") or ""
        if isinstance(desc, dict):
            desc = desc.get("en") or desc.get("zh") or ""
        out.append(
            {"name": name, "type": getattr(cfg, "type", None), "description": desc}
        )
    return _dump(out)


def info(ka_path: str, include_sources: bool = False) -> str:
    """Show information and statistics for a knowledge abstract.

    Args:
        ka_path: Path to the knowledge abstract directory.
        include_sources: When true, include source-ledger rows (same files
            ``he info --sources`` reads). Default false.

    Returns JSON with template, language, node/edge counts, optional chunks,
    created/updated timestamps, index status, and optional sources.
    Does not require LLM/embedder configuration.
    """
    path = Path(ka_path)
    data_file = path / "data.json"
    if not data_file.exists():
        return f"Not a knowledge abstract (no data.json): {ka_path}"

    data = json.loads(data_file.read_text(encoding="utf-8"))
    chunks = 0
    if isinstance(data, dict):
        # list/set KAs store "items"; docs define Nodes as entities/items
        nodes = len(data.get("nodes", data.get("entities", data.get("items", []))))
        edges = len(data.get("edges", data.get("relations", [])))
        chunks = len(data.get("chunks", []))
    elif isinstance(data, list):
        nodes, edges = len(data), 0
    else:
        nodes, edges = 0, 0

    meta: dict = {}
    meta_path = path / "metadata.json"
    if meta_path.exists():
        meta = json.loads(meta_path.read_text(encoding="utf-8"))

    index_dir = path / "index"
    index_built = index_dir.exists() and any(index_dir.iterdir())

    payload: dict[str, Any] = {
        "path": str(path),
        "template": meta.get("template"),
        "lang": meta.get("lang"),
        "nodes": nodes,
        "edges": edges,
        "index_built": index_built,
    }
    if chunks:
        payload["chunks"] = chunks
    if meta.get("created_at"):
        payload["created"] = meta["created_at"]
    if meta.get("updated_at"):
        payload["updated"] = meta["updated_at"]
    if include_sources:
        payload["sources"] = _source_ledger_rows(path)
    return _dump(payload)


def _source_ledger_rows(path: Path) -> list[dict[str, Any]]:
    """Collect source-ledger rows using the same files as ``he info --sources``."""
    ledger_files = (
        path / "sources_nodes.json",
        path / "sources_edges.json",
        path / "sources_chunks.json",
        path / "sources_items.json",
    )
    combined: dict[str, dict[str, Any]] = {}
    for ledger_path in ledger_files:
        if not ledger_path.exists():
            continue
        try:
            entries = json.loads(ledger_path.read_text(encoding="utf-8"))
        except (OSError, json.JSONDecodeError, TypeError, ValueError):
            continue
        if not isinstance(entries, list):
            continue
        for entry in entries:
            if not isinstance(entry, dict):
                continue
            sid = entry.get("source_id")
            if sid is None:
                continue
            record = combined.setdefault(
                str(sid),
                {
                    "source_id": str(sid),
                    "raw_items": 0,
                    "content_hash": entry.get("content_hash"),
                },
            )
            record["raw_items"] += len(entry.get("raw_items", []))
            if record.get("content_hash") is None:
                record["content_hash"] = entry.get("content_hash")
    return [combined[key] for key in sorted(combined)]


def search(ka_path: str, query: str, top_k: int = 5) -> str:
    """Semantic search over a knowledge abstract.

    Args:
        ka_path: Path to the knowledge abstract directory.
        query: Natural-language search query.
        top_k: Maximum number of results (for graphs: nodes and edges each).

    Returns JSON shaped by the AutoType search return via SearchHits:

    - 2-tuple → ``{"nodes": [...], "edges": [...]}``
    - 3-tuple → same plus ``community_context`` (``null`` is written)
    - dict → passed through (values recursively dumped)
    - list → ``{"results": [...]}``

    The KA must have an index (build it with `he build-index`).
    """
    from hyperextract.utils.search_results import coerce_search_results

    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    try:
        results = ka.search(query, top_k=top_k)
    except ValueError as e:
        return f"Cannot search: {e}. Build the index first with `he build-index {ka_path}`."

    return _dump(coerce_search_results(results).payload)


def ask(ka_path: str, question: str, top_k: int = 5) -> str:
    """Answer a question using retrieval-augmented generation over a KA.

    Args:
        ka_path: Path to the knowledge abstract directory.
        question: The question to answer.
        top_k: Number of context items to retrieve.

    Returns the generated answer. The KA must have an index.
    """
    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    try:
        response = ka.chat(question, top_k=top_k)
    except ValueError as e:
        return f"Cannot answer: {e}. Build the index first with `he build-index {ka_path}`."
    return getattr(response, "content", str(response))


def export_obsidian(
    ka_path: str,
    output: str,
    vault_name: str = "",
    overwrite: bool = False,
) -> str:
    """Export a knowledge abstract to an Obsidian vault.

    Args:
        ka_path: Path to the knowledge abstract directory.
        output: Destination vault directory.
        vault_name: Vault name for the index note (defaults to the output dir name).
        overwrite: Allow writing into an existing, non-empty directory.

    Returns a summary of how many notes were written.
    """
    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    if not hasattr(ka, "export_obsidian"):
        return (
            "Obsidian export is only supported for graph-type knowledge abstracts "
            "(graph, hypergraph, temporal/spatial graphs)."
        )
    name = vault_name or Path(output).name or "Knowledge Vault"
    try:
        vault = ka.export_obsidian(output, vault_name=name, overwrite=overwrite)
    except FileExistsError as e:
        return f"{e} Pass overwrite=true to write into it."
    count = len(list(Path(vault).glob("*.md")))
    return f"Exported {count} notes to {vault}"


def export_graphml(ka_path: str, output: str, overwrite: bool = False) -> str:
    """Export a knowledge abstract to GraphML.

    Args:
        ka_path: Path to the knowledge abstract directory.
        output: Destination ``.graphml`` file.
        overwrite: Overwrite an existing, non-empty GraphML file.

    Uses the same ``export_ka_graphml`` adapter as ``he export graphml``.
    Does not create, mutate, or delete the KA.
    """
    from hyperextract.utils.exporters import GraphMLHypergraphError
    from hyperextract.utils.exporters.ka import GraphTypeError, export_ka_graphml

    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    try:
        dest = export_ka_graphml(ka, output, overwrite=overwrite)
    except GraphTypeError as e:
        return str(e)
    except GraphMLHypergraphError as e:
        return str(e)
    except FileExistsError as e:
        return f"{e} Pass overwrite=true to overwrite it."
    except IsADirectoryError as e:
        return str(e)
    return f"Wrote GraphML to {dest}"


def export_csv(ka_path: str, output: str, overwrite: bool = False) -> str:
    """Export a knowledge abstract to CSV tables.

    Args:
        ka_path: Path to the knowledge abstract directory.
        output: Destination directory.
        overwrite: Allow writing into an existing, non-empty directory.

    Uses the same ``export_ka_csv`` adapter as ``he export csv``.
    Does not create, mutate, or delete the KA.
    """
    from hyperextract.utils.exporters.ka import (
        GraphTypeError,
        export_ka_csv,
        is_hypergraph_ka,
    )

    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    hypergraph = is_hypergraph_ka(ka)
    try:
        dest = export_ka_csv(ka, output, overwrite=overwrite)
    except GraphTypeError as e:
        return str(e)
    except FileExistsError as e:
        return f"{e} Pass overwrite=true to write into it."
    written = "nodes.csv + hyperedges.csv" if hypergraph else "nodes.csv + edges.csv"
    return f"Wrote {written} to {dest}"


def export_jsonld(ka_path: str, output: str, overwrite: bool = False) -> str:
    """Export a knowledge abstract to JSON-LD.

    Args:
        ka_path: Path to the knowledge abstract directory.
        output: Destination ``.jsonld`` file.
        overwrite: Overwrite an existing, non-empty JSON-LD file.

    Uses the same ``export_ka_jsonld`` adapter as ``he export jsonld``.
    Does not create, mutate, or delete the KA.
    """
    from hyperextract.utils.exporters.ka import GraphTypeError, export_ka_jsonld

    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    try:
        dest = export_ka_jsonld(ka, output, overwrite=overwrite)
    except GraphTypeError as e:
        return str(e)
    except FileExistsError as e:
        return f"{e} Pass overwrite=true to overwrite it."
    except IsADirectoryError as e:
        return str(e)
    return f"Wrote JSON-LD to {dest}"


def export_cypher(ka_path: str, output: str, overwrite: bool = False) -> str:
    """Export a knowledge abstract to a Cypher MERGE script.

    Args:
        ka_path: Path to the knowledge abstract directory.
        output: Destination ``.cypher`` file.
        overwrite: Overwrite an existing, non-empty Cypher file.

    Uses the same ``export_ka_cypher`` adapter as ``he export cypher``.
    Does not create, mutate, or delete the KA.
    """
    from hyperextract.utils.exporters.ka import GraphTypeError, export_ka_cypher

    ka = _safe_load_ka(ka_path)
    if isinstance(ka, str):
        return ka
    try:
        dest = export_ka_cypher(ka, output, overwrite=overwrite)
    except GraphTypeError as e:
        return str(e)
    except FileExistsError as e:
        return f"{e} Pass overwrite=true to overwrite it."
    except IsADirectoryError as e:
        return str(e)
    return f"Wrote Cypher to {dest}"


# ---------------------------------------------------------------------------
# Server wiring
# ---------------------------------------------------------------------------


def build_server():
    """Build the MCP server with all tools registered."""
    try:  # mcp 1.x
        from mcp.server.fastmcp import FastMCP

        server = FastMCP(SERVER_NAME)
    except ImportError:  # mcp 2.x: FastMCP was renamed to MCPServer
        from mcp.server.mcpserver import MCPServer

        server = MCPServer(SERVER_NAME)
    for fn in (
        list_templates,
        info,
        search,
        ask,
        export_obsidian,
        export_graphml,
        export_csv,
        export_jsonld,
        export_cypher,
    ):
        server.tool()(fn)
    return server


def main() -> None:
    """Entry point: run the MCP server over stdio."""
    build_server().run()


if __name__ == "__main__":
    main()