Class: LanguageOperator::Dsl::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/dsl/adapter.rb

Overview

Adapter to bridge our DSL to the official MCP Ruby SDK

Converts LanguageOperator::Dsl::ToolDefinition objects into MCP::Tool classes that can be used with the official MCP Ruby SDK.

Examples:

Convert a tool definition

tool_class = Adapter.tool_definition_to_mcp_tool(tool_def)
server = MCP::Server.new(tools: [tool_class])

Class Method Summary collapse

Class Method Details

.build_input_schema(parameters) ⇒ Object

Build MCP input schema from our parameter definitions



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/language_operator/dsl/adapter.rb', line 59

def self.build_input_schema(parameters)
  properties = {}
  required = []

  parameters.each do |name, param_def|
    properties[name.to_sym] = build_parameter_schema(param_def)
    required << name if param_def.required
  end

  schema = { properties: properties }
  schema[:required] = required unless required.empty?
  schema
end

.build_parameter_schema(param_def) ⇒ Object

Build schema for a single parameter



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/language_operator/dsl/adapter.rb', line 74

def self.build_parameter_schema(param_def)
  # Access instance variables directly since the DSL methods are setters
  param_type = param_def.instance_variable_get(:@type)
  param_desc = param_def.instance_variable_get(:@description)
  param_enum = param_def.instance_variable_get(:@enum)
  param_default = param_def.instance_variable_get(:@default)

  schema = {
    type: map_type(param_type),
    description: param_desc || '' # MCP requires description to be a string
  }

  schema[:enum] = param_enum if param_enum
  schema[:default] = param_default if param_default

  schema
end

.create_mcp_server(registry, server_name: 'langop-mcp', server_context: {}) ⇒ Object

Create an MCP::Server from our registry



112
113
114
115
116
117
118
119
120
121
# File 'lib/language_operator/dsl/adapter.rb', line 112

def self.create_mcp_server(registry, server_name: 'langop-mcp', server_context: {})
  tools = registry_to_mcp_tools(registry)

  MCP::Server.new(
    name: server_name,
    version: LanguageOperator::VERSION,
    tools: tools,
    server_context: server_context
  )
end

.map_type(ruby_type) ⇒ Object

Map our type symbols to JSON schema types



93
94
95
96
97
98
99
100
101
102
# File 'lib/language_operator/dsl/adapter.rb', line 93

def self.map_type(ruby_type)
  case ruby_type
  when :string then 'string'
  when :number, :integer then 'number'
  when :boolean then 'boolean'
  when :array then 'array'
  when :object then 'object'
  else 'string'
  end
end

.registry_to_mcp_tools(registry) ⇒ Object

Convert a registry of tools to MCP::Tool classes



105
106
107
108
109
# File 'lib/language_operator/dsl/adapter.rb', line 105

def self.registry_to_mcp_tools(registry)
  registry.all.map do |tool_def|
    tool_definition_to_mcp_tool(tool_def)
  end
end

.tool_definition_to_mcp_tool(tool_def) ⇒ Object

Convert our ToolDefinition to an MCP::Tool class



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/language_operator/dsl/adapter.rb', line 17

def self.tool_definition_to_mcp_tool(tool_def)
  # Build input schema from our parameter definitions
  schema = build_input_schema(tool_def.parameters)

  # Create a dynamic class that extends MCP::Tool
  Class.new(MCP::Tool) do
    # Set the tool name using the MCP SDK's method
    tool_name tool_def.name

    # Set the description
    description tool_def.description

    # Set input schema
    input_schema(schema)

    # Define the call method
    define_singleton_method(:call) do |server_context: {}, **args|
      # Convert args to string keys to match our DSL expectations
      params = args.transform_keys(&:to_s)

      # Execute the tool using our DSL
      result = tool_def.call(params)

      # Convert result to MCP::Tool::Response
      MCP::Tool::Response.new([
                                { type: 'text', text: result.to_s }
                              ])
    rescue ArgumentError => e
      # Return error as text response
      MCP::Tool::Response.new([
                                { type: 'text', text: "Error: #{e.message}" }
                              ])
    rescue StandardError => e
      # Return error as text response
      MCP::Tool::Response.new([
                                { type: 'text', text: "Error: #{e.message}" }
                              ])
    end
  end
end