|
| 1 | +# Protocol Extensions |
| 2 | + |
| 3 | +MCP protocol extensions advertise additional, optional capabilities during the initialize handshake. |
| 4 | +A server opts in via `Builder::enableExtension()`: |
| 5 | + |
| 6 | +```php |
| 7 | +use Mcp\Schema\Extension\Apps\McpApps; |
| 8 | +use Mcp\Server; |
| 9 | + |
| 10 | +$server = Server::builder() |
| 11 | + ->setServerInfo('My Server', '1.0.0') |
| 12 | + ->enableExtension(McpApps::class) // or pre-built instances |
| 13 | + ->build(); |
| 14 | +``` |
| 15 | + |
| 16 | +Pass either a class string (the extension is instantiated with no arguments) or |
| 17 | +a pre-built `ServerExtensionInterface` instance. Multiple extensions can be |
| 18 | +enabled in a single call. |
| 19 | + |
| 20 | +> Note: calling `setCapabilities()` overrides automatic capability detection, |
| 21 | +> so it also overrides the `extensions` field. If you set your own |
| 22 | +> `ServerCapabilities`, include the extensions you want yourself. |
| 23 | +
|
| 24 | +## MCP Apps (`io.modelcontextprotocol/ui`) |
| 25 | + |
| 26 | +The [MCP Apps extension][ext-apps] lets servers expose interactive HTML UIs as |
| 27 | +resources. Clients that support it render them in sandboxed iframes and bridge |
| 28 | +tool calls between the iframe (the *View*) and the server via the host. |
| 29 | + |
| 30 | +A UI consists of two pieces wired together by `_meta.ui`: |
| 31 | + |
| 32 | +1. **A resource** with URI scheme `ui://` and MIME type |
| 33 | + `text/html;profile=mcp-app`, returning the HTML body. |
| 34 | +2. **A tool** linked to that resource via `UiToolMeta`, so the client knows to |
| 35 | + open the UI when the tool is invoked. |
| 36 | + |
| 37 | +```php |
| 38 | +use Mcp\Schema\Content\TextResourceContents; |
| 39 | +use Mcp\Schema\Extension\Apps\McpApps; |
| 40 | +use Mcp\Schema\Extension\Apps\ToolVisibility; |
| 41 | +use Mcp\Schema\Extension\Apps\UiResourceContentMeta; |
| 42 | +use Mcp\Schema\Extension\Apps\UiResourceCsp; |
| 43 | +use Mcp\Schema\Extension\Apps\UiResourcePermissions; |
| 44 | +use Mcp\Schema\Extension\Apps\UiToolMeta; |
| 45 | + |
| 46 | +$server = Server::builder() |
| 47 | + ->enableExtension(McpApps::class) |
| 48 | + ->addResource( |
| 49 | + fn () => new TextResourceContents( |
| 50 | + uri: 'ui://my-app', |
| 51 | + mimeType: McpApps::MIME_TYPE, |
| 52 | + text: file_get_contents(__DIR__.'/app.html'), |
| 53 | + meta: ['ui' => new UiResourceContentMeta( |
| 54 | + csp: new UiResourceCsp(connectDomains: ['https://api.example.com']), |
| 55 | + permissions: new UiResourcePermissions(geolocation: true), |
| 56 | + prefersBorder: true, |
| 57 | + )], |
| 58 | + ), |
| 59 | + 'ui://my-app', |
| 60 | + mimeType: McpApps::MIME_TYPE, |
| 61 | + meta: ['ui' => new \stdClass()], |
| 62 | + ) |
| 63 | + ->addTool( |
| 64 | + $myToolHandler, |
| 65 | + 'my_tool', |
| 66 | + meta: ['ui' => new UiToolMeta( |
| 67 | + resourceUri: 'ui://my-app', |
| 68 | + visibility: [ToolVisibility::Model->value, ToolVisibility::App->value], |
| 69 | + )], |
| 70 | + ) |
| 71 | + ->build(); |
| 72 | +``` |
| 73 | + |
| 74 | +### Server-side DTOs |
| 75 | + |
| 76 | +| Class | Purpose | |
| 77 | +| --- | --- | |
| 78 | +| `McpApps` | Extension marker; provides `EXTENSION_ID`, `MIME_TYPE`, `URI_SCHEME` constants. | |
| 79 | +| `UiToolMeta` | Tool `_meta.ui` payload: `resourceUri` + `visibility`. | |
| 80 | +| `ToolVisibility` | Enum: `Model`, `App`. | |
| 81 | +| `UiResourceContentMeta` | Resource content `_meta.ui`: `csp`, `permissions`, `domain`, `prefersBorder`. | |
| 82 | +| `UiResourceCsp` | CSP allow-lists: `connectDomains`, `resourceDomains`, `frameDomains`, `baseUriDomains`. | |
| 83 | +| `UiResourcePermissions` | Sandbox permissions: `camera`, `microphone`, `geolocation`, `clipboardWrite`. | |
| 84 | + |
| 85 | +### Writing the HTML view |
| 86 | + |
| 87 | +The View and host exchange `JSONRPCMessage` **objects** (not JSON strings) via |
| 88 | +`window.parent.postMessage`. Before the host forwards `tools/call`, |
| 89 | +`tool-input`, or `tool-result`, the View must complete the spec-mandated |
| 90 | +handshake: |
| 91 | + |
| 92 | +1. View → Host: `ui/initialize` request |
| 93 | +2. Host → View: response with `hostCapabilities`, `hostInfo`, `hostContext` |
| 94 | +3. View → Host: `ui/notifications/initialized` |
| 95 | +4. View → Host: `ui/notifications/size-changed` whenever the iframe wants to |
| 96 | + resize |
| 97 | + |
| 98 | +See the [`ext-apps` repository][ext-apps] for the full protocol, official |
| 99 | +TypeScript SDK (`@modelcontextprotocol/ext-apps`), and view-side examples. A |
| 100 | +working minimal view is included in |
| 101 | +[`examples/server/mcp-apps/weather-app.html`](../examples/server/mcp-apps/weather-app.html). |
| 102 | + |
| 103 | +[ext-apps]: https://github.com/modelcontextprotocol/ext-apps |
0 commit comments