Class: Relay::Models::MCP

Inherits:
Sequel::Model
  • Object
show all
Includes:
Relay::Model
Defined in:
app/models/mcp.rb

Defined Under Namespace

Modules: Preset

Constant Summary collapse

SUMMARY_COLUMNS =
%i[
  id
  user_id
  name
  description
  transport
  enabled
  created_at
  updated_at
].freeze

stdio collapse

HTTP collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relay::Model

included

Class Method Details

.dump_data(data) ⇒ Object



24
25
26
# File 'app/models/mcp.rb', line 24

def self.dump_data(data)
  JSON.generate(data)
end

.load_data(data) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'app/models/mcp.rb', line 28

def self.load_data(data)
  case data
  when String then JSON.parse(data)
  when Hash, Array then data
  else {}
  end
rescue JSON::ParserError
  {}
end

.normalize_url(value) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'app/models/mcp.rb', line 42

def self.normalize_url(value)
  url = value.to_s.strip
  return "" if url.empty?
  uri = URI.parse(url)
  uri.path = "/" if uri.path.to_s.empty?
  uri.to_s
rescue URI::InvalidURIError
  url
end

.summary_dataset(dataset = self.dataset) ⇒ Object



38
39
40
# File 'app/models/mcp.rb', line 38

def self.summary_dataset(dataset = self.dataset)
  dataset.select(*SUMMARY_COLUMNS)
end

Instance Method Details

#argumentsArray<String>

Returns the stdio command arguments

Returns:

  • (Array<String>)

    Returns the stdio command arguments



79
80
81
# File 'app/models/mcp.rb', line 79

def arguments
  argv.drop(1)
end

#argvArray<String>

Returns the stdio command argv

Returns:

  • (Array<String>)

    Returns the stdio command argv



65
66
67
# File 'app/models/mcp.rb', line 65

def argv
  transport == "stdio" ? data["argv"] || [] : []
end

#commandString

Returns the stdio executable name

Returns:

  • (String)

    Returns the stdio executable name



72
73
74
# File 'app/models/mcp.rb', line 72

def command
  argv.first.to_s
end

#cwdString

Returns the stdio working directory

Returns:

  • (String)

    Returns the stdio working directory



93
94
95
# File 'app/models/mcp.rb', line 93

def cwd
  transport == "stdio" ? data["cwd"].to_s : ""
end

#dataHash

Returns the parsed MCP transport data

Returns:

  • (Hash)

    Returns the parsed MCP transport data



55
56
57
# File 'app/models/mcp.rb', line 55

def data
  self.class.load_data(self[:data])
end

#envHash

Returns the stdio environment variables

Returns:

  • (Hash)

    Returns the stdio environment variables



86
87
88
# File 'app/models/mcp.rb', line 86

def env
  transport == "stdio" ? data["env"] || {} : {}
end

#headersHash

Returns the MCP HTTP headers

Returns:

  • (Hash)

    Returns the MCP HTTP headers



111
112
113
# File 'app/models/mcp.rb', line 111

def headers
  transport == "http" ? data["headers"] || {} : {}
end

#startvoid

This method returns an undefined value.

Returns Starts the MCP client.



126
127
128
# File 'app/models/mcp.rb', line 126

def start
  mcp.start
end

#stopvoid

This method returns an undefined value.

Returns Stops the MCP client.



133
134
135
# File 'app/models/mcp.rb', line 133

def stop
  mcp.stop
end

#toolsArray<Class<LLM::Tool>>

Returns the cached MCP tool classes

Returns:

  • (Array<Class<LLM::Tool>>)

    Returns the cached MCP tool classes



119
120
121
# File 'app/models/mcp.rb', line 119

def tools
  @tools ||= mcp.tools
end

#urlString

Returns the MCP HTTP endpoint URL

Returns:

  • (String)

    Returns the MCP HTTP endpoint URL



104
105
106
# File 'app/models/mcp.rb', line 104

def url
  self.class.normalize_url(transport == "http" ? data["url"] : nil)
end