{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Network.MCP.Server.StdIO
  ( runServerWithSTDIO,
    withSTDIOServer,
  )
where

import Network.MCP.Server
import Network.MCP.Transport.StdIO (newSTDIOTransport)
import UnliftIO (Async)

-- | Run an MCP server using STDIO transport, handling messages until the transport is closed.
runServerWithSTDIO :: Server -> IO ()
runServerWithSTDIO :: Server -> IO ()
runServerWithSTDIO Server
server = do
  -- Create the STDIO transport
  STDIOTransport
stdioTransport <- IO STDIOTransport
newSTDIOTransport

  -- Start the transport
  Server -> STDIOTransport -> IO ()
forall t. Transport t => Server -> t -> IO ()
runServerWithTransport Server
server STDIOTransport
stdioTransport

withSTDIOServer :: Server -> (Async () -> IO a) -> IO a
withSTDIOServer :: forall a. Server -> (Async () -> IO a) -> IO a
withSTDIOServer Server
server Async () -> IO a
action = do
  STDIOTransport
stdioTransport <- IO STDIOTransport
newSTDIOTransport
  Server -> STDIOTransport -> (Async () -> IO a) -> IO a
forall t a.
Transport t =>
Server -> t -> (Async () -> IO a) -> IO a
withTransportServer Server
server STDIOTransport
stdioTransport Async () -> IO a
action