Ragex.MCP.Protocol (Ragex v0.13.0)

View Source

Implements the Model Context Protocol (MCP) JSON-RPC 2.0 protocol.

Handles encoding/decoding of MCP messages and protocol-level validation.

Summary

Functions

Decodes a JSON-RPC message from a string.

Encodes a response to JSON string.

Creates an error response message.

Creates a notification message.

Validates if a message is a notification (no id).

Standard error codes and helpers.

Validates if a message is a request (has an id).

Creates a successful response message.

Types

error_response()

@type error_response() :: %{
  jsonrpc: String.t(),
  error: %{code: integer(), message: String.t(), data: any()},
  id: id()
}

id()

@type id() :: String.t() | integer() | nil

method()

@type method() :: String.t()

notification()

@type notification() :: %{jsonrpc: String.t(), method: method(), params: params()}

params()

@type params() :: map() | list() | nil

request()

@type request() :: %{
  jsonrpc: String.t(),
  method: method(),
  params: params(),
  id: id()
}

response()

@type response() :: %{jsonrpc: String.t(), result: any(), id: id()}

Functions

decode(json_string)

@spec decode(String.t()) :: {:ok, request() | notification()} | {:error, term()}

Decodes a JSON-RPC message from a string.

encode(message)

@spec encode(response() | error_response() | notification()) ::
  {:ok, String.t()} | {:error, term()}

Encodes a response to JSON string.

error_response(code, message, data \\ nil, id)

@spec error_response(integer(), String.t(), any(), id()) :: error_response()

Creates an error response message.

internal_error(message, id)

invalid_params(message, id)

invalid_request(id)

method_not_found(method, id)

notification(method, params \\ nil)

@spec notification(method(), params()) :: notification()

Creates a notification message.

notification?(message)

@spec notification?(map()) :: boolean()

Validates if a message is a notification (no id).

parse_error(id)

Standard error codes and helpers.

request?(arg1)

@spec request?(map()) :: boolean()

Validates if a message is a request (has an id).

success_response(result, id)

@spec success_response(any(), id()) :: response()

Creates a successful response message.