Class: Legion::MCP::Client::Connection

Inherits:
Object
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/mcp/client/connection.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

TOOL_CACHE_TTL =

seconds

300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, transport:, **config) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
# File 'lib/legion/mcp/client/connection.rb', line 15

def initialize(name:, transport:, **config)
  @name = name
  @transport_type = transport.to_sym
  @config = config
  @tools_cache = nil
  @tools_cached_at = nil
  @connected = false
  @mutex = Mutex.new
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/legion/mcp/client/connection.rb', line 11

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/legion/mcp/client/connection.rb', line 11

def name
  @name
end

#transport_typeObject (readonly)

Returns the value of attribute transport_type.



11
12
13
# File 'lib/legion/mcp/client/connection.rb', line 11

def transport_type
  @transport_type
end

Instance Method Details

#call_tool(name:, arguments: {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/legion/mcp/client/connection.rb', line 99

def call_tool(name:, arguments: {})
  connect unless connected?
  LoggingSupport.info(
    'client.tool_call.start',
    connection: @name,
    transport:  @transport_type,
    tool_name:  name,
    arguments:  LoggingSupport.summarize_params(arguments)
  )
  result = execute_tool_call(name: name, arguments: arguments)
  LoggingSupport.info(
    'client.tool_call.complete',
    connection: @name,
    transport:  @transport_type,
    tool_name:  name,
    result:     LoggingSupport.summarize_result(result)
  )
  result
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'legion.mcp.client.connection.call_tool')
  LoggingSupport.warn(
    'client.tool_call.failed',
    connection: @name,
    transport:  @transport_type,
    tool_name:  name,
    error:      e.message
  )
  raise
end

#connectObject



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
57
58
# File 'lib/legion/mcp/client/connection.rb', line 29

def connect
  @mutex.synchronize do
    return if @connected

    LoggingSupport.info(
      'client.connect.start',
      connection: @name,
      transport:  @transport_type,
      config:     @config.slice(:url, :command)
    )
    case @transport_type
    when :stdio
      connect_stdio
    when :http, :streamable_http
      connect_http
    else
      raise ArgumentError, "Unknown transport: #{@transport_type}"
    end
    @connected = true
    LoggingSupport.info(
      'client.connect.complete',
      connection: @name,
      transport:  @transport_type
    )
  end
rescue StandardError => e
  @connected = false
  handle_exception(e, level: :error, operation: 'legion.mcp.client.connection.connect')
  raise
end

#connected?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/legion/mcp/client/connection.rb', line 25

def connected?
  @connected
end

#disconnectObject



60
61
62
63
64
65
66
# File 'lib/legion/mcp/client/connection.rb', line 60

def disconnect
  @mutex.synchronize do
    @transport&.close if @transport.respond_to?(:close)
    @connected = false
    @tools_cache = nil
  end
end

#tools(force_refresh: false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/mcp/client/connection.rb', line 68

def tools(force_refresh: false)
  @mutex.synchronize do
    if !force_refresh && @tools_cache && @tools_cached_at &&
       (Time.now - @tools_cached_at) < TOOL_CACHE_TTL
      LoggingSupport.info(
        'client.tools.cache_hit',
        connection: @name,
        transport:  @transport_type,
        count:      @tools_cache.size
      )
      return @tools_cache
    end

    LoggingSupport.info(
      'client.tools.fetch.start',
      connection:    @name,
      transport:     @transport_type,
      force_refresh: force_refresh
    )
    @tools_cache = fetch_tools
    @tools_cached_at = Time.now
    LoggingSupport.info(
      'client.tools.fetch.complete',
      connection: @name,
      transport:  @transport_type,
      count:      @tools_cache.size
    )
    @tools_cache
  end
end