showcase/shell-docs/src/content/docs/integrations/built-in-agent/generative-ui/mcp-apps.mdx
MCP Apps are MCP servers that expose tools with associated UI resources. When the agent calls one of these tools, CopilotKit automatically fetches and renders the UI component in the chat — no additional frontend code required.
Key benefits:
```bash
npm install @ag-ui/mcp-apps-middleware
```
Use `.use()` to attach the `MCPAppsMiddleware` to your `BuiltInAgent`:
```typescript title="app/api/copilotkit/route.ts"
import {
CopilotRuntime,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { BuiltInAgent } from "@copilotkit/runtime/v2"; // [!code highlight]
import { MCPAppsMiddleware } from "@ag-ui/mcp-apps-middleware"; // [!code highlight]
import { NextRequest } from "next/server";
const agent = new BuiltInAgent({
model: "openai:gpt-5.4-mini",
prompt: "You are a helpful assistant.",
}).use( // [!code highlight:9]
new MCPAppsMiddleware({
mcpServers: [
{
type: "http",
url: "http://localhost:3108/mcp",
serverId: "my-server",
},
],
}),
);
const runtime = new CopilotRuntime({
agents: { default: agent },
});
export const POST = async (req: NextRequest) => {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
endpoint: "/api/copilotkit",
});
return handleRequest(req);
};
```
<Callout type="info" title="Server ID">
Always provide a `serverId` for production deployments. Without it, CopilotKit generates a hash from the server URL. If your URL changes (e.g., different environments), previously stored MCP Apps in conversation history won't load correctly.
</Callout>
That's it. MCP Apps will render automatically when the agent uses tools that have associated UI resources. No changes to your frontend are needed.
The middleware supports two transport types:
For MCP servers using HTTP-based communication:
{
type: "http",
url: "http://localhost:3101/mcp",
serverId: "my-http-server"
}
For MCP servers using Server-Sent Events:
{
type: "sse",
url: "https://mcp.example.com/sse",
headers: {
"Authorization": "Bearer token"
},
serverId: "my-sse-server"
}
Try these open-source MCP Apps servers to get started: