Skip to content

Commit 5f9c84d

Browse files
authored
Merge pull request #441 from koic/mcp_apps
Add Server-Side MCP Apps Helpers per SEP-1865
2 parents a793dde + 7d9fda4 commit 5f9c84d

5 files changed

Lines changed: 294 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,46 @@ On the client, pass extensions through `connect`:
283283
client.connect(capabilities: { extensions: { "com.example/feature" => {} } })
284284
```
285285

286+
### MCP Apps (SEP-1865)
287+
288+
MCP Apps is a Final extension (negotiated via the Capability Extensions mechanism above) that lets a server ship interactive
289+
HTML user interfaces which the host renders for tool results. On the server side the extension is a thin convention,
290+
and `MCP::Apps` provides the vocabulary and helpers:
291+
292+
```ruby
293+
capabilities = MCP::Server::Capabilities.new
294+
capabilities.support_tools
295+
capabilities.support_resources
296+
capabilities.support_extensions(MCP::Apps.capability) # { "io.modelcontextprotocol/ui" => { mimeTypes: [...] } }
297+
298+
server = MCP::Server.new(
299+
name: "weather_server",
300+
capabilities: capabilities,
301+
# UI templates are ordinary resources with a `ui://` URI and the `text/html;profile=mcp-app` MIME type.
302+
resources: [MCP::Apps.ui_resource(uri: "ui://weather-server/dashboard", name: "weather_dashboard")],
303+
)
304+
305+
server.resources_read_handler do |params|
306+
[{ uri: params[:uri], mimeType: MCP::Apps::RESOURCE_MIME_TYPE, text: "<html>...</html>" }]
307+
end
308+
309+
# Link the tool to its template via `_meta.ui.resourceUri` (pass `legacy: true` to also
310+
# emit the older flat `"ui/resourceUri"` alias for hosts that predate the Final spec).
311+
server.define_tool(
312+
name: "get_weather",
313+
meta: MCP::Apps.tool_meta(resource_uri: "ui://weather-server/dashboard"),
314+
) do |server_context:|
315+
# The extension is optional: always return a meaningful text result, and use
316+
# `MCP::Apps.client_supports?` when UI-capable clients should get richer structured content.
317+
MCP::Apps.client_supports?(server.client_capabilities) # => true when the host declared the extension
318+
MCP::Tool::Response.new([{ type: "text", text: "Sunny, 22 degrees Celsius" }])
319+
end
320+
```
321+
322+
Everything else the extension defines (the sandboxed iframe, the `ui/*` postMessage bridge, consent for UI-initiated actions)
323+
is the HOST's responsibility; a server only ever receives ordinary `resources/read` and `tools/call` requests.
324+
See the [MCP Apps specification](https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx).
325+
286326
### Server Context and Configuration Block Data
287327

288328
#### `server_context`

‎lib/mcp.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
module MCP
1010
autoload :Annotations, "mcp/annotations"
11+
autoload :Apps, "mcp/apps"
1112
autoload :Cancellation, "mcp/cancellation"
1213
autoload :CancelledError, "mcp/cancelled_error"
1314
autoload :Client, "mcp/client"

‎lib/mcp/apps.rb‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# frozen_string_literal: true
2+
3+
module MCP
4+
# Server-side helpers for the MCP Apps extension (SEP-1865, Extensions Track, Final):
5+
# interactive user interfaces delivered as `ui://` HTML template resources that hosts
6+
# render for tool results. The extension is negotiated per SEP-2133 through
7+
# `capabilities.extensions` on both sides; a server declares {EXTENSION_ID}, registers
8+
# UI templates as ordinary resources, and links tools to templates via `_meta`.
9+
#
10+
# Everything else defined by the extension (the sandboxed iframe, the `ui/*`
11+
# postMessage bridge methods, consent for UI-initiated calls) is HOST responsibility:
12+
# a server only ever receives ordinary `resources/read` and `tools/call` requests.
13+
#
14+
# Because the extension is optional, a UI-enabled tool MUST still return a meaningful
15+
# text-only result for clients that did not declare the capability;
16+
# use {Apps.client_supports?} to branch.
17+
#
18+
# @example Declaring an Apps-enabled server
19+
# capabilities = MCP::Server::Capabilities.new
20+
# capabilities.support_tools
21+
# capabilities.support_resources
22+
# capabilities.support_extensions(MCP::Apps.capability)
23+
# server = MCP::Server.new(
24+
# name: "weather_server",
25+
# capabilities: capabilities,
26+
# resources: [MCP::Apps.ui_resource(uri: "ui://weather-server/dashboard", name: "weather_dashboard")],
27+
# )
28+
# server.define_tool(
29+
# name: "get_weather",
30+
# meta: MCP::Apps.tool_meta(resource_uri: "ui://weather-server/dashboard"),
31+
# ) { |server_context:|
32+
# ...
33+
# }
34+
#
35+
# https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx
36+
module Apps
37+
# Reverse-DNS extension identifier (note: `/ui`, not `/apps`), shared wire vocabulary with
38+
# the reference `@modelcontextprotocol/ext-apps` package and the Python SDK's `Apps` extension.
39+
EXTENSION_ID = "io.modelcontextprotocol/ui"
40+
# UI template resources MUST use this parameterized MIME type.
41+
RESOURCE_MIME_TYPE = "text/html;profile=mcp-app"
42+
# UI template resource URIs MUST use this scheme.
43+
URI_SCHEME = "ui://"
44+
# Legacy flat `_meta` key linking a tool to its template; the canonical shape is
45+
# the nested `_meta.ui.resourceUri`. {Apps.tool_meta} can emit both.
46+
RESOURCE_URI_META_KEY = "ui/resourceUri"
47+
48+
extend self
49+
50+
# The `capabilities.extensions` fragment advertising Apps support. Pass to
51+
# `MCP::Server::Capabilities.new(extensions: ...)` or `support_extensions(...)`,
52+
# or merge into a client's `connect(capabilities: { extensions: ... })`.
53+
def capability(mime_types: [RESOURCE_MIME_TYPE])
54+
{ EXTENSION_ID => { mimeTypes: mime_types } }
55+
end
56+
57+
# Builds an `MCP::Resource` for a UI template, enforcing the spec's MUSTs:
58+
# a `ui://` URI and the `text/html;profile=mcp-app` MIME type by default.
59+
def ui_resource(uri:, name:, mime_type: RESOURCE_MIME_TYPE, **rest)
60+
unless uri.is_a?(String) && uri.start_with?(URI_SCHEME)
61+
raise ArgumentError, "MCP Apps template URIs must start with #{URI_SCHEME.inspect} (got #{uri.inspect})"
62+
end
63+
64+
Resource.new(uri: uri, name: name, mime_type: mime_type, **rest)
65+
end
66+
67+
# Builds the tool `_meta` linking a tool to its UI template, merged non-destructively into
68+
# caller-supplied `meta`. `visibility` restricts who sees the tool (an array of `"model"` / `"app"`).
69+
# `legacy: true` also writes the flat `"ui/resourceUri"` alias, matching the reference server helper
70+
# that keeps both key shapes in sync for older hosts.
71+
def tool_meta(resource_uri:, visibility: nil, meta: nil, legacy: false)
72+
unless resource_uri.is_a?(String) && resource_uri.start_with?(URI_SCHEME)
73+
raise ArgumentError, "resource_uri must start with #{URI_SCHEME.inspect} (got #{resource_uri.inspect})"
74+
end
75+
76+
ui_entry = { resourceUri: resource_uri }
77+
ui_entry[:visibility] = visibility if visibility
78+
79+
merged = (meta || {}).merge(ui: ui_entry)
80+
merged[RESOURCE_URI_META_KEY.to_sym] = resource_uri if legacy
81+
merged
82+
end
83+
84+
# Whether the client declared Apps support for `mime_type` in its `capabilities.extensions`
85+
# (symbol or string keys). UI-enabled tools use this to fall back to a text-only result for
86+
# clients without the extension.
87+
def client_supports?(client_capabilities, mime_type: RESOURCE_MIME_TYPE)
88+
extensions = read_key(client_capabilities, :extensions)
89+
declaration = read_key(extensions, EXTENSION_ID)
90+
return false unless declaration.is_a?(Hash)
91+
92+
mime_types = read_key(declaration, :mimeTypes)
93+
94+
# A declaration without mimeTypes advertises the extension without narrowing.
95+
return true if mime_types.nil?
96+
97+
mime_types.is_a?(Array) && mime_types.include?(mime_type)
98+
end
99+
100+
private
101+
102+
def read_key(hash, key)
103+
return unless hash.is_a?(Hash)
104+
105+
value = hash[key.to_sym]
106+
value.nil? ? hash[key.to_s] : value
107+
end
108+
end
109+
end

‎test/mcp/apps_test.rb‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class AppsTest < ActiveSupport::TestCase
7+
test "exposes the SEP-1865 wire vocabulary" do
8+
# The exact strings are shared with the reference ext-apps package and the Python SDK.
9+
assert_equal "io.modelcontextprotocol/ui", Apps::EXTENSION_ID
10+
assert_equal "text/html;profile=mcp-app", Apps::RESOURCE_MIME_TYPE
11+
assert_equal "ui://", Apps::URI_SCHEME
12+
assert_equal "ui/resourceUri", Apps::RESOURCE_URI_META_KEY
13+
end
14+
15+
test ".capability builds the extensions fragment" do
16+
assert_equal({ "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html;profile=mcp-app"] } }, Apps.capability)
17+
assert_equal({ "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html"] } }, Apps.capability(mime_types: ["text/html"]))
18+
end
19+
20+
test ".ui_resource builds a Resource with the spec defaults and validates the scheme" do
21+
resource = Apps.ui_resource(uri: "ui://weather/dashboard", name: "dashboard", description: "Weather UI")
22+
23+
assert_instance_of Resource, resource
24+
assert_equal "ui://weather/dashboard", resource.uri
25+
assert_equal "text/html;profile=mcp-app", resource.mime_type
26+
assert_equal "Weather UI", resource.description
27+
assert_raises(ArgumentError) do
28+
Apps.ui_resource(uri: "file:///dashboard.html", name: "dashboard")
29+
end
30+
end
31+
32+
test ".tool_meta links a tool to its template without clobbering caller meta" do
33+
meta = Apps.tool_meta(
34+
resource_uri: "ui://weather/dashboard",
35+
visibility: ["model", "app"],
36+
meta: { custom: "value" },
37+
)
38+
39+
assert_equal({ custom: "value", ui: { resourceUri: "ui://weather/dashboard", visibility: ["model", "app"] } }, meta)
40+
assert_raises(ArgumentError) do
41+
Apps.tool_meta(resource_uri: "https://example.com")
42+
end
43+
end
44+
45+
test ".tool_meta emits the legacy flat key when requested" do
46+
meta = Apps.tool_meta(resource_uri: "ui://weather/dashboard", legacy: true)
47+
48+
assert_equal "ui://weather/dashboard", meta.dig(:ui, :resourceUri)
49+
assert_equal "ui://weather/dashboard", meta[:"ui/resourceUri"]
50+
end
51+
52+
test ".client_supports? checks the declared extension and mime types" do
53+
declared = { extensions: { "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html;profile=mcp-app"] } } }
54+
string_keys = {
55+
"extensions" => { "io.modelcontextprotocol/ui" => { "mimeTypes" => ["text/html;profile=mcp-app"] } },
56+
}
57+
without_mime_types = { extensions: { "io.modelcontextprotocol/ui" => {} } }
58+
other_mime_type = { extensions: { "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html"] } } }
59+
60+
assert Apps.client_supports?(declared)
61+
assert Apps.client_supports?(string_keys)
62+
assert Apps.client_supports?(without_mime_types)
63+
refute Apps.client_supports?(other_mime_type)
64+
refute Apps.client_supports?({})
65+
refute Apps.client_supports?(nil)
66+
refute Apps.client_supports?({ extensions: {} })
67+
end
68+
end
69+
end

‎test/mcp/server_test.rb‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,5 +3539,80 @@ def server_context
35393539
assert_equal "b", second_result[:resourceTemplates][0][:name]
35403540
assert_nil second_result[:nextCursor]
35413541
end
3542+
3543+
test "an MCP Apps server advertises the extension and serves ui:// templates" do
3544+
# SEP-1865: the extension rides the SEP-2133 `capabilities.extensions` machinery,
3545+
# UI templates are ordinary resources, and tools link to them via `_meta.ui.resourceUri`.
3546+
dashboard = Apps.ui_resource(uri: "ui://weather/dashboard", name: "weather_dashboard")
3547+
capabilities = Server::Capabilities.new
3548+
capabilities.support_tools
3549+
capabilities.support_resources
3550+
capabilities.support_extensions(Apps.capability)
3551+
server = Server.new(name: "apps_test", capabilities: capabilities, resources: [dashboard])
3552+
server.define_tool(
3553+
name: "get_weather",
3554+
meta: Apps.tool_meta(resource_uri: "ui://weather/dashboard"),
3555+
) do
3556+
Tool::Response.new([{ type: "text", text: "Sunny" }])
3557+
end
3558+
3559+
server.resources_read_handler do |params|
3560+
[{ uri: params[:uri], mimeType: Apps::RESOURCE_MIME_TYPE, text: "<html>dashboard</html>" }]
3561+
end
3562+
3563+
init_response = server.handle({
3564+
jsonrpc: "2.0",
3565+
method: "initialize",
3566+
id: 1,
3567+
params: {
3568+
protocolVersion: "2025-11-25",
3569+
clientInfo: { name: "host", version: "1.0" },
3570+
capabilities: { extensions: Apps.capability },
3571+
},
3572+
})
3573+
3574+
tools_response = server.handle({
3575+
jsonrpc: "2.0",
3576+
method: "tools/list",
3577+
id: 2,
3578+
})
3579+
3580+
read_response = server.handle({
3581+
jsonrpc: "2.0",
3582+
method: "resources/read",
3583+
id: 3,
3584+
params: { uri: "ui://weather/dashboard" },
3585+
})
3586+
3587+
assert_equal(
3588+
{ mimeTypes: [Apps::RESOURCE_MIME_TYPE] },
3589+
init_response.dig(:result, :capabilities, :extensions, Apps::EXTENSION_ID),
3590+
)
3591+
tool_entry = tools_response.dig(:result, :tools, 0)
3592+
3593+
assert_equal "ui://weather/dashboard", tool_entry.dig(:_meta, :ui, :resourceUri)
3594+
assert_equal "<html>dashboard</html>", read_response.dig(:result, :contents, 0, :text)
3595+
# The server can gate the text-only fallback on the client's declaration.
3596+
assert Apps.client_supports?(server.client_capabilities)
3597+
end
3598+
3599+
test "an MCP Apps tool falls back to text for clients without the extension" do
3600+
capabilities = Server::Capabilities.new
3601+
capabilities.support_extensions(Apps.capability)
3602+
server = Server.new(name: "apps_test", capabilities: capabilities)
3603+
3604+
server.handle({
3605+
jsonrpc: "2.0",
3606+
method: "initialize",
3607+
id: 1,
3608+
params: {
3609+
protocolVersion: "2025-11-25",
3610+
clientInfo: { name: "plain_client", version: "1.0" },
3611+
capabilities: {},
3612+
},
3613+
})
3614+
3615+
refute Apps.client_supports?(server.client_capabilities)
3616+
end
35423617
end
35433618
end

0 commit comments

Comments
 (0)