MCPEx.Protocol.Types (MCPEx v0.1.0)
Type definitions for MCP protocol messages.
This module provides Elixir structs representing the various message types in the MCP protocol, along with conversion functions between structs and JSON-RPC maps.
Summary
Functions
Detects the message type based on JSON-RPC message structure.
Converts a JSON-RPC map to the appropriate protocol struct.
Converts a protocol struct to a JSON-RPC map representation.
Types
@type message() :: map()
Functions
Detects the message type based on JSON-RPC message structure.
Parameters
map
- The JSON-RPC message to analyze
Returns
{:ok, module()}
- The detected struct type{:error, String.t()}
- Error message if the type is unknown
Converts a JSON-RPC map to the appropriate protocol struct.
Parameters
map
- The JSON-RPC map to convert
Returns
{:ok, struct()}
- The converted protocol struct{:error, String.t()}
- Error message if the map doesn't match a known type
Examples
iex> map = %{jsonrpc: "2.0", id: 1, method: "initialize", params: %{}}
iex> {:ok, request} = MCPEx.Protocol.Types.from_map(map)
iex> request.__struct__
MCPEx.Protocol.Types.InitializeRequest
Converts a protocol struct to a JSON-RPC map representation.
Parameters
struct
- The protocol struct to convert
Returns
map()
- The JSON-RPC map
Examples
iex> request = %InitializeRequest{id: 1, client_info: %{name: "client"}}
iex> MCPEx.Protocol.Types.to_map(request)
%{jsonrpc: "2.0", id: 1, method: "initialize", params: %{clientInfo: %{name: "client"}}}