Class: Silas::Connections

Inherits:
Object
  • Object
show all
Defined in:
lib/silas/connections.rb

Overview

Discovery + boot-warm for app/agent/connections/*.yml. Lists each remote MCP server's tools ONCE at boot (cached), surfaces them namespaced as "__" (model-visible -> in the digest), and resolves those names to fresh RemoteTool instances for the Ledger.

Instance Method Summary collapse

Constructor Details

#initialize(root:, client_factory: nil) ⇒ Connections

Returns a new instance of Connections.



7
8
9
10
11
12
# File 'lib/silas/connections.rb', line 7

def initialize(root:, client_factory: nil)
  @root = Pathname(root)
  @client_factory = client_factory
  @definitions = []
  @by_name = {}
end

Instance Method Details

#definitionsObject



41
# File 'lib/silas/connections.rb', line 41

def definitions = @definitions.dup

#descriptorsObject



14
15
16
# File 'lib/silas/connections.rb', line 14

def descriptors
  @descriptors ||= Dir[@root.join("app/agent/connections/*.yml")].sort.filter_map { |f| Connection.parse(f) }
end

#resolve(name) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/silas/connections.rb', line 43

def resolve(name)
  pair = @by_name[name]
  return nil unless pair

  conn, remote = pair
  client = @client_factory&.call(conn) || conn.client
  Connection::RemoteTool.new(connection: conn, remote_name: remote, client: client)
end

#warm!Object

Boot-time tools/list, cached. Fail-loud (like the Registry's boot tool validation): a misconfigured/unreachable connection raises HERE, never inside a turn or a mid-turn digest check.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/silas/connections.rb', line 21

def warm!
  return self if @warmed

  descriptors.each do |conn|
    client = @client_factory&.call(conn) || conn.client
    client.list_tools.each do |remote|
      ns = "#{conn.name}__#{remote['name']}"
      @definitions << {
        "name" => ns, "description" => remote["description"].to_s,
        "input_schema" => remote["inputSchema"] || { "type" => "object", "properties" => {} }
      }
      @by_name[ns] = [ conn, remote["name"] ]
    end
  rescue StandardError => e
    raise Error, "connection #{conn.name}: tools/list failed at boot — #{e.class}: #{e.message}"
  end
  @warmed = true
  self
end