What are MCP Apps?
MCP Apps are MCP servers that expose tools with associated UI resources. When your agent calls one of these tools, CopilotKit automatically fetches and renders the HTML/JavaScript UI in the chat — no frontend code required. This is the most flexible generative UI approach, providing:- Zero frontend code - UI components are served by the MCP server
- Unlimited creativity - Any HTML, CSS, JavaScript is possible
- Full interactivity - Components can use forms, buttons, charts, animations
- Secure sandboxing - Content runs in isolated iframes
- Direct server communication - The middleware proxies interactions between UI and MCP server
- Thread persistence - MCP Apps are saved in conversation history
How MCP Apps Work
- Agent invokes tool - The LLM decides to call an MCP tool
- MCP Server returns UI - The server sends HTML/JavaScript as a tool response
- Middleware handles it -
MCPAppsMiddlewareprocesses the UI resource - Frontend renders - CopilotKit displays it in a sandboxed iframe
- Interactions flow back - User actions are proxied to the MCP server
Quick Start
Prerequisites
- Node.js 20+
- An MCP server running (see Example Servers)
- OpenAI API key (or your preferred LLM provider)
Step 1: Install Dependencies
Step 2: Configure the Runtime
Add theMCPAppsMiddleware to your agent configuration:
app/api/copilotkit/route.ts
Server ID Importance
Always provide a
serverId for production. Without it, CopilotKit generates a hash from the URL. If your URL changes between environments, previously stored MCP Apps in conversation history won’t load correctly.Step 3: Configure Environment
.env.local
Step 4: Set Up Frontend
app/layout.tsx
Step 5: Add Chat Interface
app/page.tsx
Transport Types
The middleware supports two transport protocols:HTTP Transport
For MCP servers using HTTP-based communication:SSE Transport
For MCP servers using Server-Sent Events:- Your MCP server pushes real-time updates
- You need long-lived connections
- Your infrastructure requires SSE for streaming
Multiple MCP Servers
You can configure multiple MCP servers simultaneously:Creating an MCP Server with UI
Here’s a simple example of an MCP server that returns HTML UI:mcp-server/index.ts
Interactive MCP Apps
MCP Apps can be fully interactive. Users can click buttons, fill forms, and the interactions flow back to the MCP server:Threading and Persistence
MCP Apps integrate with CopilotKit’s threading system:Persistence
When you save a thread, MCP Apps are stored as part of the conversation history.Restoration
Loading a thread restores all MCP Apps with their original state.Server ID Stability
Using consistentserverId values ensures MCP Apps load correctly across sessions:
Security Considerations
MCP Apps run in sandboxed iframes with security restrictions:1. Same-Origin Policy
The iframe is isolated from your main application.2. Content Security Policy
Consider setting CSP headers on your MCP server:3. Input Sanitization
Always sanitize any user input in your HTML:4. HTTPS in Production
Always use HTTPS for production MCP servers, especially with SSE transport.Example MCP Servers
Try these open-source MCP Apps to get started:MCP Apps Examples
Official repository with demo servers including:
- Budget allocators
- Data visualizations
- Interactive dashboards
- Form builders
- Chart components
Comparison with Other Approaches
| Feature | MCP Apps | Static AG-UI | Declarative A2UI |
|---|---|---|---|
| Frontend code | None | React components | Theme config |
| Flexibility | Unlimited | Limited | Medium |
| Setup time | Minutes | Hours | Hours |
| Security | Sandboxed | Trusted | Trusted |
| Cross-platform | Web-only | React-based | Multi-platform |
| Type safety | None | Full | Schema-level |
| Visual consistency | Variable | Guaranteed | Good |
| Third-party | Easy | Hard | Medium |
When to Use MCP Apps
Best For:
- Rapid prototyping and experimentation
- Third-party integrations from external MCP servers
- Internal tools where visual consistency isn’t critical
- Complex visualizations requiring custom HTML/CSS/JS
- Web-only applications
- When development speed is the top priority
Not Ideal For:
- Mission-critical features requiring brand consistency (use Static AG-UI)
- Native mobile/desktop apps (use Declarative A2UI)
- Features needing strong type safety (use Static AG-UI)
- Public-facing, high-traffic features
Best Practices
1. Use Semantic HTML
2. Include Inline Styles
Since the HTML is isolated, include all styles inline or in a<style> tag:
3. Handle Loading States
4. Provide Clear Metadata
5. Test in Isolation First
Before integrating, test your MCP server HTML in a standalone browser:Debugging Tips
Check MCP Server Logs
Inspect iframe Content
In browser DevTools:- Right-click the MCP App
- Select “Inspect”
- View iframe content in Elements tab
Test Transport Directly
Next Steps
MCP SDK Documentation
Official MCP protocol documentation
Example MCP Apps
Open-source MCP Apps servers
Static AG-UI
Compare with React component approach
Declarative A2UI
Explore structured JSON specifications
