|
5 | 5 |
|
6 | 6 | module MCP |
7 | 7 | class MethodsTest < ActiveSupport::TestCase |
8 | | - test "ensure_capability! for tools/list method raises an error if tools capability is not present" do |
9 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
10 | | - Methods.ensure_capability!(Methods::TOOLS_LIST, {}) |
| 8 | + class << self |
| 9 | + def ensure_capability_raises_error_for(method, required_capability_name:, capabilities: {}) |
| 10 | + test("ensure_capability! for #{method} raises an error if #{required_capability_name} capability is not present") do |
| 11 | + error = assert_raises(Methods::MissingRequiredCapabilityError) do |
| 12 | + Methods.ensure_capability!(method, capabilities) |
| 13 | + end |
| 14 | + assert_equal("Server does not support #{required_capability_name} (required for #{method})", error.message) |
| 15 | + end |
11 | 16 | end |
12 | | - assert_equal "Server does not support tools (required for tools/list)", error.message |
13 | | - end |
14 | 17 |
|
15 | | - test "ensure_capability! for sampling/createMessage raises an error if sampling capability is not present" do |
16 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
17 | | - Methods.ensure_capability!(Methods::SAMPLING_CREATE_MESSAGE, {}) |
| 18 | + def ensure_capability_does_not_raise_for(method, capabilities: {}) |
| 19 | + test("ensure_capability! does not raise for #{method}") do |
| 20 | + assert_nothing_raised { Methods.ensure_capability!(method, capabilities) } |
| 21 | + end |
18 | 22 | end |
19 | | - assert_equal "Server does not support sampling (required for sampling/createMessage)", error.message |
20 | 23 | end |
21 | 24 |
|
22 | | - test "ensure_capability! for completion/complete raises an error if completions capability is not present" do |
23 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
24 | | - Methods.ensure_capability!(Methods::COMPLETION_COMPLETE, {}) |
25 | | - end |
26 | | - assert_equal "Server does not support completions (required for completion/complete)", error.message |
27 | | - end |
| 25 | + # Server methods and notifications |
| 26 | + ensure_capability_does_not_raise_for Methods::INITIALIZE |
28 | 27 |
|
29 | | - test "ensure_capability! for logging/setLevel raises an error if logging capability is not present" do |
30 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
31 | | - Methods.ensure_capability!(Methods::LOGGING_SET_LEVEL, {}) |
32 | | - end |
33 | | - assert_equal "Server does not support logging (required for logging/setLevel)", error.message |
34 | | - end |
| 28 | + ensure_capability_raises_error_for Methods::PROMPTS_LIST, required_capability_name: "prompts" |
| 29 | + ensure_capability_raises_error_for Methods::PROMPTS_GET, required_capability_name: "prompts" |
| 30 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED, required_capability_name: "prompts" |
| 31 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED, |
| 32 | + required_capability_name: "prompts.listChanged", |
| 33 | + capabilities: { prompts: {} } |
35 | 34 |
|
36 | | - test "ensure_capability! for prompts/get and prompts/list raise an error if prompts capability is not present" do |
37 | | - [Methods::PROMPTS_GET, Methods::PROMPTS_LIST].each do |method| |
38 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
39 | | - Methods.ensure_capability!(method, {}) |
40 | | - end |
41 | | - assert_equal "Server does not support prompts (required for #{method})", error.message |
42 | | - end |
43 | | - end |
| 35 | + ensure_capability_raises_error_for Methods::RESOURCES_LIST, required_capability_name: "resources" |
| 36 | + ensure_capability_raises_error_for Methods::RESOURCES_READ, required_capability_name: "resources" |
| 37 | + ensure_capability_raises_error_for Methods::RESOURCES_TEMPLATES_LIST, required_capability_name: "resources" |
| 38 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED, required_capability_name: "resources" |
| 39 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED, |
| 40 | + required_capability_name: "resources.listChanged", |
| 41 | + capabilities: { resources: {} } |
| 42 | + ensure_capability_raises_error_for Methods::RESOURCES_SUBSCRIBE, required_capability_name: "resources" |
| 43 | + ensure_capability_raises_error_for Methods::RESOURCES_SUBSCRIBE, |
| 44 | + required_capability_name: "resources.subscribe", |
| 45 | + capabilities: { resources: {} } |
| 46 | + ensure_capability_raises_error_for Methods::RESOURCES_UNSUBSCRIBE, required_capability_name: "resources" |
| 47 | + ensure_capability_raises_error_for Methods::RESOURCES_UNSUBSCRIBE, |
| 48 | + required_capability_name: "resources.subscribe", |
| 49 | + capabilities: { resources: {} } |
| 50 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_RESOURCES_UPDATED, required_capability_name: "resources" |
| 51 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_RESOURCES_UPDATED, |
| 52 | + required_capability_name: "resources.subscribe", |
| 53 | + capabilities: { resources: {} } |
44 | 54 |
|
45 | | - test "ensure_capability! for resources/list, resources/templates/list, resources/read raise an error if resources capability is not present" do |
46 | | - [Methods::RESOURCES_LIST, Methods::RESOURCES_TEMPLATES_LIST, Methods::RESOURCES_READ].each do |method| |
47 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
48 | | - Methods.ensure_capability!(method, {}) |
49 | | - end |
50 | | - assert_equal "Server does not support resources (required for #{method})", error.message |
51 | | - end |
52 | | - end |
| 55 | + ensure_capability_raises_error_for Methods::TOOLS_LIST, required_capability_name: "tools" |
| 56 | + ensure_capability_raises_error_for Methods::TOOLS_CALL, required_capability_name: "tools" |
| 57 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED, required_capability_name: "tools" |
| 58 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED, |
| 59 | + required_capability_name: "tools.listChanged", |
| 60 | + capabilities: { tools: {} } |
53 | 61 |
|
54 | | - test "ensure_capability! for tools/call and tools/list raise an error if tools capability is not present" do |
55 | | - [Methods::TOOLS_CALL, Methods::TOOLS_LIST].each do |method| |
56 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
57 | | - Methods.ensure_capability!(method, {}) |
58 | | - end |
59 | | - assert_equal "Server does not support tools (required for #{method})", error.message |
60 | | - end |
61 | | - end |
| 62 | + ensure_capability_raises_error_for Methods::LOGGING_SET_LEVEL, required_capability_name: "logging" |
| 63 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_MESSAGE, required_capability_name: "logging" |
62 | 64 |
|
63 | | - test "ensure_capability! for resources/subscribe raises an error if resources subscribe capability is not present" do |
64 | | - error = assert_raises(Methods::MissingRequiredCapabilityError) do |
65 | | - Methods.ensure_capability!(Methods::RESOURCES_SUBSCRIBE, { resources: {} }) |
66 | | - end |
67 | | - assert_equal "Server does not support resources_subscribe (required for resources/subscribe)", error.message |
68 | | - end |
| 65 | + ensure_capability_raises_error_for Methods::COMPLETION_COMPLETE, required_capability_name: "completions" |
69 | 66 |
|
70 | | - test "ensure_capability! does not raise for ping and initialize methods" do |
71 | | - assert_nothing_raised { Methods.ensure_capability!(Methods::PING, {}) } |
72 | | - assert_nothing_raised { Methods.ensure_capability!(Methods::INITIALIZE, {}) } |
73 | | - end |
| 67 | + # Client methods and notifications |
| 68 | + ensure_capability_does_not_raise_for Methods::NOTIFICATIONS_INITIALIZED |
| 69 | + |
| 70 | + ensure_capability_raises_error_for Methods::ROOTS_LIST, required_capability_name: "roots" |
| 71 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED, required_capability_name: "roots" |
| 72 | + ensure_capability_raises_error_for Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED, |
| 73 | + required_capability_name: "roots.listChanged", |
| 74 | + capabilities: { roots: {} } |
| 75 | + |
| 76 | + ensure_capability_raises_error_for Methods::SAMPLING_CREATE_MESSAGE, required_capability_name: "sampling" |
| 77 | + |
| 78 | + # Methods and notifications of both server and client |
| 79 | + ensure_capability_does_not_raise_for Methods::PING |
| 80 | + ensure_capability_does_not_raise_for Methods::NOTIFICATIONS_PROGRESS |
| 81 | + ensure_capability_does_not_raise_for Methods::NOTIFICATIONS_CANCELLED |
74 | 82 | end |
75 | 83 | end |
0 commit comments