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

message()

@type message() :: map()

Functions

detect_message_type(map)

@spec detect_message_type(map()) :: {:ok, module()} | {:error, String.t()}

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

from_map(map)

@spec from_map(map()) :: {:ok, struct()} | {:error, String.t()}

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

to_map(request)

@spec to_map(struct()) :: map()

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"}}}