Module: McpInspector::Web::UiResourcesHelper
- Defined in:
- lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb
Instance Method Summary collapse
-
#contains_ui_resources?(result) ⇒ Boolean
Check if a result contains MCP UI resources According to the protocol, check for ui:// URI scheme first.
-
#extract_non_ui_content(result) ⇒ Object
Extract non-UI content from a result array.
-
#extract_ui_resources(result) ⇒ Object
Extract UI resources from a result array.
-
#generate_remote_dom_html(script) ⇒ Object
Generate HTML for remote DOM execution.
-
#parse_uri_list(content) ⇒ Object
Parse URI list content (RFC 2483) Returns the first valid HTTP/HTTPS URL.
-
#ui_resource?(item) ⇒ Boolean
Check if a single item is a UI resource.
-
#ui_resource_content(ui_resource) ⇒ Object
Get the content from a UI resource, handling both text and blob encoding.
-
#ui_resource_iframe_id(ui_resource) ⇒ Object
Generate a unique ID for an iframe.
-
#ui_resource_mime_type(ui_resource) ⇒ Object
Get the MIME type of a UI resource.
-
#ui_resource_render_mode(ui_resource) ⇒ Object
Determine the rendering mode based on MIME type.
-
#ui_resource_uri(ui_resource) ⇒ Object
Get the URI of a UI resource.
Instance Method Details
#contains_ui_resources?(result) ⇒ Boolean
Check if a result contains MCP UI resources According to the protocol, check for ui:// URI scheme first
11 12 13 14 15 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 11 def contains_ui_resources?(result) return false unless result.is_a?(Array) result.any? { |item| ui_resource?(item) } end |
#extract_non_ui_content(result) ⇒ Object
Extract non-UI content from a result array
34 35 36 37 38 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 34 def extract_non_ui_content(result) return result unless result.is_a?(Array) result.reject { |item| ui_resource?(item) } end |
#extract_ui_resources(result) ⇒ Object
Extract UI resources from a result array
27 28 29 30 31 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 27 def extract_ui_resources(result) return [] unless result.is_a?(Array) result.select { |item| ui_resource?(item) } end |
#generate_remote_dom_html(script) ⇒ Object
Generate HTML for remote DOM execution
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 115 def generate_remote_dom_html(script) <<~HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body { margin: 0; padding: 16px; font-family: system-ui, -apple-system, sans-serif; } </style> </head> <body> <div id="root"></div> <script> const root = document.getElementById('root'); #{script} </script> </body> </html> HTML end |
#parse_uri_list(content) ⇒ Object
Parse URI list content (RFC 2483) Returns the first valid HTTP/HTTPS URL
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 92 def parse_uri_list(content) return nil if content.blank? content.lines.each do |line| # Skip comments and blank lines next if line.strip.empty? || line.strip.start_with?("#") url = line.strip # Return first valid HTTP/HTTPS URL return url if url.match?(/^https?:\/\//) end nil end |
#ui_resource?(item) ⇒ Boolean
Check if a single item is a UI resource
18 19 20 21 22 23 24 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 18 def ui_resource?(item) return false unless item.is_a?(Hash) # Check for ui:// URI scheme in the resource item.dig("resource", "uri")&.start_with?("ui://") || item.dig(:resource, :uri)&.start_with?("ui://") end |
#ui_resource_content(ui_resource) ⇒ Object
Get the content from a UI resource, handling both text and blob encoding
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 41 def ui_resource_content(ui_resource) resource = ui_resource["resource"] || ui_resource[:resource] return nil unless resource # Check for text content first if resource["text"] || resource[:text] return resource["text"] || resource[:text] end # Handle blob (base64 encoded) content if resource["blob"] || resource[:blob] blob = resource["blob"] || resource[:blob] return Base64.decode64(blob) end nil end |
#ui_resource_iframe_id(ui_resource) ⇒ Object
Generate a unique ID for an iframe
108 109 110 111 112 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 108 def ui_resource_iframe_id(ui_resource) uri = ui_resource_uri(ui_resource) # Create a safe ID from the URI uri&.gsub(/[^a-zA-Z0-9\-_]/, "-") || "ui-resource-#{SecureRandom.hex(4)}" end |
#ui_resource_mime_type(ui_resource) ⇒ Object
Get the MIME type of a UI resource
60 61 62 63 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 60 def ui_resource_mime_type(ui_resource) resource = ui_resource["resource"] || ui_resource[:resource] resource&.dig("mimeType") || resource&.dig(:mimeType) end |
#ui_resource_render_mode(ui_resource) ⇒ Object
Determine the rendering mode based on MIME type
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 72 def ui_resource_render_mode(ui_resource) mime_type = ui_resource_mime_type(ui_resource) return :unknown unless mime_type case mime_type when "text/html" :raw_html when "text/uri-list" :external_url when /^application\/vnd\.mcp-ui\.remote-dom/ # Matches application/vnd.mcp-ui.remote-dom or # application/vnd.mcp-ui.remote-dom+javascript; framework=webcomponents :remote_dom else :unknown end end |
#ui_resource_uri(ui_resource) ⇒ Object
Get the URI of a UI resource
66 67 68 69 |
# File 'lib/mcp_inspector/web/app/helpers/mcp_inspector/web/ui_resources_helper.rb', line 66 def ui_resource_uri(ui_resource) resource = ui_resource["resource"] || ui_resource[:resource] resource&.dig("uri") || resource&.dig(:uri) end |