# Copyright (c) 2025 Dedalus Labs, Inc. and its contributors
# SPDX-License-Identifier: MIT

"""STDIO transport adapter."""

from __future__ import annotations

from .base import BaseTransport
from mcp.server.stdio import stdio_server


def get_stdio_server():
    """Return the SDK's stdio context manager.

    Separated into a helper so tests can patch it with in-memory transports.
    """
    return stdio_server


class StdioTransport(BaseTransport):
    """Run an :class:`dedalus_mcp.server.MCPServer` over STDIO."""

    TRANSPORT = ("stdio", "STDIO", "Standard IO")

    async def run(self, *, raise_exceptions: bool = False, stateless: bool = False) -> None:
        stdio_ctx = get_stdio_server()
        init_options = self.server.create_initialization_options()

        async with stdio_ctx() as (read_stream, write_stream):
            await self.server.run(
                read_stream, write_stream, init_options, raise_exceptions=raise_exceptions, stateless=stateless
            )

    async def stop(self) -> None:
        """Stop is a no-op for STDIO transport as lifecycle is managed by the context manager."""
