hyperlane-plugin-mcp 22.14.1

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::*;

/// Configuration for an MCP Streamable HTTP server endpoint.
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct McpServerConfig {
    /// Server implementation name advertised in `initialize`.
    pub(crate) name: String,
    /// Server implementation version advertised in `initialize`.
    pub(crate) version: String,
    /// MCP protocol revision string (default `"2025-03-26"`).
    pub(crate) protocol_version: String,
    /// HTTP path matched by [`McpHook`] (default `"/mcp"`).
    pub(crate) endpoint: String,
    /// Explicit allowlist of `Origin` values (empty permits all origins; non-empty blocks DNS rebinding).
    pub(crate) allowed_origins: Vec<String>,
    /// Maximum concurrent active MCP sessions tracked in memory.
    #[get(type(copy))]
    pub(crate) max_sessions: usize,
    /// Maximum incoming request body size in bytes.
    #[get(type(copy))]
    pub(crate) max_body_size: usize,
    /// Session idle TTL in milliseconds (`0` disables expiration).
    #[get(type(copy))]
    pub(crate) session_ttl_ms: u64,
}

/// Metadata describing one registered MCP tool.
#[derive(Clone, Data, Debug, Deserialize, PartialEq, Serialize)]
pub struct McpTool {
    /// Unique tool name.
    pub(crate) name: String,
    /// Human-readable description of what the tool does.
    pub(crate) description: String,
    /// JSON Schema describing the tool's input arguments object.
    #[serde(rename = "inputSchema")]
    pub(crate) input_schema: Value,
}

/// Result returned by an MCP tool invocation.
#[derive(Clone, Data, Debug, Deserialize, PartialEq, Serialize)]
pub struct McpToolResult {
    /// MCP content blocks (such as `{"type":"text","text":"..."}`).
    pub(crate) content: Vec<Value>,
    /// Marks the tool call result as a tool-level error when `true`.
    #[serde(rename = "isError")]
    #[get(type(copy))]
    pub(crate) is_error: bool,
}

/// Metadata describing one readable MCP resource.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct McpResource {
    /// Canonical resource URI.
    pub(crate) uri: String,
    /// Human-readable resource display name.
    pub(crate) name: String,
    /// Short resource description.
    pub(crate) description: String,
    /// MIME media type (for example `"text/plain"` or `"application/json"`).
    #[serde(rename = "mimeType")]
    pub(crate) mime_type: String,
}

/// Content returned by reading one MCP resource.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct McpResourceContent {
    /// Resource URI that produced this content.
    pub(crate) uri: String,
    /// MIME media type of `text`.
    #[serde(rename = "mimeType")]
    pub(crate) mime_type: String,
    /// UTF-8 textual content.
    pub(crate) text: String,
}

/// Metadata describing one prompt argument.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct McpPromptArgument {
    /// Argument name.
    pub(crate) name: String,
    /// Argument description.
    pub(crate) description: String,
    /// Whether callers must supply this argument.
    #[get(type(copy))]
    pub(crate) required: bool,
}

/// Metadata describing one registered MCP prompt template.
#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct McpPrompt {
    /// Unique prompt name.
    pub(crate) name: String,
    /// Prompt description.
    pub(crate) description: String,
    /// Declared prompt arguments.
    pub(crate) arguments: Vec<McpPromptArgument>,
}

/// Zero-copy typed JSON-RPC 2.0 request envelope decoded directly from raw request bytes.
#[derive(Deserialize)]
pub(crate) struct McpRpcRequest<'a> {
    /// JSON-RPC version string (`"2.0"`).
    #[serde(borrow, default)]
    pub(crate) jsonrpc: Option<Cow<'a, str>>,
    /// MCP method name.
    #[serde(borrow, default)]
    pub(crate) method: Option<Cow<'a, str>>,
    /// Optional correlation id (`None` for notifications).
    #[serde(default)]
    pub(crate) id: Option<Value>,
    /// Optional parameters object.
    #[serde(default)]
    pub(crate) params: Option<Value>,
}

/// Typed JSON-RPC 2.0 success envelope serialized directly without intermediate `Value` trees.
#[derive(Serialize)]
pub(crate) struct McpRpcSuccess<'a, T> {
    /// Protocol version (`"2.0"`).
    pub(crate) jsonrpc: &'static str,
    /// Correlation identifier.
    pub(crate) id: &'a Value,
    /// Typed result payload.
    pub(crate) result: T,
}

/// Typed JSON-RPC 2.0 error body.
#[derive(Serialize)]
pub(crate) struct McpRpcErrorBody<'a> {
    /// JSON-RPC error code.
    pub(crate) code: i64,
    /// Error description.
    pub(crate) message: &'a str,
}

/// Typed JSON-RPC 2.0 error envelope serialized directly without intermediate `Value` trees.
#[derive(Serialize)]
pub(crate) struct McpRpcError<'a> {
    /// Protocol version (`"2.0"`).
    pub(crate) jsonrpc: &'static str,
    /// Correlation identifier.
    pub(crate) id: &'a Value,
    /// Error detail object.
    pub(crate) error: McpRpcErrorBody<'a>,
}

/// Empty JSON object `{}` serialized without heap allocations.
#[derive(Serialize)]
pub(crate) struct McpEmptyObject {}

/// Typed `tools` capability flags advertised in `initialize`.
#[derive(Serialize)]
pub(crate) struct McpToolsCap {
    /// Whether `tools/list_changed` notifications are supported.
    #[serde(rename = "listChanged")]
    pub(crate) list_changed: bool,
}

