McpServer.JsonRpc (HTTP MCP Server v0.6.0)

View Source

The JSONRpc module handles JSON-RPC communication for the McpServer.

This module provides functionality to process JSON-RPC requests and responses according to the JSON-RPC 2.0 specification. It handles request parsing, validation, method dispatch, and response formatting.

Summary

Functions

Decodes a map (from Jason) into a Request struct.

Decodes a map (from Jason) into a Response struct.

Encodes a Request struct into a map that can be JSON encoded.

Encodes a Response struct into a map that can be JSON encoded.

Creates a new JSON-RPC error response object.

Creates a new JSON-RPC request object.

Creates a new JSON-RPC response object with a result.

Functions

decode_request(map)

@spec decode_request(map()) ::
  {:ok, McpServer.JsonRpc.Request.t()} | {:error, String.t()}

Decodes a map (from Jason) into a Request struct.

Parameters

  • map: A map representing a JSON-RPC request

Returns

  • {:ok, Request.t()} on success
  • {:error, String.t()} on failure

Examples

iex> VibersServerMCP.JsonRpc.decode_request(%{
...>   "jsonrpc" => "2.0",
...>   "method" => "get_user",
...>   "params" => %{"user_id" => 123},
...>   "id" => "req-1"
...> })
{:ok, %VibersServerMCP.JsonRpc.Request{
  jsonrpc: "2.0",
  method: "get_user",
  params: %{"user_id" => 123},
  id: "req-1"
}}

decode_response(map)

@spec decode_response(map()) ::
  {:ok, McpServer.JsonRpc.Response.t()} | {:error, String.t()}

Decodes a map (from Jason) into a Response struct.

Parameters

  • map: A map representing a JSON-RPC response

Returns

  • {:ok, Response.t()} on success
  • {:error, String.t()} on failure

Examples

iex> VibersServerMCP.JsonRpc.decode_response(%{
...>   "jsonrpc" => "2.0",
...>   "result" => %{"name" => "John"},
...>   "id" => "req-1"
...> })
{:ok, %VibersServerMCP.JsonRpc.Response{
  jsonrpc: "2.0",
  result: %{"name" => "John"},
  error: nil,
  id: "req-1"
}}

encode_request(request)

@spec encode_request(McpServer.JsonRpc.Request.t()) :: map()

Encodes a Request struct into a map that can be JSON encoded.

Parameters

  • request: A Request struct

Returns

  • A map representation of the request

Examples

iex> request = %VibersServerMCP.JsonRpc.Request{
...>   jsonrpc: "2.0",
...>   method: "get_user",
...>   params: %{user_id: 123},
...>   id: "req-1"
...> }
iex> VibersServerMCP.JsonRpc.encode_request(request)
%{
  "jsonrpc" => "2.0",
  "method" => "get_user",
  "params" => %{user_id: 123},
  "id" => "req-1"
}

encode_response(response)

@spec encode_response(McpServer.JsonRpc.Response.t()) :: map()

Encodes a Response struct into a map that can be JSON encoded.

Parameters

  • response: A Response struct

Returns

  • A map representation of the response

Examples

iex> response = %VibersServerMCP.JsonRpc.Response{
...>   jsonrpc: "2.0",
...>   result: %{name: "John"},
...>   error: nil,
...>   id: "req-1"
...> }
iex> VibersServerMCP.JsonRpc.encode_response(response)
%{
  "jsonrpc" => "2.0",
  "result" => %{name: "John"},
  "id" => "req-1"
}

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

@spec new_error_response(integer(), String.t(), any(), String.t() | integer() | nil) ::
  McpServer.JsonRpc.Response.t()

Creates a new JSON-RPC error response object.

Parameters

  • code: Error code
  • message: Error message
  • data: Additional error data (optional)
  • id: Request identifier

Examples

iex> VibersServerMCP.JsonRpc.new_error_response(-32601, "Method not found", nil, "req-1")
%VibersServerMCP.JsonRpc.Response{
  jsonrpc: "2.0",
  result: nil,
  error: %VibersServerMCP.JsonRpc.Error{code: -32601, message: "Method not found", data: nil},
  id: "req-1"
}

new_request(method, params \\ nil, id \\ nil)

@spec new_request(String.t(), map() | list() | nil, String.t() | integer() | nil) ::
  McpServer.JsonRpc.Request.t()

Creates a new JSON-RPC request object.

Parameters

  • method: The method name to call
  • params: Parameters for the method (optional)
  • id: Request identifier (optional, for notifications use nil)

Examples

iex> VibersServerMCP.JsonRpc.new_request("get_user", %{user_id: 123}, "req-1")
%VibersServerMCP.JsonRpc.Request{
  jsonrpc: "2.0",
  method: "get_user",
  params: %{user_id: 123},
  id: "req-1"
}

new_response(result, id)

@spec new_response(any(), String.t() | integer() | nil) ::
  McpServer.JsonRpc.Response.t()

Creates a new JSON-RPC response object with a result.

Parameters

  • result: The result of the method call
  • id: Request identifier

Examples

iex> VibersServerMCP.JsonRpc.new_response(%{name: "John"}, "req-1")
%VibersServerMCP.JsonRpc.Response{
  jsonrpc: "2.0",
  result: %{name: "John"},
  error: nil,
  id: "req-1"
}