"""Local STDIO proxy for Claude Desktop.

Creates a local MCP server that proxies requests to the remote Kal MCP
server. Runs via STDIO transport so Claude Desktop can spawn it directly.

Configuration (via environment variables):
    KAL_MCP_URL  - URL of the remote MCP server
    KAL_API_KEY  - Your personal MCP API key (from Kal Settings page)
"""

import os

from fastmcp import FastMCP, Client
from fastmcp.client.auth import BearerAuth

KAL_MCP_URL = os.environ.get("KAL_MCP_URL", "")
KAL_API_KEY = os.environ.get("KAL_API_KEY", "")


def main():
    if not KAL_MCP_URL:
        raise EnvironmentError(
            "KAL_MCP_URL must be set to the remote Kal MCP server URL "
            "(e.g. https://kal-mcp-server-staging.calmsea-5e9b87e0.uksouth.azurecontainerapps.io/mcp)"
        )

    client = Client(KAL_MCP_URL)
    if KAL_API_KEY:
        client = Client(KAL_MCP_URL, auth=BearerAuth(token=KAL_API_KEY))

    proxy = FastMCP.as_proxy(client, name="Kal (Local Proxy)")
    proxy.run()


if __name__ == "__main__":
    main()
