You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Enabling Next.js MCP Server for Coding Agents
3
3
nav_title: Next.js MCP Server
4
-
description: Learn how to enable the experimental Next.js MCP server to allow agents access to your application state
4
+
description: Learn how to use Next.js MCP support to allow coding agents access to your application state
5
5
---
6
6
7
7
The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) is an open standard that allows AI agents and coding assistants to interact with your applications through a standardized interface.
8
8
9
-
Next.js provides two MCP servers, we recommend using both together for the best agent experience:
10
-
11
-
### 1. Next.js MCP Server (Built-in)
12
-
13
-
**Available in Next.js 16 and above**
14
-
15
-
The Next.js MCP server exposes your application's internals, allowing agents to:
16
-
17
-
- Access real-time application state and runtime information
18
-
- Query page metadata, routes, and rendering details
19
-
- Retrieve build errors, runtime errors, and development logs
20
-
- Inspect Server Actions and component hierarchies
21
-
22
-
This server runs within your Next.js dev server and provides direct access to your application's internal state.
23
-
24
-
### 2. Next DevTools MCP (External Package)
25
-
26
-
[`next-devtools-mcp`](https://www.npmjs.com/package/next-devtools-mcp) is a separate package that provides high-level development tools and guidance:
27
-
28
-
-**Next.js Knowledge Base**: Query comprehensive Next.js documentation and best practices
29
-
-**Migration and Upgrade Tools**: Automated helpers for upgrading to Next.js 16 with codemods
30
-
-**Cache Components Guide**: Setup and configuration assistance for Cache Components
31
-
-**Browser Testing**: [Playwright MCP](https://github.com/microsoft/playwright-mcp) integration for verifying pages in the browser
32
-
33
-
Additionally, `next-devtools-mcp` can automatically discover and communicate directly with running Next.js dev servers to access their MCP tools (`get_errors`, `get_logs`, `get_page_metadata`, etc.).
34
-
35
-
Together, these two MCP servers give agents both low-level application internals and high-level development guidance.
9
+
Next.js 16+ includes MCP support that enables coding agents to access your application's internals in real-time. To use this functionality, install the [`next-devtools-mcp`](https://www.npmjs.com/package/next-devtools-mcp) package.
36
10
37
11
## Getting started
38
12
39
-
The built-in Next.js MCP server is enabled by default in development in Next.js 16 or above.
13
+
**Requirements:**Next.js 16 or above
40
14
41
-
### Configuring Next DevTools MCP
42
-
43
-
To use the Next DevTools MCP, add [`next-devtools-mcp`](https://www.npmjs.com/package/next-devtools-mcp) to the `.mcp.json` file at the root of your project
15
+
Add `next-devtools-mcp` to the `.mcp.json` file at the root of your project:
44
16
45
17
```json filename=".mcp.json"
46
18
{
@@ -53,20 +25,30 @@ To use the Next DevTools MCP, add [`next-devtools-mcp`](https://www.npmjs.com/pa
53
25
}
54
26
```
55
27
56
-
For more devtools functionalities and configuration options, check out the [next-devtools-mcp repository](https://github.com/vercel/next-devtools-mcp).
28
+
That's it! When you start your development server, `next-devtools-mcp` will automatically discover and connect to your running Next.js instance.
57
29
58
-
## Using with agents
30
+
For more configuration options, see the [next-devtools-mcp repository](https://github.com/vercel/next-devtools-mcp).
59
31
60
-
MCP-compatible Coding Agents can automatically discover and connect to your Next.js development server. This provides agents with rich context about your project.
32
+
## Capabilities
61
33
62
-
### Benefits for agent-assisted development
34
+
`next-devtools-mcp` provides coding agents with a growing set of capabilities:
63
35
64
-
Agents can:
36
+
### Application Runtime Access
65
37
66
-
-**Make context-aware suggestions**: Recommend the right place to add new features based on your existing structure
67
-
-**Query live application state**: Check current configuration, routes, and middleware during development
68
-
-**Understand your app router pages layout**: Know exactly which page and layout are rendered in the page
69
-
-**Provide accurate implementations**: Generate code that follows your project's patterns and conventions
38
+
-**Error Detection**: Retrieve current build errors, runtime errors, and type errors from your dev server
39
+
-**Live State Queries**: Access real-time application state and runtime information
40
+
-**Page Metadata**: Query page routes, components, and rendering details
41
+
-**Server Actions**: Inspect Server Actions and component hierarchies
42
+
-**Development Logs**: Access development server logs and console output
43
+
44
+
### Development Tools
45
+
46
+
-**Next.js Knowledge Base**: Query comprehensive Next.js documentation and best practices
47
+
-**Migration and Upgrade Tools**: Automated helpers for upgrading to Next.js 16 with codemods
48
+
-**Cache Components Guide**: Setup and configuration assistance for Cache Components
49
+
-**Browser Testing**: [Playwright MCP](https://github.com/microsoft/playwright-mcp) integration for verifying pages in the browser
50
+
51
+
> **Note:** The Next.js team is actively expanding these capabilities. New tools and features are added regularly to improve the agent development experience.
70
52
71
53
## Development workflow
72
54
@@ -76,37 +58,50 @@ Agents can:
76
58
npm run dev
77
59
```
78
60
79
-
2.Connect your Coding Agent to the running Next.js instance
61
+
2.Your Coding Agent will automatically connect to the running Next.js instance via `next-devtools-mcp`
80
62
81
-
3. Open your application in the browser to view the page.
63
+
3. Open your application in the browser to view pages
82
64
83
65
4. Query your agent for insights and diagnostics (see examples below)
84
66
85
-
### Available Next.js MCP Tools
67
+
### Available tools
86
68
87
-
The experimental Next.js MCP server provides the following tools:
69
+
Through `next-devtools-mcp`, agents can use the following tools:
88
70
89
71
-**`get_errors`**: Retrieve current build errors, runtime errors, and type errors from your dev server
90
-
-**`get_logs`**: Access development server logs and console output
72
+
-**`get_logs`**: Get the path to the development log file containing browser console logs and server output
91
73
-**`get_page_metadata`**: Get metadata about specific pages including routes, components, and rendering information
92
-
-**`get_project_metadata`**: Retrieve project structure, configuration, and overall metadata
93
-
-**`get_server_action_by_id`**: Look up Server Actions by their ID for debugging and inspection
74
+
-**`get_project_metadata`**: Retrieve project structure, configuration, and dev server URL
75
+
-**`get_server_action_by_id`**: Look up Server Actions by their ID to find the source file and function name
76
+
77
+
## Using with agents
78
+
79
+
MCP-compatible Coding Agents can automatically discover and connect to your Next.js development server, providing them with rich context about your project.
80
+
81
+
### Benefits for agent-assisted development
82
+
83
+
Agents can:
84
+
85
+
-**Make context-aware suggestions**: Recommend the right place to add new features based on your existing structure
86
+
-**Query live application state**: Check current configuration, routes, and middleware during development
87
+
-**Understand your app router pages layout**: Know exactly which page and layout are rendered
88
+
-**Provide accurate implementations**: Generate code that follows your project's patterns and conventions
94
89
95
90
## Examples
96
91
97
92
### Error detection and diagnosis
98
93
99
-
The agent can query your running Next.js application for real-time error information:
94
+
Ask your agent about errors in real-time:
100
95
101
96
```bash
102
97
User: "What errors are currently in my application?"
103
98
```
104
99
105
-
Agent action:
100
+
The agent will:
106
101
107
-
-Calls the `get_errors` tool provided by the Next.js MCP server
108
-
-Retrieves current build errors, runtime errors, and type errors from the dev server
109
-
-Analyzes the errors and provides actionable fixes
102
+
-Query your running Next.js application via `next-devtools-mcp`
103
+
-Retrieve current build errors, runtime errors, and type errors
104
+
-Analyze the errors and provide actionable fixes
110
105
111
106
<details>
112
107
<summary>Agent response (click to expand)</summary>
@@ -141,32 +136,40 @@ Agent action:
141
136
142
137
</details>
143
138
144
-
This demonstrates the power of having both MCP servers enabled: the Next.js MCP server provides real-time diagnostics directly from your application, while next-devtools-mcp can provide guidance on best practices and migration paths.
145
-
146
139
### Upgrading and best practices
147
140
148
-
You can also ask for help with Next.js concepts and migrations:
141
+
Get help with Next.js concepts and migrations:
149
142
150
143
```bash
151
144
User: "Help me upgrade my Next.js app to version 16"
152
145
```
153
146
154
-
The agent will use next-devtools-mcp to analyze your current version, guide you through automated migrations with codemods, and provide step-by-step instructions for handling breaking changes like async Request APIs, dynamic route segments, and route handler return types.
147
+
The agent will analyze your current version, guide you through automated migrations with codemods, and provide step-by-step instructions for handling breaking changes.
155
148
156
-
You can also ask conceptual questions:
149
+
Ask conceptual questions:
157
150
158
151
```bash
159
152
User: "When should I use 'use client' in App Router?"
160
153
```
161
154
162
-
The agent will query the Next.js knowledge base via next-devtools-mcp and provide documentation-backed explanations with examples from your codebase.
155
+
The agent will query the Next.js knowledge base and provide documentation-backed explanations with examples from your codebase.
156
+
157
+
## How it works
158
+
159
+
Next.js 16+ includes a built-in MCP endpoint at `/_next/mcp` that runs within your development server. The `next-devtools-mcp` package automatically discovers and communicates with these endpoints, allowing it to:
160
+
161
+
- Connect to multiple Next.js instances running on different ports
162
+
- Forward tool calls to the appropriate Next.js dev server
163
+
- Provide a unified interface for coding agents
164
+
165
+
This architecture decouples the agent interface from the internal implementation, enabling `next-devtools-mcp` to work seamlessly across different Next.js projects.
163
166
164
167
## Troubleshooting
165
168
166
169
### MCP server not connecting
167
170
168
171
- Ensure you're using Next.js v16 or above
172
+
- Verify `next-devtools-mcp` is configured in your `.mcp.json`
169
173
- Start your development server: `npm run dev`
170
-
- Restart your development server
171
-
- Check that your MCP client is configured with the correct path
172
-
- Add MCP server to your agent's settings
174
+
- Restart your development server if it was already running
175
+
- Check that your coding agent has loaded the MCP server configuration
0 commit comments