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

create_initialize_request(client_info, protocol_version, capabilities)

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

Creates an initialize request message as defined in the MCP specification.

Parameters

  • client_info - Information about the client
  • protocol_version - Protocol version to use
  • capabilities - Client capabilities

Returns

  • map() - The initialize request message

decode_batch(json)

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

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

decode_message(json)

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

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

encode_batch(messages)

@spec encode_batch([map()]) :: String.t()

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

encode_error(id, error)

@spec encode_error(integer() | String.t(), map()) :: String.t()

Encodes a JSON-RPC error response.

Parameters

  • id - The request ID
  • error - The error details

Returns

  • String.t() - The encoded JSON-RPC error response

encode_notification(method, params)

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

Encodes a JSON-RPC notification.

Parameters

  • method - The method name to call
  • params - The parameters to pass to the method

Returns

  • String.t() - The encoded JSON-RPC notification

encode_request(id, method, params)

@spec encode_request(integer() | String.t(), String.t(), map()) :: String.t()

Encodes a JSON-RPC request.

Parameters

  • id - The request ID
  • method - The method name to call
  • params - The parameters to pass to the method

Returns

  • String.t() - The encoded JSON-RPC request

encode_response(id, result)

@spec encode_response(integer() | String.t(), map()) :: String.t()

Encodes a JSON-RPC response.

Parameters

  • id - The request ID
  • result - The result of the request

Returns

  • String.t() - The encoded JSON-RPC response

generate_id()

@spec generate_id() :: integer()

Generates a unique request ID.

Returns

  • integer() - A unique request ID

validate_message(message)

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

Validates a decoded JSON-RPC message.

Parameters

  • message - The decoded message to validate

Returns

  • {:ok, map()} - The validated message
  • {:error, reason} - Invalid message