"""GA4GH MCP server implementation.

Exposes GA4GH specification plugins as MCP tools, resources, and prompts.
Uses the official MCP Python SDK for stdio/SSE transport.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from mcp.server import Server
from mcp.server.stdio import stdio_server

from ai4gh.mcp.prompts import get_prompt, list_prompts
from ai4gh.mcp.resources import get_resource, list_resources
from ai4gh.mcp.tools import handle_tool_call, list_tools

if TYPE_CHECKING:
    from mcp.types import GetPromptResult, Resource, TextContent, Tool

server = Server("ai4gh")


@server.list_tools()
async def server_list_tools() -> list[Tool]:
    """Return all available GA4GH tools."""
    return list_tools()


@server.call_tool()
async def server_call_tool(name: str, arguments: dict) -> list[TextContent]:
    """Call a GA4GH tool by name."""
    return await handle_tool_call(name, arguments)


@server.list_resources()
async def server_list_resources() -> list[Resource]:
    """Return all available GA4GH resources."""
    return list_resources()


@server.read_resource()
async def server_read_resource(uri: str) -> str:
    """Read a GA4GH resource by URI."""
    return get_resource(uri)


@server.list_prompts()
async def server_list_prompts() -> list[dict]:
    """Return all available GA4GH prompts."""
    return list_prompts()


@server.get_prompt()
async def server_get_prompt(name: str, arguments: dict | None = None) -> GetPromptResult:
    """Get a GA4GH prompt by name."""
    return get_prompt(name, arguments)


async def run_server() -> None:
    """Run the MCP server on stdio transport."""
    async with stdio_server() as (read_stream, write_stream):
        await server.run(
            read_stream,
            write_stream,
            server.create_initialization_options(),
        )
