Class: ActionMCP::OutputSchemaBuilder
- Inherits:
-
Object
- Object
- ActionMCP::OutputSchemaBuilder
- 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
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Instance Method Summary collapse
-
#additional_properties(enabled = nil) ⇒ Object
Set additionalProperties for the root schema.
-
#array(name, description: nil, min_items: nil, max_items: nil, items: nil, &block) ⇒ Object
Define an array property.
-
#boolean(name, required: false, description: nil, default: nil) ⇒ Object
Define a boolean property.
-
#initialize ⇒ OutputSchemaBuilder
constructor
A new instance of OutputSchemaBuilder.
-
#number(name, required: false, description: nil, minimum: nil, maximum: nil, default: nil) ⇒ Object
Define a number property.
-
#object(name = nil, required: false, description: nil, additional_properties: nil, &block) ⇒ Object
Define an object property.
-
#property(name, type: "string", required: false, description: nil, **options) ⇒ Object
Define a property with specified type.
-
#string(name = nil, required: false, description: nil, format: nil, enum: nil, default: nil, min_length: nil, max_length: nil) ⇒ Object
Define a string property.
-
#to_json_schema ⇒ Object
Generate the final JSON Schema.
Constructor Details
#initialize ⇒ OutputSchemaBuilder
Returns a new instance of OutputSchemaBuilder.
13 14 15 16 |
# File 'lib/action_mcp/output_schema_builder.rb', line 13 def initialize @properties = {} @required = [] end |
Instance Attribute Details
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
11 12 13 |
# File 'lib/action_mcp/output_schema_builder.rb', line 11 def properties @properties end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
11 12 13 |
# File 'lib/action_mcp/output_schema_builder.rb', line 11 def required @required end |
Instance Method Details
#additional_properties(enabled = nil) ⇒ Object
Set additionalProperties for the root schema
165 166 167 168 169 170 171 |
# File 'lib/action_mcp/output_schema_builder.rb', line 165 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
90 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 |
# File 'lib/action_mcp/output_schema_builder.rb', line 90 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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/action_mcp/output_schema_builder.rb', line 72 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
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/action_mcp/output_schema_builder.rb', line 57 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
135 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 |
# File 'lib/action_mcp/output_schema_builder.rb', line 135 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
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/action_mcp/output_schema_builder.rb', line 24 def property(name, type: "string", required: false, description: nil, **) schema = { "type" => type } schema["description"] = description if description schema.merge!() if .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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/action_mcp/output_schema_builder.rb', line 36 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_schema ⇒ Object
Generate the final JSON Schema
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/action_mcp/output_schema_builder.rb', line 174 def to_json_schema schema = { "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 |