-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(samples): add generic A2UI MCP App renderer in React #1953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liady
wants to merge
7
commits into
a2ui-project:main
Choose a base branch
from
liady:feat/generic-a2ui-react-renderer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17d8049
feat(samples): add generic A2UI MCP App renderer in React
liady 5a7e368
refactor(samples): extract reusable GenericA2uiApp component
liady eb027de
fix(samples): guard iframe target when relaying tool input/result
liady 58f63c0
fix(community): update yarn.lock checksums and resolutions for CI
sugoi-yuzuru 81135aa
Merge branch 'main' into feat/generic-a2ui-react-renderer
liady b9d45b5
Merge branch 'main' into feat/generic-a2ui-react-renderer
liady 6b00cb8
fix(samples): harden generic React renderer per review findings
liady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
samples/community/mcp/a2ui-in-mcpapps/server/apps/react/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Generic A2UI MCP App renderer (React) | ||
|
|
||
| A server-agnostic [MCP Apps](https://github.com/modelcontextprotocol/ext-apps) view that renders | ||
| [A2UI](https://a2ui.org) (v0.9+) payloads. It contains **zero server-specific logic**: all content | ||
| arrives through tool results, so any A2UI-speaking MCP server can serve the built `react.html` as | ||
| its `ui://` resource — this directory is written to be extracted and reused as-is. | ||
|
|
||
| A server is compatible if it follows two conventions: | ||
|
|
||
| 1. **A2UI payloads travel as embedded resources.** Tool results (including the entry tool's | ||
| result) carry A2UI messages as `EmbeddedResource` content blocks with mimeType | ||
| `application/a2ui+json` (a single message or an array). | ||
| 2. **A2UI actions map to tools.** Each A2UI action `name` matches an app-visible tool name | ||
| (`_meta.ui.visibility` includes `"app"`), and the action's resolved `context` becomes the | ||
| tool's `arguments`. The tool's response A2UI is applied incrementally to the same surfaces. | ||
|
|
||
| ## Build | ||
|
|
||
| ```bash | ||
| yarn install | ||
| yarn build:all # emits ../public/react.html (single, self-contained file) | ||
| ``` | ||
|
|
||
| The build uses Vite + `vite-plugin-singlefile`, so the output HTML has no external references and | ||
| can be delivered verbatim via `resources/read`. | ||
|
|
||
| ## Implementation notes | ||
|
|
||
| - The renderer is a reusable React component, `GenericA2uiApp` (`src/generic-a2ui-app.tsx`); | ||
| `src/main.tsx` only mounts it. Embedders can use the component directly and pass an optional | ||
| `actionToToolName` map to route A2UI action names to differently-named server tools (actions | ||
| without an entry call the tool named after the action itself). | ||
| - The MCP Apps handshake and host bridge come from the official | ||
| `@modelcontextprotocol/ext-apps` SDK (`App` class); iframe auto-resizing is handled by the | ||
| SDK's built-in `size-changed` notifications. | ||
| - Rendering uses `@a2ui/react` (v0.9 basic catalog) driven by a `MessageProcessor` from | ||
| `@a2ui/web_core`. | ||
| - The entry tool's result resets and fully renders the view; subsequent action-triggered tool | ||
| results patch it in place (`src/extract-a2ui-messages.ts` does the payload extraction and is | ||
| unit-tested via `yarn test`). |
33 changes: 33 additions & 0 deletions
33
samples/community/mcp/a2ui-in-mcpapps/server/apps/react/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "generic-a2ui-mcp-app-react", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "dependencies": { | ||
| "@a2ui/markdown-it": "^0.0.4", | ||
| "@a2ui/react": "^0.10.1", | ||
| "@a2ui/web_core": "^0.10.3", | ||
| "@modelcontextprotocol/ext-apps": "^1.7.4", | ||
| "@modelcontextprotocol/sdk": "^1.21.0", | ||
| "react": "^19.2.7", | ||
| "react-dom": "^19.2.7", | ||
| "zod": "^3.25.76" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/react": "^19.2.7", | ||
| "@types/react-dom": "^19.2.3", | ||
| "@vitejs/plugin-react": "^5.1.2", | ||
| "prettier": "^3.8.4", | ||
| "typescript": "5.9.3", | ||
| "vite": "^7.3.4", | ||
| "vite-plugin-singlefile": "^2.3.3", | ||
| "vitest": "^4.1.8" | ||
| }, | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "build:all": "yarn run build", | ||
| "format": "prettier --write .", | ||
| "format:check": "prettier --check .", | ||
| "lint": "tsc --noEmit", | ||
| "test": "vitest run" | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
samples/community/mcp/a2ui-in-mcpapps/server/apps/react/react.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <!doctype html> | ||
| <!-- | ||
| Copyright 2026 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Generic A2UI Renderer</title> | ||
| <style> | ||
| body { | ||
| margin: 0; | ||
| font-family: system-ui, sans-serif; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/main.tsx"></script> | ||
| </body> | ||
| </html> |
95 changes: 95 additions & 0 deletions
95
samples/community/mcp/a2ui-in-mcpapps/server/apps/react/src/extract-a2ui-messages.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /** | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import {describe, expect, it, vi} from 'vitest'; | ||
| import {extractA2uiMessages} from './extract-a2ui-messages'; | ||
|
|
||
| const MESSAGE = { | ||
| version: 'v0.9', | ||
| updateDataModel: {surfaceId: 'counter', path: '/counter', value: 1}, | ||
| }; | ||
|
|
||
| function resourceBlock(text: string, mimeType = 'application/a2ui+json') { | ||
| return {type: 'resource', resource: {uri: 'a2ui://test', mimeType, text}}; | ||
| } | ||
|
|
||
| describe('extractA2uiMessages', () => { | ||
| it('returns [] for non-array content', () => { | ||
| expect(extractA2uiMessages(undefined)).toEqual([]); | ||
| expect(extractA2uiMessages(null)).toEqual([]); | ||
| expect(extractA2uiMessages({})).toEqual([]); | ||
| }); | ||
|
|
||
| it('extracts an array payload from an a2ui+json resource', () => { | ||
| const content = [resourceBlock(JSON.stringify([MESSAGE, MESSAGE]))]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE, MESSAGE]); | ||
| }); | ||
|
|
||
| it('wraps a single-message payload in an array', () => { | ||
| const content = [resourceBlock(JSON.stringify(MESSAGE))]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE]); | ||
| }); | ||
|
|
||
| it('accepts the legacy application/json+a2ui mime type', () => { | ||
| const content = [resourceBlock(JSON.stringify([MESSAGE]), 'application/json+a2ui')]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE]); | ||
| }); | ||
|
|
||
| it('collects payloads from multiple resources in order', () => { | ||
| const other = {...MESSAGE, updateDataModel: {...MESSAGE.updateDataModel, value: 2}}; | ||
| const content = [ | ||
| resourceBlock(JSON.stringify([MESSAGE])), | ||
| {type: 'text', text: 'ignored'}, | ||
| resourceBlock(JSON.stringify([other])), | ||
| ]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE, other]); | ||
| }); | ||
|
|
||
| it('ignores non-resource blocks and other mime types', () => { | ||
| const content = [ | ||
| {type: 'text', text: JSON.stringify([MESSAGE])}, | ||
| resourceBlock(JSON.stringify([MESSAGE]), 'text/html'), | ||
| {type: 'resource', resource: {uri: 'a2ui://no-text', mimeType: 'application/a2ui+json'}}, | ||
| ]; | ||
| expect(extractA2uiMessages(content)).toEqual([]); | ||
| }); | ||
|
|
||
| it('ignores JSON scalars and null payloads', () => { | ||
| const content = [resourceBlock('null'), resourceBlock('123'), resourceBlock('"text"')]; | ||
| expect(extractA2uiMessages(content)).toEqual([]); | ||
| }); | ||
|
|
||
| it('drops null and scalar entries inside array payloads', () => { | ||
| const content = [resourceBlock(JSON.stringify([null, 42, 'text', MESSAGE]))]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE]); | ||
| }); | ||
|
|
||
| it('accepts mime types with parameters and different casing', () => { | ||
| const content = [ | ||
| resourceBlock(JSON.stringify([MESSAGE]), 'application/a2ui+json; charset=utf-8'), | ||
| resourceBlock(JSON.stringify([MESSAGE]), 'Application/A2UI+JSON'), | ||
| ]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE, MESSAGE]); | ||
| }); | ||
|
|
||
| it('skips resources with invalid JSON and keeps the rest', () => { | ||
| const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
| const content = [resourceBlock('{not json'), resourceBlock(JSON.stringify([MESSAGE]))]; | ||
| expect(extractA2uiMessages(content)).toEqual([MESSAGE]); | ||
| expect(errorSpy).toHaveBeenCalledOnce(); | ||
| errorSpy.mockRestore(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.