Raxol.MCP.Protocol (Raxol MCP v2.6.0)

Copy Markdown View Source

JSON-RPC 2.0 message encoding/decoding for the Model Context Protocol.

Handles both client-side (requests, notifications) and server-side (responses, error responses) message construction. All MCP communication flows through this module.

Summary

Functions

Decode a JSON string into a message map with atom keys for known fields.

Encode a message to a JSON string with newline delimiter.

Encode a message, raising on failure.

Check if a decoded message is an error response.

Build a JSON-RPC error response.

JSON-RPC internal error code (-32603).

JSON-RPC invalid params code (-32602).

JSON-RPC invalid request code (-32600).

MCP protocol version string.

JSON-RPC method not found code (-32601).

Build a JSON-RPC notification (no id, no response expected).

Check if a decoded message is a notification (no id).

JSON-RPC parse error code (-32700).

Build a JSON-RPC request.

Check if a decoded message is a request (has id + method).

Build a JSON-RPC success response.

Check if a decoded message is a response (has id + result or error).

Types

error_response()

@type error_response() :: %{
  jsonrpc: String.t(),
  id: pos_integer() | nil,
  error: %{code: integer(), message: String.t()}
}

notification()

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

request()

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

response()

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

Functions

decode(json)

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

Decode a JSON string into a message map with atom keys for known fields.

encode(message)

@spec encode(map()) :: {:ok, iodata()} | {:error, term()}

Encode a message to a JSON string with newline delimiter.

encode!(message)

@spec encode!(map()) :: iodata()

Encode a message, raising on failure.

Returns iodata (JSON + newline).

error?(arg1)

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

Check if a decoded message is an error response.

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

@spec error_response(pos_integer() | nil, integer(), String.t(), term()) ::
  error_response()

Build a JSON-RPC error response.

internal_error()

@spec internal_error() :: integer()

JSON-RPC internal error code (-32603).

invalid_params()

@spec invalid_params() :: integer()

JSON-RPC invalid params code (-32602).

invalid_request()

@spec invalid_request() :: integer()

JSON-RPC invalid request code (-32600).

mcp_protocol_version()

@spec mcp_protocol_version() :: String.t()

MCP protocol version string.

method_not_found()

@spec method_not_found() :: integer()

JSON-RPC method not found code (-32601).

notification(method, params \\ %{})

@spec notification(String.t(), map()) :: notification()

Build a JSON-RPC notification (no id, no response expected).

notification?(msg)

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

Check if a decoded message is a notification (no id).

parse_error()

@spec parse_error() :: integer()

JSON-RPC parse error code (-32700).

request(id, method, params \\ %{})

@spec request(pos_integer(), String.t(), map()) :: request()

Build a JSON-RPC request.

request?(arg1)

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

Check if a decoded message is a request (has id + method).

response(id, result)

@spec response(pos_integer(), term()) :: response()

Build a JSON-RPC success response.

response?(arg1)

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

Check if a decoded message is a response (has id + result or error).