Skip to content

The platform for continuously improving AI agents.

2.6 KB
Raw Blame
from asgiref.sync import sync_to_async

from overbae.core.errors import InputValidationError
from overbae.services import context_graph
from overbae.services.mcp.contracts.context_graph import (
    ContextCapture,
    ContextPage,
    ContextQuery,
    ContextReceipt,
    ContextRefresh,
    ContextSummary,
)
from overbae.services.mcp.errors import MCPError


def invoke(name, payload, context):
    try:
        if name == "inspect_context_graph":
            return ContextPage(**context_graph.inspect(context.project, **payload.model_dump()))
        if name == "refresh_context_graph":
            return ContextSummary(**context_graph.refresh(context.project))
        snapshot = context_graph.capture(context.project, **payload.model_dump())
        return ContextReceipt(**context_graph.snapshot_receipt(snapshot))
    except InputValidationError as exc:
        raise MCPError("invalid_context", exc.detail) from exc


def register_context_tools(catalog):
    from overbae.services.mcp.catalog import ToolDefinition

    for name, model, output, description, read_only in (
        (
            "inspect_context_graph",
            ContextQuery,
            ContextPage,
            "Read project context and upstream/downstream relationships. Page nodes; typed roots select neighbors. Training/workshop views withhold evaluation results.",
            True,
        ),
        (
            "refresh_context_graph",
            ContextRefresh,
            ContextSummary,
            "Rebuild project context from retained agent, data, training, evaluation and serving records. Starts no provider work.",
            False,
        ),
        (
            "capture_context",
            ContextCapture,
            ContextReceipt,
            "Pin a purpose-scoped context view by request_key. Returns an immutable snapshot and evidence fingerprint.",
            False,
        ),
    ):

        async def handler(payload, context, name=name):
            return await sync_to_async(invoke, thread_sensitive=True)(name, payload, context)

        catalog.register(
            ToolDefinition(
                name=name,
                title=name.replace("_", " ").capitalize(),
                description=description,
                input_model=model,
                output_model=output,
                read_only=read_only,
                idempotent=True,
                open_world=False,
                required_scopes=frozenset(
                    {"overmind:read" if read_only else "overmind:data:write"}
                ),
                cost_class="free",
                async_mode="sync",
            ),
            handler,
        )