MCPEx.Protocol.Schemas (MCPEx v0.1.0)

JSON Schema validation for MCP protocol messages.

This module provides validation of incoming and outgoing messages against the MCP protocol specification schemas.

Summary

Functions

Detects the message type based on its structure.

Gets the resolved JSON schema for MCP protocol validation.

Validates a message against a specific schema definition.

Validates an initialize request message.

Validates an initialize response message.

Validates a message by detecting its type and then validating against the appropriate schema.

Functions

detect_type(message)

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

Detects the message type based on its structure.

Parameters

  • message - The message to analyze

Returns

  • {:ok, type} - Successfully detected the message type
  • {:error, reason} - Unable to determine the message type

get_resolved_schema()

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

Gets the resolved JSON schema for MCP protocol validation.

Returns

  • {:ok, map()} - The resolved schema
  • {:error, reason} - Failed to get the schema

validate(message, schema_type)

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

Validates a message against a specific schema definition.

Parameters

  • message - The message to validate
  • schema_type - The name of the schema type (e.g., "InitializeRequest")

Returns

  • :ok - The message is valid
  • {:error, reason} - The message is invalid

validate_initialize_request(message)

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

Validates an initialize request message.

Parameters

  • message - The initialize request message to validate

Returns

  • :ok - The message is valid
  • {:error, reason} - The message is invalid

validate_initialize_result(message)

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

Validates an initialize response message.

Parameters

  • message - The initialize response message to validate

Returns

  • :ok - The message is valid
  • {:error, reason} - The message is invalid

validate_message(message)

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

Validates a message by detecting its type and then validating against the appropriate schema.

Parameters

  • message - The message to validate

Returns

  • :ok - The message is valid
  • {:error, reason} - The message is invalid