Class: SmartAgent::MCPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_agent/mcp_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ MCPClient

Returns a new instance of MCPClient.



3
4
5
6
7
# File 'lib/smart_agent/mcp_client.rb', line 3

def initialize(name)
  SmartAgent.logger.info "Create mcp server's name is #{name}"
  @name = name
  @code = self.class.servers[name]
end

Class Method Details

.define(name, &block) ⇒ Object



47
48
49
# File 'lib/smart_agent/mcp_client.rb', line 47

def define(name, &block)
  servers[name] = block
end

.find_server_by_tool_name(tool_name) ⇒ Object



55
56
57
# File 'lib/smart_agent/mcp_client.rb', line 55

def find_server_by_tool_name(tool_name)
  tool_to_server[tool_name]
end

.serversObject



39
40
41
# File 'lib/smart_agent/mcp_client.rb', line 39

def servers
  @servers ||= {}
end

.set_server(tool_name, server_name) ⇒ Object



51
52
53
# File 'lib/smart_agent/mcp_client.rb', line 51

def set_server(tool_name, server_name)
  tool_to_server[tool_name] = server_name
end

.tool_to_serverObject



43
44
45
# File 'lib/smart_agent/mcp_client.rb', line 43

def tool_to_server
  @tool_to_server ||= {}
end

Instance Method Details

#call(tool_name, params) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/smart_agent/mcp_client.rb', line 24

def call(tool_name, params)
  @context = MCPContext.new
  @context.instance_eval(&@code)
  command_path = @context.command_path
  client = MCP::Client.new(command_path)
  client.start
  client.call_method(
    {
      "name": tool_name.to_s,
      "arguments": params,
    }
  )
end

#to_jsonObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/smart_agent/mcp_client.rb', line 9

def to_json
  @context = MCPContext.new
  @context.instance_eval(&@code)
  command_path = @context.command_path
  client = MCP::Client.new(command_path)
  client.start
  mcp_server_json = client.list_tools
  if mcp_server_json
    mcp_server_json["tools"].each do |tool|
      MCPClient.set_server(tool["name"].to_sym, @name)
    end
  end
  convertFormat(mcp_server_json)
end