|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'mcp' |
| 4 | + |
| 5 | +# rubocop:disable RSpec/SpecFilePathFormat |
| 6 | +RSpec.describe ToolForge::ToolDefinition, '#to_mcp_tool' do |
| 7 | + # rubocop:enable RSpec/SpecFilePathFormat |
| 8 | + it 'raises an error if MCP::Tool is not loaded' do |
| 9 | + tool = described_class.new(:my_tool) do |
| 10 | + description 'A test tool' |
| 11 | + end |
| 12 | + |
| 13 | + # Stub to simulate MCP not being loaded |
| 14 | + stub_const('MCP::Tool', nil) |
| 15 | + |
| 16 | + expect { tool.to_mcp_tool }.to raise_error(LoadError, /MCP SDK is not loaded/) |
| 17 | + end |
| 18 | + |
| 19 | + it 'returns a class that inherits from MCP::Tool' do |
| 20 | + tool = described_class.new(:my_tool) do |
| 21 | + description 'A test tool' |
| 22 | + end |
| 23 | + |
| 24 | + tool_class = tool.to_mcp_tool |
| 25 | + expect(tool_class).to be_a(Class) |
| 26 | + expect(tool_class.ancestors).to include(MCP::Tool) |
| 27 | + end |
| 28 | + |
| 29 | + it 'sets the tool description' do |
| 30 | + tool = described_class.new(:greeting_tool) do |
| 31 | + description 'Greets a user' |
| 32 | + end |
| 33 | + |
| 34 | + tool_class = tool.to_mcp_tool |
| 35 | + |
| 36 | + # MCP::Tool stores description at class level |
| 37 | + expect(tool_class.description).to eq('Greets a user') |
| 38 | + end |
| 39 | + |
| 40 | + it 'defines input schema with parameters' do |
| 41 | + tool = described_class.new(:my_tool) do |
| 42 | + description 'A test tool' |
| 43 | + param :name, type: :string, description: 'User name', required: true |
| 44 | + param :age, type: :integer, description: 'User age', required: false |
| 45 | + end |
| 46 | + |
| 47 | + tool_class = tool.to_mcp_tool |
| 48 | + schema = tool_class.input_schema.to_h |
| 49 | + |
| 50 | + expect(schema[:properties]).to have_key('name') |
| 51 | + expect(schema[:properties]['name'][:type]).to eq('string') |
| 52 | + expect(schema[:properties]['name'][:description]).to eq('User name') |
| 53 | + |
| 54 | + expect(schema[:properties]).to have_key('age') |
| 55 | + expect(schema[:properties]['age'][:type]).to eq('integer') |
| 56 | + expect(schema[:properties]['age'][:description]).to eq('User age') |
| 57 | + |
| 58 | + expect(schema[:required]).to eq([:name]) |
| 59 | + end |
| 60 | + |
| 61 | + it 'creates a call method that executes the block' do |
| 62 | + tool = described_class.new(:greeting_tool) do |
| 63 | + description 'Greets a user' |
| 64 | + param :name, type: :string |
| 65 | + execute { |name:| "Hello, #{name}!" } |
| 66 | + end |
| 67 | + |
| 68 | + tool_class = tool.to_mcp_tool |
| 69 | + |
| 70 | + result = tool_class.call(server_context: nil, name: 'Alice') |
| 71 | + expect(result).to be_a(MCP::Tool::Response) |
| 72 | + expect(result.content.first[:text]).to eq('Hello, Alice!') |
| 73 | + end |
| 74 | + |
| 75 | + it 'handles tools with default values' do |
| 76 | + tool = described_class.new(:greeting_tool) do |
| 77 | + description 'Greets a user' |
| 78 | + param :name, type: :string |
| 79 | + param :greeting, type: :string, default: 'Hello' |
| 80 | + |
| 81 | + execute do |name:, greeting: 'Hello'| |
| 82 | + "#{greeting}, #{name}!" |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + tool_class = tool.to_mcp_tool |
| 87 | + |
| 88 | + result1 = tool_class.call(server_context: nil, name: 'Bob') |
| 89 | + expect(result1.content.first[:text]).to eq('Hello, Bob!') |
| 90 | + |
| 91 | + result2 = tool_class.call(server_context: nil, name: 'Bob', greeting: 'Hi') |
| 92 | + expect(result2.content.first[:text]).to eq('Hi, Bob!') |
| 93 | + end |
| 94 | + |
| 95 | + it 'handles tools with multiple parameter types' do |
| 96 | + tool = described_class.new(:complex_tool) do |
| 97 | + description 'A complex tool' |
| 98 | + param :name, type: :string |
| 99 | + param :count, type: :integer |
| 100 | + param :active, type: :boolean |
| 101 | + |
| 102 | + execute do |name:, count:, active:| |
| 103 | + { name: name, count: count, active: active }.to_s |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + tool_class = tool.to_mcp_tool |
| 108 | + schema = tool_class.input_schema.to_h |
| 109 | + |
| 110 | + expect(schema[:properties]['name'][:type]).to eq('string') |
| 111 | + expect(schema[:properties]['count'][:type]).to eq('integer') |
| 112 | + expect(schema[:properties]['active'][:type]).to eq('boolean') |
| 113 | + |
| 114 | + result = tool_class.call(server_context: nil, name: 'test', count: 5, active: true) |
| 115 | + expect(result).to be_a(MCP::Tool::Response) |
| 116 | + end |
| 117 | + |
| 118 | + it 'converts type symbols to JSON schema types' do |
| 119 | + tool = described_class.new(:type_test) do |
| 120 | + description 'Tests type conversion' |
| 121 | + param :str, type: :string |
| 122 | + param :int, type: :integer |
| 123 | + param :bool, type: :boolean |
| 124 | + param :arr, type: :array |
| 125 | + param :obj, type: :object |
| 126 | + end |
| 127 | + |
| 128 | + tool_class = tool.to_mcp_tool |
| 129 | + schema = tool_class.input_schema.to_h |
| 130 | + |
| 131 | + expect(schema[:properties]['str'][:type]).to eq('string') |
| 132 | + expect(schema[:properties]['int'][:type]).to eq('integer') |
| 133 | + expect(schema[:properties]['bool'][:type]).to eq('boolean') |
| 134 | + expect(schema[:properties]['arr'][:type]).to eq('array') |
| 135 | + expect(schema[:properties]['obj'][:type]).to eq('object') |
| 136 | + end |
| 137 | +end |
0 commit comments