actionmcp 0.201.3 → 0.202.0
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 +4 -4
- data/README.md +40 -0
- data/app/controllers/action_mcp/application_controller.rb +21 -36
- data/app/jobs/action_mcp/tool_execution_job.rb +2 -16
- data/app/models/action_mcp/session.rb +1 -233
- data/lib/action_mcp/callbacks.rb +22 -115
- data/lib/action_mcp/capability.rb +44 -0
- data/lib/action_mcp/configuration.rb +26 -14
- data/lib/action_mcp/content/audio.rb +3 -21
- data/lib/action_mcp/content/base.rb +7 -0
- data/lib/action_mcp/content/binary.rb +30 -0
- data/lib/action_mcp/content/image.rb +3 -21
- data/lib/action_mcp/content/validation.rb +37 -38
- data/lib/action_mcp/content.rb +0 -33
- data/lib/action_mcp/custom_methods.rb +61 -0
- data/lib/action_mcp/dev_reloader.rb +19 -0
- data/lib/action_mcp/lifecycle_callbacks.rb +28 -0
- data/lib/action_mcp/log_subscriber.rb +15 -21
- data/lib/action_mcp/output_schema_builder.rb +18 -31
- data/lib/action_mcp/prompt.rb +8 -59
- data/lib/action_mcp/request_meta.rb +11 -0
- data/lib/action_mcp/resource_callbacks.rb +22 -149
- data/lib/action_mcp/resource_response.rb +3 -7
- data/lib/action_mcp/resource_template.rb +1 -0
- data/lib/action_mcp/server/base_session.rb +7 -173
- data/lib/action_mcp/server/handlers/dispatching.rb +38 -0
- data/lib/action_mcp/server/handlers/prompt_handler.rb +6 -12
- data/lib/action_mcp/server/handlers/resource_handler.rb +6 -23
- data/lib/action_mcp/server/handlers/task_handler.rb +14 -31
- data/lib/action_mcp/server/handlers/tool_handler.rb +4 -11
- data/lib/action_mcp/server/json_rpc_handler.rb +17 -1
- data/lib/action_mcp/server/prompts.rb +8 -17
- data/lib/action_mcp/server/resources.rb +4 -28
- data/lib/action_mcp/server/result_response.rb +19 -0
- data/lib/action_mcp/server/sampling_request.rb +26 -43
- data/lib/action_mcp/server/session_registries.rb +127 -0
- data/lib/action_mcp/server/tools.rb +5 -26
- data/lib/action_mcp/tagged_stream_logging.rb +9 -5
- data/lib/action_mcp/tool.rb +24 -60
- data/lib/action_mcp/version.rb +1 -1
- data/lib/tasks/action_mcp_tasks.rake +22 -44
- metadata +10 -3
- data/lib/action_mcp/server/handlers/router.rb +0 -33
|
@@ -7,43 +7,32 @@ module ActionMCP
|
|
|
7
7
|
# Tasks provide durable state machines for tracking async request execution
|
|
8
8
|
module TaskHandler
|
|
9
9
|
include ErrorAware
|
|
10
|
+
include Dispatching
|
|
11
|
+
|
|
12
|
+
GATED_TASK_METHODS = {
|
|
13
|
+
JsonRpcHandlerBase::Methods::TASKS_LIST => [ :list, "Task listing" ],
|
|
14
|
+
JsonRpcHandlerBase::Methods::TASKS_CANCEL => [ :cancel, "Task cancellation" ]
|
|
15
|
+
}.freeze
|
|
10
16
|
|
|
11
17
|
def process_tasks(rpc_method, id, params)
|
|
12
18
|
params ||= {}
|
|
13
19
|
|
|
14
20
|
with_error_handling(id) do
|
|
15
|
-
|
|
16
|
-
raise JSON_RPC::JsonRpcError.new(:invalid_params, message: "Task params must be an object")
|
|
17
|
-
end
|
|
21
|
+
require_params_object!(params, "Task")
|
|
18
22
|
|
|
19
23
|
unless transport.session.protocol_version == "2025-11-25"
|
|
20
|
-
|
|
21
|
-
message: "Tasks are only available in MCP 2025-11-25")
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
task_capabilities = negotiated_task_capabilities
|
|
25
|
-
unless task_capabilities
|
|
26
|
-
raise JSON_RPC::JsonRpcError.new(:method_not_found,
|
|
27
|
-
message: "Tasks are not available for this session")
|
|
24
|
+
method_not_available!("Tasks are only available in MCP 2025-11-25")
|
|
28
25
|
end
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
message: "Task listing is not available for this session")
|
|
33
|
-
end
|
|
27
|
+
task_capabilities = negotiated_capabilities(:tasks)
|
|
28
|
+
method_not_available!("Tasks are not available for this session") unless task_capabilities
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
capability, feature = GATED_TASK_METHODS[rpc_method]
|
|
31
|
+
if capability && !task_capabilities.key?(capability)
|
|
32
|
+
method_not_available!("#{feature} is not available for this session")
|
|
38
33
|
end
|
|
39
34
|
|
|
40
|
-
|
|
41
|
-
if handler
|
|
42
|
-
send(handler, id, params)
|
|
43
|
-
else
|
|
44
|
-
Rails.logger.warn("Unknown tasks method: #{rpc_method}")
|
|
45
|
-
raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Unknown tasks method: #{rpc_method}")
|
|
46
|
-
end
|
|
35
|
+
dispatch_rpc(task_method_handlers, "tasks", rpc_method, id, params)
|
|
47
36
|
end
|
|
48
37
|
end
|
|
49
38
|
|
|
@@ -58,12 +47,6 @@ module ActionMCP
|
|
|
58
47
|
}
|
|
59
48
|
end
|
|
60
49
|
|
|
61
|
-
def negotiated_task_capabilities
|
|
62
|
-
capabilities = (transport.session.server_capabilities || {}).with_indifferent_access
|
|
63
|
-
task_capabilities = capabilities[:tasks]
|
|
64
|
-
task_capabilities.with_indifferent_access if task_capabilities.is_a?(Hash)
|
|
65
|
-
end
|
|
66
|
-
|
|
67
50
|
def handle_tasks_get(id, params)
|
|
68
51
|
task_id = validate_required_param(params, "taskId", "Task ID is required")
|
|
69
52
|
task = find_task_or_error(id, task_id)
|
|
@@ -5,22 +5,15 @@ module ActionMCP
|
|
|
5
5
|
module Handlers
|
|
6
6
|
module ToolHandler
|
|
7
7
|
include ErrorAware
|
|
8
|
+
include Dispatching
|
|
8
9
|
|
|
9
10
|
def process_tools(rpc_method, id, params)
|
|
10
11
|
params ||= {}
|
|
11
12
|
|
|
12
13
|
with_error_handling(id) do
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
handler = tool_method_handlers[rpc_method]
|
|
18
|
-
if handler
|
|
19
|
-
send(handler, id, params)
|
|
20
|
-
else
|
|
21
|
-
Rails.logger.warn("Unknown tools method: #{rpc_method}")
|
|
22
|
-
raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Unknown tools method: #{rpc_method}")
|
|
23
|
-
end
|
|
14
|
+
require_params_object!(params, "Tool")
|
|
15
|
+
|
|
16
|
+
dispatch_rpc(tool_method_handlers, "tools", rpc_method, id, params)
|
|
24
17
|
end
|
|
25
18
|
end
|
|
26
19
|
|
|
@@ -62,10 +62,26 @@ module ActionMCP
|
|
|
62
62
|
when Methods::LOGGING_SET_LEVEL
|
|
63
63
|
handle_logging_set_level(id, params)
|
|
64
64
|
else
|
|
65
|
-
|
|
65
|
+
process_custom_method(rpc_method, id, params)
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
def process_custom_method(rpc_method, id, params)
|
|
70
|
+
handler = ActionMCP.configuration.custom_methods[rpc_method]
|
|
71
|
+
raise JSON_RPC::JsonRpcError.new(:method_not_found, message: "Method not found: #{rpc_method}") unless handler
|
|
72
|
+
|
|
73
|
+
result = begin
|
|
74
|
+
handler.call(params || {}, transport.session)
|
|
75
|
+
rescue JSON_RPC::JsonRpcError
|
|
76
|
+
raise
|
|
77
|
+
rescue StandardError => e
|
|
78
|
+
Rails.error.report(e, handled: true, severity: :error, context: { rpc_method: rpc_method })
|
|
79
|
+
raise JSON_RPC::JsonRpcError.new(:internal_error, message: "Error while handling #{rpc_method}")
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
transport.send_jsonrpc_response(id, result: result)
|
|
83
|
+
end
|
|
84
|
+
|
|
69
85
|
def handle_initialize(id, params)
|
|
70
86
|
transport.send_capabilities(id, params)
|
|
71
87
|
end
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
module ActionMCP
|
|
4
4
|
module Server
|
|
5
5
|
module Prompts
|
|
6
|
+
include ResultResponse
|
|
7
|
+
|
|
6
8
|
def send_prompts_list(request_id, params = {})
|
|
7
9
|
page, next_cursor = paginate(session.registered_prompts, cursor: params["cursor"])
|
|
8
10
|
|
|
@@ -14,29 +16,18 @@ module ActionMCP
|
|
|
14
16
|
send_jsonrpc_error(request_id, :invalid_params, e.message)
|
|
15
17
|
end
|
|
16
18
|
|
|
17
|
-
def send_prompts_get(request_id, prompt_name, params)
|
|
19
|
+
def send_prompts_get(request_id, prompt_name, params, _meta = {})
|
|
18
20
|
# Find prompt in session's registry
|
|
19
21
|
prompt_class = session.registered_prompts.find { |p| p.prompt_name == prompt_name }
|
|
20
22
|
|
|
21
23
|
if prompt_class
|
|
22
24
|
# Create prompt and set execution context
|
|
23
25
|
prompt = prompt_class.new(params)
|
|
24
|
-
prompt.with_context({ session: session })
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
prompt.call
|
|
30
|
-
end
|
|
31
|
-
else
|
|
32
|
-
prompt.call
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
if result.is_error
|
|
36
|
-
send_jsonrpc_response(request_id, error: result.to_h)
|
|
37
|
-
else
|
|
38
|
-
send_jsonrpc_response(request_id, result: result.to_h)
|
|
39
|
-
end
|
|
26
|
+
prompt.with_context({ session: session, request: { params: { name: prompt_name, _meta: _meta } } })
|
|
27
|
+
|
|
28
|
+
result = DevReloader.wrap { prompt.call }
|
|
29
|
+
|
|
30
|
+
send_result_response(request_id, result)
|
|
40
31
|
else
|
|
41
32
|
send_jsonrpc_error(request_id, :invalid_params, "Prompt '#{prompt_name}' not available in this session")
|
|
42
33
|
end
|
|
@@ -48,15 +48,7 @@ module ActionMCP
|
|
|
48
48
|
uri = params["uri"]
|
|
49
49
|
template = find_registered_template_for_uri(uri)
|
|
50
50
|
|
|
51
|
-
unless template
|
|
52
|
-
error = {
|
|
53
|
-
code: -32_002,
|
|
54
|
-
message: "Resource not found",
|
|
55
|
-
data: { uri: uri }
|
|
56
|
-
}
|
|
57
|
-
send_jsonrpc_response(id, error: error)
|
|
58
|
-
return
|
|
59
|
-
end
|
|
51
|
+
return send_jsonrpc_response(id, error: resource_not_found_error(uri)) unless template
|
|
60
52
|
|
|
61
53
|
# Check if resource requires consent and if consent is granted
|
|
62
54
|
if template.respond_to?(:requires_consent?) && template.requires_consent? && !session.consent_granted_for?("resource:#{template.name}")
|
|
@@ -68,30 +60,14 @@ module ActionMCP
|
|
|
68
60
|
return
|
|
69
61
|
end
|
|
70
62
|
|
|
71
|
-
unless template.readable_uri?(uri)
|
|
72
|
-
error = {
|
|
73
|
-
code: -32_002,
|
|
74
|
-
message: "Resource not found",
|
|
75
|
-
data: { uri: uri }
|
|
76
|
-
}
|
|
77
|
-
send_jsonrpc_response(id, error: error)
|
|
78
|
-
return
|
|
79
|
-
end
|
|
63
|
+
return send_jsonrpc_response(id, error: resource_not_found_error(uri)) unless template.readable_uri?(uri)
|
|
80
64
|
|
|
81
65
|
begin
|
|
82
66
|
# Create template instance and set execution context
|
|
83
67
|
record = template.process(uri)
|
|
84
|
-
unless record
|
|
85
|
-
error = {
|
|
86
|
-
code: -32_002,
|
|
87
|
-
message: "Resource not found",
|
|
88
|
-
data: { uri: uri }
|
|
89
|
-
}
|
|
90
|
-
send_jsonrpc_response(id, error: error)
|
|
91
|
-
return
|
|
92
|
-
end
|
|
68
|
+
return send_jsonrpc_response(id, error: resource_not_found_error(uri)) unless record
|
|
93
69
|
|
|
94
|
-
record.with_context({ session: session })
|
|
70
|
+
record.with_context({ session: session, request: { params: { uri: uri, _meta: params["_meta"] || {} } } })
|
|
95
71
|
|
|
96
72
|
response = record.call
|
|
97
73
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActionMCP
|
|
4
|
+
module Server
|
|
5
|
+
# Sends a prompt or tool result as a JSON-RPC error when it is a protocol error,
|
|
6
|
+
# otherwise as the request result.
|
|
7
|
+
module ResultResponse
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def send_result_response(request_id, result)
|
|
11
|
+
if result.is_error
|
|
12
|
+
send_jsonrpc_response(request_id, error: result.to_h)
|
|
13
|
+
else
|
|
14
|
+
send_jsonrpc_response(request_id, result: result.to_h)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -38,7 +38,7 @@ module ActionMCP
|
|
|
38
38
|
"properties" => {
|
|
39
39
|
"hints" => {
|
|
40
40
|
"type" => "array",
|
|
41
|
-
"items" =>
|
|
41
|
+
"items" => Content::Validation.ref("ModelHint")
|
|
42
42
|
},
|
|
43
43
|
"costPriority" => { "type" => "number", "minimum" => 0, "maximum" => 1 },
|
|
44
44
|
"speedPriority" => { "type" => "number", "minimum" => 0, "maximum" => 1 },
|
|
@@ -68,13 +68,13 @@ module ActionMCP
|
|
|
68
68
|
"type" => "object",
|
|
69
69
|
"required" => %w[inputSchema name],
|
|
70
70
|
"properties" => Content::Validation::META_PROPERTY.merge(
|
|
71
|
-
"annotations" =>
|
|
71
|
+
"annotations" => Content::Validation.ref("ToolAnnotations"),
|
|
72
72
|
"description" => { "type" => "string" },
|
|
73
|
-
"execution" =>
|
|
74
|
-
"icons" => { "type" => "array", "items" =>
|
|
75
|
-
"inputSchema" =>
|
|
73
|
+
"execution" => Content::Validation.ref("ToolExecution"),
|
|
74
|
+
"icons" => { "type" => "array", "items" => Content::Validation.ref("Icon") },
|
|
75
|
+
"inputSchema" => Content::Validation.ref("ToolSchema"),
|
|
76
76
|
"name" => { "type" => "string" },
|
|
77
|
-
"outputSchema" =>
|
|
77
|
+
"outputSchema" => Content::Validation.ref("ToolSchema"),
|
|
78
78
|
"title" => { "type" => "string" }
|
|
79
79
|
)
|
|
80
80
|
},
|
|
@@ -104,7 +104,7 @@ module ActionMCP
|
|
|
104
104
|
"properties" => Content::Validation::META_PROPERTY.merge(
|
|
105
105
|
"content" => {
|
|
106
106
|
"type" => "array",
|
|
107
|
-
"items" =>
|
|
107
|
+
"items" => Content::Validation.ref("ContentBlock")
|
|
108
108
|
},
|
|
109
109
|
"isError" => { "type" => "boolean" },
|
|
110
110
|
"structuredContent" => { "type" => "object" },
|
|
@@ -113,9 +113,7 @@ module ActionMCP
|
|
|
113
113
|
)
|
|
114
114
|
},
|
|
115
115
|
"SamplingMessageContentBlock" => {
|
|
116
|
-
"anyOf" =>
|
|
117
|
-
{ "$ref" => "#/$defs/#{name}" }
|
|
118
|
-
end
|
|
116
|
+
"anyOf" => Content::Validation.refs(*%w[TextContent ImageContent AudioContent ToolUseContent ToolResultContent])
|
|
119
117
|
},
|
|
120
118
|
"SamplingMessage" => {
|
|
121
119
|
"type" => "object",
|
|
@@ -123,14 +121,14 @@ module ActionMCP
|
|
|
123
121
|
"properties" => Content::Validation::META_PROPERTY.merge(
|
|
124
122
|
"content" => {
|
|
125
123
|
"anyOf" => [
|
|
126
|
-
|
|
124
|
+
Content::Validation.ref("SamplingMessageContentBlock"),
|
|
127
125
|
{
|
|
128
126
|
"type" => "array",
|
|
129
|
-
"items" =>
|
|
127
|
+
"items" => Content::Validation.ref("SamplingMessageContentBlock")
|
|
130
128
|
}
|
|
131
129
|
]
|
|
132
130
|
},
|
|
133
|
-
"role" =>
|
|
131
|
+
"role" => Content::Validation.ref("Role")
|
|
134
132
|
)
|
|
135
133
|
},
|
|
136
134
|
"CreateMessageRequestParams" => {
|
|
@@ -140,7 +138,7 @@ module ActionMCP
|
|
|
140
138
|
"_meta" => {
|
|
141
139
|
"type" => "object",
|
|
142
140
|
"properties" => {
|
|
143
|
-
"progressToken" =>
|
|
141
|
+
"progressToken" => Content::Validation.ref("ProgressToken")
|
|
144
142
|
}
|
|
145
143
|
},
|
|
146
144
|
"includeContext" => {
|
|
@@ -150,16 +148,16 @@ module ActionMCP
|
|
|
150
148
|
"maxTokens" => { "type" => "integer" },
|
|
151
149
|
"messages" => {
|
|
152
150
|
"type" => "array",
|
|
153
|
-
"items" =>
|
|
151
|
+
"items" => Content::Validation.ref("SamplingMessage")
|
|
154
152
|
},
|
|
155
153
|
"metadata" => { "type" => "object" },
|
|
156
|
-
"modelPreferences" =>
|
|
154
|
+
"modelPreferences" => Content::Validation.ref("ModelPreferences"),
|
|
157
155
|
"stopSequences" => { "type" => "array", "items" => { "type" => "string" } },
|
|
158
156
|
"systemPrompt" => { "type" => "string" },
|
|
159
|
-
"task" =>
|
|
157
|
+
"task" => Content::Validation.ref("TaskMetadata"),
|
|
160
158
|
"temperature" => { "type" => "number" },
|
|
161
|
-
"toolChoice" =>
|
|
162
|
-
"tools" => { "type" => "array", "items" =>
|
|
159
|
+
"toolChoice" => Content::Validation.ref("ToolChoice"),
|
|
160
|
+
"tools" => { "type" => "array", "items" => Content::Validation.ref("Tool") }
|
|
163
161
|
}
|
|
164
162
|
}
|
|
165
163
|
).freeze
|
|
@@ -184,35 +182,20 @@ module ActionMCP
|
|
|
184
182
|
@default_messages ||= []
|
|
185
183
|
end
|
|
186
184
|
|
|
187
|
-
def system_prompt(value = UNSET)
|
|
188
|
-
@default_system_prompt = value unless value.equal?(UNSET)
|
|
189
|
-
@default_system_prompt
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
def include_context(value = UNSET)
|
|
193
|
-
@default_context = value unless value.equal?(UNSET)
|
|
194
|
-
@default_context
|
|
195
|
-
end
|
|
196
|
-
|
|
197
185
|
def model_hints(value = UNSET)
|
|
198
186
|
@default_model_hints = value unless value.equal?(UNSET)
|
|
199
187
|
@default_model_hints ||= []
|
|
200
188
|
end
|
|
201
189
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
def temperature(value = UNSET)
|
|
213
|
-
@default_temperature = value unless value.equal?(UNSET)
|
|
214
|
-
@default_temperature.nil? ? 0.7 : @default_temperature
|
|
215
|
-
end
|
|
190
|
+
# Class-level defaults: called with a value to set it, without one to read it.
|
|
191
|
+
{ system_prompt: nil, include_context: nil, intelligence_priority: 0.9, max_tokens: 500, temperature: 0.7 }
|
|
192
|
+
.each do |name, fallback|
|
|
193
|
+
define_method(name) do |value = UNSET|
|
|
194
|
+
instance_variable_set(:"@default_#{name}", value) unless value.equal?(UNSET)
|
|
195
|
+
stored = instance_variable_get(:"@default_#{name}")
|
|
196
|
+
stored.nil? ? fallback : stored
|
|
197
|
+
end
|
|
198
|
+
end
|
|
216
199
|
|
|
217
200
|
def validate_params!(params)
|
|
218
201
|
raise ArgumentError, "Sampling request params must be an object" unless params.is_a?(Hash)
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActionMCP
|
|
4
|
+
module Server
|
|
5
|
+
# Per-session tool, prompt and resource-template registries shared by the
|
|
6
|
+
# in-memory BaseSession and the ActiveRecord Session. A registry of ["*"]
|
|
7
|
+
# exposes everything the active profile allows.
|
|
8
|
+
module SessionRegistries
|
|
9
|
+
Kind = Data.define(:registry, :lookup, :name_method, :filtered, :list)
|
|
10
|
+
|
|
11
|
+
KINDS = {
|
|
12
|
+
tool: Kind.new(:tool_registry, "ActionMCP::ToolsRegistry", :tool_name, :filtered_tools, "tools"),
|
|
13
|
+
prompt: Kind.new(:prompt_registry, "ActionMCP::PromptsRegistry", :prompt_name, :filtered_prompts, "prompts"),
|
|
14
|
+
resource_template: Kind.new(:resource_registry, "ActionMCP::ResourceTemplatesRegistry", :capability_name,
|
|
15
|
+
:filtered_resources, "resources")
|
|
16
|
+
}.freeze
|
|
17
|
+
|
|
18
|
+
WILDCARD = [ "*" ].freeze
|
|
19
|
+
|
|
20
|
+
def initialize!
|
|
21
|
+
return false unless status == "initializing" && !initialized?
|
|
22
|
+
|
|
23
|
+
self.initialized = true
|
|
24
|
+
self.status = "initialized"
|
|
25
|
+
save
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def send_progress_notification(progressToken:, progress:, total: nil, message: nil)
|
|
29
|
+
ActionMCP::Server::TransportHandler.new(self).send_progress_notification(
|
|
30
|
+
progressToken: progressToken, progress: progress, total: total, message: message
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
{ tool: "tools", prompt: "prompts", resource_template: "resource_templates" }.each do |kind, plural|
|
|
35
|
+
define_method(:"register_#{kind}") { |class_or_name| register_capability(kind, class_or_name) }
|
|
36
|
+
define_method(:"unregister_#{kind}") { |class_or_name| unregister_capability(kind, class_or_name) }
|
|
37
|
+
define_method(:"registered_#{plural}") { registered_capabilities(kind) }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
{ tools: :tool, prompts: :prompt, resources: :resource_template }.each do |suffix, kind|
|
|
41
|
+
define_method(:"uses_all_#{suffix}?") { uses_all?(kind) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
KINDS.each_value do |kind|
|
|
47
|
+
define_method(:"send_#{kind.list}_list_changed_notification") do
|
|
48
|
+
return unless server_capabilities.dig(kind.list, "listChanged")
|
|
49
|
+
|
|
50
|
+
write(JSON_RPC::Notification.new(method: "notifications/#{kind.list}/list_changed"))
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def register_capability(kind, class_or_name)
|
|
55
|
+
name = normalize_name(class_or_name, kind)
|
|
56
|
+
return false unless capability_exists?(kind, name)
|
|
57
|
+
return true if uses_all?(kind)
|
|
58
|
+
|
|
59
|
+
names = registry_names(kind)
|
|
60
|
+
unless names.include?(name)
|
|
61
|
+
names << name
|
|
62
|
+
save!
|
|
63
|
+
send("send_#{KINDS[kind].list}_list_changed_notification")
|
|
64
|
+
end
|
|
65
|
+
true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def unregister_capability(kind, class_or_name)
|
|
69
|
+
name = normalize_name(class_or_name, kind)
|
|
70
|
+
|
|
71
|
+
if uses_all?(kind)
|
|
72
|
+
return unless capability_exists?(kind, name)
|
|
73
|
+
|
|
74
|
+
expand_registry!(kind)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
return unless registry_names(kind).delete(name)
|
|
78
|
+
|
|
79
|
+
save!
|
|
80
|
+
send("send_#{KINDS[kind].list}_list_changed_notification")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def registered_capabilities(kind)
|
|
84
|
+
return ActionMCP.configuration.public_send(KINDS[kind].filtered).map(&:klass) if uses_all?(kind)
|
|
85
|
+
|
|
86
|
+
(public_send(KINDS[kind].registry) || []).filter_map do |name|
|
|
87
|
+
registry_for(kind).find(name)
|
|
88
|
+
rescue StandardError
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def uses_all?(kind)
|
|
94
|
+
public_send(KINDS[kind].registry) == WILDCARD
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def registry_names(kind)
|
|
98
|
+
public_send(KINDS[kind].registry) || public_send(:"#{KINDS[kind].registry}=", [])
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Replaces the wildcard with the explicit names it stood for, so one entry can be removed.
|
|
102
|
+
def expand_registry!(kind)
|
|
103
|
+
names = ActionMCP.configuration.public_send(KINDS[kind].filtered).map(&:name)
|
|
104
|
+
public_send(:"#{KINDS[kind].registry}=", names)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def capability_exists?(kind, name)
|
|
108
|
+
registry_for(kind).find(name)
|
|
109
|
+
true
|
|
110
|
+
rescue ActionMCP::RegistryBase::NotFound
|
|
111
|
+
false
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def registry_for(kind)
|
|
115
|
+
KINDS[kind].lookup.constantize
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def normalize_name(class_or_name, kind)
|
|
119
|
+
case class_or_name
|
|
120
|
+
when String then class_or_name
|
|
121
|
+
when Class then class_or_name.public_send(KINDS[kind].name_method)
|
|
122
|
+
else raise ArgumentError, "Expected String or Class, got #{class_or_name.class}"
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
module ActionMCP
|
|
4
4
|
module Server
|
|
5
5
|
module Tools
|
|
6
|
+
include ResultResponse
|
|
7
|
+
|
|
6
8
|
def send_tools_list(request_id, params = {})
|
|
7
9
|
protocol_version = session.protocol_version
|
|
8
10
|
progress_token = params.dig("_meta", "progressToken")
|
|
@@ -100,33 +102,10 @@ module ActionMCP
|
|
|
100
102
|
}
|
|
101
103
|
})
|
|
102
104
|
|
|
103
|
-
|
|
104
|
-
result = if Rails.env.development?
|
|
105
|
-
# Preserve Current attributes across reloader boundary
|
|
106
|
-
current_user = ActionMCP::Current.user
|
|
107
|
-
current_gateway = ActionMCP::Current.gateway
|
|
108
|
-
current_session_data = ActionMCP::Current.session_data
|
|
109
|
-
|
|
110
|
-
Rails.application.reloader.wrap do
|
|
111
|
-
# Restore Current attributes inside reloader
|
|
112
|
-
ActionMCP::Current.user = current_user
|
|
113
|
-
ActionMCP::Current.gateway = current_gateway
|
|
114
|
-
ActionMCP::Current.session_data = current_session_data
|
|
115
|
-
tool.call
|
|
116
|
-
end
|
|
117
|
-
else
|
|
118
|
-
tool.call
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
result = ToolResult.normalize(result)
|
|
105
|
+
result = DevReloader.wrap { tool.call }
|
|
122
106
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
send_jsonrpc_response(request_id, error: result.to_h)
|
|
126
|
-
else
|
|
127
|
-
# Success OR tool execution error - both are valid JSON-RPC responses
|
|
128
|
-
send_jsonrpc_response(request_id, result: result.to_h)
|
|
129
|
-
end
|
|
107
|
+
# Tool execution errors travel as results; only protocol errors become JSON-RPC errors
|
|
108
|
+
send_result_response(request_id, ToolResult.normalize(result))
|
|
130
109
|
rescue ArgumentError => e
|
|
131
110
|
send_jsonrpc_response(request_id, result: ToolResult.execution_error(e.message).to_h)
|
|
132
111
|
rescue ActiveModel::UnknownAttributeError => e
|
|
@@ -12,28 +12,32 @@ module ActionMCP
|
|
|
12
12
|
# ——— Outbound: any frame we ‘write’ to the wire ———
|
|
13
13
|
def write_message(data)
|
|
14
14
|
pretty = json_normalise(data)
|
|
15
|
-
|
|
15
|
+
log_frame("TX", :debug, BLUE_TX, pretty)
|
|
16
16
|
super
|
|
17
17
|
rescue StandardError => e
|
|
18
|
-
|
|
18
|
+
log_frame("TX", :error, RED_FATAL, e.message)
|
|
19
19
|
raise
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
# ——— Inbound: every raw line handed to the JSON‑RPC handler ———
|
|
23
23
|
def read(line)
|
|
24
24
|
pretty = json_normalise(line)
|
|
25
|
-
|
|
25
|
+
log_frame("RX", :debug, GREEN_RX, pretty)
|
|
26
26
|
super
|
|
27
27
|
rescue MultiJSON::ParseError => e
|
|
28
|
-
|
|
28
|
+
log_frame("RX", :warn, YELLOW_ERR, "Bad JSON → #{e.message}")
|
|
29
29
|
raise
|
|
30
30
|
rescue StandardError => e
|
|
31
|
-
|
|
31
|
+
log_frame("RX", :error, RED_FATAL, e.message)
|
|
32
32
|
raise
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
private
|
|
36
36
|
|
|
37
|
+
def log_frame(direction, level, color, text)
|
|
38
|
+
log_with_tags("MCP", direction) { ActionMCP.logger.public_send(level, "#{color}#{text}#{CLR}") }
|
|
39
|
+
end
|
|
40
|
+
|
|
37
41
|
# Helper method to handle tagged logging across different logger types
|
|
38
42
|
def log_with_tags(*tags, &block)
|
|
39
43
|
if ActionMCP.logger.respond_to?(:tagged)
|