MCPEx.Protocol.JsonRpc (MCPEx v0.1.0)
JSON-RPC 2.0 protocol implementation for MCP client.
This module handles encoding and decoding of JSON-RPC 2.0 messages according to the Model Context Protocol specification. It supports both individual messages and batches of messages as defined in the JSON-RPC 2.0 specification.
Summary
Functions
Creates an initialize request message as defined in the MCP specification.
Decodes a JSON-RPC batch message.
Decodes a JSON-RPC message.
Encodes a batch of JSON-RPC messages.
Encodes a JSON-RPC error response.
Encodes a JSON-RPC notification.
Encodes a JSON-RPC request.
Encodes a JSON-RPC response.
Generates a unique request ID.
Validates a decoded JSON-RPC message.
Functions
Creates an initialize request message as defined in the MCP specification.
Parameters
client_info
- Information about the clientprotocol_version
- Protocol version to usecapabilities
- Client capabilities
Returns
map()
- The initialize request message
Decodes a JSON-RPC batch message.
Parameters
json
- The JSON-RPC batch to decode
Returns
{:ok, [map()]}
- The decoded messages{:error, reason}
- Error decoding the messages
Decodes a JSON-RPC message.
Parameters
json
- The JSON-RPC message to decode
Returns
{:ok, map()}
- The decoded message{:error, reason}
- Error decoding the message
Encodes a batch of JSON-RPC messages.
Parameters
messages
- A list of JSON-RPC messages (requests, notifications, responses)
Returns
String.t()
- The encoded JSON-RPC batch
Encodes a JSON-RPC error response.
Parameters
id
- The request IDerror
- The error details
Returns
String.t()
- The encoded JSON-RPC error response
Encodes a JSON-RPC notification.
Parameters
method
- The method name to callparams
- The parameters to pass to the method
Returns
String.t()
- The encoded JSON-RPC notification
Encodes a JSON-RPC request.
Parameters
id
- The request IDmethod
- The method name to callparams
- The parameters to pass to the method
Returns
String.t()
- The encoded JSON-RPC request
Encodes a JSON-RPC response.
Parameters
id
- The request IDresult
- The result of the request
Returns
String.t()
- The encoded JSON-RPC response
@spec generate_id() :: integer()
Generates a unique request ID.
Returns
integer()
- A unique request ID
Validates a decoded JSON-RPC message.
Parameters
message
- The decoded message to validate
Returns
{:ok, map()}
- The validated message{:error, reason}
- Invalid message