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
8
9
10
11
12
13
14
15
16
# 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]
  @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
end

Class Method Details

.clear_server_tools(server_name) ⇒ Object



83
84
85
86
87
88
# File 'lib/smart_agent/mcp_client.rb', line 83

def clear_server_tools(server_name)
  server_to_tools[server_name].each do |tool_name|
    tool_to_server.delete(tool_name)
  end
  server_to_tools[server_name] = []
end

.define(name, &block) ⇒ Object



72
73
74
75
76
# File 'lib/smart_agent/mcp_client.rb', line 72

def define(name, &block)
  servers[name] = block
  client = MCPClient.new(name)
  client.to_json
end

.find_server_by_tool_name(tool_name) ⇒ Object



90
91
92
# File 'lib/smart_agent/mcp_client.rb', line 90

def find_server_by_tool_name(tool_name)
  tool_to_server[tool_name]
end

.server_to_toolsObject



68
69
70
# File 'lib/smart_agent/mcp_client.rb', line 68

def server_to_tools
  @server_to_tools ||= Hash.new { |hash, key| hash[key] = [] }
end

.serversObject



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

def servers
  @servers ||= {}
end

.set_server(tool_name, server_name) ⇒ Object



78
79
80
81
# File 'lib/smart_agent/mcp_client.rb', line 78

def set_server(tool_name, server_name)
  tool_to_server[tool_name] = server_name
  server_to_tools[server_name] << tool_name unless server_to_tools[server_name].include?(tool_name)
end

.tool_to_serverObject



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

def tool_to_server
  @tool_to_server ||= {}
end

Instance Method Details

#call(tool_name, params, agent = nil) ⇒ Object



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

def call(tool_name, params, agent = nil)
  if agent
    agent.processor(:tool).call({ :content => "MCP Server is `#{@name}`, ToolName is `#{tool_name}`\n" }) if agent.processor(:tool)
    agent.processor(:tool).call({ :content => "params is `#{params}`\n" }) if agent.processor(:tool)
  end
  @client.call_method(
    {
      "name": tool_name.to_s,
      "arguments": params,
    }
  )
end

#closeObject



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

def close
  @client.stop
end

#to_jsonObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smart_agent/mcp_client.rb', line 18

def to_json
  mcp_server_json = @client.list_tools
  if mcp_server_json
    mcp_server_json["tools"] = filter_tools(mcp_server_json["tools"])
    MCPClient.clear_server_tools(@name)
    mcp_server_json["tools"].each do |tool|
      MCPClient.set_server(tool["name"].to_sym, @name)
    end
  end
  convertFormat(mcp_server_json)
end