Module: ActionMCP::Server::Resources
- Included in:
- TransportHandler
- Defined in:
- lib/action_mcp/server/resources.rb
Instance Method Summary collapse
-
#send_resource_read(id, params) ⇒ Object
Read and return the contents of a resource.
-
#send_resource_templates_list(request_id, params = {}) ⇒ Object
Send list of resource templates to the client.
-
#send_resources_list(request_id, params = {}) ⇒ Object
Send list of concrete resources to the client.
Instance Method Details
#send_resource_read(id, params) ⇒ Object
Read and return the contents of a resource
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/action_mcp/server/resources.rb', line 47 def send_resource_read(id, params) uri = params["uri"] template = ResourceTemplatesRegistry.find_template_for_uri(uri) unless template error = { code: -32_002, message: "Resource not found", data: { uri: uri } } send_jsonrpc_response(id, error: error) return end # Check if resource requires consent and if consent is granted if template.respond_to?(:requires_consent?) && template. && !session.("resource:#{template.name}") error = { code: -32_002, message: "Consent required for resource template '#{template.name}'" } send_jsonrpc_response(id, error: error) return end unless template.readable_uri?(uri) error = { code: -32_002, message: "Resource not found", data: { uri: uri } } send_jsonrpc_response(id, error: error) return end begin # Create template instance and set execution context record = template.process(uri) unless record error = { code: -32_002, message: "Resource not found", data: { uri: uri } } send_jsonrpc_response(id, error: error) return end record.with_context({ session: session }) response = record.call if response.error? send_jsonrpc_response(id, error: response.to_h) else # Normalize contents to MCP ReadResourceResult shape contents = response.contents.map { |c| normalize_read_content(c, uri) } send_jsonrpc_response(id, result: { contents: contents }) end rescue StandardError => e Rails.logger.error "[MCP Error] #{e.class}: #{e.}" send_jsonrpc_error(id, :internal_error, "Failed to read resource: #{e.}") end end |
#send_resource_templates_list(request_id, params = {}) ⇒ Object
Send list of resource templates to the client
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/action_mcp/server/resources.rb', line 29 def send_resource_templates_list(request_id, params = {}) templates = session.registered_resource_templates.map(&:to_h) log_resource_templates page, next_cursor = paginate(templates, cursor: params["cursor"]) result = { resourceTemplates: page } result[:nextCursor] = next_cursor if next_cursor send_jsonrpc_response(request_id, result: result) rescue Server::CursorError => e send_jsonrpc_error(request_id, :invalid_params, e.) end |
#send_resources_list(request_id, params = {}) ⇒ Object
Send list of concrete resources to the client. Aggregates resources from templates that implement self.list.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/action_mcp/server/resources.rb', line 11 def send_resources_list(request_id, params = {}) all_resources = collect_resources(request_id) return unless all_resources # nil means an error was already sent page, next_cursor = paginate(all_resources, cursor: params["cursor"]) result = { resources: page.map(&:to_h) } result[:nextCursor] = next_cursor if next_cursor send_jsonrpc_response(request_id, result: result) rescue Server::CursorError => e send_jsonrpc_error(request_id, :invalid_params, e.) end |