Merge pull request #441 from koic/mcp_apps

Add Server-Side MCP Apps Helpers per SEP-1865
This commit is contained in:
Koichi ITO 2026-07-12 02:17:49 +09:00 • committed by GitHub
commit 5f9c84df1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 294 additions and 0 deletions

69
test/mcp/apps_test.rb Normal file
View file

@ -0,0 +1,69 @@
# frozen_string_literal: true
require "test_helper"
module MCP
class AppsTest < ActiveSupport::TestCase
test "exposes the SEP-1865 wire vocabulary" do
# The exact strings are shared with the reference ext-apps package and the Python SDK.
assert_equal "io.modelcontextprotocol/ui", Apps::EXTENSION_ID
assert_equal "text/html;profile=mcp-app", Apps::RESOURCE_MIME_TYPE
assert_equal "ui://", Apps::URI_SCHEME
assert_equal "ui/resourceUri", Apps::RESOURCE_URI_META_KEY
end
test ".capability builds the extensions fragment" do
assert_equal({ "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html;profile=mcp-app"] } }, Apps.capability)
assert_equal({ "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html"] } }, Apps.capability(mime_types: ["text/html"]))
end
test ".ui_resource builds a Resource with the spec defaults and validates the scheme" do
resource = Apps.ui_resource(uri: "ui://weather/dashboard", name: "dashboard", description: "Weather UI")
assert_instance_of Resource, resource
assert_equal "ui://weather/dashboard", resource.uri
assert_equal "text/html;profile=mcp-app", resource.mime_type
assert_equal "Weather UI", resource.description
assert_raises(ArgumentError) do
Apps.ui_resource(uri: "file:///dashboard.html", name: "dashboard")
end
end
test ".tool_meta links a tool to its template without clobbering caller meta" do
meta = Apps.tool_meta(
resource_uri: "ui://weather/dashboard",
visibility: ["model", "app"],
meta: { custom: "value" },
)
assert_equal({ custom: "value", ui: { resourceUri: "ui://weather/dashboard", visibility: ["model", "app"] } }, meta)
assert_raises(ArgumentError) do
Apps.tool_meta(resource_uri: "https://example.com")
end
end
test ".tool_meta emits the legacy flat key when requested" do
meta = Apps.tool_meta(resource_uri: "ui://weather/dashboard", legacy: true)
assert_equal "ui://weather/dashboard", meta.dig(:ui, :resourceUri)
assert_equal "ui://weather/dashboard", meta[:"ui/resourceUri"]
end
test ".client_supports? checks the declared extension and mime types" do
declared = { extensions: { "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html;profile=mcp-app"] } } }
string_keys = {
"extensions" => { "io.modelcontextprotocol/ui" => { "mimeTypes" => ["text/html;profile=mcp-app"] } },
}
without_mime_types = { extensions: { "io.modelcontextprotocol/ui" => {} } }
other_mime_type = { extensions: { "io.modelcontextprotocol/ui" => { mimeTypes: ["text/html"] } } }
assert Apps.client_supports?(declared)
assert Apps.client_supports?(string_keys)
assert Apps.client_supports?(without_mime_types)
refute Apps.client_supports?(other_mime_type)
refute Apps.client_supports?({})
refute Apps.client_supports?(nil)
refute Apps.client_supports?({ extensions: {} })
end
end
end

View file

@ -3539,5 +3539,80 @@ module MCP
assert_equal "b", second_result[:resourceTemplates][0][:name]
assert_nil second_result[:nextCursor]
end
test "an MCP Apps server advertises the extension and serves ui:// templates" do
# SEP-1865: the extension rides the SEP-2133 `capabilities.extensions` machinery,
# UI templates are ordinary resources, and tools link to them via `_meta.ui.resourceUri`.
dashboard = Apps.ui_resource(uri: "ui://weather/dashboard", name: "weather_dashboard")
capabilities = Server::Capabilities.new
capabilities.support_tools
capabilities.support_resources
capabilities.support_extensions(Apps.capability)
server = Server.new(name: "apps_test", capabilities: capabilities, resources: [dashboard])
server.define_tool(
name: "get_weather",
meta: Apps.tool_meta(resource_uri: "ui://weather/dashboard"),
) do
Tool::Response.new([{ type: "text", text: "Sunny" }])
end
server.resources_read_handler do |params|
[{ uri: params[:uri], mimeType: Apps::RESOURCE_MIME_TYPE, text: "<html>dashboard</html>" }]
end
init_response = server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
clientInfo: { name: "host", version: "1.0" },
capabilities: { extensions: Apps.capability },
},
})
tools_response = server.handle({
jsonrpc: "2.0",
method: "tools/list",
id: 2,
})
read_response = server.handle({
jsonrpc: "2.0",
method: "resources/read",
id: 3,
params: { uri: "ui://weather/dashboard" },
})
assert_equal(
{ mimeTypes: [Apps::RESOURCE_MIME_TYPE] },
init_response.dig(:result, :capabilities, :extensions, Apps::EXTENSION_ID),
)
tool_entry = tools_response.dig(:result, :tools, 0)
assert_equal "ui://weather/dashboard", tool_entry.dig(:_meta, :ui, :resourceUri)
assert_equal "<html>dashboard</html>", read_response.dig(:result, :contents, 0, :text)
# The server can gate the text-only fallback on the client's declaration.
assert Apps.client_supports?(server.client_capabilities)
end
test "an MCP Apps tool falls back to text for clients without the extension" do
capabilities = Server::Capabilities.new
capabilities.support_extensions(Apps.capability)
server = Server.new(name: "apps_test", capabilities: capabilities)
server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
clientInfo: { name: "plain_client", version: "1.0" },
capabilities: {},
},
})
refute Apps.client_supports?(server.client_capabilities)
end
end
end