Hermes.Message (hermes_mcp v0.3.1)
Handles parsing and validation of MCP (Model Context Protocol) messages using the Peri library.
This module provides functions to parse and validate MCP messages based on the Model Context Protocol schema
Summary
Functions
Decodes raw data (possibly containing multiple messages) into JSON-RPC messages.
Encodes a log message notification to be sent to the client.
Encodes a notification message to a JSON-RPC 2.0 compliant string.
Encodes a progress notification message to a JSON-RPC 2.0 compliant string.
Encodes a request message to a JSON-RPC 2.0 compliant string.
Encodes a response message to a JSON-RPC 2.0 compliant string.
Generates a unique progress token that can be used for tracking progress.
Determines if a JSON-RPC message is an error.
Determines if a JSON-RPC message is a notification.
Determines if a JSON-RPC message is a request.
Determines if a JSON-RPC message is a response.
Validates a decoded JSON message to ensure it complies with the MCP schema.
Functions
Decodes raw data (possibly containing multiple messages) into JSON-RPC messages.
Returns either:
{:ok, messages}
where messages is a list of parsed JSON-RPC messages{:error, reason}
if parsing fails
@spec encode_log_message(String.t(), term(), String.t() | nil) :: {:ok, String.t()} | {:error, term()}
Encodes a log message notification to be sent to the client.
Parameters
level
- The log level (debug, info, notice, warning, error, critical, alert, emergency)data
- The data to be logged (any JSON-serializable value)logger
- Optional name of the logger issuing the message
Returns the encoded notification string with a newline character appended.
Encodes a notification message to a JSON-RPC 2.0 compliant string.
Returns the encoded string with a newline character appended.
@spec encode_progress_notification(String.t() | integer(), number(), number() | nil) :: {:ok, String.t()} | {:error, term()}
Encodes a progress notification message to a JSON-RPC 2.0 compliant string.
Parameters
progress_token
- The token that was provided in the original request (string or integer)progress
- The current progress value (number)total
- Optional total value for the operation (number)
Returns the encoded string with a newline character appended.
Encodes a request message to a JSON-RPC 2.0 compliant string.
Returns the encoded string with a newline character appended.
Encodes a response message to a JSON-RPC 2.0 compliant string.
Returns the encoded string with a newline character appended.
@spec generate_progress_token() :: String.t()
Generates a unique progress token that can be used for tracking progress.
This is a convenience function to create tokens for the progress notification system. Returns a string token with "progress_" prefix followed by a Base64-encoded unique identifier.
Determines if a JSON-RPC message is an error.
Determines if a JSON-RPC message is a notification.
Determines if a JSON-RPC message is a request.
Determines if a JSON-RPC message is a response.
Validates a decoded JSON message to ensure it complies with the MCP schema.