Class: SmartAgent::MCPClient
- Inherits:
-
Object
- Object
- SmartAgent::MCPClient
- Defined in:
- lib/smart_agent/mcp_client.rb
Class Method Summary collapse
- .define(name, &block) ⇒ Object
- .find_server_by_tool_name(tool_name) ⇒ Object
- .servers ⇒ Object
- .set_server(tool_name, server_name) ⇒ Object
- .tool_to_server ⇒ Object
Instance Method Summary collapse
- #call(tool_name, params, agent = nil) ⇒ Object
- #close ⇒ Object
-
#initialize(name) ⇒ MCPClient
constructor
A new instance of MCPClient.
- #to_json ⇒ Object
Constructor Details
#initialize(name) ⇒ 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
.define(name, &block) ⇒ Object
54 55 56 57 58 |
# File 'lib/smart_agent/mcp_client.rb', line 54 def define(name, &block) servers[name] = block client = MCPClient.new(name) client.to_json end |
.find_server_by_tool_name(tool_name) ⇒ Object
64 65 66 |
# File 'lib/smart_agent/mcp_client.rb', line 64 def find_server_by_tool_name(tool_name) tool_to_server[tool_name] end |
.servers ⇒ Object
46 47 48 |
# File 'lib/smart_agent/mcp_client.rb', line 46 def servers @servers ||= {} end |
.set_server(tool_name, server_name) ⇒ Object
60 61 62 |
# File 'lib/smart_agent/mcp_client.rb', line 60 def set_server(tool_name, server_name) tool_to_server[tool_name] = server_name end |
.tool_to_server ⇒ Object
50 51 52 |
# File 'lib/smart_agent/mcp_client.rb', line 50 def tool_to_server @tool_to_server ||= {} end |
Instance Method Details
#call(tool_name, params, agent = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/smart_agent/mcp_client.rb', line 28 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 |
#close ⇒ Object
41 42 43 |
# File 'lib/smart_agent/mcp_client.rb', line 41 def close @client.stop end |
#to_json ⇒ Object
18 19 20 21 22 23 24 25 26 |
# 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"].each do |tool| MCPClient.set_server(tool["name"].to_sym, @name) end end convertFormat(mcp_server_json) end |