Mcp42.Protocol (mcp42 v0.1.1)

Copy Markdown

Pure JSON-RPC 2.0 encoding/decoding for MCP messages.

All functions are pure; the only side effect is Jason serialization.

Summary

Types

A JSON-RPC error object: %{code: integer, message: String.t(), data: term | nil}

Functions

Decodes raw JSON into a single MCP message.

Encodes a JSON-RPC error response. id may be nil for unparseable requests.

Encodes a JSON-RPC request. params of nil is omitted.

Encodes a JSON-RPC success response.

True when the message carries no id (a JSON-RPC notification).

Types

rpc_error()

@type rpc_error() :: %{code: integer(), message: String.t(), data: term() | nil}

A JSON-RPC error object: %{code: integer, message: String.t(), data: term | nil}

Functions

decode(raw)

@spec decode(binary()) :: {:ok, map()} | {:error, map()}

Decodes raw JSON into a single MCP message.

Note: JSON-RPC batching is NOT supported — MCP Streamable HTTP (2025-06-18) removed batching; arrays are rejected as invalid request.

Returns:

  • {:ok, map} — one request/notification
  • {:error, error_response_map} — a ready-to-send JSON-RPC error (parse error or invalid request)

encode_error!(id, code, message, data \\ nil)

@spec encode_error!(term(), integer(), String.t(), term() | nil) :: String.t()

Encodes a JSON-RPC error response. id may be nil for unparseable requests.

encode_request!(id, method, params \\ nil)

@spec encode_request!(term(), String.t(), map() | nil) :: String.t()

Encodes a JSON-RPC request. params of nil is omitted.

encode_response!(id, result)

@spec encode_response!(term(), term()) :: String.t()

Encodes a JSON-RPC success response.

internal_error(data \\ nil)

invalid_params(data \\ nil)

invalid_request(data \\ nil)

method_not_found(method, data \\ nil)

notification?(arg1)

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

True when the message carries no id (a JSON-RPC notification).

parse_error(data \\ nil)