actionmcp 0.201.3 → 0.203.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 +51 -0
- data/app/controllers/action_mcp/application_controller.rb +21 -36
- data/app/jobs/action_mcp/tool_execution_job.rb +57 -27
- data/app/models/action_mcp/session/task.rb +2 -1
- 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/schema_context.rb +50 -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 +23 -33
- data/lib/action_mcp/server/transport_handler.rb +0 -6
- data/lib/action_mcp/tagged_stream_logging.rb +9 -5
- data/lib/action_mcp/tool.rb +67 -71
- data/lib/action_mcp/version.rb +1 -1
- data/lib/action_mcp.rb +4 -0
- data/lib/tasks/action_mcp_tasks.rake +22 -44
- metadata +11 -3
- data/lib/action_mcp/server/handlers/router.rb +0 -33
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d98b2f4f19143a122611fc330732bd719369c39a3b896db1f662e03d15c6d7dd
|
|
4
|
+
data.tar.gz: 77f6fa86b2e8435bd029009208eff74dbb7f10611cfb897be6785b503a34f23f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ebc4d34788ef4bdbf4a6e173396edd1df904643e89ef4401b65bae81e95559b8211ab453ea01701aee86c199d8f50ce1753f940cdf9feed281a8a02b5b886a51
|
|
7
|
+
data.tar.gz: cf4daa2a1b61f50e6e04f3256cda2c3aaf5ba84bb6cf883c8683284f8eccdec08f6fb722a099cf6da27a16705927ff888e95a1d4711adae2c31617ac70e341d8
|
data/README.md
CHANGED
|
@@ -186,6 +186,27 @@ class CalculateSumTool < ApplicationMCPTool
|
|
|
186
186
|
end
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
+
#### Schema Keywords
|
|
190
|
+
|
|
191
|
+
Extra keyword arguments on `property` and `collection` become JSON Schema keywords, and arguments are validated against that schema before `perform` runs. Write them in snake_case (`min_length`, `multiple_of`); they are emitted with their JSON Schema names (`minLength`, `multipleOf`). On a `collection`, `min_items`, `max_items` and `unique_items` describe the array; every other keyword describes each item:
|
|
192
|
+
|
|
193
|
+
```ruby
|
|
194
|
+
property :priority, type: "string", enum: %w[low normal high]
|
|
195
|
+
property :retries, type: "integer", minimum: 0, maximum: 5
|
|
196
|
+
collection :labels, type: "string", enum: %w[bug feature docs], min_items: 1, unique_items: true
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
A keyword given as a proc is resolved each time the schema is built, both for `tools/list` and for argument validation. The proc runs with `session`, `session_data`, `current_user` and `current_gateway` in scope, so each client sees and is held to its own values:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
property :tool, type: "string", required: true, enum: -> { current_user.allowed_tools }
|
|
203
|
+
collection :labels, type: "string", enum: -> { current_user.project.labels.pluck(:name) }, max_items: -> { session_data["label_limit"] }
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Tools without procs keep a cached validator. Outside a request (rake tasks), `session` and `current_user` are `nil`.
|
|
207
|
+
|
|
208
|
+
A proc that raises is reported to `Rails.error` as `ActionMCP::SchemaResolutionError`. That tool is left out of `tools/list` and answers `tools/call` like an unknown tool; every other tool is unaffected.
|
|
209
|
+
|
|
189
210
|
#### Consent Management
|
|
190
211
|
|
|
191
212
|
For tools that perform sensitive operations (file system access, database modifications, external API calls), you can require explicit user consent:
|
|
@@ -524,6 +545,21 @@ When set, responses include an opaque `nextCursor` when more items are available
|
|
|
524
545
|
|
|
525
546
|
`tasks/list` always paginates regardless of this setting (defaults to 50 per page, or `pagination_page_size` if configured).
|
|
526
547
|
|
|
548
|
+
### Custom JSON-RPC Methods
|
|
549
|
+
|
|
550
|
+
Vendor methods can be served next to the MCP core methods. Names take the form `vendor/method`; the MCP namespaces (`tools/`, `prompts/`, `resources/`, `notifications/`, ...) are reserved. The handler receives the request params and the session, and its return value becomes the JSON-RPC result:
|
|
551
|
+
|
|
552
|
+
```ruby
|
|
553
|
+
# config/initializers/action_mcp.rb
|
|
554
|
+
ActionMCP.configure do |config|
|
|
555
|
+
config.register_method("acme/toolsets/list") do |params, session|
|
|
556
|
+
{ toolsets: Toolset.visible_to(ActionMCP::Current.user).as_json }
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
Raise `JSON_RPC::JsonRpcError` to answer with a specific error code; any other exception is reported to `Rails.error` and answered with `internal_error`.
|
|
562
|
+
|
|
527
563
|
### Server Instructions
|
|
528
564
|
|
|
529
565
|
Server instructions help LLMs understand **what your server is for** and **when to use it**. They describe the server's purpose and goal, not technical details like rate limits or authentication (tools are self-documented via their own descriptions).
|
|
@@ -846,6 +882,21 @@ end
|
|
|
846
882
|
|
|
847
883
|
Tools access it via `session_data["user_id"]`. See [GATEWAY.md](GATEWAY.md) for details.
|
|
848
884
|
|
|
885
|
+
### Per-Request `_meta`
|
|
886
|
+
|
|
887
|
+
Tools, prompts and resource templates read the `_meta` object of the current `tools/call`, `prompts/get` or `resources/read` request through `request_meta` (an indifferent-access hash, empty when the client sent none):
|
|
888
|
+
|
|
889
|
+
```ruby
|
|
890
|
+
class OrdersTemplate < ApplicationMCPResTemplate
|
|
891
|
+
def resolve
|
|
892
|
+
trace_id = request_meta["example.com/trace"]
|
|
893
|
+
# ...
|
|
894
|
+
end
|
|
895
|
+
end
|
|
896
|
+
```
|
|
897
|
+
|
|
898
|
+
The MCP specification does not authenticate `_meta`. Use it as a lookup hint, and authorize from the session.
|
|
899
|
+
|
|
849
900
|
### 1. Create `mcp/config.ru`
|
|
850
901
|
|
|
851
902
|
The install generator (`rails generate action_mcp:install`) creates this automatically. If you need to create it manually:
|
|
@@ -285,45 +285,30 @@ module ActionMCP
|
|
|
285
285
|
|
|
286
286
|
# --- Error Rendering Methods ---
|
|
287
287
|
|
|
288
|
-
#
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
render json: { jsonrpc: "2.0", id: id, error: { code: -32_001, message: message } }, status: :not_found
|
|
304
|
-
end
|
|
305
|
-
|
|
306
|
-
# Renders a 405 Method Not Allowed response.
|
|
307
|
-
def render_method_not_allowed(message = "Method Not Allowed", id = nil)
|
|
308
|
-
id ||= extract_jsonrpc_id_from_request
|
|
309
|
-
render json: { jsonrpc: "2.0", id: id, error: { code: -32_601, message: message } }, status: :method_not_allowed
|
|
310
|
-
end
|
|
311
|
-
|
|
312
|
-
# Renders a 406 Not Acceptable response.
|
|
313
|
-
def render_not_acceptable(message = "Not Acceptable", id = nil)
|
|
314
|
-
id ||= extract_jsonrpc_id_from_request
|
|
315
|
-
render json: { jsonrpc: "2.0", id: id, error: { code: -32_000, message: message } }, status: :not_acceptable
|
|
288
|
+
# HTTP-level errors rendered with a JSON-RPC error body:
|
|
289
|
+
# name => [default message, JSON-RPC error code, HTTP status]
|
|
290
|
+
HTTP_ERRORS = {
|
|
291
|
+
bad_request: [ "Bad Request", -32_600, :bad_request ],
|
|
292
|
+
protocol_version_error: [ "Protocol Version Error", -32_000, :bad_request ],
|
|
293
|
+
not_found: [ "Not Found", -32_001, :not_found ],
|
|
294
|
+
method_not_allowed: [ "Method Not Allowed", -32_601, :method_not_allowed ],
|
|
295
|
+
not_acceptable: [ "Not Acceptable", -32_000, :not_acceptable ],
|
|
296
|
+
not_implemented: [ "Not Implemented", -32_003, :not_implemented ]
|
|
297
|
+
}.freeze
|
|
298
|
+
|
|
299
|
+
HTTP_ERRORS.each do |name, (default_message, code, status)|
|
|
300
|
+
define_method(:"render_#{name}") do |message = default_message, id = nil|
|
|
301
|
+
render_http_error(code, message, status, id || extract_jsonrpc_id_from_request)
|
|
302
|
+
end
|
|
316
303
|
end
|
|
317
304
|
|
|
318
|
-
#
|
|
319
|
-
def
|
|
320
|
-
|
|
321
|
-
render json: { jsonrpc: "2.0", id: id, error: { code: -32_003, message: message } }, status: :not_implemented
|
|
305
|
+
# The request id is left unset here: the body may be what failed.
|
|
306
|
+
def render_internal_server_error(message = "Internal Server Error", id = nil)
|
|
307
|
+
render_http_error(-32_603, message, :internal_server_error, id)
|
|
322
308
|
end
|
|
323
309
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
render json: { jsonrpc: "2.0", id: id, error: { code: -32_603, message: message } }, status: :internal_server_error
|
|
310
|
+
def render_http_error(code, message, status, id)
|
|
311
|
+
render json: { jsonrpc: "2.0", id: id, error: { code: code, message: message } }, status: status
|
|
327
312
|
end
|
|
328
313
|
|
|
329
314
|
def render_jsonrpc_error(symbol, message, id = nil)
|
|
@@ -400,7 +385,7 @@ module ActionMCP
|
|
|
400
385
|
challenge = "Bearer"
|
|
401
386
|
challenge += ' error="invalid_token"' if request.headers["Authorization"].present?
|
|
402
387
|
response.headers["WWW-Authenticate"] = challenge
|
|
403
|
-
|
|
388
|
+
render_http_error(-32_000, message, :unauthorized, id)
|
|
404
389
|
end
|
|
405
390
|
end
|
|
406
391
|
end
|
|
@@ -8,12 +8,51 @@ module ActionMCP
|
|
|
8
8
|
|
|
9
9
|
queue_as :mcp_tasks
|
|
10
10
|
|
|
11
|
-
#
|
|
12
|
-
|
|
11
|
+
# The gateway resolves identities per request into ActionMCP::Current; they
|
|
12
|
+
# travel with the job so perform and schema procs see the same current_user
|
|
13
|
+
# as a synchronous call. Values ActiveJob cannot serialize stay behind.
|
|
14
|
+
CARRIED_CURRENT_ATTRIBUTES = %i[user session_data].freeze
|
|
15
|
+
|
|
16
|
+
attr_reader :current_attributes
|
|
17
|
+
|
|
18
|
+
before_enqueue { @current_attributes ||= self.class.capture_current }
|
|
19
|
+
around_perform { |job, block| ActionMCP::Current.set(job.current_attributes.to_h) { block.call } }
|
|
20
|
+
|
|
21
|
+
# A failure marks the task failed, so clients polling tasks/get see a
|
|
22
|
+
# terminal state, then re-raises so the job fails visibly in the queue
|
|
23
|
+
# backend. A manual retry of such a job finds the task terminal and stops.
|
|
24
|
+
rescue_from StandardError do |error|
|
|
25
|
+
self.class.fail_task_for(self, error)
|
|
26
|
+
raise error
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Declared last, so checked first: transient infrastructure errors retry.
|
|
30
|
+
retry_on ActiveRecord::Deadlocked, ActiveRecord::ConnectionNotEstablished, ActiveRecord::LockWaitTimeout,
|
|
31
|
+
wait: :polynomially_longer, attempts: 3 do |job, error|
|
|
32
|
+
fail_task_for(job, error)
|
|
33
|
+
raise error
|
|
34
|
+
end
|
|
13
35
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
36
|
+
def self.capture_current
|
|
37
|
+
CARRIED_CURRENT_ATTRIBUTES.each_with_object({}) do |name, carried|
|
|
38
|
+
value = ActionMCP::Current.public_send(name)
|
|
39
|
+
next if value.blank?
|
|
40
|
+
|
|
41
|
+
ActiveJob::Arguments.serialize([ value ])
|
|
42
|
+
carried[name] = value
|
|
43
|
+
rescue ActiveJob::SerializationError
|
|
44
|
+
Rails.logger.warn "[ToolExecutionJob] ActionMCP::Current.#{name} (#{value.class}) " \
|
|
45
|
+
"cannot be serialized for ActiveJob; the task runs without it"
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def serialize
|
|
50
|
+
super.merge("action_mcp_current" => ActiveJob::Arguments.serialize([ current_attributes.to_h ]))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def deserialize(job_data)
|
|
54
|
+
super
|
|
55
|
+
@current_attributes = ActiveJob::Arguments.deserialize(job_data.fetch("action_mcp_current", [ {} ])).first
|
|
17
56
|
end
|
|
18
57
|
|
|
19
58
|
# @param task_id [String] Task ID
|
|
@@ -21,16 +60,18 @@ module ActionMCP
|
|
|
21
60
|
# @param arguments [Hash] Tool arguments
|
|
22
61
|
# @param meta [Hash] Request metadata
|
|
23
62
|
def perform(task_id, tool_name, arguments, meta = {})
|
|
24
|
-
|
|
63
|
+
# Loading reruns on every resumption; only the tool run is a checkpointed
|
|
64
|
+
# step, so a resumed job never executes the tool twice.
|
|
65
|
+
@task = load_task(task_id)
|
|
25
66
|
return if @task.nil? || @task.terminal?
|
|
26
67
|
|
|
27
|
-
@session =
|
|
68
|
+
@session = validate_session(@task)
|
|
28
69
|
return unless @session
|
|
29
70
|
|
|
30
|
-
@tool =
|
|
71
|
+
@tool = prepare_tool(@session, tool_name, arguments, @task)
|
|
31
72
|
return unless @tool
|
|
32
73
|
|
|
33
|
-
step
|
|
74
|
+
step :execute_tool do
|
|
34
75
|
result = execute_with_reloader(@tool, @session)
|
|
35
76
|
update_task_result(@task, result)
|
|
36
77
|
end
|
|
@@ -112,22 +153,8 @@ module ActionMCP
|
|
|
112
153
|
true
|
|
113
154
|
end
|
|
114
155
|
|
|
115
|
-
def execute_with_reloader(tool,
|
|
116
|
-
|
|
117
|
-
# Preserve Current attributes across reloader boundary
|
|
118
|
-
current_user = ActionMCP::Current.user
|
|
119
|
-
current_gateway = ActionMCP::Current.gateway
|
|
120
|
-
current_session_data = ActionMCP::Current.session_data
|
|
121
|
-
|
|
122
|
-
Rails.application.reloader.wrap do
|
|
123
|
-
ActionMCP::Current.user = current_user
|
|
124
|
-
ActionMCP::Current.gateway = current_gateway
|
|
125
|
-
ActionMCP::Current.session_data = current_session_data
|
|
126
|
-
tool.call
|
|
127
|
-
end
|
|
128
|
-
else
|
|
129
|
-
tool.call
|
|
130
|
-
end
|
|
156
|
+
def execute_with_reloader(tool, _session)
|
|
157
|
+
DevReloader.wrap { tool.call }
|
|
131
158
|
end
|
|
132
159
|
|
|
133
160
|
def update_task_result(task, result)
|
|
@@ -149,12 +176,12 @@ module ActionMCP
|
|
|
149
176
|
end
|
|
150
177
|
end
|
|
151
178
|
|
|
152
|
-
def self.
|
|
179
|
+
def self.fail_task_for(job, error)
|
|
153
180
|
task_id = job.arguments.first
|
|
154
181
|
task = Session::Task.find_by(id: task_id)
|
|
155
182
|
return unless task&.persisted?
|
|
156
183
|
|
|
157
|
-
Rails.logger.error "[ToolExecutionJob]
|
|
184
|
+
Rails.logger.error "[ToolExecutionJob] Task #{task_id} failed: #{error.class} - #{error.message}"
|
|
158
185
|
Rails.logger.error error.backtrace&.first(10)&.join("\n")
|
|
159
186
|
|
|
160
187
|
task.with_lock do
|
|
@@ -174,6 +201,9 @@ module ActionMCP
|
|
|
174
201
|
)
|
|
175
202
|
task.mark_failed!
|
|
176
203
|
end
|
|
204
|
+
rescue StandardError => e
|
|
205
|
+
# The original error still propagates; this one is reported alongside it.
|
|
206
|
+
Rails.error.report(e, handled: true, severity: :error, context: { task_id: task_id })
|
|
177
207
|
end
|
|
178
208
|
end
|
|
179
209
|
end
|
|
@@ -295,7 +295,8 @@ module ActionMCP
|
|
|
295
295
|
return unless resumed
|
|
296
296
|
|
|
297
297
|
# Re-enqueue the job to continue execution
|
|
298
|
-
|
|
298
|
+
params = (request_params || {}).with_indifferent_access
|
|
299
|
+
ActionMCP::ToolExecutionJob.perform_later(id, request_name, params[:arguments] || {}, params[:_meta] || {})
|
|
299
300
|
end
|
|
300
301
|
|
|
301
302
|
private
|
|
@@ -38,6 +38,7 @@ module ActionMCP
|
|
|
38
38
|
# It also manages the association with messages and subscriptions related to the session.
|
|
39
39
|
class Session < ApplicationRecord
|
|
40
40
|
include MCPConsoleHelpers
|
|
41
|
+
include Server::SessionRegistries
|
|
41
42
|
attribute :id, :string, default: -> { SecureRandom.hex(16) }
|
|
42
43
|
has_many :messages,
|
|
43
44
|
class_name: "ActionMCP::Session::Message",
|
|
@@ -176,14 +177,6 @@ module ActionMCP
|
|
|
176
177
|
update(status: "initializing")
|
|
177
178
|
end
|
|
178
179
|
|
|
179
|
-
def initialize!
|
|
180
|
-
return false unless status == "initializing" && !initialized?
|
|
181
|
-
|
|
182
|
-
self.initialized = true
|
|
183
|
-
self.status = "initialized"
|
|
184
|
-
save
|
|
185
|
-
end
|
|
186
|
-
|
|
187
180
|
def send_ping!
|
|
188
181
|
Session.logger.silence do
|
|
189
182
|
write(JSON_RPC::Request.new(id: Time.now.to_i, method: "ping"))
|
|
@@ -202,159 +195,6 @@ module ActionMCP
|
|
|
202
195
|
subscriptions.exists?(uri: uri)
|
|
203
196
|
end
|
|
204
197
|
|
|
205
|
-
def send_progress_notification(progressToken:, progress:, total: nil, message: nil)
|
|
206
|
-
# Create a transport handler to send the notification
|
|
207
|
-
handler = ActionMCP::Server::TransportHandler.new(self)
|
|
208
|
-
handler.send_progress_notification(
|
|
209
|
-
progressToken: progressToken,
|
|
210
|
-
progress: progress,
|
|
211
|
-
total: total,
|
|
212
|
-
message: message
|
|
213
|
-
)
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
# Registry management methods
|
|
217
|
-
def register_tool(tool_class_or_name)
|
|
218
|
-
tool_name = normalize_name(tool_class_or_name, :tool)
|
|
219
|
-
return false unless tool_exists?(tool_name)
|
|
220
|
-
return true if uses_all_tools?
|
|
221
|
-
|
|
222
|
-
self.tool_registry ||= []
|
|
223
|
-
unless self.tool_registry.include?(tool_name)
|
|
224
|
-
self.tool_registry << tool_name
|
|
225
|
-
save!
|
|
226
|
-
send_tools_list_changed_notification
|
|
227
|
-
end
|
|
228
|
-
true
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
def unregister_tool(tool_class_or_name)
|
|
232
|
-
tool_name = normalize_name(tool_class_or_name, :tool)
|
|
233
|
-
|
|
234
|
-
if uses_all_tools?
|
|
235
|
-
return unless tool_exists?(tool_name)
|
|
236
|
-
expand_tool_registry!
|
|
237
|
-
end
|
|
238
|
-
|
|
239
|
-
self.tool_registry ||= []
|
|
240
|
-
return unless self.tool_registry.delete(tool_name)
|
|
241
|
-
|
|
242
|
-
save!
|
|
243
|
-
send_tools_list_changed_notification
|
|
244
|
-
end
|
|
245
|
-
|
|
246
|
-
def register_prompt(prompt_class_or_name)
|
|
247
|
-
prompt_name = normalize_name(prompt_class_or_name, :prompt)
|
|
248
|
-
return false unless prompt_exists?(prompt_name)
|
|
249
|
-
return true if uses_all_prompts?
|
|
250
|
-
|
|
251
|
-
self.prompt_registry ||= []
|
|
252
|
-
unless self.prompt_registry.include?(prompt_name)
|
|
253
|
-
self.prompt_registry << prompt_name
|
|
254
|
-
save!
|
|
255
|
-
send_prompts_list_changed_notification
|
|
256
|
-
end
|
|
257
|
-
true
|
|
258
|
-
end
|
|
259
|
-
|
|
260
|
-
def unregister_prompt(prompt_class_or_name)
|
|
261
|
-
prompt_name = normalize_name(prompt_class_or_name, :prompt)
|
|
262
|
-
|
|
263
|
-
if uses_all_prompts?
|
|
264
|
-
return unless prompt_exists?(prompt_name)
|
|
265
|
-
expand_prompt_registry!
|
|
266
|
-
end
|
|
267
|
-
|
|
268
|
-
self.prompt_registry ||= []
|
|
269
|
-
return unless self.prompt_registry.delete(prompt_name)
|
|
270
|
-
|
|
271
|
-
save!
|
|
272
|
-
send_prompts_list_changed_notification
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
def register_resource_template(template_class_or_name)
|
|
276
|
-
template_name = normalize_name(template_class_or_name, :resource_template)
|
|
277
|
-
return false unless resource_template_exists?(template_name)
|
|
278
|
-
return true if uses_all_resources?
|
|
279
|
-
|
|
280
|
-
self.resource_registry ||= []
|
|
281
|
-
unless self.resource_registry.include?(template_name)
|
|
282
|
-
self.resource_registry << template_name
|
|
283
|
-
save!
|
|
284
|
-
send_resources_list_changed_notification
|
|
285
|
-
end
|
|
286
|
-
true
|
|
287
|
-
end
|
|
288
|
-
|
|
289
|
-
def unregister_resource_template(template_class_or_name)
|
|
290
|
-
template_name = normalize_name(template_class_or_name, :resource_template)
|
|
291
|
-
|
|
292
|
-
if uses_all_resources?
|
|
293
|
-
return unless resource_template_exists?(template_name)
|
|
294
|
-
expand_resource_registry!
|
|
295
|
-
end
|
|
296
|
-
|
|
297
|
-
self.resource_registry ||= []
|
|
298
|
-
return unless self.resource_registry.delete(template_name)
|
|
299
|
-
|
|
300
|
-
save!
|
|
301
|
-
send_resources_list_changed_notification
|
|
302
|
-
end
|
|
303
|
-
|
|
304
|
-
# Get registered items for this session
|
|
305
|
-
def registered_tools
|
|
306
|
-
# Special case: ['*'] means use all available tools dynamically
|
|
307
|
-
if tool_registry == [ "*" ]
|
|
308
|
-
# filtered_tools returns a RegistryScope with Item objects, need to extract the klass
|
|
309
|
-
ActionMCP.configuration.filtered_tools.map(&:klass)
|
|
310
|
-
else
|
|
311
|
-
(self.tool_registry || []).filter_map do |tool_name|
|
|
312
|
-
ActionMCP::ToolsRegistry.find(tool_name)
|
|
313
|
-
rescue StandardError
|
|
314
|
-
nil
|
|
315
|
-
end
|
|
316
|
-
end
|
|
317
|
-
end
|
|
318
|
-
|
|
319
|
-
def registered_prompts
|
|
320
|
-
if prompt_registry == [ "*" ]
|
|
321
|
-
# filtered_prompts returns a RegistryScope with Item objects, need to extract the klass
|
|
322
|
-
ActionMCP.configuration.filtered_prompts.map(&:klass)
|
|
323
|
-
else
|
|
324
|
-
(self.prompt_registry || []).filter_map do |prompt_name|
|
|
325
|
-
ActionMCP::PromptsRegistry.find(prompt_name)
|
|
326
|
-
rescue StandardError
|
|
327
|
-
nil
|
|
328
|
-
end
|
|
329
|
-
end
|
|
330
|
-
end
|
|
331
|
-
|
|
332
|
-
def registered_resource_templates
|
|
333
|
-
if resource_registry == [ "*" ]
|
|
334
|
-
# filtered_resources returns a RegistryScope with Item objects, need to extract the klass
|
|
335
|
-
ActionMCP.configuration.filtered_resources.map(&:klass)
|
|
336
|
-
else
|
|
337
|
-
(self.resource_registry || []).filter_map do |template_name|
|
|
338
|
-
ActionMCP::ResourceTemplatesRegistry.find(template_name)
|
|
339
|
-
rescue StandardError
|
|
340
|
-
nil
|
|
341
|
-
end
|
|
342
|
-
end
|
|
343
|
-
end
|
|
344
|
-
|
|
345
|
-
# Helper methods to check if using all capabilities
|
|
346
|
-
def uses_all_tools?
|
|
347
|
-
tool_registry == [ "*" ]
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
def uses_all_prompts?
|
|
351
|
-
prompt_registry == [ "*" ]
|
|
352
|
-
end
|
|
353
|
-
|
|
354
|
-
def uses_all_resources?
|
|
355
|
-
resource_registry == [ "*" ]
|
|
356
|
-
end
|
|
357
|
-
|
|
358
198
|
# Consent management methods as per MCP specification
|
|
359
199
|
# These methods manage user consents for tools and resources
|
|
360
200
|
|
|
@@ -453,78 +293,6 @@ module ActionMCP
|
|
|
453
293
|
self.resource_registry = [ "*" ]
|
|
454
294
|
end
|
|
455
295
|
|
|
456
|
-
# Expand wildcard registries to explicit name lists so individual
|
|
457
|
-
# entries can be removed without breaking the wildcard check.
|
|
458
|
-
def expand_tool_registry!
|
|
459
|
-
self.tool_registry = ActionMCP.configuration.filtered_tools.map(&:name)
|
|
460
|
-
end
|
|
461
|
-
|
|
462
|
-
def expand_prompt_registry!
|
|
463
|
-
self.prompt_registry = ActionMCP.configuration.filtered_prompts.map(&:name)
|
|
464
|
-
end
|
|
465
|
-
|
|
466
|
-
def expand_resource_registry!
|
|
467
|
-
self.resource_registry = ActionMCP.configuration.filtered_resources.map(&:name)
|
|
468
|
-
end
|
|
469
|
-
|
|
470
|
-
def normalize_name(class_or_name, type)
|
|
471
|
-
case class_or_name
|
|
472
|
-
when String
|
|
473
|
-
class_or_name
|
|
474
|
-
when Class
|
|
475
|
-
case type
|
|
476
|
-
when :tool
|
|
477
|
-
class_or_name.tool_name
|
|
478
|
-
when :prompt
|
|
479
|
-
class_or_name.prompt_name
|
|
480
|
-
when :resource_template
|
|
481
|
-
class_or_name.capability_name
|
|
482
|
-
end
|
|
483
|
-
else
|
|
484
|
-
raise ArgumentError, "Expected String or Class, got #{class_or_name.class}"
|
|
485
|
-
end
|
|
486
|
-
end
|
|
487
|
-
|
|
488
|
-
def tool_exists?(tool_name)
|
|
489
|
-
ActionMCP::ToolsRegistry.find(tool_name)
|
|
490
|
-
true
|
|
491
|
-
rescue ActionMCP::RegistryBase::NotFound
|
|
492
|
-
false
|
|
493
|
-
end
|
|
494
|
-
|
|
495
|
-
def prompt_exists?(prompt_name)
|
|
496
|
-
ActionMCP::PromptsRegistry.find(prompt_name)
|
|
497
|
-
true
|
|
498
|
-
rescue ActionMCP::RegistryBase::NotFound
|
|
499
|
-
false
|
|
500
|
-
end
|
|
501
|
-
|
|
502
|
-
def resource_template_exists?(template_name)
|
|
503
|
-
ActionMCP::ResourceTemplatesRegistry.find(template_name)
|
|
504
|
-
true
|
|
505
|
-
rescue ActionMCP::RegistryBase::NotFound
|
|
506
|
-
false
|
|
507
|
-
end
|
|
508
|
-
|
|
509
|
-
def send_tools_list_changed_notification
|
|
510
|
-
# Only send if server capabilities allow it
|
|
511
|
-
return unless server_capabilities.dig("tools", "listChanged")
|
|
512
|
-
|
|
513
|
-
write(JSON_RPC::Notification.new(method: "notifications/tools/list_changed"))
|
|
514
|
-
end
|
|
515
|
-
|
|
516
|
-
def send_prompts_list_changed_notification
|
|
517
|
-
return unless server_capabilities.dig("prompts", "listChanged")
|
|
518
|
-
|
|
519
|
-
write(JSON_RPC::Notification.new(method: "notifications/prompts/list_changed"))
|
|
520
|
-
end
|
|
521
|
-
|
|
522
|
-
def send_resources_list_changed_notification
|
|
523
|
-
return unless server_capabilities.dig("resources", "listChanged")
|
|
524
|
-
|
|
525
|
-
write(JSON_RPC::Notification.new(method: "notifications/resources/list_changed"))
|
|
526
|
-
end
|
|
527
|
-
|
|
528
296
|
def parsed_json_attribute(value)
|
|
529
297
|
return value unless value.is_a?(String)
|
|
530
298
|
|