-
Notifications
You must be signed in to change notification settings - Fork 36
Feat/mcp apps spec support #2315
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
vibegui
wants to merge
13
commits into
main
Choose a base branch
from
feat/mcp-apps-spec-support
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
13 commits
Select commit
Hold shift + click to select a range
e2c9d6a
feat(mcp-apps): initial implementation of MCP Apps spec
vibegui 68a5161
feat(mcp-apps): fix chat UI rendering and resource loading
vibegui 1f36d15
feat(mcp-apps): enhance MCP app rendering and resource loading
vibegui 37dcd30
fix(mcp-apps): handle iframe already loaded race condition
vibegui 537370c
feat(ui-widgets): add 20 core UI widget tools
vibegui 3a23ee4
fix(mcp-apps): prevent UI flash during resource preview loading
vibegui d8865bb
Implement cubic review fixes
vibegui 194222c
test(mcp-apps): add unit tests for MCP Apps feature
vibegui 4d4ba55
fix(mcp-apps): defer render-time state updates using queueMicrotask
vibegui b491f3c
Fix errors
vibegui 54f272a
feat(ui-widgets): add 10 new shadcn-inspired widgets and improvements
vibegui 0944ddc
refactor(ui-widgets): remove Badge and Skeleton widgets
vibegui 803e191
refactor(mcp-apps): improve resource loading logic in AppPreviewDialog
vibegui 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "permissions": { | ||
| "allow": ["Bash(git remote prune:*)"] | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * App Preview Dialog | ||
| * | ||
| * Dialog component for previewing MCP Apps in the connection detail page. | ||
| * Shows the app in a sandboxed iframe with full interactive capabilities. | ||
| */ | ||
|
|
||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from "@deco/ui/components/dialog.tsx"; | ||
| import { useState, useRef } from "react"; | ||
| import { MCPAppRenderer } from "./mcp-app-renderer.tsx"; | ||
| import type { UIResourcesReadResult, UIToolsCallResult } from "./types.ts"; | ||
| import { UIResourceLoader, UIResourceLoadError } from "./resource-loader.ts"; | ||
|
|
||
| // ============================================================================ | ||
| // Types | ||
| // ============================================================================ | ||
|
|
||
| export interface AppPreviewDialogProps { | ||
| /** Whether the dialog is open */ | ||
| open: boolean; | ||
| /** Callback when the dialog should close */ | ||
| onOpenChange: (open: boolean) => void; | ||
| /** The URI of the resource to preview */ | ||
| uri: string; | ||
| /** The name of the resource */ | ||
| name?: string; | ||
| /** Connection ID for the MCP server */ | ||
| connectionId: string; | ||
| /** Function to read resources from the MCP server */ | ||
| readResource: (uri: string) => Promise<{ | ||
| contents: Array<{ | ||
| uri: string; | ||
| mimeType?: string; | ||
| text?: string; | ||
| blob?: string; | ||
| }>; | ||
| }>; | ||
| /** Function to call tools on the MCP server */ | ||
| callTool: ( | ||
| name: string, | ||
| args: Record<string, unknown>, | ||
| ) => Promise<UIToolsCallResult>; | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Component | ||
| // ============================================================================ | ||
|
|
||
| /** | ||
| * Dialog for previewing MCP Apps | ||
| * | ||
| * Fetches the UI resource content and renders it in the MCPAppRenderer. | ||
| */ | ||
| /** | ||
| * Component that triggers loading on mount (used to avoid render-time side effects) | ||
| */ | ||
| function LoadTrigger({ onLoad }: { onLoad: () => void }) { | ||
| const loadedRef = useRef(false); | ||
| if (!loadedRef.current) { | ||
cubic-dev-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| loadedRef.current = true; | ||
| queueMicrotask(onLoad); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export function AppPreviewDialog({ | ||
| open, | ||
| onOpenChange, | ||
| uri, | ||
| name, | ||
| connectionId, | ||
| readResource, | ||
| callTool, | ||
| }: AppPreviewDialogProps) { | ||
| const [html, setHtml] = useState<string | null>(null); | ||
| const [loading, setLoading] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| // Load resource content | ||
| const loadResource = () => { | ||
| setLoading(true); | ||
| setError(null); | ||
| (async () => { | ||
| try { | ||
| const loader = new UIResourceLoader(); | ||
| const content = await loader.load(uri, readResource); | ||
| setHtml(content.html); | ||
| } catch (err) { | ||
| console.error("Failed to load UI resource:", err); | ||
| if (err instanceof UIResourceLoadError) { | ||
| setError(err.message); | ||
| } else { | ||
| setError( | ||
| err instanceof Error ? err.message : "Failed to load resource", | ||
| ); | ||
| } | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| })(); | ||
| }; | ||
|
|
||
| // Handle dialog close - resets state | ||
| const handleOpenChange = (newOpen: boolean) => { | ||
| if (!newOpen) { | ||
| // Reset state after close animation | ||
| setTimeout(() => { | ||
| setHtml(null); | ||
| setError(null); | ||
| }, 200); | ||
| } | ||
| onOpenChange(newOpen); | ||
| }; | ||
|
|
||
| // Determine if we need to trigger a load | ||
| const needsLoad = open && !html && !loading && !error; | ||
|
|
||
| // Wrapper for readResource to match the expected interface | ||
| const handleReadResource = async ( | ||
| resourceUri: string, | ||
| ): Promise<UIResourcesReadResult> => { | ||
| const result = await readResource(resourceUri); | ||
| return { contents: result.contents }; | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={open} onOpenChange={handleOpenChange}> | ||
| <DialogContent className="max-w-4xl max-h-[90vh] overflow-hidden flex flex-col"> | ||
| <DialogHeader> | ||
| <DialogTitle>{name || uri}</DialogTitle> | ||
| </DialogHeader> | ||
|
|
||
| {/* Trigger load when dialog is open and content not loaded */} | ||
| {needsLoad && <LoadTrigger onLoad={loadResource} />} | ||
|
|
||
| <div className="flex-1 min-h-0 overflow-auto"> | ||
| {loading && ( | ||
| <div className="flex items-center justify-center h-64"> | ||
| <div className="flex items-center gap-2 text-muted-foreground"> | ||
| <div className="size-5 border-2 border-current border-t-transparent rounded-full animate-spin" /> | ||
| <span>Loading app...</span> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| {error && ( | ||
| <div className="flex items-center justify-center h-64"> | ||
| <div className="text-destructive text-center"> | ||
| <p className="font-medium">Failed to load app</p> | ||
| <p className="text-sm text-muted-foreground mt-1">{error}</p> | ||
| </div> | ||
| </div> | ||
| )} | ||
|
|
||
| {html && !loading && !error && ( | ||
| <MCPAppRenderer | ||
| html={html} | ||
| uri={uri} | ||
| connectionId={connectionId} | ||
| displayMode="fullscreen" | ||
| minHeight={300} | ||
| maxHeight={600} | ||
| callTool={callTool} | ||
| readResource={handleReadResource} | ||
| className="border border-border" | ||
| /> | ||
| )} | ||
| </div> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
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,112 @@ | ||
| /** | ||
| * CSP Injector Tests | ||
| */ | ||
|
|
||
| import { describe, expect, it } from "bun:test"; | ||
| import { injectCSP, DEFAULT_CSP } from "./csp-injector"; | ||
|
|
||
| describe("CSP Injector", () => { | ||
| describe("DEFAULT_CSP", () => { | ||
| it("should have default-src 'none'", () => { | ||
| expect(DEFAULT_CSP).toContain("default-src 'none'"); | ||
| }); | ||
|
|
||
| it("should allow inline scripts and styles", () => { | ||
| expect(DEFAULT_CSP).toContain("script-src 'unsafe-inline'"); | ||
| expect(DEFAULT_CSP).toContain("style-src 'unsafe-inline'"); | ||
| }); | ||
|
|
||
| it("should block external connections by default", () => { | ||
| expect(DEFAULT_CSP).toContain("connect-src 'none'"); | ||
| }); | ||
|
|
||
| it("should prevent framing", () => { | ||
| expect(DEFAULT_CSP).toContain("frame-ancestors 'none'"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("injectCSP", () => { | ||
| it("should inject CSP into existing <head>", () => { | ||
| const html = "<html><head><title>Test</title></head><body></body></html>"; | ||
| const result = injectCSP(html); | ||
|
|
||
| expect(result).toContain('<meta http-equiv="Content-Security-Policy"'); | ||
| expect(result).toContain(DEFAULT_CSP); | ||
| // Should be after <head> | ||
| expect(result.indexOf("<head>")).toBeLessThan( | ||
| result.indexOf("Content-Security-Policy"), | ||
| ); | ||
| }); | ||
|
|
||
| it("should create <head> if missing", () => { | ||
| const html = "<html><body>Content</body></html>"; | ||
| const result = injectCSP(html); | ||
|
|
||
| expect(result).toContain("<head>"); | ||
| expect(result).toContain("Content-Security-Policy"); | ||
| }); | ||
|
|
||
| it("should work with <!DOCTYPE html>", () => { | ||
| const html = "<!DOCTYPE html><html><body>Test</body></html>"; | ||
| const result = injectCSP(html); | ||
|
|
||
| expect(result).toContain("Content-Security-Policy"); | ||
| expect(result).toContain("<!DOCTYPE html>"); | ||
| }); | ||
|
|
||
| it("should handle uppercase HEAD tag", () => { | ||
| const html = "<html><HEAD><title>Test</title></HEAD><body></body></html>"; | ||
| const result = injectCSP(html); | ||
|
|
||
| expect(result).toContain("Content-Security-Policy"); | ||
| }); | ||
|
|
||
| it("should use custom CSP if provided", () => { | ||
| const customCSP = "default-src 'self'"; | ||
| const html = "<html><head></head></html>"; | ||
| const result = injectCSP(html, { csp: customCSP }); | ||
|
|
||
| expect(result).toContain(customCSP); | ||
| expect(result).not.toContain(DEFAULT_CSP); | ||
| }); | ||
|
|
||
| describe("external connections", () => { | ||
| it("should allow all hosts when allowExternalConnections is true without allowedHosts", () => { | ||
| const html = "<html><head></head></html>"; | ||
| const result = injectCSP(html, { allowExternalConnections: true }); | ||
|
|
||
| expect(result).toContain("connect-src *"); | ||
| expect(result).not.toContain("connect-src 'none'"); | ||
| }); | ||
|
|
||
| it("should use specified hosts when allowedHosts is provided", () => { | ||
| const html = "<html><head></head></html>"; | ||
| const result = injectCSP(html, { | ||
| allowExternalConnections: true, | ||
| allowedHosts: ["https://api.example.com", "https://cdn.example.com"], | ||
| }); | ||
|
|
||
| expect(result).toContain( | ||
| "connect-src https://api.example.com https://cdn.example.com", | ||
| ); | ||
| }); | ||
|
|
||
| it("should treat empty allowedHosts array as wildcard", () => { | ||
| const html = "<html><head></head></html>"; | ||
| const result = injectCSP(html, { | ||
| allowExternalConnections: true, | ||
| allowedHosts: [], | ||
| }); | ||
|
|
||
| expect(result).toContain("connect-src *"); | ||
| }); | ||
|
|
||
| it("should not modify connect-src when allowExternalConnections is false", () => { | ||
| const html = "<html><head></head></html>"; | ||
| const result = injectCSP(html, { allowExternalConnections: false }); | ||
|
|
||
| expect(result).toContain("connect-src 'none'"); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.