MCPKit.Protocol (mcp_kit v0.2.4)

Copy Markdown View Source

JSON-RPC helpers used by the MCP transport.

This module is intentionally small and focused on message shape detection, decoding, and standard protocol error payloads.

Examples

iex> MCPKit.Protocol.request?(%{"jsonrpc" => "2.0", "method" => "ping", "id" => 1})
true

iex> MCPKit.Protocol.success(1, %{"ok" => true})
%{"jsonrpc" => "2.0", "id" => 1, "result" => %{"ok" => true}}

Summary

Functions

Decodes a raw JSON-RPC request body.

Encodes a JSON-RPC payload to JSON.

Builds a JSON-RPC forbidden payload.

Builds a JSON-RPC internal error payload.

Builds a JSON-RPC invalid params payload.

Builds a JSON-RPC invalid request payload.

Builds a JSON-RPC method not found payload.

Returns true when the payload is a JSON-RPC notification.

Builds a JSON-RPC parse error payload.

Returns true when the payload is a JSON-RPC request.

Returns true when the payload is a JSON-RPC response.

Builds a successful JSON-RPC response payload.

Functions

decode(raw_body)

Decodes a raw JSON-RPC request body.

Examples

iex> MCPKit.Protocol.decode(~s({"jsonrpc":"2.0","method":"ping","id":1}))
{:ok, %{"id" => 1, "jsonrpc" => "2.0", "method" => "ping"}}

iex> MCPKit.Protocol.decode("{")
{:error, :parse_error}

encode!(payload)

Encodes a JSON-RPC payload to JSON.

forbidden(id, message \\ "Forbidden")

Builds a JSON-RPC forbidden payload.

internal_error(id, message \\ "Internal error")

Builds a JSON-RPC internal error payload.

invalid_params(id, message \\ "Invalid params")

Builds a JSON-RPC invalid params payload.

invalid_request(id \\ nil, message \\ "Invalid request")

Builds a JSON-RPC invalid request payload.

method_not_found(id, message \\ "Method not found")

Builds a JSON-RPC method not found payload.

notification?(message)

Returns true when the payload is a JSON-RPC notification.

parse_error(id \\ nil, message \\ "Invalid JSON")

Builds a JSON-RPC parse error payload.

request?(arg1)

Returns true when the payload is a JSON-RPC request.

response?(message)

Returns true when the payload is a JSON-RPC response.

success(id, result)

Builds a successful JSON-RPC response payload.