Skip to main content

Module protocol

Module protocol 

Source
Expand description

§mcpr-protocol

MCP specification layer: JSON-RPC 2.0 parsing, MCP method classification, and session lifecycle management.

This crate is the foundation of the mcpr workspace. It contains everything related to understanding the MCP protocol itself, with zero coupling to HTTP frameworks or proxy logic.

§Responsibilities

  • JSON-RPC 2.0 parsing (lib.rs): Parse and classify JSON-RPC 2.0 messages (requests, notifications, responses). Provides ParsedBody for batch-aware parsing and McpMethod for typed MCP method discrimination.

  • MCP method constants: Single source of truth for MCP method strings (initialize, tools/call, resources/read, etc.).

  • Error handling: JSON-RPC error codes, error response builders, and error extraction from response bodies.

  • Session management (session module): MCP session state machine (Created -> Initialized -> Active -> Closed), SessionStore trait for pluggable storage backends, and MemorySessionStore for in-memory use.

  • Schema primitives (schema module): Pagination detection/merging, schema diffing (detect added/removed/modified tools, resources, prompts), and schema method classification. Pure helpers, no state.

  • Schema manager (schema_manager module): Top-level per-upstream view of an MCP server. Owns versioned snapshots built from ingested discovery responses, exposes query APIs (list_tools, get_tool, …), tracks stale flags, and defines the SchemaScanner trait for active discovery.

§Module Structure

mcpr-protocol/src/
+-- lib.rs              # JSON-RPC 2.0 types, parsing, MCP method classification
+-- schema.rs           # Pagination merge + diff primitives
+-- schema_manager/     # SchemaManager, SchemaStore, SchemaScanner, SchemaVersion
+-- session.rs          # Session state, SessionStore trait, MemorySessionStore

§Dependencies

Minimal: serde, serde_json, chrono, dashmap. No HTTP framework deps.

Modules§

error_code
schema
MCP schema capture: types, pagination merging, and diff logic.
schema_manager
Schema manager: the top-level per-upstream view of an MCP server.
session

Structs§

JsonRpcError
JsonRpcNotification
JsonRpcRequest
JsonRpcResponse
ParsedBody
Result of parsing a POST body as JSON-RPC 2.0.

Enums§

JsonRpcId
A parsed JSON-RPC 2.0 id (number or string).
JsonRpcMessage
A classified JSON-RPC 2.0 message.
McpMethod
Known MCP methods — lets the proxy know exactly what function is being called.

Constants§

COMPLETION_COMPLETE
INITIALIZE
INITIALIZED
LOGGING_SET_LEVEL
NOTIFICATIONS_CANCELLED
NOTIFICATIONS_PROGRESS
NOTIFICATIONS_TOOLS_LIST_CHANGED
PING
PROMPTS_GET
PROMPTS_LIST
RESOURCES_LIST
RESOURCES_READ
RESOURCES_SUBSCRIBE
RESOURCES_TEMPLATES_LIST
RESOURCES_UNSUBSCRIBE
TOOLS_CALL
TOOLS_LIST

Functions§

error_response
Build a JSON-RPC 2.0 error response as bytes. id can be a request id or Value::Null if the request id couldn’t be parsed.
extract_error_code
Extract the JSON-RPC error code from a response body (if it’s an error response).
parse_body
Parse a POST body as JSON-RPC 2.0 — single message or batch. Returns None if the body is not valid JSON-RPC 2.0.
parse_message
Parse a single JSON value as a JSON-RPC 2.0 message. Returns None if it doesn’t have "jsonrpc": "2.0".