smart_agent 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e36441cd494df0719fcfe3ecff04976392e091372706e04e65949c6d17f32e95
4
+ data.tar.gz: dabe51abbd3e8cb0b8184f042b32361a0a50fe9cbc4f7b86e8fa93b88ee222aa
5
+ SHA512:
6
+ metadata.gz: '09335af182cc6402c814098f1de24dbf62e5ef5187fb6297c3f4b2b301e04d8b37e1da242b0d7bb0ea55d42722ca90c7ce451fbdd7d7832ff84dd1f1f6643751'
7
+ data.tar.gz: 910c95df53d2564bc1f842c56925866d8ac255365c9329e7113450dbb9fffd9d8221d51c3c72e3342c4bbfec0b33bf79642eeb736457b188c76f4025cf96c71a
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 zhuangbiaowei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Smart Agent Framework
2
+
3
+ [![Ruby Version](https://img.shields.io/badge/Ruby-3.2%2B-red)](https://www.ruby-lang.org)
4
+ [![Gem Version](https://img.shields.io/gem/v/smart_agent)](https://rubygems.org/gems/smart_agent)
5
+ [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
+
7
+ An intelligent agent framework built on [smart_prompt](https://github.com/zhuangbiaowei/smart_prompt), featuring DSL definition, function calling and MCP protocol integration.
8
+
9
+ ## Key Features
10
+
11
+ - **Declarative DSL** - Define agents and workflows using concise Ruby syntax
12
+ - **Function Calling** - Seamless integration with LLM capabilities
13
+ - **MCP Protocol** - Built-in Model Context Protocol support
14
+ - **Task Orchestration** - Coordinate multiple agents for complex tasks
15
+ - **Extensible Architecture** - Support custom functions and protocol handlers
16
+
17
+ ## Quick Start
18
+
19
+ ```ruby
20
+ require 'smart_agent'
21
+
22
+ SmartAgent.define :weather_bot do
23
+ result = call_worker(:weather, params, with_tools: true)
24
+ if result.call_tools?
25
+ weather_result = call_tools(result)
26
+ return call_worker(:weather_summary, params, weather_result, with_tools: false)
27
+ else
28
+ return result
29
+ end
30
+ end
31
+
32
+ SmartAgent::Tool.define :get_weather do |location, date|
33
+ param_define :location, "City or More Specific Address", :str
34
+ param_define :date, "Specific Date or Today or Tomorrow", :date
35
+ # Call the Weather API
36
+ end
37
+
38
+ engine = SmartPrompt::Engine.new("./config/llm_config.yml")
39
+ SmartAgent.engine = engine
40
+ agent = SmartAgent.create(:weather_bot, [:get_weather])
41
+
42
+ puts agent.please("Get tomorrow's weather forecast in Shanghai")
43
+ ```
44
+
45
+ ## Installation
46
+
47
+ Add to your Gemfile:
48
+ ```ruby
49
+ gem 'smart_agent'
50
+ ```
51
+
52
+ Then execute:
53
+ ```bash
54
+ bundle install
55
+ ```
56
+
57
+ Or install directly:
58
+ ```bash
59
+ gem install smart_agent
60
+ ```
61
+
62
+ ## Documentation
63
+
64
+ Full documentation available at: [docs.smartagent.dev](https://docs.smartagent.dev)
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork the repository
69
+ 2. Create feature branch (`git checkout -b feature/amazing-feature`)
70
+ 3. Commit changes (`git commit -m 'Add some amazing feature'`)
71
+ 4. Push branch (`git push origin feature/amazing-feature`)
72
+ 5. Open a Pull Request
73
+
74
+ ## License
75
+
76
+ Released under the MIT License. See [LICENSE](LICENSE) for details.
@@ -0,0 +1,128 @@
1
+ module SmartAgent
2
+ class Agent
3
+ attr_accessor :tools, :servers
4
+
5
+ def initialize(name, tools: nil, mcp_servers: nil)
6
+ SmartAgent.logger.info "Create agent's name is #{name}"
7
+ @name = name
8
+ @tools = tools
9
+ @servers = mcp_servers
10
+ @code = self.class.agents[name]
11
+ end
12
+
13
+ def on_reasoning(&block)
14
+ @reasoning_event_proc = block
15
+ end
16
+
17
+ def on_content(&block)
18
+ @content_event_proc = block
19
+ end
20
+
21
+ def on_tool_call(&block)
22
+ @tool_call_proc = block
23
+ end
24
+
25
+ def on_event
26
+ if @reasoning_event_proc || @content_event_proc
27
+ return true
28
+ else
29
+ return false
30
+ end
31
+ end
32
+
33
+ def processor(name)
34
+ case name
35
+ when :reasoning
36
+ return @reasoning_event_proc
37
+ when :content
38
+ return @content_event_proc
39
+ when :tool
40
+ return @tool_call_proc
41
+ else
42
+ return nil
43
+ end
44
+ end
45
+
46
+ def please(prompt)
47
+ context = AgentContext.new(self)
48
+ context.params[:text] = prompt
49
+ return context.instance_eval(&@code)
50
+ end
51
+
52
+ class << self
53
+ def agents
54
+ @agents ||= {}
55
+ end
56
+
57
+ def define(name, &block)
58
+ agents[name] = block
59
+ end
60
+ end
61
+ end
62
+
63
+ class AgentContext
64
+ def initialize(agent)
65
+ @agent = agent
66
+ end
67
+
68
+ def call_worker(name, params, result: nil, with_tools: true)
69
+ if result
70
+ params[:result] = result
71
+ end
72
+ if with_tools
73
+ if @agent.tools
74
+ simple_tools = @agent.tools.map { |tool_name| Tool.new(tool_name).to_json }
75
+ end
76
+ if @agent.servers
77
+ mcp_tools = @agent.servers.map { |mcp_name| MCPClient.new(mcp_name).to_json }
78
+ mcp_tools.each do |tools|
79
+ tools["tools"].each do |tool|
80
+ simple_tools << tool
81
+ end
82
+ end
83
+ end
84
+ params[:tools] = simple_tools
85
+ end
86
+ ret = nil
87
+ if @agent.on_event && with_tools == false
88
+ SmartAgent.prompt_engine.call_worker_by_stream(name, params) do |chunk, _bytesize|
89
+ if chunk.dig("choices", 0, "delta", "reasoning_content")
90
+ @agent.processor(:reasoning).call(chunk)
91
+ end
92
+ if chunk.dig("choices", 0, "delta", "content")
93
+ @agent.processor(:content).call(chunk)
94
+ end
95
+ end
96
+ else
97
+ result = SmartAgent.prompt_engine.call_worker(name, params)
98
+ ret = Result.new(result)
99
+ return ret
100
+ end
101
+ end
102
+
103
+ def call_tools(result)
104
+ @agent.processor(:tool).call({ :status => :start })
105
+ SmartAgent.logger.info("call tools: " + result.to_s)
106
+ results = []
107
+ result.call_tools.each do |tool|
108
+ tool_name = tool["function"]["name"].to_sym
109
+ params = JSON.parse(tool["function"]["arguments"])
110
+ if Tool.find_tool(tool_name)
111
+ @agent.processor(:tool).call({ :content => "ToolName is `#{tool_name}`" })
112
+ results << Tool.new(tool_name).call(params)
113
+ end
114
+ if server_name = MCPClient.find_server_by_tool_name(tool_name)
115
+ @agent.processor(:tool).call({ :content => "MCP Server is `#{server_name}`, ToolName is `#{tool_name}`" })
116
+ results << MCPClient.new(server_name).call(tool_name, params)
117
+ end
118
+ @agent.processor(:tool).call({ :content => " ... done\n" })
119
+ end
120
+ @agent.processor(:tool).call({ :status => :end })
121
+ return results
122
+ end
123
+
124
+ def params
125
+ @params ||= {}
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,78 @@
1
+ module SmartAgent
2
+ class Engine
3
+ def initialize(config_file)
4
+ @config_file = config_file
5
+ load_config(config_file)
6
+ SmartAgent.logger.info "Started create the SmartAgent engine."
7
+ end
8
+
9
+ def create_dir(filename)
10
+ path = File::path(filename).to_s
11
+ parent_dir = File::dirname(path)
12
+ Dir.mkdir(parent_dir, 0755) unless File.directory?(parent_dir)
13
+ end
14
+
15
+ def load_config(config_file)
16
+ begin
17
+ @config_file = config_file
18
+ @config = YAML.load_file(config_file)
19
+ if @config["logger_file"]
20
+ create_dir(@config["logger_file"])
21
+ SmartAgent.logger = Logger.new(@config["logger_file"])
22
+ end
23
+ if engine_config = @config["engine_config"]
24
+ SmartAgent.prompt_engine = SmartPrompt::Engine.new(engine_config)
25
+ else
26
+ SmartAgent.logger.error "SmartPrompt Config file not found: #{ex.message}"
27
+ raise ConfigurationError, "SmartPrompt Config file not found: #{ex.message}"
28
+ end
29
+ load_tools
30
+ load_mcp_server
31
+ load_agents
32
+ SmartAgent.logger.info "Loading configuration from file: #{config_file}"
33
+ rescue Psych::SyntaxError => ex
34
+ SmartAgent.logger.error "YAML syntax error in config file: #{ex.message}"
35
+ raise ConfigurationError, "Invalid YAML syntax in config file: #{ex.message}"
36
+ rescue Errno::ENOENT => ex
37
+ SmartAgent.logger.error "Config file not found: #{ex.message}"
38
+ raise ConfigurationError, "Config file not found: #{ex.message}"
39
+ rescue StandardError => ex
40
+ SmartAgent.logger.error "Error loading configuration: #{ex.message}"
41
+ raise ConfigurationError, "Error loading configuration: #{ex.message}"
42
+ ensure
43
+ SmartAgent.logger.info "Configuration loaded successfully"
44
+ end
45
+ end
46
+
47
+ def load_tools
48
+ Dir.glob(File.join(@config["tools_path"], "*.rb")).each do |file|
49
+ require(file)
50
+ end
51
+ end
52
+
53
+ def load_mcp_server
54
+ Dir.glob(File.join(@config["mcp_path"], "*.rb")).each do |file|
55
+ require(file)
56
+ end
57
+ end
58
+
59
+ def load_agents
60
+ Dir.glob(File.join(@config["agent_path"], "*.rb")).each do |file|
61
+ require(file)
62
+ end
63
+ end
64
+
65
+ def build_agent(name, tools: nil, mcp_servers: nil)
66
+ agent = Agent.new(name, tools: tools, mcp_servers: mcp_servers)
67
+ agents[name] = agent
68
+ end
69
+
70
+ def agents
71
+ self.class.agents
72
+ end
73
+
74
+ def self.agents
75
+ @agents ||= {}
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,74 @@
1
+ module SmartAgent
2
+ class MCPClient
3
+ def initialize(name)
4
+ SmartAgent.logger.info "Create mcp server's name is #{name}"
5
+ @name = name
6
+ @code = self.class.servers[name]
7
+ end
8
+
9
+ def to_json
10
+ @context = MCPContext.new
11
+ @context.instance_eval(&@code)
12
+ command_path = @context.command_path
13
+ client = MCP::Client.new(command_path)
14
+ client.start
15
+ mcp_server_json = client.list_tools
16
+ if mcp_server_json
17
+ mcp_server_json["tools"].each do |tool|
18
+ MCPClient.set_server(tool["name"].to_sym, @name)
19
+ end
20
+ end
21
+ convertFormat(mcp_server_json)
22
+ end
23
+
24
+ def call(tool_name, params)
25
+ @context = MCPContext.new
26
+ @context.instance_eval(&@code)
27
+ command_path = @context.command_path
28
+ client = MCP::Client.new(command_path)
29
+ client.start
30
+ client.call_method(
31
+ {
32
+ "name": tool_name.to_s,
33
+ "arguments": params,
34
+ }
35
+ )
36
+ end
37
+
38
+ class << self
39
+ def servers
40
+ @servers ||= {}
41
+ end
42
+
43
+ def tool_to_server
44
+ @tool_to_server ||= {}
45
+ end
46
+
47
+ def define(name, &block)
48
+ servers[name] = block
49
+ end
50
+
51
+ def set_server(tool_name, server_name)
52
+ tool_to_server[tool_name] = server_name
53
+ end
54
+
55
+ def find_server_by_tool_name(tool_name)
56
+ tool_to_server[tool_name]
57
+ end
58
+ end
59
+ end
60
+
61
+ class MCPContext
62
+ def type(mcp_type)
63
+ @mcp_type = mcp_type
64
+ end
65
+
66
+ def command_path
67
+ @command_path
68
+ end
69
+
70
+ def command(path)
71
+ @command_path = path
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ module SmartAgent
2
+ class Result
3
+ def initialize(response)
4
+ SmartAgent.logger.info("response is:" + response.to_s)
5
+ @response = response
6
+ end
7
+
8
+ def call_tools
9
+ if @response.class == String
10
+ return nil
11
+ else
12
+ @response.dig("choices", 0, "message", "tool_calls")
13
+ end
14
+ end
15
+
16
+ def content
17
+ @response.dig("choices", 0, "message", "content")
18
+ end
19
+
20
+ def response
21
+ @response
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,67 @@
1
+ module SmartAgent
2
+ class Tool
3
+ def initialize(name)
4
+ SmartAgent.logger.info "Create tool's name is #{name}"
5
+ @name = name
6
+ @code = self.class.tools[name]
7
+ end
8
+
9
+ def call(params)
10
+ @context = ToolContext.new
11
+ @context.input_params = params
12
+ @context.instance_eval(&@code)
13
+ end
14
+
15
+ def to_json
16
+ @context = ToolContext.new
17
+ @context.instance_eval(&@code)
18
+ params = @context.params
19
+
20
+ properties = params.each_with_object({}) do |(name, details), hash|
21
+ hash[name] = {
22
+ type: details[:type],
23
+ description: details[:description],
24
+ }
25
+ end
26
+
27
+ return {
28
+ type: "function",
29
+ function: {
30
+ name: @name,
31
+ description: "",
32
+ parameters: {
33
+ type: "object",
34
+ properties: properties,
35
+ required: params.keys,
36
+ },
37
+ },
38
+ }
39
+ end
40
+
41
+ class << self
42
+ def tools
43
+ @tools ||= {}
44
+ end
45
+
46
+ def define(name, &block)
47
+ tools[name] = block
48
+ end
49
+
50
+ def find_tool(name)
51
+ tools[name]
52
+ end
53
+ end
54
+ end
55
+
56
+ class ToolContext
57
+ attr_accessor :input_params
58
+
59
+ def params
60
+ @params ||= {}
61
+ end
62
+
63
+ def param_define(name, description, type)
64
+ params[name] = { description: description, type: type }
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module SmartAgent
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require "../mcp-sdk.rb/lib/mcp"
2
+
3
+ require File.expand_path("../smart_agent/version", __FILE__)
4
+ require File.expand_path("../smart_agent/engine", __FILE__)
5
+ require File.expand_path("../smart_agent/tool", __FILE__)
6
+ require File.expand_path("../smart_agent/mcp_client", __FILE__)
7
+ require File.expand_path("../smart_agent/result", __FILE__)
8
+ require File.expand_path("../smart_agent/agent", __FILE__)
9
+
10
+ module SmartAgent
11
+ class Error < StandardError; end
12
+ class ConfigurationError < Error; end
13
+ class APIError < Error; end
14
+ class CallAgentError < Error; end
15
+
16
+ attr_writer :logger
17
+
18
+ def self.define(name, &block)
19
+ Agent.define(name, &block)
20
+ end
21
+
22
+ def self.create(name, tools: nil, mcp_servers: nil)
23
+ Agent.new(name, tools: tools, mcp_servers: mcp_servers)
24
+ end
25
+
26
+ def self.build_agent(name, tools: nil, mcp_servers: nil)
27
+ agent = Agent.new(name, tools: tools, mcp_servers: mcp_servers)
28
+ SmartAgent::Engine.agents[name] = agent
29
+ end
30
+
31
+ def self.logger=(logger)
32
+ @logger = logger
33
+ end
34
+
35
+ def self.logger
36
+ @logger
37
+ end
38
+
39
+ def self.prompt_engine
40
+ @prompt_engine
41
+ end
42
+
43
+ def self.prompt_engine=(engine)
44
+ @prompt_engine = engine
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Your Name
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-03-28 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: smart_prompt
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ description: Build AI agents with declarative DSL and Model Context Protocol support
41
+ email:
42
+ - your.email@example.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - README.md
49
+ - lib/smart_agent.rb
50
+ - lib/smart_agent/agent.rb
51
+ - lib/smart_agent/engine.rb
52
+ - lib/smart_agent/mcp_client.rb
53
+ - lib/smart_agent/result.rb
54
+ - lib/smart_agent/tool.rb
55
+ - lib/smart_agent/version.rb
56
+ homepage: https://github.com/yourname/smart_agent
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 3.2.0
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.6.4
75
+ specification_version: 4
76
+ summary: Intelligent agent framework with DSL and MCP integration
77
+ test_files: []