baikai-mcp-0.1.0.0: src/Baikai/Mcp/Error.hs
-- | Failures of an MCP operation.
--
-- Every failure says whether the request reached the server. That matters
-- most for @tools/call@: a request that was 'NotSent' had no effect, one the
-- server 'Answered' with an error was refused, and one whose reply was lost
-- is 'Uncertain' — the remote action may have run. This package never
-- replays a request whose outcome is uncertain; the owner of the attempt
-- decides.
--
-- Values of 'McpError' are safe to render and log: they never carry
-- request or response headers, bodies, or credentials. Text that came from
-- the server is clipped.
module Baikai.Mcp.Error
( McpError (..),
McpErrorKind (..),
Delivery (..),
mcpError,
setMessage,
renderMcpError,
)
where
import Baikai.Error (BaikaiError)
import Baikai.Mcp.Protocol (RpcError (..), clipText)
import Control.Exception (Exception (..))
import Data.Text (Text)
import Data.Text qualified as Text
import GHC.Generics (Generic)
-- | Whether the server saw the request.
data Delivery
= -- | Nothing was transmitted: a local check, the host's hook or a
-- connection failure stopped the request first.
NotSent
| -- | The server answered definitively (an error response or status).
Answered
| -- | The request may have been processed but no usable reply arrived.
Uncertain
deriving stock (Eq, Ord, Show, Generic)
data McpErrorKind
= -- | The host's request hook refused the send.
RequestDenied
| -- | The connection handle was already released.
ConnectionReleased
| -- | The server returned a JSON-RPC error; see 'rpc'.
RemoteRpcError
| -- | The server returned an HTTP failure status without a JSON-RPC
-- error, or a status this client does not follow (such as a redirect).
HttpFailure
| -- | The network or the peer failed; see 'transport' for the core
-- classification.
TransportFailure
| -- | The operation's deadline passed.
TimedOut
| -- | The reply could not be understood.
MalformedReply
| -- | A configured size, count or round limit was reached.
LimitExceeded
| -- | The server speaks no protocol version this client implements.
IncompatibleServer
| -- | A legacy server no longer recognizes the session it issued.
SessionExpired
| -- | The operation is not available on this connection's profile.
UnsupportedOperation
| -- | The request could not be built from the caller's input.
InvalidInput
deriving stock (Eq, Show, Generic)
data McpError = McpError
{ kind :: !McpErrorKind,
-- | A bounded, credential-free description.
message :: !Text,
delivery :: !Delivery,
-- | The HTTP status, when an HTTP response arrived.
httpStatus :: !(Maybe Int),
-- | The JSON-RPC error, when the server sent one.
rpc :: !(Maybe RpcError),
-- | The @WWW-Authenticate@ challenge of a 401 or 403 response.
challenge :: !(Maybe Text),
-- | Core's classification of a transport exception, including whether
-- a fresh attempt could succeed.
transport :: !(Maybe BaikaiError)
}
deriving stock (Eq, Show, Generic)
instance Exception McpError where
displayException = Text.unpack . renderMcpError
mcpError :: McpErrorKind -> Delivery -> Text -> McpError
mcpError k d m =
McpError
{ kind = k,
message = clipText 512 m,
delivery = d,
httpStatus = Nothing,
rpc = Nothing,
challenge = Nothing,
transport = Nothing
}
-- | Replace the description, clipping it.
setMessage :: Text -> McpError -> McpError
setMessage m e = McpError {kind = e.kind, message = clipText 512 m, delivery = e.delivery, httpStatus = e.httpStatus, rpc = e.rpc, challenge = e.challenge, transport = e.transport}
-- | One line for a log or a user.
renderMcpError :: McpError -> Text
renderMcpError e =
Text.intercalate
" "
( [e.message]
<> maybe [] (\s -> ["(HTTP " <> Text.pack (show s) <> ")"]) e.httpStatus
<> maybe [] (\r -> ["(JSON-RPC " <> Text.pack (show r.code) <> ": " <> r.message <> ")"]) e.rpc
<> case e.delivery of
Uncertain -> ["[the server may have processed the request]"]
_ -> []
)