-
-
Save ochafik/c024f6749b7207988544705a7a38b3d9 to your computer and use it in GitHub Desktop.
Pattern to reuse iframe-embed as an MCP App
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
| import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps"; | |
| function getMode() { | |
| return new URL(window.location.href).searchParams.has('mcp-app')) ? 'mcp-app' : 'embed'; | |
| } | |
| // Returns the query params when embedded, or the MCP tool result' structuredContent | |
| async function getParameters(): Record<string, string> { | |
| if (getMode() === 'mcp-app') { | |
| const app = new McpApp({name: 'Our App', version: ''}); | |
| await app.connect(new PostMessageTransport()); | |
| return new Promise(resolve => { | |
| app.ontoolresult = ({structuredContent}) => resolve(structuredContent); | |
| // Note: inputs are available too. | |
| }); | |
| } else { | |
| return Object.fromEntries([...new URL(window.location.href).searchParams]) | |
| } | |
| } | |
| async function main() { | |
| const params = await getParameters(); | |
| ... | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment