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
/// Default MCP Streamable HTTP endpoint path.
pub const DEFAULT_MCP_ENDPOINT: &str = "/mcp";

/// Default MCP protocol revision advertised during `initialize`.
pub const DEFAULT_MCP_PROTOCOL_VERSION: &str = "2025-03-26";

/// HTTP header carrying the negotiated MCP session identifier.
pub const MCP_SESSION_ID_HEADER: &str = "mcp-session-id";

/// Default maximum concurrent MCP sessions stored in memory.
pub const DEFAULT_MCP_MAX_SESSIONS: usize = 1024;

/// Default maximum incoming JSON-RPC request body size (1 MiB).
pub const DEFAULT_MCP_MAX_BODY_SIZE: usize = 1024 * 1024;

/// Default MCP session idle TTL in milliseconds (30 minutes).
pub const DEFAULT_MCP_SESSION_TTL_MS: u64 = 1_800_000;

/// Number of independent session shards to reduce write-lock contention.
pub(crate) const MCP_SESSION_SHARD_COUNT: usize = 16;

/// Exact byte length of a minted MCP session identifier (`"mcp-sess-"` + 32 lowercase hex digits).
pub(crate) const MCP_SESSION_ID_LEN: usize = MCP_SESSION_PREFIX.len() + 32;

/// Strict lowercase ASCII hex decode lookup table (`0..=15` for `0-9a-f`, `0xFF` otherwise).
pub(crate) const MCP_HEX_DECODE_LUT: [u8; 256] = {
    let mut lut: [u8; 256] = [0xFF; 256];
    let mut b: usize = b'0' as usize;
    while b <= b'9' as usize {
        lut[b] = (b - b'0' as usize) as u8;
        b += 1;
    }
    let mut c: usize = b'a' as usize;
    while c <= b'f' as usize {
        lut[c] = (c - b'a' as usize + 10) as u8;
        c += 1;
    }
    lut
};

/// Two-digit ASCII decimal lookup table for direct JSON-RPC id and code formatting.
pub(crate) const MCP_DEC_DIGITS_LUT: &[u8; 200] = b"00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";

/// JSON-RPC 2.0 envelope prefix preceding the correlation `id`.
pub(crate) const MCP_WIRE_ENVELOPE_PREFIX: &[u8] = br#"{"jsonrpc":"2.0","id":"#;

/// JSON-RPC 2.0 separator introducing the `"result"` payload.
pub(crate) const MCP_WIRE_RESULT_SEP: &[u8] = br#","result":"#;

/// JSON-RPC 2.0 suffix completing a `"ping"` response (`,"result":{}}`).
pub(crate) const MCP_WIRE_PING_SUFFIX: &[u8] = br#","result":{}}"#;

/// JSON-RPC 2.0 separator introducing `"error":{"code":`.
pub(crate) const MCP_WIRE_ERROR_CODE_SEP: &[u8] = br#","error":{"code":"#;

/// JSON-RPC 2.0 separator introducing `"message":` inside an error object.
pub(crate) const MCP_WIRE_ERROR_MSG_SEP: &[u8] = br#","message":"#;

/// JSON-RPC 2.0 closing braces for an error envelope.
pub(crate) const MCP_WIRE_ERROR_SUFFIX: &[u8] = b"}}";

/// JSON prefix introducing `"protocolVersion":` in an `initialize` result.
pub(crate) const MCP_WIRE_INIT_PROTO: &[u8] = br#"{"protocolVersion":"#;

/// Static capabilities block and `"serverInfo":{"name":` prefix in an `initialize` result.
pub(crate) const MCP_WIRE_INIT_CAPS_NAME: &[u8] = br#","capabilities":{"tools":{"listChanged":false},"resources":{"subscribe":false,"listChanged":false},"prompts":{"listChanged":false}},"serverInfo":{"name":"#;

/// Separator introducing `"version":` in an `initialize` result.
pub(crate) const MCP_WIRE_INIT_VERSION: &[u8] = br#","version":"#;

/// Closing braces completing an `initialize` envelope.
pub(crate) const MCP_WIRE_INIT_SUFFIX: &[u8] = b"}}}";

/// JSON literal `null` bytes.
pub(crate) const MCP_JSON_NULL: &[u8] = b"null";

/// JSON field key for JSON-RPC version (`"jsonrpc"`).
pub(crate) const KEY_JSONRPC: &str = "jsonrpc";

/// JSON field key for JSON-RPC method (`"method"`).
pub(crate) const KEY_METHOD: &str = "method";

/// JSON field key for JSON-RPC params (`"params"`).
pub(crate) const KEY_PARAMS: &str = "params";

/// JSON field key for JSON-RPC correlation id (`"id"`).
pub(crate) const KEY_ID: &str = "id";

/// Serde visitor expectation message for JSON-RPC request field names.
pub(crate) const EXPECTING_MCP_FIELD: &str = "a JSON-RPC 2.0 field name";

/// Serde visitor expectation message for JSON-RPC correlation ids.
pub(crate) const EXPECTING_MCP_ID: &str = "a JSON-RPC 2.0 correlation id";

/// Serde visitor expectation message for JSON-RPC request objects.
pub(crate) const EXPECTING_MCP_REQUEST: &str = "a JSON-RPC 2.0 request object";

/// Serde visitor expectation message for MCP params objects.
pub(crate) const EXPECTING_MCP_PARAMS: &str = "an MCP params JSON value";

/// Prefix prepended to generated MCP session identifiers.
pub(crate) const MCP_SESSION_PREFIX: &str = "mcp-sess-";

/// Lowercase hexadecimal alphabet used for stack-allocated session token formatting.
pub(crate) const MCP_HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";

/// SplitMix64 Weyl increment constant.
pub(crate) const SPLITMIX_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15;

/// SplitMix64 first avalanche multiplier.
pub(crate) const SPLITMIX_MUL1: u64 = 0xBF58_476D_1CE4_E5B9;

/// SplitMix64 second avalanche multiplier.
pub(crate) const SPLITMIX_MUL2: u64 = 0x94D0_49BB_1331_11EB;

/// JSON-RPC version string required by MCP messages.
pub(crate) const MCP_JSONRPC_VERSION: &str = "2.0";

/// Default server display name when none is supplied.
pub(crate) const DEFAULT_MCP_SERVER_NAME: &str = "hyperlane-mcp";

/// Default server version when none is supplied.
pub(crate) const DEFAULT_MCP_SERVER_VERSION: &str = "1.0.0";

/// JSON-RPC Parse Error code.
pub(crate) const MCP_ERR_PARSE: i64 = -32700;

/// JSON-RPC Invalid Request code.
pub(crate) const MCP_ERR_INVALID_REQUEST: i64 = -32600;

/// JSON-RPC Method Not Found code.
pub(crate) const MCP_ERR_METHOD_NOT_FOUND: i64 = -32601;

/// JSON-RPC Invalid Params code.
pub(crate) const MCP_ERR_INVALID_PARAMS: i64 = -32602;

/// JSON-RPC Internal Error code.
pub(crate) const MCP_ERR_INTERNAL: i64 = -32603;

/// MCP `initialize` method name.
pub(crate) const METHOD_INITIALIZE: &str = "initialize";

/// MCP `notifications/initialized` method name.
pub(crate) const METHOD_NOTIF_INITIALIZED: &str = "notifications/initialized";

/// MCP `ping` method name.
pub(crate) const METHOD_PING: &str = "ping";

/// MCP `tools/list` method name.
pub(crate) const METHOD_TOOLS_LIST: &str = "tools/list";

/// MCP `tools/call` method name.
pub(crate) const METHOD_TOOLS_CALL: &str = "tools/call";

/// MCP `resources/list` method name.
pub(crate) const METHOD_RESOURCES_LIST: &str = "resources/list";

/// MCP `resources/read` method name.
pub(crate) const METHOD_RESOURCES_READ: &str = "resources/read";

/// MCP `prompts/list` method name.
pub(crate) const METHOD_PROMPTS_LIST: &str = "prompts/list";

/// MCP `prompts/get` method name.
pub(crate) const METHOD_PROMPTS_GET: &str = "prompts/get";

/// Error text for JSON parse failure.
pub(crate) const MSG_MCP_PARSE_ERROR: &str = "Parse error";

/// Error text for malformed JSON-RPC envelope.
pub(crate) const MSG_MCP_INVALID_REQUEST: &str = "Invalid Request";

/// Error text for unknown MCP method.
pub(crate) const MSG_MCP_METHOD_NOT_FOUND: &str = "Method not found";

/// Error text for missing or invalid method parameters.
pub(crate) const MSG_MCP_INVALID_PARAMS: &str = "Invalid params";

/// Error text when the MCP session table reaches `max_sessions`.
pub(crate) const MSG_MCP_SESSION_LIMIT: &str = "MCP session limit reached";

/// Error text when an unknown tool name is invoked.
pub(crate) const MSG_MCP_UNKNOWN_TOOL: &str = "Unknown MCP tool";

/// Error text when an unknown resource URI is requested.
pub(crate) const MSG_MCP_UNKNOWN_RESOURCE: &str = "Unknown MCP resource";

/// Error text when an unknown prompt name is requested.
pub(crate) const MSG_MCP_UNKNOWN_PROMPT: &str = "Unknown MCP prompt";

/// JSON field key for MCP content block type.
pub(crate) const KEY_TYPE: &str = "type";

/// JSON field key and value for MCP text content.
pub(crate) const KEY_TEXT: &str = "text";

/// JSON field key for entity name.
pub(crate) const KEY_NAME: &str = "name";

/// JSON field key for tool or prompt arguments.
pub(crate) const KEY_ARGUMENTS: &str = "arguments";

/// JSON field key for resource URI.
pub(crate) const KEY_URI: &str = "uri";

/// ASCII digit zero byte (`0`).
pub(crate) const ASCII_ZERO: u8 = b'0';

/// ASCII minus byte (`-`).
pub(crate) const ASCII_MINUS: u8 = b'-';

/// ASCII double quote byte (`"`).
pub(crate) const ASCII_QUOTE: u8 = b'"';

/// ASCII backslash byte (`\`).
pub(crate) const ASCII_BACKSLASH: u8 = b'\\';

/// ASCII closing brace byte (`}`).
pub(crate) const ASCII_RBRACE: u8 = b'}';