Skip to content

Commit 036570f

Browse files
committed
Add Server-Side MCP Apps Helpers per SEP-1865
## Motivation and Context SEP-1865 "MCP Apps - Interactive User Interfaces for MCP" is Final on the Extensions Track: (modelcontextprotocol/modelcontextprotocol#1865) an optional extension, negotiated through the SEP-2133 `capabilities.extensions` mechanism, that lets a server ship HTML user interfaces which the host renders for tool results. The normative spec lives in the ext-apps repository (`specification/2026-01-26/apps.mdx`). The Python SDK implements it in-SDK (python-sdk#3003, the `Apps` extension); the TypeScript ecosystem ships it as the separate `@modelcontextprotocol/ext-apps` package on top of the core SDK's SEP-2133 support. Server-side support is a thin convention layer over primitives this SDK already has (SEP-2133 extension capabilities, `_meta` on tools and resources, free-form resource URIs and MIME types). New `MCP::Apps` module: - Wire vocabulary constants shared with the reference implementations: `EXTENSION_ID` (`"io.modelcontextprotocol/ui"` - note `/ui`, not `/apps`), `RESOURCE_MIME_TYPE` (`"text/html;profile=mcp-app"`), `URI_SCHEME` (`"ui://"`), and the legacy flat meta key `RESOURCE_URI_META_KEY` (`"ui/resourceUri"`). - `Apps.capability(mime_types:)` builds the `capabilities.extensions` fragment for `support_extensions` on the server or `connect` on the client. - `Apps.ui_resource(uri:, name:, ...)` builds an `MCP::Resource` for a UI template, enforcing the spec's MUSTs (a `ui://` URI; the parameterized HTML MIME type by default). - `Apps.tool_meta(resource_uri:, visibility:, meta:, legacy:)` builds the `_meta.ui.resourceUri` linkage (optionally also emitting the legacy flat alias, matching the reference server helper that keeps both shapes in sync), merged non-destructively into caller-supplied meta. - `Apps.client_supports?(client_capabilities, mime_type:)` gates the text-only fallback the extension requires for clients that did not declare the capability (symbol/string tolerant). Everything else the extension defines (the sandboxed iframe, the `ui/*` postMessage bridge methods, consent for UI-initiated calls) is HOST responsibility and out of scope for a server SDK; a server only ever receives ordinary `resources/read` and `tools/call` requests. The README section states this boundary. Resolves #376. ## How Has This Been Tested? New `test/mcp/apps_test.rb` pins the wire vocabulary and covers the capability fragment, `ui_resource` scheme validation and MIME default, `tool_meta` non-destructive merging, visibility, the legacy alias, and `client_supports?` across symbol/string keys, mimeTypes narrowing, and missing declarations. New integration tests in `test/mcp/server_test.rb` drive a full server: the `initialize` result advertises the extension, tools/list serializes `_meta.ui.resourceUri`, `resources/read` serves the HTML template, and `client_supports?` distinguishes hosts with and without the declared extension. `bundle exec rake` (tests, RuboCop, and conformance baseline) passes. Also verified end-to-end over stdio: a client connecting with the extension declared negotiates the capability, discovers the tool linkage via `_meta.ui.resourceUri`, and reads the `ui://` template as `text/html;profile=mcp-app` content. ## Breaking Changes None. Purely additive helper module; no existing code paths change.
1 parent 8b1a41a commit 036570f

5 files changed

Lines changed: 292 additions & 0 deletions

File tree

README.md

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

