Module: MCPClient::SchemaValidator::Dialects

Included in:
MCPClient::SchemaValidator
Defined in:
lib/mcp_client/schema_validator/dialects.rb

Overview

Dialect selection: the $schema a document (or an embedded resource root) declares, its canonical supported form, and the problems an unusable declaration reports. Extended into SchemaValidator, so the methods are its own.

Instance Method Summary collapse

Instance Method Details

#canonical_dialect(uri) ⇒ String?

The supported dialect a declared URI stands for (scheme-insensitive), or nil.

Parameters:

  • uri (String)

Returns:

  • (String, nil)


28
29
30
31
# File 'lib/mcp_client/schema_validator/dialects.rb', line 28

def canonical_dialect(uri)
  canonical = uri.sub(/#\z/, '').sub(%r{\Ahttps?://}, '')
  SUPPORTED_DIALECTS.find { |d| d.sub(%r{\Ahttps?://}, '') == canonical }
end

#dialect(schema) ⇒ String

The dialect a schema declares, or the default.

Parameters:

  • schema (Object) —

    the schema

Returns:

  • (String) —

    the $schema value (without a trailing #), or DEFAULT_DIALECT



13
14
15
16
17
18
19
20
21
22
# File 'lib/mcp_client/schema_validator/dialects.rb', line 13

def dialect(schema)
  return DEFAULT_DIALECT unless schema.is_a?(Hash)
  return DEFAULT_DIALECT unless schema.key?('$schema') || schema.key?(:$schema)

  declared = schema.key?('$schema') ? schema['$schema'] : schema[:$schema]
  # A declaration that is present but unusable is not the default.
  return nil unless declared.is_a?(String) && !declared.empty?

  declared.sub(/#\z/, '')
end

#embedded_dialect(schema, inherited) ⇒ String?

The dialect in force at a schema object: an embedded resource root (a subschema whose $id is a URI) may declare its own $schema, which is that resource's dialect (JSON Schema 2020-12 Core Section 8.1.1); anywhere else the inherited dialect applies (a $schema that is not at a resource root is ignored).

Parameters:

  • schema (Hash)
  • inherited (String, nil) —

    the enclosing resource's dialect

Returns:

  • (String, nil) —

    the canonical dialect, or nil when the embedded declaration is malformed or unsupported



48
49
50
51
52
53
# File 'lib/mcp_client/schema_validator/dialects.rb', line 48

def embedded_dialect(schema, inherited)
  return inherited unless resource_root?(schema, inherited) && schema.key?('$schema')

  declared = dialect(schema)
  declared && canonical_dialect(declared)
end

#embedded_dialect_problem(schema) ⇒ String?

Returns why an embedded resource's $schema is unusable.

Returns:

  • (String, nil) —

    why an embedded resource's $schema is unusable



56
57
58
59
60
61
62
63
# File 'lib/mcp_client/schema_validator/dialects.rb', line 56

def embedded_dialect_problem(schema)
  declared = dialect(schema)
  return 'embedded resource $schema must be a non-empty string naming the dialect' if declared.nil?
  return nil if supported_dialect?(declared)

  "embedded resource dialect #{clip(declared.inspect)} is not supported " \
    "(supported: #{SUPPORTED_DIALECTS.join(', ')})"
end

#supported_dialect?(uri) ⇒ Boolean

Parameters:

  • uri (String)

Returns:

  • (Boolean)


35
36
37
# File 'lib/mcp_client/schema_validator/dialects.rb', line 35

def supported_dialect?(uri)
  !canonical_dialect(uri).nil?
end