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



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

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

.find_server_by_tool_name(tool_name) ⇒ Object



63
64
65
# File 'lib/smart_agent/mcp_client.rb', line 63

def find_server_by_tool_name(tool_name)
  tool_to_server[tool_name]
end

.serversObject



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

def servers
  @servers ||= {}
end

.set_server(tool_name, server_name) ⇒ Object



59
60
61
# File 'lib/smart_agent/mcp_client.rb', line 59

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

.tool_to_serverObject



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

def tool_to_server
  @tool_to_server ||= {}
end

Instance Method Details

#call(tool_name, params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smart_agent/mcp_client.rb', line 28

def call(tool_name, params)
  @context = MCPContext.new
  @context.instance_eval(&@code)
  command_path = @context.command_path
  if @context.mcp_type == :stdio
    client = MCP::StdioClient.new(command_path)
  else
    client = MCP::SSEClient.new(command_path)
  end
  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
23
24
25
26
# File 'lib/smart_agent/mcp_client.rb', line 9

def to_json
  @context = MCPContext.new
  @context.instance_eval(&@code)
  command_path = @context.command_path
  if @context.mcp_type == :stdio
    client = MCP::StdioClient.new(command_path)
  else
    client = MCP::SSEClient.new(command_path)
  end
  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