Class: ActionMCP::OutputSchemaBuilder

Inherits:
Object
  • Object
show all
Includes:
SchemaHelpers
Defined in:
lib/action_mcp/output_schema_builder.rb

Overview

DSL builder for creating output JSON Schema from Ruby-like syntax Unlike SchemaBuilder, this preserves nested structure for validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOutputSchemaBuilder

Returns a new instance of OutputSchemaBuilder.



14
15
16
17
# File 'lib/action_mcp/output_schema_builder.rb', line 14

def initialize
  @properties = {}
  @required = []
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



12
13
14
# File 'lib/action_mcp/output_schema_builder.rb', line 12

def properties
  @properties
end

#requiredObject (readonly)

Returns the value of attribute required.



12
13
14
# File 'lib/action_mcp/output_schema_builder.rb', line 12

def required
  @required
end

Instance Method Details

#additional_properties(enabled = nil) ⇒ Object

Set additionalProperties for the root schema

Parameters:

  • enabled (Boolean, Hash) (defaults to: nil)

    true to allow any additional properties, false to disallow them, or a Hash for typed additional properties



166
167
168
169
170
171
172
# File 'lib/action_mcp/output_schema_builder.rb', line 166

def additional_properties(enabled = nil)
  if enabled.nil?
    @additional_properties
  else
    @additional_properties = enabled
  end
end

#array(name, description: nil, min_items: nil, max_items: nil, items: nil, &block) ⇒ Object

Define an array property

Parameters:

  • name (Symbol)

    Array property name

  • description (String) (defaults to: nil)

    Property description

  • min_items (Integer) (defaults to: nil)

    Minimum number of items

  • max_items (Integer) (defaults to: nil)

    Maximum number of items

  • items (Hash) (defaults to: nil)

    Items schema (if not using block)

  • block (Proc)

    Block defining item schema



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/action_mcp/output_schema_builder.rb', line 91

def array(name, description: nil, min_items: nil, max_items: nil, items: nil, &block)
  schema = { "type" => "array" }
  schema["description"] = description if description
  schema["minItems"] = min_items if min_items
  schema["maxItems"] = max_items if max_items

  if block_given?
    # Create nested builder for items
    item_builder = OutputSchemaBuilder.new
    result = item_builder.instance_eval(&block)

    # If the block returned a schema directly (e.g., from string()),
    # use that. Otherwise, build an object schema from properties.
    if result.is_a?(Hash) && result["type"]
      schema["items"] = result
    elsif item_builder.properties.empty?
      # Block didn't define properties, assume string items
      schema["items"] = { "type" => "string" }
    else
      # Block defined object properties
      item_schema = {
        "type" => "object",
        "properties" => item_builder.properties
      }
      item_schema["required"] = item_builder.required if item_builder.required.any?
      schema["items"] = item_schema
    end
  elsif items
    schema["items"] = items
  else
    # Default to string items
    schema["items"] = { "type" => "string" }
  end

  @properties[name.to_s] = schema

  name.to_s
end

#boolean(name, required: false, description: nil, default: nil) ⇒ Object

Define a boolean property



73
74
75
76
77
78
79
80
81
82
# File 'lib/action_mcp/output_schema_builder.rb', line 73

def boolean(name, required: false, description: nil, default: nil)
  schema = { "type" => "boolean" }
  schema["description"] = description if description
  schema["default"] = default unless default.nil?

  @properties[name.to_s] = schema
  @required << name.to_s if required

  name.to_s
end

#number(name, required: false, description: nil, minimum: nil, maximum: nil, default: nil) ⇒ Object

Define a number property



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/action_mcp/output_schema_builder.rb', line 58

def number(name, required: false, description: nil, minimum: nil,
           maximum: nil, default: nil)
  schema = { "type" => "number" }
  schema["description"] = description if description
  schema["minimum"] = minimum if minimum
  schema["maximum"] = maximum if maximum
  schema["default"] = default if default

  @properties[name.to_s] = schema
  @required << name.to_s if required

  name.to_s
end

#object(name = nil, required: false, description: nil, additional_properties: nil, &block) ⇒ Object

Define an object property

Parameters:

  • name (Symbol, nil) (defaults to: nil)

    Object property name. If nil, returns schema directly (for array items)

  • required (Boolean) (defaults to: false)

    Whether the object is required

  • description (String) (defaults to: nil)

    Property description

  • additional_properties (Boolean, Hash) (defaults to: nil)

    Whether to allow additional properties

  • block (Proc)

    Block defining object properties

Raises:

  • (ArgumentError)


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/action_mcp/output_schema_builder.rb', line 136

def object(name = nil, required: false, description: nil, additional_properties: nil, &block)
  raise ArgumentError, "Object definition requires a block" unless block_given?

  # Create nested builder for object properties
  object_builder = OutputSchemaBuilder.new
  object_builder.instance_eval(&block)

  schema = {
    "type" => "object",
    "properties" => object_builder.properties
  }
  schema["description"] = description if description
  schema["required"] = object_builder.required if object_builder.required.any?

  # Add additionalProperties if specified
  add_additional_properties_to_schema(schema, additional_properties)

  if name
    @properties[name.to_s] = schema
    @required << name.to_s if required
    name.to_s
  else
    # Return schema directly for use in array items
    schema
  end
end

#property(name, type: "string", required: false, description: nil, **options) ⇒ Object

Define a property with specified type

Parameters:

  • name (Symbol)

    Property name

  • type (String) (defaults to: "string")

    JSON Schema type

  • required (Boolean) (defaults to: false)

    Whether the property is required

  • description (String) (defaults to: nil)

    Property description

  • options (Hash)

    Additional JSON Schema options



25
26
27
28
29
30
31
32
33
34
# File 'lib/action_mcp/output_schema_builder.rb', line 25

def property(name, type: "string", required: false, description: nil, **options)
  schema = { "type" => type }
  schema["description"] = description if description
  schema.merge!(options) if options.any?

  @properties[name.to_s] = schema
  @required << name.to_s if required

  name.to_s
end

#string(name = nil, required: false, description: nil, format: nil, enum: nil, default: nil, min_length: nil, max_length: nil) ⇒ Object

Define a string property



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/action_mcp/output_schema_builder.rb', line 37

def string(name = nil, required: false, description: nil, format: nil, enum: nil,
           default: nil, min_length: nil, max_length: nil)
  schema = { "type" => "string" }
  schema["description"] = description if description
  schema["format"] = format if format
  schema["enum"] = enum if enum
  schema["default"] = default if default
  schema["minLength"] = min_length if min_length
  schema["maxLength"] = max_length if max_length

  if name
    @properties[name.to_s] = schema
    @required << name.to_s if required
    name.to_s
  else
    # Return schema for use in array items or other contexts
    schema
  end
end

#to_json_schemaObject

Generate the final JSON Schema



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/action_mcp/output_schema_builder.rb', line 175

def to_json_schema
  schema = {
    "$schema" => SchemaValidator::DEFAULT_DIALECT,
    "type" => "object",
    "properties" => @properties
  }

  schema["required"] = @required.uniq if @required.any?

  # Add additionalProperties if configured
  add_additional_properties_to_schema(schema, @additional_properties)

  schema
end