|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module MCP |
| 6 | + class Server |
| 7 | + class CapabilitiesTest < ActiveSupport::TestCase |
| 8 | + test "can be initialized with a capabilities hash" do |
| 9 | + capabilities = MCP::Server::Capabilities.new(prompts: {}) |
| 10 | + |
| 11 | + assert_equal({ prompts: {} }, capabilities.to_h) |
| 12 | + end |
| 13 | + |
| 14 | + test "ignores unknown values in capabilities hash" do |
| 15 | + capabilities = MCP::Server::Capabilities.new(prompts: {}, invalid: :value) |
| 16 | + |
| 17 | + assert_equal({ prompts: {} }, capabilities.to_h) |
| 18 | + end |
| 19 | + |
| 20 | + test "support prompts" do |
| 21 | + capabilities = MCP::Server::Capabilities.new |
| 22 | + |
| 23 | + capabilities.support_prompts |
| 24 | + |
| 25 | + assert_operator capabilities.to_h, :key?, :prompts |
| 26 | + end |
| 27 | + |
| 28 | + test "support resources" do |
| 29 | + capabilities = MCP::Server::Capabilities.new |
| 30 | + |
| 31 | + capabilities.support_resources |
| 32 | + |
| 33 | + assert_operator capabilities.to_h, :key?, :resources |
| 34 | + end |
| 35 | + |
| 36 | + test "support tools" do |
| 37 | + capabilities = MCP::Server::Capabilities.new |
| 38 | + |
| 39 | + capabilities.support_tools |
| 40 | + |
| 41 | + assert_operator capabilities.to_h, :key?, :tools |
| 42 | + end |
| 43 | + |
| 44 | + test "support completions" do |
| 45 | + capabilities = MCP::Server::Capabilities.new |
| 46 | + |
| 47 | + capabilities.support_completions |
| 48 | + |
| 49 | + assert_operator capabilities.to_h, :key?, :completions |
| 50 | + assert_empty(capabilities.to_h[:completions]) |
| 51 | + end |
| 52 | + |
| 53 | + test "support logging" do |
| 54 | + capabilities = MCP::Server::Capabilities.new |
| 55 | + |
| 56 | + capabilities.support_logging |
| 57 | + |
| 58 | + assert_operator capabilities.to_h, :key?, :logging |
| 59 | + assert_empty(capabilities.to_h[:logging]) |
| 60 | + end |
| 61 | + |
| 62 | + test "support experimental with custom config" do |
| 63 | + capabilities = MCP::Server::Capabilities.new |
| 64 | + |
| 65 | + capabilities.support_experimental({ myFeature: { enabled: true } }) |
| 66 | + |
| 67 | + assert_operator capabilities.to_h, :key?, :experimental |
| 68 | + assert_equal({ myFeature: { enabled: true } }, capabilities.to_h[:experimental]) |
| 69 | + end |
| 70 | + |
| 71 | + test "support prompts list changed" do |
| 72 | + capabilities = MCP::Server::Capabilities.new |
| 73 | + |
| 74 | + capabilities.support_prompts_list_changed |
| 75 | + |
| 76 | + assert_equal({ prompts: { listChanged: true } }, capabilities.to_h) |
| 77 | + end |
| 78 | + |
| 79 | + test "support resources list changed" do |
| 80 | + capabilities = MCP::Server::Capabilities.new |
| 81 | + |
| 82 | + capabilities.support_resources_list_changed |
| 83 | + |
| 84 | + assert_equal({ resources: { listChanged: true } }, capabilities.to_h) |
| 85 | + end |
| 86 | + |
| 87 | + test "support resources subscribe" do |
| 88 | + capabilities = MCP::Server::Capabilities.new |
| 89 | + |
| 90 | + capabilities.support_resources_subscribe |
| 91 | + |
| 92 | + assert_equal({ resources: { subscribe: true } }, capabilities.to_h) |
| 93 | + end |
| 94 | + |
| 95 | + test "support resources with both list changed and subscribe" do |
| 96 | + capabilities = MCP::Server::Capabilities.new |
| 97 | + |
| 98 | + capabilities.support_resources_list_changed |
| 99 | + capabilities.support_resources_subscribe |
| 100 | + |
| 101 | + assert_equal({ resources: { listChanged: true, subscribe: true } }, capabilities.to_h) |
| 102 | + end |
| 103 | + |
| 104 | + test "support tools list changed" do |
| 105 | + capabilities = MCP::Server::Capabilities.new |
| 106 | + |
| 107 | + capabilities.support_tools_list_changed |
| 108 | + |
| 109 | + assert_equal({ tools: { listChanged: true } }, capabilities.to_h) |
| 110 | + end |
| 111 | + |
| 112 | + test "initializes with prompts list changed from hash" do |
| 113 | + capabilities = MCP::Server::Capabilities.new(prompts: { listChanged: true }) |
| 114 | + |
| 115 | + assert_equal({ prompts: { listChanged: true } }, capabilities.to_h) |
| 116 | + end |
| 117 | + |
| 118 | + test "initializes with resources list changed and subscribe from hash" do |
| 119 | + capabilities = MCP::Server::Capabilities.new(resources: { listChanged: true, subscribe: true }) |
| 120 | + |
| 121 | + assert_equal({ resources: { listChanged: true, subscribe: true } }, capabilities.to_h) |
| 122 | + end |
| 123 | + |
| 124 | + test "initializes with tools list changed from hash" do |
| 125 | + capabilities = MCP::Server::Capabilities.new(tools: { listChanged: true }) |
| 126 | + |
| 127 | + assert_equal({ tools: { listChanged: true } }, capabilities.to_h) |
| 128 | + end |
| 129 | + |
| 130 | + test "initializes with completions from hash" do |
| 131 | + capabilities = MCP::Server::Capabilities.new(completions: {}) |
| 132 | + |
| 133 | + assert_equal({ completions: {} }, capabilities.to_h) |
| 134 | + end |
| 135 | + |
| 136 | + test "initializes with logging from hash" do |
| 137 | + capabilities = MCP::Server::Capabilities.new(logging: {}) |
| 138 | + |
| 139 | + assert_equal({ logging: {} }, capabilities.to_h) |
| 140 | + end |
| 141 | + |
| 142 | + test "initializes with experimental config from hash" do |
| 143 | + capabilities = MCP::Server::Capabilities.new(experimental: { feature1: { enabled: true }, feature2: "config" }) |
| 144 | + |
| 145 | + assert_equal({ experimental: { feature1: { enabled: true }, feature2: "config" } }, capabilities.to_h) |
| 146 | + end |
| 147 | + |
| 148 | + test "initializes with all capabilities from hash" do |
| 149 | + capabilities = MCP::Server::Capabilities.new( |
| 150 | + completions: {}, |
| 151 | + experimental: { customFeature: true }, |
| 152 | + logging: {}, |
| 153 | + prompts: { listChanged: true }, |
| 154 | + resources: { listChanged: true, subscribe: true }, |
| 155 | + tools: { listChanged: true }, |
| 156 | + ) |
| 157 | + |
| 158 | + expected = { |
| 159 | + completions: {}, |
| 160 | + experimental: { customFeature: true }, |
| 161 | + logging: {}, |
| 162 | + prompts: { listChanged: true }, |
| 163 | + resources: { listChanged: true, subscribe: true }, |
| 164 | + tools: { listChanged: true }, |
| 165 | + } |
| 166 | + |
| 167 | + assert_equal(expected, capabilities.to_h) |
| 168 | + end |
| 169 | + |
| 170 | + test "handles nil values in capabilities hash gracefully" do |
| 171 | + capabilities = MCP::Server::Capabilities.new( |
| 172 | + prompts: nil, |
| 173 | + resources: nil, |
| 174 | + tools: nil, |
| 175 | + ) |
| 176 | + |
| 177 | + assert_equal({ prompts: {}, resources: {}, tools: {} }, capabilities.to_h) |
| 178 | + end |
| 179 | + |
| 180 | + test "to_h returns empty hash when no capabilities are set" do |
| 181 | + capabilities = MCP::Server::Capabilities.new |
| 182 | + |
| 183 | + assert_empty(capabilities.to_h) |
| 184 | + end |
| 185 | + |
| 186 | + test "calling support methods multiple times does not duplicate entries" do |
| 187 | + capabilities = MCP::Server::Capabilities.new |
| 188 | + |
| 189 | + capabilities.support_prompts |
| 190 | + capabilities.support_prompts |
| 191 | + capabilities.support_prompts_list_changed |
| 192 | + |
| 193 | + assert_equal({ prompts: { listChanged: true } }, capabilities.to_h) |
| 194 | + end |
| 195 | + end |
| 196 | + end |
| 197 | +end |
0 commit comments