Skip to content

Instantly share code, notes, and snippets.

@ochafik
Created November 26, 2025 21:24
Show Gist options
  • Select an option

  • Save ochafik/c024f6749b7207988544705a7a38b3d9 to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/c024f6749b7207988544705a7a38b3d9 to your computer and use it in GitHub Desktop.
Pattern to reuse iframe-embed as an MCP App
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