"""Remote MCP governance (Context7).

Shows tool filtering + tag activation + boundary visibility against a remote MCP
server. Requires network access.
"""

import asyncio
import os
from enum import Enum

from pydantic_ai._run_context import RunContext
from pydantic_ai.mcp import MCPServerStreamableHTTP

from rgltr import boundary, configure, context, tag
from rgltr.pydantic_ai import RegulatedMCPAdapter


class Tag(Enum):
    DOCS = "docs"


class Boundary(Enum):
    EXTERNAL = "external"


def _run_ctx() -> RunContext:
    return RunContext(deps=None, model=None, usage=None)  # type: ignore[arg-type]


async def main() -> None:
    configure(boundaries={Boundary.EXTERNAL: [Tag.DOCS]})

    server = MCPServerStreamableHTTP(url="https://mcp.context7.com/mcp", timeout=5)
    adapter = RegulatedMCPAdapter(
        wrapped=server,
        tools={
            "resolve-library-id": [tag(activates=[Tag.DOCS])],
            "query-docs": [boundary(Boundary.EXTERNAL)],
        },
    )

    try:
        async with adapter:
            with context():
                tools = await adapter.get_tools(_run_ctx())
                print("tools before tag:", sorted(tools))

                tool = tools["resolve-library-id"]
                await adapter.call_tool(
                    "resolve-library-id",
                    {"libraryName": "react", "query": "react hooks"},
                    _run_ctx(),
                    tool,
                )

                tools_after = await adapter.get_tools(_run_ctx())
                print("tools after tag:", sorted(tools_after))
    except Exception as exc:  # pragma: no cover - network environment may be unavailable
        print(f"remote MCP unavailable: {exc}")
        os._exit(0)


if __name__ == "__main__":
    asyncio.run(main())
