Hermes.MCP.Message (hermes_mcp v0.4.0)

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 an error message to a JSON-RPC 2.0 compliant string.

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.

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

decode(data)

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

encode_error(response, id)

Encodes an error message to a JSON-RPC 2.0 compliant string.

Returns the encoded string with a newline character appended.

encode_log_message(level, data, logger \\ nil)

@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.

encode_notification(notification)

Encodes a notification message to a JSON-RPC 2.0 compliant string.

Returns the encoded string with a newline character appended.

encode_progress_notification(progress_token, progress, total \\ nil)

@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.

encode_request(request, id)

Encodes a request message to a JSON-RPC 2.0 compliant string.

Returns the encoded string with a newline character appended.

encode_response(response, id)

Encodes a response message to a JSON-RPC 2.0 compliant string.

Returns the encoded string with a newline character appended.

error_schema(data)

error_schema!(data)

get_schema(atom)

is_error(data)

(macro)

Determines if a JSON-RPC message is an error.

is_notification(data)

(macro)

Determines if a JSON-RPC message is a notification.

is_request(data)

(macro)

Determines if a JSON-RPC message is a request.

is_response(data)

(macro)

Determines if a JSON-RPC message is a response.

mcp_message_schema(data)

mcp_message_schema!(data)

notification_schema(data)

notification_schema!(data)

request_schema(data)

request_schema!(data)

response_schema(data)

response_schema!(data)

validate_message(message)

Validates a decoded JSON message to ensure it complies with the MCP schema.