/// Typed `resources` capability flags advertised in `initialize`.
#[derive(Serialize)]
pub(crate) struct McpResourcesCap {
    /// Whether resource subscriptions are supported.
    pub(crate) subscribe: bool,
    /// Whether `resources/list_changed` notifications are supported.
    #[serde(rename = "listChanged")]
    pub(crate) list_changed: bool,
}

/// Typed `prompts` capability flags advertised in `initialize`.
#[derive(Serialize)]
pub(crate) struct McpPromptsCap {
    /// Whether `prompts/list_changed` notifications are supported.
    #[serde(rename = "listChanged")]
    pub(crate) list_changed: bool,
}

/// Typed server capabilities block advertised in `initialize`.
#[derive(Serialize)]
pub(crate) struct McpCapabilities {
    /// Tool capability flags.
    pub(crate) tools: McpToolsCap,
    /// Resource capability flags.
    pub(crate) resources: McpResourcesCap,
    /// Prompt capability flags.
    pub(crate) prompts: McpPromptsCap,
}

/// Typed `serverInfo` block advertised in `initialize`.
#[derive(Serialize)]
pub(crate) struct McpServerInfo<'a> {
    /// Server implementation name.
    pub(crate) name: &'a str,
    /// Server implementation version.
    pub(crate) version: &'a str,
}

/// Typed `initialize` result payload serialized directly without allocating `serde_json::Map`.
#[derive(Serialize)]
pub(crate) struct McpInitResult<'a> {
    /// Negotiated protocol version.
    #[serde(rename = "protocolVersion")]
    pub(crate) protocol_version: &'a str,
    /// Supported capabilities.
    pub(crate) capabilities: McpCapabilities,
    /// Server name and version metadata.
    #[serde(rename = "serverInfo")]
    pub(crate) server_info: McpServerInfo<'a>,
}

/// Typed `tools/list` result wrapper.
#[derive(Serialize)]
pub(crate) struct McpToolsListResult<'a> {
    /// Registered tool descriptors.
    pub(crate) tools: &'a [&'a McpTool],
}

/// Typed `resources/list` result wrapper.
#[derive(Serialize)]
pub(crate) struct McpResourcesListResult<'a> {
    /// Registered resource descriptors.
    pub(crate) resources: &'a [&'a McpResource],
}

/// Typed `prompts/list` result wrapper.
#[derive(Serialize)]
pub(crate) struct McpPromptsListResult<'a> {
    /// Registered prompt descriptors.
    pub(crate) prompts: &'a [&'a McpPrompt],
}

/// Typed `resources/read` result wrapper.
#[derive(Serialize)]
pub(crate) struct McpResourceReadResult<'a> {
    /// Single-element contents list.
    pub(crate) contents: [&'a McpResourceContent; 1],
}

/// Registered tool descriptor paired with its async handler.
#[derive(Clone)]
pub(crate) struct RegisteredTool {
    /// Public metadata advertised by `tools/list`.
    pub(crate) meta: McpTool,
    /// Async invocation handler.
    pub(crate) handler: McpToolHandler,
}

/// Registered resource descriptor paired with its async reader.
#[derive(Clone)]
pub(crate) struct RegisteredResource {
    /// Public metadata advertised by `resources/list`.
    pub(crate) meta: McpResource,
    /// Async content reader.
    pub(crate) handler: McpResourceHandler,
}

/// Registered prompt descriptor paired with its async renderer.
#[derive(Clone)]
pub(crate) struct RegisteredPrompt {
    /// Public metadata advertised by `prompts/list`.
    pub(crate) meta: McpPrompt,
    /// Async prompt renderer.
    pub(crate) handler: McpPromptHandler,
}

/// Thread-safe registry of MCP tools, resources, prompts, and sharded active client sessions.
#[derive(Clone)]
pub struct McpRegistry {
    /// Registered tools keyed by tool name (`XxHash3_64`).
    pub(crate) tools: HashMapXxHash3_64<String, RegisteredTool>,
    /// Registered resources keyed by resource URI (`XxHash3_64`).
    pub(crate) resources: HashMapXxHash3_64<String, RegisteredResource>,
    /// Registered prompts keyed by prompt name (`XxHash3_64`).
    pub(crate) prompts: HashMapXxHash3_64<String, RegisteredPrompt>,
    /// Fixed-size 16-shard active session tables mapping session id to creation/activity timestamp.
    pub(crate) session_shards: McpSessionShardArray,
    /// Lock-free active session count maintained across all shards.
    pub(crate) session_count: Arc<AtomicUsize>,
    /// Monotonic counter mixed with entropy to mint unpredictable session ids.
    pub(crate) next_session_seq: Arc<AtomicU64>,
    /// Per-registry entropy seed used when minting session identifiers.
    pub(crate) session_seed: u64,
}

/// Process-wide [`McpPlugin`] registry holding [`McpServerConfig`] and [`McpRegistry`].
#[derive(Clone)]
pub struct McpPlugin {
    /// Active MCP server configuration.
    pub(crate) config: McpServerConfig,
    /// Shared MCP tool/resource/prompt/session registry.
    pub(crate) registry: McpRegistry,
}

/// Opt-in [`ServerHook`] serving MCP Streamable HTTP (`POST` / `DELETE`) on the configured endpoint.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-60)]
pub struct McpHook;