259+
### MCP Apps (SEP-1865)
260+
261+
MCP Apps is a Final extension (negotiated via the Capability Extensions mechanism above) that lets a server ship interactive
262+
HTML user interfaces which the host renders for tool results. On the server side the extension is a thin convention,
263+
and `MCP::Apps` provides the vocabulary and helpers:
264+
265+
```ruby
266+
capabilities = MCP::Server::Capabilities.new
267+
capabilities.support_tools
268+
capabilities.support_resources
269+
capabilities.support_extensions(MCP::Apps.capability) # { "io.modelcontextprotocol/ui" => { mimeTypes: [...] } }
270+
271+
server = MCP::Server.new(
272+
name: "weather_server",
273+
capabilities: capabilities,
274+
# UI templates are ordinary resources with a `ui://` URI and the `text/html;profile=mcp-app` MIME type.
275+
resources: [MCP::Apps.ui_resource(uri: "ui://weather-server/dashboard", name: "weather_dashboard")],
276+
)
277+
278+
server.resources_read_handler do |params|
279+
[{ uri: params[:uri], mimeType: MCP::Apps::RESOURCE_MIME_TYPE, text: "<html>...</html>" }]
280+
end
281+
282+
# Link the tool to its template via `_meta.ui.resourceUri` (pass `legacy: true` to also
283+
# emit the older flat `"ui/resourceUri"` alias for hosts that predate the Final spec).
284+
server.define_tool(
285+
name: "get_weather",
286+
meta: MCP::Apps.tool_meta(resource_uri: "ui://weather-server/dashboard"),
287+
) do |server_context:|
288+
# The extension is optional: always return a meaningful text result, and use
289+
# `MCP::Apps.client_supports?` when UI-capable clients should get richer structured content.
290+
MCP::Apps.client_supports?(server.client_capabilities) # => true when the host declared the extension
291+
MCP::Tool::Response.new([{ type: "text", text: "Sunny, 22 degrees Celsius" }])
292+
end
293+
```
294+
295+
Everything else the extension defines (the sandboxed iframe, the `ui/*` postMessage bridge, consent for UI-initiated actions)
296+
is the HOST's responsibility; a server only ever receives ordinary `resources/read` and `tools/call` requests.
297+
See the [MCP Apps specification](https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx).
298+
259299
### Server Context and Configuration Block Data
260300

261301
#### `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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
# https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx
34+
module Apps
35+
# Reverse-DNS extension identifier (note: `/ui`, not `/apps`), shared wire vocabulary with
36+
# the reference `@modelcontextprotocol/ext-apps` package and the Python SDK's `Apps` extension.
37+
EXTENSION_ID = "io.modelcontextprotocol/ui"
38+
# UI template resources MUST use this parameterized MIME type.
39+
RESOURCE_MIME_TYPE = "text/html;profile=mcp-app"
40+
# UI template resource URIs MUST use this scheme.
41+
URI_SCHEME = "ui://"
42+
# Legacy flat `_meta` key linking a tool to its template; the canonical shape is
43+
# the nested `_meta.ui.resourceUri`. {Apps.tool_meta} can emit both.
44+
RESOURCE_URI_META_KEY = "ui/resourceUri"
45+
46+
extend self
47+
48+
# The `capabilities.extensions` fragment advertising Apps support. Pass to
49+
# `MCP::Server::Capabilities.new(extensions: ...)` or `support_extensions(...)`,
50+
# or merge into a client's `connect(capabilities: { extensions: ... })`.
51+
def capability(mime_types: [RESOURCE_MIME_TYPE])
52+
{ EXTENSION_ID => { mimeTypes: mime_types } }
53+
end
54+
55+
# Builds an `MCP::Resource` for a UI template, enforcing the spec's MUSTs:
56+
# a `ui://` URI and the `text/html;profile=mcp-app` MIME type by default.
57+
def ui_resource(uri:, name:, mime_type: RESOURCE_MIME_TYPE, **rest)
58+
unless uri.is_a?(String) && uri.start_with?(URI_SCHEME)
59+
raise ArgumentError, "MCP Apps template URIs must start with #{URI_SCHEME.inspect} (got #{uri.inspect})"
60+
end
61+
62+
Resource.new(uri: uri, name: name, mime_type: mime_type, **rest)
63+
end
64+
65+
# Builds the tool `_meta` linking a tool to its UI template, merged non-destructively into
66+
# caller-supplied `meta`. `visibility` restricts who sees the tool (an array of `"model"` / `"app"`).
67+
# `legacy: true` also writes the flat `"ui/resourceUri"` alias, matching the reference server helper
68+
# that keeps both key shapes in sync for older hosts.
69+
def tool_meta(resource_uri:, visibility: nil, meta: nil, legacy: false)
70+
unless resource_uri.is_a?(String) && resource_uri.start_with?(URI_SCHEME)
71+
raise ArgumentError, "resource_uri must start with #{URI_SCHEME.inspect} (got #{resource_uri.inspect})"
72+
end
73+
74+
ui_entry = { resourceUri: resource_uri }
75+
ui_entry[:visibility] = visibility if visibility
76+
77+
merged = (meta || {}).merge(ui: ui_entry)
78+
merged[RESOURCE_URI_META_KEY.to_sym] = resource_uri if legacy
79+
merged
80+
end
81+
82+
# Whether the client declared Apps support for `mime_type` in its `capabilities.extensions`
83+
# (symbol or string keys). UI-enabled tools use this to fall back to a text-only result for
84+
# clients without the extension.
85+
def client_supports?(client_capabilities, mime_type: RESOURCE_MIME_TYPE)
86+
extensions = read_key(client_capabilities, :extensions)
87+
declaration = read_key(extensions, EXTENSION_ID)
88+
return false unless declaration.is_a?(Hash)
89+
90+
mime_types = read_key(declaration, :mimeTypes)
91+
92+
# A declaration without mimeTypes advertises the extension without narrowing.
93+
return true if mime_types.nil?
94+
95+
mime_types.is_a?(Array) && mime_types.include?(mime_type)
96+
end
97+
98+
private
99+
100+
def read_key(hash, key)
101+
return unless hash.is_a?(Hash)
102+
103+
value = hash[key.to_sym]
104+
value.nil? ? hash[key.to_s] : value
105+
end
106+
end
107+
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
@@ -3096,5 +3096,80 @@ def server_context
30963096
assert_equal "b", second_result[:resourceTemplates][0][:name]
30973097
assert_nil second_result[:nextCursor]
30983098
end
3099+
3100+
test "an MCP Apps server advertises the extension and serves ui:// templates" do
3101+
# SEP-1865: the extension rides the SEP-2133 `capabilities.extensions` machinery,
3102+
# UI templates are ordinary resources, and tools link to them via `_meta.ui.resourceUri`.
3103+
dashboard = Apps.ui_resource(uri: "ui://weather/dashboard", name: "weather_dashboard")
3104+
capabilities = Server::Capabilities.new
3105+
capabilities.support_tools
3106+
capabilities.support_resources
3107+
capabilities.support_extensions(Apps.capability)
3108+
server = Server.new(name: "apps_test", capabilities: capabilities, resources: [dashboard])
3109+
server.define_tool(
3110+
name: "get_weather",
3111+
meta: Apps.tool_meta(resource_uri: "ui://weather/dashboard"),
3112+
) do
3113+
Tool::Response.new([{ type: "text", text: "Sunny" }])
3114+
end
3115+
3116+
server.resources_read_handler do |params|
3117+
[{ uri: params[:uri], mimeType: Apps::RESOURCE_MIME_TYPE, text: "<html>dashboard</html>" }]
3118+
end
3119+
3120+
init_response = server.handle({
3121+
jsonrpc: "2.0",
3122+
method: "initialize",
3123+
id: 1,
3124+
params: {
3125+
protocolVersion: "2025-11-25",
3126+
clientInfo: { name: "host", version: "1.0" },
3127+
capabilities: { extensions: Apps.capability },
3128+
},
3129+
})
3130+
3131+
tools_response = server.handle({
3132+
jsonrpc: "2.0",
3133+
method: "tools/list",
3134+
id: 2,
3135+
})
3136+
3137+
read_response = server.handle({
3138+
jsonrpc: "2.0",
3139+
method: "resources/read",
3140+
id: 3,
3141+
params: { uri: "ui://weather/dashboard" },
3142+
})
3143+
3144+
assert_equal(
3145+
{ mimeTypes: [Apps::RESOURCE_MIME_TYPE] },
3146+
init_response.dig(:result, :capabilities, :extensions, Apps::EXTENSION_ID),
3147+
)
3148+
tool_entry = tools_response.dig(:result, :tools, 0)
3149+
3150+
assert_equal "ui://weather/dashboard", tool_entry.dig(:_meta, :ui, :resourceUri)
3151+
assert_equal "<html>dashboard</html>", read_response.dig(:result, :contents, 0, :text)
3152+
# The server can gate the text-only fallback on the client's declaration.
3153+
assert Apps.client_supports?(server.client_capabilities)
3154+
end
3155+
3156+
test "an MCP Apps tool falls back to text for clients without the extension" do
3157+
capabilities = Server::Capabilities.new
3158+
capabilities.support_extensions(Apps.capability)
3159+
server = Server.new(name: "apps_test", capabilities: capabilities)
3160+
3161+
server.handle({
3162+
jsonrpc: "2.0",
3163+
method: "initialize",
3164+
id: 1,
3165+
params: {
3166+
protocolVersion: "2025-11-25",
3167+
clientInfo: { name: "plain_client", version: "1.0" },
3168+
capabilities: {},
3169+
},
3170+
})
3171+
3172+
refute Apps.client_supports?(server.client_capabilities)
3173+
end
30993174
end
31003175
end

0 commit comments

Comments
 (0)