ConduitMcp.Validation (ConduitMCP v0.9.3)

Copy Markdown View Source

Runtime parameter validation using NimbleOptions.

This module provides runtime parameter validation for MCP tools, prompts, and resources using NimbleOptions schemas. It works alongside the existing JSON Schema generation to provide both client-side validation (JSON Schema) and server-side validation (NimbleOptions).

Features

  • Runtime parameter validation with detailed error messages
  • Type coercion (string "123" -> integer 123)
  • Advanced constraints (min/max, length limits)
  • Custom validator functions
  • Compile-time schema generation for performance

Configuration

Validation behavior can be configured in your application config:

config :conduit_mcp, :validation,
  runtime_validation: true,           # Enable/disable validation
  strict_mode: true,                  # Fail on errors vs. log and continue
  type_coercion: true,                # Automatic type conversion
  log_validation_errors: false        # Log validation failures

Summary

Functions

Formats validation errors into a standardized format for JSON-RPC responses.

Updates the validation configuration at runtime.

Validates prompt arguments using the compiled NimbleOptions schema.

Validates tool parameters using the compiled NimbleOptions schema.

Functions

format_validation_errors(errors)

Formats validation errors into a standardized format for JSON-RPC responses.

Takes NimbleOptions validation errors and converts them to a format suitable for MCP error responses.

Examples

iex> errors = [%{parameter: "age", value: -5, message: "must be >= 0"}]
iex> ConduitMcp.Validation.format_validation_errors(errors)
[%{"parameter" => "age", "value" => -5, "message" => "must be >= 0"}]

update_validation_config(config)

Updates the validation configuration at runtime.

Writes to both Application env and persistent_term so that the change is visible immediately to all concurrent readers.

validate_prompt_args(server_module, prompt_name, args)

Validates prompt arguments using the compiled NimbleOptions schema.

Similar to validate_tool_params/3 but for prompt arguments.

validate_tool_params(server_module, tool_name, params)

Validates tool parameters using the compiled NimbleOptions schema.

Returns {:ok, validated_params} with potentially coerced types, or {:error, validation_errors} with detailed error information.

Examples

iex> ConduitMcp.Validation.validate_tool_params(MyServer, "greet", %{"name" => "Alice"})
{:ok, %{"name" => "Alice"}}

iex> ConduitMcp.Validation.validate_tool_params(MyServer, "calc", %{"age" => "-5"})
{:error, [%{parameter: "age", value: -5, message: "must be greater than or equal to 0"}]}