"""Identity-revealing stub backend for live group / canary verification.

Unlike ``examples/provider_math`` (whose tool results are anonymous), this stub
echoes *which* backend instance served a call. Each group member is launched as
a separate subprocess with a distinct identity -- passed as the first CLI
argument (or the ``PROVIDER_IDENTITY`` env var) -- and its ``whoami`` tool
returns that identity in the result. A live test can therefore observe the
member a ``hangar_call`` was routed to and assert group-invocation and canary
routing behaviour end to end.

No official server can replace this one (#1097): none of them echoes which
backend instance served a call, which is the entire point -- group routing and
canary assertions need to observe the member, not the answer.

It speaks MCP over **stdio** by default (the transport hangar uses for
``mode: subprocess`` members); override with ``MCP_TRANSPORT=streamable-http``.
"""

import os
import sys

try:  # SDK v2: FastMCP -> MCPServer; host/port bind at run(), not construction.
    from mcp.server.mcpserver import MCPServer as FastMCP

    _MCP_V2 = True
except ImportError:  # SDK v1
    from mcp.server.fastmcp import FastMCP

    _MCP_V2 = False

# Identity precedence: first CLI arg, then env var, then a placeholder.
_IDENTITY = (sys.argv[1] if len(sys.argv) > 1 else "") or os.environ.get("PROVIDER_IDENTITY", "unknown")

_HOST = os.environ.get("MCP_HOST", "127.0.0.1")
_PORT = int(os.environ.get("MCP_PORT", "8080"))

if _MCP_V2:
    mcp = FastMCP(f"identity-provider[{_IDENTITY}]")
else:
    mcp = FastMCP(f"identity-provider[{_IDENTITY}]", host=_HOST, port=_PORT, transport_security=None)


@mcp.tool(name="whoami")
def whoami() -> dict:
    """Return the identity of the backend member that served this call.

    Returns:
        Dictionary with a 'member' key naming the serving backend instance.
    """
    return {"member": _IDENTITY}


@mcp.tool(name="echo")
def echo(text: str) -> dict:
    """Echo text back, tagged with the serving member's identity.

    Args:
        text: Arbitrary text to echo.

    Returns:
        Dictionary with 'member' and 'text' keys.
    """
    return {"member": _IDENTITY, "text": text}


def main():
    """Run the identity provider.

    Defaults to stdio transport (subprocess mode). Override with
    MCP_TRANSPORT=streamable-http for a standalone HTTP backend.
    """
    transport = os.environ.get("MCP_TRANSPORT", "stdio")
    if transport != "streamable-http":
        mcp.run(transport="stdio")
    elif _MCP_V2:
        mcp.run(transport="streamable-http", host=_HOST, port=_PORT)
    else:
        mcp.run(transport="streamable-http")


if __name__ == "__main__":
    main()
