MCPEx.Protocol.Errors (MCPEx v0.1.0)

Defines and handles MCP protocol errors.

This module provides utilities for creating and processing JSON-RPC error objects according to the MCP specification.

JSON-RPC Error Codes

Pre-defined JSON-RPC 2.0 error codes:

  • -32700: Parse error - Invalid JSON was received
  • -32600: Invalid Request - The JSON sent is not a valid Request object
  • -32601: Method not found - The method does not exist / is not available
  • -32602: Invalid params - Invalid method parameter(s)
  • -32603: Internal error - Internal JSON-RPC error
  • -32000 to -32099: Server error - Reserved for implementation-defined server-errors

MCP-specific error codes:

  • -32800: Request cancelled - The request was cancelled
  • -32801: Content too large - The content is too large

Summary

Functions

Creates a content too large error object.

Creates a JSON-RPC error object.

Creates a complete JSON-RPC error response.

Returns a JSON string for an error response.

Handles various error types and converts them to standardized JSON-RPC errors.

Creates an internal error object.

Creates an invalid params error object.

Creates an invalid request error object.

Creates a method not found error object.

Creates a parse error object.

Creates a request cancelled error object.

Functions

content_too_large(details)

@spec content_too_large(String.t()) :: map()

Creates a content too large error object.

Parameters

  • details - Details about the content size issues

Returns

  • map() - The error object

create_error(code, message, data \\ nil)

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

Creates a JSON-RPC error object.

Parameters

  • code - The error code
  • message - The error message
  • data - Optional additional data

Returns

  • map() - The error object

Examples

iex> MCPEx.Protocol.Errors.create_error(-32600, "Invalid Request")
%{code: -32600, message: "Invalid Request"}

iex> MCPEx.Protocol.Errors.create_error(-32602, "Missing param", %{param: "id"})
%{code: -32602, message: "Missing param", data: %{param: "id"}}

encode_error_response(id, error)

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

Creates a complete JSON-RPC error response.

Parameters

  • id - The request ID (or nil for notifications)
  • error - The error object

Returns

  • map() - The complete error response

error_response_json(id, error)

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

Returns a JSON string for an error response.

Parameters

  • id - The request ID
  • error - The error object

Returns

  • String.t() - The JSON string

handle_error(error)

@spec handle_error(term()) :: {:error, map()}

Handles various error types and converts them to standardized JSON-RPC errors.

Parameters

  • error - The error to handle

Returns

  • {:error, map()} - Standardized error map

internal_error(details \\ "Server error")

@spec internal_error(String.t()) :: map()

Creates an internal error object.

Parameters

  • details - Optional details about the internal error

Returns

  • map() - The error object

invalid_params(details)

@spec invalid_params(String.t()) :: map()

Creates an invalid params error object.

Parameters

  • details - Details about the invalid parameters

Returns

  • map() - The error object

invalid_request(message \\ "Invalid Request")

@spec invalid_request(String.t()) :: map()

Creates an invalid request error object.

Parameters

  • message - Optional custom error message

Returns

  • map() - The error object

method_not_found(method)

@spec method_not_found(String.t()) :: map()

Creates a method not found error object.

Parameters

  • method - The method that was not found

Returns

  • map() - The error object

parse_error(message \\ "Parse error")

@spec parse_error(String.t()) :: map()

Creates a parse error object.

Parameters

  • message - Optional custom error message

Returns

  • map() - The error object

Examples

iex> MCPEx.Protocol.Errors.parse_error()
%{code: -32700, message: "Parse error"}

request_cancelled(details)

@spec request_cancelled(String.t()) :: map()

Creates a request cancelled error object.

Parameters

  • details - Details about the cancellation

Returns

  • map() - The error object