baikai-mcp-0.1.0.0: src/Baikai/Mcp/Internal/Binding.hs
-- | The seam between the protocol client and a wire binding.
--
-- A binding moves already-encoded JSON-RPC messages. It knows nothing
-- about eras, @_meta@, hooks or continuation: the client layer in
-- "Baikai.Mcp.Client" decides what to send and with which headers, and a
-- binding delivers it and returns the correlated reply. Request-scoped
-- bindings (Streamable HTTP) read the reply from the response to the same
-- request; multiplexed bindings (the deprecated HTTP+SSE transport and
-- caller-owned byte streams) route replies from one shared channel with
-- "Baikai.Mcp.Internal.Mux".
--
-- This module is internal. Its contents may change in any release.
module Baikai.Mcp.Internal.Binding
( WireLimits (..),
Exchange (..),
Reply (..),
StreamItem (..),
MessageStream (..),
Binding (..),
BindingShape (..),
)
where
import Baikai.Mcp.Error (McpError)
import Baikai.Mcp.Protocol (IncomingMessage, RequestId, RpcError)
import Data.Aeson.Types (Object)
import Data.ByteString (ByteString)
import Data.IORef (IORef)
import Data.Text (Text)
import Network.HTTP.Types (Header)
-- | Size ceilings a binding enforces while reading.
data WireLimits = WireLimits
{ -- | Bytes in one reply, including request-scoped notifications that
-- precede it.
maxResponseBytes :: !Int,
-- | Bytes in one message, SSE event or line.
maxFrameBytes :: !Int
}
-- | One request to send and how to treat what arrives before its reply.
data Exchange = Exchange
{ requestId :: !RequestId,
method :: !Text,
body :: !ByteString,
-- | Every header to send: protocol mirrors and the host hook's
-- additions. Ignored by bindings without a header layer.
headers :: ![Header],
-- | A request-scoped notification (progress, log) arrived.
onNotification :: Text -> Object -> IO (),
-- | A legacy server sent a request on this reply's stream.
onServerRequest :: RequestId -> Text -> Object -> IO ()
}
-- | The correlated reply to an 'Exchange'.
data Reply = Reply
{ outcome :: !(Either RpcError Object),
-- | Bytes read for this reply.
bytes :: !Int,
status :: !(Maybe Int),
-- | A legacy @Mcp-Session-Id@ issued with the reply.
sessionId :: !(Maybe ByteString)
}
-- | What a long-lived stream yields.
data StreamItem
= StreamMessage !IncomingMessage
| -- | The stream ended: 'Nothing' for a clean end of input, or the
-- failure that ended it.
StreamEnded !(Maybe McpError)
data MessageStream = MessageStream
{ next :: IO StreamItem,
-- | Release the stream. On HTTP this closes the response, which the
-- server treats as cancellation; on a shared channel it sends
-- @notifications/cancelled@. Safe to call more than once.
close :: IO ()
}
-- | Whether the binding has a per-request header layer.
data BindingShape = RequestScopedHttp | MultiplexedHttp | ByteStream
deriving stock (Eq, Show)
data Binding = Binding
{ shape :: !BindingShape,
-- | Rendered endpoint, without credentials, for diagnostics.
endpointText :: !Text,
-- | @scheme://host:port@ of the endpoint, for credential scoping.
origin :: !Text,
exchange :: WireLimits -> Exchange -> IO (Either McpError Reply),
-- | Send a notification or a response; no reply is expected.
post :: [Header] -> ByteString -> IO (Either McpError ()),
-- | Open a long-lived reply stream (@subscriptions/listen@), when the
-- binding supports one.
openStream :: Maybe (WireLimits -> Exchange -> IO (Either McpError MessageStream)),
-- | End a legacy session (HTTP @DELETE@), when the binding has one.
terminate :: Maybe ([Header] -> IO ()),
-- | Release every resource the binding owns. Idempotent.
release :: IO (),
-- | Where a multiplexed binding delivers server-initiated requests that
-- belong to no exchange. Set by the client after construction.
unsolicited :: !(Maybe (IORef (RequestId -> Text -> Object -> IO ())))
}