Skip to main content
Complete guide for integrating mcp-ts with Next.js applications (App Router and Pages Router).

Step 1: Create API Route

Create an API route handler at app/api/mcp/route.ts:

Step 2: Create Client Component

Create a component at components/McpConnections.tsx:

Step 3: Use in Page

Use the component in your page at app/page.tsx:

AI SDK

To build agentic workflows that use tools from multiple MCP servers, use MultiSessionClient.
For more details, see the AI SDK Adapter documentation.

Pages Router

Step 1: Create API Route

Create pages/api/mcp.ts:

Step 2: Create Component

Same as App Router component above.

Step 3: Use in Page

OAuth Callback Handler

Handle OAuth callbacks at app/oauth/callback-popup/page.tsx (for popups) or app/oauth/callback/page.tsx (for redirects):
If you want a turnkey popup flow in Next.js, the React client also exports:
  • createOAuthPopupRedirectHandler() for useMcp({ onRedirect })
  • useMcpOAuthPopup(...) for opener-side popup coordination
  • McpOAuthCallbackContent for the callback popup page UI/logic
These helpers are optional. If you prefer a branded popup page, pass custom styles/props to McpOAuthCallbackContent, or skip popups entirely and use a normal redirect callback page with finishAuth(state, code).

Environment Variables

Add to .env.local:

Production Deployment

Vercel

  1. Add environment variable in Vercel dashboard:
    • REDIS_URL - Your Redis connection string
  2. Deploy:

Other Platforms

Ensure your platform supports:
  • Node.js runtime (for API routes)
  • Environment variables
  • WebSocket/SSE connections

Complete Example

Here’s a full working example:
app/api/mcp/route.ts
components/McpClient.tsx

Next Steps