hyperlane-plugin-mcp 23.0.0

A Model Context Protocol (MCP) Streamable HTTP server plugin for the Hyperlane framework with tool, resource, and prompt registries, session lifecycle management, DNS-rebinding Origin validation, and an opt-in ServerHook.
Documentation
use super::*;

/// Errors returned by MCP tool, resource, prompt, or protocol handlers.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum McpError {
    /// Invalid or missing parameters (`-32602`).
    InvalidParams(String),
    /// Requested tool, resource, or prompt was not found (`-32601`).
    NotFound(String),
    /// Internal execution failure (`-32603`).
    Internal(String),
}

/// Outcome returned by [`crate::McpRegistry::handle_json_bytes`] for HTTP transport translation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum McpDispatchOutcome {
    /// JSON response payload plus optional newly assigned `Mcp-Session-Id`.
    Response {
        /// Serialized JSON-RPC response bytes.
        body: Vec<u8>,
        /// Newly minted session id when answering `initialize`.
        new_session_id: Option<String>,
    },
    /// Notification accepted with HTTP `202 Accepted` and no body.
    Accepted,
    /// Caller supplied an unknown or expired `Mcp-Session-Id` (`404 Not Found`).
    UnknownSession,
    /// Active session table reached `max_sessions` (`503 Service Unavailable`).
    SessionLimitReached,
}

/// Zero-allocation JSON-RPC 2.0 correlation identifier decoded without `serde_json::Value` allocations.
pub(crate) enum McpRpcId<'a> {
    /// JSON `null`.
    Null,
    /// Unsigned 64-bit integer id.
    U64(u64),
    /// Signed 64-bit integer id.
    I64(i64),
    /// Borrowed or unescaped string id.
    Str(Cow<'a, str>),
    /// Fallback JSON value for uncommon id types (floats, booleans).
    Other(Value),
}

/// Zero-allocation JSON-RPC 2.0 envelope field discriminator.
pub(crate) enum McpRpcField {
    /// `"jsonrpc"` version field.
    JsonRpc,
    /// `"method"` field.
    Method,
    /// `"id"` correlation field.
    Id,
    /// `"params"` payload field.
    Params,
    /// Unknown top-level field skipped via `IgnoredAny`.
    Ignore,
}

/// Zero-allocation field discriminator for `tools/call` and `prompts/get` params.
pub(crate) enum McpNamedParamsField {
    /// `"name"` field.
    Name,
    /// `"arguments"` field.
    Arguments,
    /// Unknown params field skipped via `IgnoredAny`.
    Ignore,
}

/// Zero-allocation field discriminator for `resources/read` params.
pub(crate) enum McpUriParamsField {
    /// `"uri"` field.
    Uri,
    /// Unknown params field skipped via `IgnoredAny`.
    Ignore,
}

/// Parsed MCP `params` representation avoiding intermediate `serde_json::Map` trees on known methods.
pub(crate) enum McpParsedParams<'a> {
    /// No params supplied or skipped via `IgnoredAny`.
    None,
    /// Non-object params value (produces `-32602 Invalid params` when an object is required).
    Invalid,
    /// Directly decoded `tools/call` or `prompts/get` parameters.
    Named {
        /// Tool or prompt name borrowed from the raw request when unescaped.
        name: Option<Cow<'a, str>>,
        /// Optional arguments JSON value.
        arguments: Option<Value>,
    },
    /// Directly decoded `resources/read` parameters.
    Uri {
        /// Resource URI string.
        uri: Option<String>,
    },
    /// Fallback raw `Value` used only when `"params"` appeared before `"method"` in JSON key order.
    Raw(Value),
}