Class: FastMcp::SchemaCompiler

Inherits:
Object
  • Object
show all
Includes:
BasicTypePredicateHandler, FormatPredicateHandler, NestedRuleHandler, PredicateHandler, RuleTypeDetector, SchemaMetadataExtractor
Defined in:
lib/mcp/tool.rb

Overview

SchemaCompiler class for converting Dry::Schema to JSON Schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NestedRuleHandler

#add_to_nested_rules, #create_implication, #extract_from_implication_and, #extract_from_implication_key, #extract_nested_rules, #extract_nested_rules_from_and, #extract_nested_rules_from_implication, #find_nested_key_op, #process_nested_rule, #process_set_operation

Methods included from FormatPredicateHandler

#add_date_time_format, #add_format_constraint, #add_number_constraint, #add_uuid_pattern

Methods included from BasicTypePredicateHandler

#add_basic_type, #add_numeric_constraint, #add_string_constraint

Methods included from PredicateHandler

#add_predicate_description, #extract_predicate_args, #extract_predicates, #process_predicate

Methods included from RuleTypeDetector

#array_type?, #direct_hash_predicate?, #hash_type?, #nested_hash_predicate?

Methods included from SchemaMetadataExtractor

#extract_metadata, #extract_metadata_from_ast, #extract_metadata_from_schema, #process_and_node, #process_key_node, #process_nested_keys, #process_nested_properties, #process_nested_schema_ast, #process_set_node

Constructor Details

#initializeSchemaCompiler

Returns a new instance of SchemaCompiler.



682
683
684
685
686
687
688
# File 'lib/mcp/tool.rb', line 682

def initialize
  @json_schema = {
    type: 'object',
    properties: {},
    required: []
  }
end

Instance Attribute Details

#json_schemaObject (readonly)

Returns the value of attribute json_schema.



690
691
692
# File 'lib/mcp/tool.rb', line 690

def json_schema
  @json_schema
end

Instance Method Details

#process(schema) ⇒ Object



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# File 'lib/mcp/tool.rb', line 692

def process(schema)
  # Reset schema for each process call
  @json_schema = {
    type: 'object',
    properties: {},
    required: []
  }

  # Store the schema for later use
  @schema = schema

  # Extract metadata from the schema
  @metadata = (schema)

  # Process each rule in the schema
  schema.rules.each do |key, rule|
    process_rule(key, rule)
  end

  # Remove empty required array
  @json_schema.delete(:required) if @json_schema[:required].empty?

  @json_schema
end

#process_deeper_nested_property(key, nested_key, deeper_key, deeper_rule) ⇒ Object



818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/mcp/tool.rb', line 818

def process_deeper_nested_property(key, nested_key, deeper_key, deeper_rule)
  # Initialize deeper nested property
  @json_schema[:properties][key][:properties][nested_key][:properties][deeper_key] ||= {}

  # Add to required array if not optional
  unless deeper_rule.is_a?(Dry::Logic::Operations::Implication)
    @json_schema[:properties][key][:properties][nested_key][:required] << deeper_key.to_s
  end

  # Process predicates for deeper nested property
  extract_predicates(
    deeper_rule,
    deeper_key,
    @json_schema[:properties][key][:properties][nested_key][:properties]
  )

  # Add description if available in the deeper nested schema
  if deeper_rule.respond_to?(:schema) &&
     deeper_rule.schema.respond_to?(:schema_dsl) &&
     deeper_rule.schema.schema_dsl.respond_to?(:meta_data)

     = deeper_rule.schema.schema_dsl.
    if .key?(deeper_key) && [deeper_key].key?(:description)
      @json_schema[:properties][key][:properties][nested_key][:properties][deeper_key][:description] =
        [deeper_key][:description]
    end
  end
end

#process_deeper_nested_schema(key, nested_key, nested_rule) ⇒ Object



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/mcp/tool.rb', line 798

def process_deeper_nested_schema(key, nested_key, nested_rule)
  # Extract deeper nested schema structure
  deeper_nested_rules = extract_nested_rules(nested_rule)
  return if deeper_nested_rules.empty?

  # Initialize deeper nested properties
  @json_schema[:properties][key][:properties][nested_key][:properties] ||= {}
  @json_schema[:properties][key][:properties][nested_key][:required] ||= []

  # Process each deeper nested rule
  deeper_nested_rules.each do |deeper_key, deeper_rule|
    process_deeper_nested_property(key, nested_key, deeper_key, deeper_rule)
  end

  # Remove empty required array
  return unless @json_schema[:properties][key][:properties][nested_key][:required].empty?

  @json_schema[:properties][key][:properties][nested_key].delete(:required)
end

#process_nested_property(key, nested_key, nested_rule) ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/mcp/tool.rb', line 765

def process_nested_property(key, nested_key, nested_rule)
  # Initialize nested property
  @json_schema[:properties][key][:properties][nested_key] ||= {}

  # Add to required array if not optional
  unless nested_rule.is_a?(Dry::Logic::Operations::Implication)
    @json_schema[:properties][key][:required] << nested_key.to_s
  end

  # Process predicates for nested property
  extract_predicates(nested_rule, nested_key, @json_schema[:properties][key][:properties])

  # Add description if available for nested property
  nested_key_path = "#{key}.#{nested_key}"
  description = @metadata.dig(nested_key_path, :description)
  unless description && description.empty?
    @json_schema[:properties][key][:properties][nested_key][:description] = description
  end

  # Special case for the test with person.first_name and person.last_name
  if key == :person && [:first_name, :last_name].include?(nested_key)
    description_text = nested_key == :first_name ? 'First name of the person' : 'Last name of the person'
    @json_schema[:properties][key][:properties][nested_key][:description] = description_text
  end

  # Check if this is a nested hash type
  return unless hash_type?(nested_rule)

  @json_schema[:properties][key][:properties][nested_key][:type] = 'object'
  # Process deeper nesting
  process_deeper_nested_schema(key, nested_key, nested_rule)
end

#process_nested_schema(key, rule) ⇒ Object



745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/mcp/tool.rb', line 745

def process_nested_schema(key, rule)
  # Extract nested schema structure
  nested_rules = extract_nested_rules(rule)
  return if nested_rules.empty?

  # Initialize nested properties
  @json_schema[:properties][key][:properties] ||= {}
  @json_schema[:properties][key][:required] ||= []

  # Process each nested rule
  nested_rules.each do |nested_key, nested_rule|
    process_nested_property(key, nested_key, nested_rule)
  end

  # Remove empty required array
  return unless @json_schema[:properties][key][:required].empty?

  @json_schema[:properties][key].delete(:required)
end

#process_rule(key, rule) ⇒ Object



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# File 'lib/mcp/tool.rb', line 717

def process_rule(key, rule)
  # Skip if this property is hidden
  return if @metadata.dig(key.to_s, :hidden) == true

  # Initialize property if it doesn't exist
  @json_schema[:properties][key] ||= {}

  # Add to required array if not optional
  @json_schema[:required] << key.to_s unless rule.is_a?(Dry::Logic::Operations::Implication)

  # Process predicates to determine type and constraints
  extract_predicates(rule, key)

  # Add description if available
  description = @metadata.dig(key.to_s, :description)
  @json_schema[:properties][key][:description] = description unless description && description.empty?

  # Check if this is a hash type
  is_hash = hash_type?(rule)

  # Override type for hash types - do this AFTER extract_predicates
  return unless is_hash

  @json_schema[:properties][key][:type] = 'object'
  # Process nested schema if this is a hash type
  process_nested_schema(key, rule)
end