ConduitMcp.DSL.SchemaBuilder (ConduitMCP v0.9.4)

Copy Markdown View Source

Builds JSON Schema and NimbleOptions schemas from DSL parameter definitions.

This module converts the accumulated parameter definitions from the DSL into both JSON Schema format (for MCP client validation) and NimbleOptions schemas (for runtime server-side validation).

Dual Schema Generation

The module now generates two types of schemas:

  1. JSON Schema - Used by MCP clients for input validation and introspection
  2. NimbleOptions Schema - Used for runtime server-side parameter validation

Both schemas are generated from the same DSL parameter definitions but serve different purposes in the validation pipeline.

Summary

Functions

Builds dual schemas for prompt definitions.

Builds both JSON Schema and NimbleOptions validation schema from tool definition.

Builds a JSON Schema input schema from parameter definitions.

Builds a NimbleOptions validation schema for prompt arguments.

Builds a NimbleOptions validation schema from tool definition.

Builds a prompt schema from DSL definition.

Builds a resource schema from DSL definition.

Builds a resource-template schema from a templated DSL definition.

Builds a complete tool schema from DSL definition.

Compiles validation schemas for all prompts in a server module.

Compiles validation schemas for all tools in a server module.

Generates compile-time validation schema lookup functions.

Returns true when the given resource definition's URI contains a {param} template placeholder.

Converts a static resource JSON schema (with "uri") into a resource-template schema (with "uriTemplate"). Other keys are preserved.

Validates all schemas in a tool/prompt definition list.

Validates that a NimbleOptions schema is properly formed.

Functions

build_dual_prompt_schemas(prompt_def)

Builds dual schemas for prompt definitions.

build_dual_schemas(tool_def)

Builds both JSON Schema and NimbleOptions validation schema from tool definition.

Returns a map containing both schema types for comprehensive validation.

Examples

iex> tool_def = %{name: "greet", params: [...]}
iex> ConduitMcp.DSL.SchemaBuilder.build_dual_schemas(tool_def)
%{
  json_schema: %{"name" => "greet", "inputSchema" => %{...}},
  nimble_options_schema: [name: [type: :string, required: true], ...]
}

build_input_schema(params)

Builds a JSON Schema input schema from parameter definitions.

build_nimble_options_prompt_schema(map)

Builds a NimbleOptions validation schema for prompt arguments.

build_nimble_options_schema(map)

Builds a NimbleOptions validation schema from tool definition.

Converts DSL parameter definitions into a NimbleOptions schema format that can be used for runtime parameter validation with type coercion and advanced constraints.

Examples

iex> tool_def = %{
...>   name: "calculate",
...>   params: [
...>     %{name: :a, type: :integer, opts: [required: true, min: 0]},
...>     %{name: :b, type: :integer, opts: [required: true, min: 0]}
...>   ]
...> }
iex> ConduitMcp.DSL.SchemaBuilder.build_nimble_options_schema(tool_def)
[
  a: [type: :integer, required: true, min: 0],
  b: [type: :integer, required: true, min: 0]
]

build_prompt_schema(map)

Builds a prompt schema from DSL definition.

build_resource_schema(map)

Builds a resource schema from DSL definition.

Used for resources/list (static URIs only).

build_resource_template_schema(map)

Builds a resource-template schema from a templated DSL definition.

Used for resources/templates/list (URIs with {param} placeholders). Emits uriTemplate instead of uri per MCP spec.

build_tool_schema(tool)

Builds a complete tool schema from DSL definition.

Example

iex> tool_def = %{
...>   name: "greet",
...>   description: "Greets someone",
...>   params: [
...>     %{name: :name, type: :string, description: "Name", opts: [required: true]},
...>     %{name: :age, type: :number, description: "Age", opts: []}
...>   ],
...>   handler: {:fn, fn _conn, p -> {:ok, p} end}
...> }
iex> ConduitMcp.DSL.SchemaBuilder.build_tool_schema(tool_def)
%{
  "name" => "greet",
  "description" => "Greets someone",
  "inputSchema" => %{
    "type" => "object",
    "properties" => %{
      "name" => %{"type" => "string", "description" => "Name"},
      "age" => %{"type" => "number", "description" => "Age"}
    },
    "required" => ["name"]
  }
}

compile_prompt_validation_schemas(prompts)

Compiles validation schemas for all prompts in a server module.

compile_tool_validation_schemas(tools)

Compiles validation schemas for all tools in a server module.

This function is used during the @before_compile hook to generate validation schemas for all tools defined in the DSL.

Examples

iex> tools = [
...>   %{name: "greet", params: [...]},
...>   %{name: "calc", params: [...]}
...> ]
iex> ConduitMcp.DSL.SchemaBuilder.compile_tool_validation_schemas(tools)
%{
  "greet" => [name: [type: :string, required: true], ...],
  "calc" => [a: [type: :integer, min: 0], ...]
}

generate_validation_lookup_functions(tools, prompts)

Generates compile-time validation schema lookup functions.

This creates the AST for functions that will be injected into the server module to provide fast schema lookups at runtime.

Returns quoted AST that defines:

  • __validation_schema_for_tool__/1
  • __validation_schema_for_prompt__/1

templated?(arg1)

Returns true when the given resource definition's URI contains a {param} template placeholder.

to_resource_template_schema(schema)

Converts a static resource JSON schema (with "uri") into a resource-template schema (with "uriTemplate"). Other keys are preserved.

validate_all_schemas(tools, prompts)

Validates all schemas in a tool/prompt definition list.

Used during compile time to catch schema generation errors early. Returns :ok if all schemas are valid, or {:error, details} if any are invalid.

validate_nimble_options_schema(schema)

Validates that a NimbleOptions schema is properly formed.

This is used during compile time to ensure the generated schema is valid for NimbleOptions validation.