Overview
The Next.js adapter allows you to add MCP capabilities to your Next.js applications using App Router API routes. It provides first-class support for OAuth 2.0 Bearer token authentication and follows Next.js conventions.Installation
Install xmcp in your Next.js project:Configuration
1. Configure xmcp
Createxmcp.config.ts in your project root:
xmcp.config.ts
2. Create API Route
Createsrc/app/mcp/route.ts (or your preferred endpoint):
src/app/mcp/route.ts
- Accepts POST requests (required for MCP)
- Parses JSON-RPC messages
- Routes to your tools, prompts, and resources
- Returns proper error responses
3. Create Tools
Create tools in your configured directory:src/tools/get-weather.ts
Development
Run both xmcp and Next.js in development:package.json:
package.json
Authentication
The Next.js adapter includes built-in OAuth 2.0 Bearer token authentication following RFC 9728.Basic Authentication
Wrap your handler withwithAuth:
src/app/mcp/route.ts
Required Authentication
Make authentication mandatory:Protected Resource Metadata
Implement the OAuth Protected Resource Metadata endpoint at/.well-known/oauth-protected-resource:
src/app/.well-known/oauth-protected-resource/route.ts
Access Auth Info in Tools
Authentication information is available in your tools via request context:src/tools/get-user-data.ts
API Reference
xmcpHandler
Main handler for MCP requests.
- Parameters:
request: Web API Request object
- Returns: Web API Response object
- Handles: POST requests only, returns 405 for other methods
withAuth
Wraps a handler with OAuth 2.0 Bearer token authentication.
- Parameters:
handler: The base MCP handlerconfig: Authentication configurationverifyToken: Function to verify bearer tokensrequired: Whether auth is required (default: false)requiredScopes: Array of required OAuth scopes
- Returns: Authenticated handler function
resourceMetadataHandler
Creates a handler for the OAuth Protected Resource Metadata endpoint.
- Parameters:
authorizationServers: Array of authorization server URLs
- Returns: Handler function for GET requests
resourceMetadataOptions
CORS OPTIONS handler for the metadata endpoint.
Types
AuthConfig
OAuthProtectedResourceMetadata
Error Handling
The adapter automatically handles errors:- 405 Method Not Allowed: Non-POST requests
- 401 Unauthorized: Invalid or missing token (when required)
- 403 Forbidden: Insufficient scopes
- 500 Internal Server Error: Server errors
Example Project Structure
Best Practices
Use environment variables for secrets
Use environment variables for secrets
Never hardcode API keys or secrets. Use Next.js environment variables:
Implement proper token verification
Implement proper token verification
Use established libraries for JWT verification:
Set appropriate CORS headers
Set appropriate CORS headers
The adapter handles CORS automatically, but customize if needed in your route configuration.
Use TypeScript for type safety
Use TypeScript for type safety
xmcp provides full TypeScript support. Always use types for better developer experience.
Troubleshooting
405 Method Not Allowed
405 Method Not Allowed
MCP requires POST requests. Ensure you’re exporting the handler for both GET and POST:
Tools not found
Tools not found
Verify your
xmcp.config.ts paths match your directory structure and run xmcp build.Authentication not working
Authentication not working
Check that:
- Bearer token is in the Authorization header
- Token verification logic is correct
- Required scopes match what’s in the token
Next Steps
Building Tools
Learn how to create powerful tools
Express Adapter
Check out the Express adapter