McpServer.Schema (HTTP MCP Server v0.6.0)

View Source

Represents a JSON Schema object for validating tool parameters.

This module provides a structured way to define JSON schemas that are used for input and output validation in MCP tools, following the MCP specification which implements recursive schema definitions.

Schemas can be:

  • StringSchema: Simple string values with optional enum and description
  • NumberSchema: Numeric values (number or integer) with optional description
  • BooleanSchema: Boolean values with optional description
  • ArraySchema: Arrays with items that can be any schema type
  • ObjectSchema: Objects with typed properties, optional required fields, and description

All schema types support optional description fields.

Fields (shared across schema types)

  • type - The JSON type: "object", "string", "number", "integer", "boolean", "array"
  • description - Human-readable description (optional)
  • properties - Map of property schemas for object types (ObjectSchema only)
  • required - List of required property names (ObjectSchema only)
  • items - Schema for array items (ArraySchema only)
  • enum - List of allowed values (StringSchema only)
  • default - Default value if not provided (any type)

Examples

# StringSchema
iex> string_schema = McpServer.Schema.new(
...>   type: "string",
...>   description: "A user name",
...>   enum: ["alice", "bob", "charlie"]
...> )
%McpServer.Schema{type: "string", description: "A user name", enum: ["alice", "bob", "charlie"]}

# NumberSchema
iex> number_schema = McpServer.Schema.new(
...>   type: "number",
...>   description: "A decimal value"
...> )
%McpServer.Schema{type: "number", description: "A decimal value"}

# BooleanSchema
iex> bool_schema = McpServer.Schema.new(
...>   type: "boolean",
...>   description: "A flag",
...>   default: true
...> )
%McpServer.Schema{type: "boolean", description: "A flag", default: true}

# ArraySchema
iex> array_schema = McpServer.Schema.new(
...>   type: "array",
...>   items: McpServer.Schema.new(type: "string")
...> )
%McpServer.Schema{
  type: "array",
  items: %McpServer.Schema{type: "string"}
}

# ObjectSchema (recursive)
iex> object_schema = McpServer.Schema.new(
...>   type: "object",
...>   properties: %{
...>     "name" => McpServer.Schema.new(type: "string", description: "User name"),
...>     "age" => McpServer.Schema.new(type: "integer", description: "User age")
...>   },
...>   required: ["name"]
...> )
%McpServer.Schema{
  type: "object",
  properties: %{
    "name" => %McpServer.Schema{type: "string", description: "User name"},
    "age" => %McpServer.Schema{type: "integer", description: "User age"}
  },
  required: ["name"]
}

Summary

Functions

Creates a new Schema struct.

Types

schema_type()

@type schema_type() :: String.t()

t()

@type t() :: %McpServer.Schema{
  default: any() | nil,
  description: String.t() | nil,
  enum: list() | nil,
  items: t() | nil,
  properties: map() | nil,
  required: [String.t()] | nil,
  type: schema_type()
}

Functions

new(opts)

@spec new(keyword()) :: t()

Creates a new Schema struct.

Supports recursive schema definitions where properties and items can be nested schemas.

Parameters

  • opts - Keyword list of schema options:
    • :type (required) - The JSON type ("object", "string", "number", "integer", "boolean", "array")
    • :properties - Map of property schemas (for object types, maps to McpServer.Schema.t())
    • :required - List of required property names (for object types)
    • :description - Human-readable description
    • :items - Schema for array items (for array types, must be McpServer.Schema.t())
    • :enum - List of allowed values (for string types)
    • :default - Default value

Examples

iex> McpServer.Schema.new(type: "string", description: "A name")
%McpServer.Schema{type: "string", description: "A name"}

iex> McpServer.Schema.new(
...>   type: "array",
...>   items: McpServer.Schema.new(type: "string")
...> )
%McpServer.Schema{
  type: "array",
  items: %McpServer.Schema{type: "string"}
}

iex> McpServer.Schema.new(
...>   type: "object",
...>   properties: %{
...>     "name" => McpServer.Schema.new(type: "string", description: "User name")
...>   },
...>   required: ["name"]
...> )
%McpServer.Schema{
  type: "object",
  properties: %{
    "name" => %McpServer.Schema{type: "string", description: "User name"}
  },
  required: ["name"]
}