Module: McpUiServer

Defined in:
lib/mcp_ui_server.rb,
lib/mcp_ui_server/version.rb

Overview

The McpUiServer module provides helper methods for creating UI resources compatible with the Model Context Protocol UI (mcp-ui) client.

Defined Under Namespace

Classes: Error

Constant Summary collapse

MIME_TYPE_HTML =

MIME type constants

'text/html'
MIME_TYPE_URI_LIST =
'text/uri-list'
MIME_TYPE_REMOTE_DOM =
'application/vnd.mcp-ui.remote-dom+javascript; framework=%s'
CONTENT_TYPE_RAW_HTML =

Content type constants

:raw_html
CONTENT_TYPE_EXTERNAL_URL =
:external_url
CONTENT_TYPE_REMOTE_DOM =
:remote_dom
PROTOCOL_CONTENT_TYPES =

Protocol mapping (snake_case to camelCase for protocol consistency)

{
  raw_html: 'rawHtml',
  external_url: 'externalUrl',
  remote_dom: 'remoteDom'
}.freeze
REQUIRED_CONTENT_KEYS =

Required content keys for each content type

{
  CONTENT_TYPE_RAW_HTML => :htmlString,
  CONTENT_TYPE_EXTERNAL_URL => :iframeUrl,
  CONTENT_TYPE_REMOTE_DOM => :script
}.freeze
UI_URI_SCHEME =

URI scheme constant

'ui://'
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.create_ui_resource(uri:, content:, encoding: :text) ⇒ Hash

Creates a UIResource hash structure for an MCP response. This structure can then be serialized to JSON by your web framework.

Parameters:

  • uri (String)

    The unique identifier for the resource (e.g., ‘ui://greeting/1’).

  • content (Hash)

    A hash describing the UI content.

    • :type [Symbol] The type of content. One of :raw_html, :external_url, or :remote_dom.

    • :htmlString [String] The raw HTML content (required if type is :raw_html).

    • :iframeUrl [String] The URL for an external page (required if type is :external_url).

    • :script [String] The remote-dom script (required if type is :remote_dom).

    • :framework [Symbol] The remote-dom framework, e.g., :react or :webcomponents (required, for :remote_dom).

  • encoding (Symbol) (defaults to: :text)

    The encoding method. :text for plain string, :blob for base64 encoded.

Returns:

  • (Hash)

    A UIResource hash ready to be included in an MCP response.

Raises:

  • (McpUiServer::Error)

    if URI scheme is invalid, content type is unknown, encoding type is unknown, or required content keys are missing.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mcp_ui_server.rb', line 54

def self.create_ui_resource(uri:, content:, encoding: :text)
  validate_uri_scheme(uri)

  resource = { uri: uri }

  content_value = process_content(content, resource)
  process_encoding(encoding, resource, content_value)

  {
    type: 'resource',
    resource: resource
  }
end