|
| 1 | +import { tool } from "ai" |
| 2 | +import { z } from "zod" |
| 3 | +import { |
| 4 | + discoverNextJsServer, |
| 5 | + listNextJsTools, |
| 6 | + callNextJsTool, |
| 7 | + getAllAvailableServers, |
| 8 | +} from "../lib/nextjs-runtime-manager.js" |
| 9 | + |
| 10 | +const nextjsRuntimeInputSchema = z.object({ |
| 11 | + action: z |
| 12 | + .enum(["discover_servers", "list_tools", "call_tool"]) |
| 13 | + .describe( |
| 14 | + "Action to perform: 'discover_servers' finds running Next.js servers, 'list_tools' lists available MCP tools from the Next.js runtime, 'call_tool' calls a specific tool" |
| 15 | + ), |
| 16 | + |
| 17 | + port: z |
| 18 | + .number() |
| 19 | + .optional() |
| 20 | + .describe( |
| 21 | + "Port number of the Next.js dev server. If not provided, will attempt to auto-discover. Required for 'list_tools' and 'call_tool' actions." |
| 22 | + ), |
| 23 | + |
| 24 | + toolName: z |
| 25 | + .string() |
| 26 | + .optional() |
| 27 | + .describe( |
| 28 | + "Name of the Next.js MCP tool to call. Required for 'call_tool' action. Use 'list_tools' first to discover available tool names." |
| 29 | + ), |
| 30 | + |
| 31 | + args: z |
| 32 | + .record(z.string(), z.unknown()) |
| 33 | + .optional() |
| 34 | + .describe( |
| 35 | + "Arguments object to pass to the Next.js MCP tool. MUST be an object (e.g., {param: 'value'}), NOT a string. Only provide this parameter if the tool requires arguments - omit it entirely for tools that take no arguments. Use 'list_tools' to see the inputSchema for each tool." |
| 36 | + ), |
| 37 | +}) |
| 38 | + |
| 39 | +export const nextjsRuntimeTool = tool({ |
| 40 | + description: `Interact with a running Next.js development server's MCP endpoint. |
| 41 | +
|
| 42 | +REQUIREMENTS: |
| 43 | +- Next.js 16 or later (MCP support was added in v16) |
| 44 | +- If you're on Next.js 15 or earlier, use the 'upgrade-nextjs-16' MCP prompt to upgrade first |
| 45 | +
|
| 46 | +Next.js exposes an MCP (Model Context Protocol) endpoint at /_next/mcp when started with: |
| 47 | +- experimental.mcpServer: true in next.config.js, OR |
| 48 | +- __NEXT_EXPERIMENTAL_MCP_SERVER=true environment variable |
| 49 | +
|
| 50 | +This tool allows you to: |
| 51 | +1. Discover running Next.js dev servers and their ports |
| 52 | +2. List available MCP tools/functions exposed by the Next.js runtime |
| 53 | +3. Call those tools to interact with Next.js internals (e.g., get route info, clear cache, etc.) |
| 54 | +
|
| 55 | +Typical workflow: |
| 56 | +1. Use action='discover_servers' to find running Next.js servers |
| 57 | +2. Use action='list_tools' with the discovered port to see available tools and their input schemas |
| 58 | +3. Use action='call_tool' with port, toolName, and args (as an object, only if required) to invoke a specific tool |
| 59 | +
|
| 60 | +IMPORTANT: When calling tools: |
| 61 | +- The 'args' parameter MUST be an object (e.g., {key: "value"}), NOT a string |
| 62 | +- If a tool doesn't require arguments, OMIT the 'args' parameter entirely - do NOT pass {} or "{}" |
| 63 | +- Check the tool's inputSchema from 'list_tools' to see what arguments are required |
| 64 | +
|
| 65 | +If the MCP endpoint is not available: |
| 66 | +1. Check if you're running Next.js 16+ (if not, use the 'upgrade-nextjs-16' prompt) |
| 67 | +2. Ensure the dev server is started with __NEXT_EXPERIMENTAL_MCP_SERVER=true or experimental.mcpServer: true`, |
| 68 | + inputSchema: nextjsRuntimeInputSchema, |
| 69 | + execute: async (args: z.infer<typeof nextjsRuntimeInputSchema>): Promise<string> => { |
| 70 | + try { |
| 71 | + switch (args.action) { |
| 72 | + case "discover_servers": { |
| 73 | + const servers = await getAllAvailableServers() |
| 74 | + |
| 75 | + if (servers.length === 0) { |
| 76 | + return JSON.stringify({ |
| 77 | + success: false, |
| 78 | + message: "No running Next.js dev servers found", |
| 79 | + hint: "Start a Next.js dev server with __NEXT_EXPERIMENTAL_MCP_SERVER=true or experimental.mcpServer: true", |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + return JSON.stringify({ |
| 84 | + success: true, |
| 85 | + servers: servers.map((s) => ({ |
| 86 | + port: s.port, |
| 87 | + pid: s.pid, |
| 88 | + command: s.command, |
| 89 | + })), |
| 90 | + message: `Found ${servers.length} Next.js server(s)`, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + case "list_tools": { |
| 95 | + if (!args.port) { |
| 96 | + const discovered = await discoverNextJsServer() |
| 97 | + if (!discovered) { |
| 98 | + return JSON.stringify({ |
| 99 | + success: false, |
| 100 | + error: |
| 101 | + "No port specified and auto-discovery failed. Use action='discover_servers' first or provide a port.", |
| 102 | + }) |
| 103 | + } |
| 104 | + args.port = discovered.port |
| 105 | + } |
| 106 | + |
| 107 | + const tools = await listNextJsTools(args.port) |
| 108 | + |
| 109 | + return JSON.stringify({ |
| 110 | + success: true, |
| 111 | + port: args.port, |
| 112 | + tools: tools.map((t) => ({ |
| 113 | + name: t.name, |
| 114 | + description: t.description, |
| 115 | + inputSchema: t.inputSchema, |
| 116 | + })), |
| 117 | + message: `Found ${tools.length} tool(s) available on Next.js server at port ${args.port}`, |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + case "call_tool": { |
| 122 | + if (!args.port) { |
| 123 | + const discovered = await discoverNextJsServer() |
| 124 | + if (!discovered) { |
| 125 | + return JSON.stringify({ |
| 126 | + success: false, |
| 127 | + error: |
| 128 | + "No port specified and auto-discovery failed. Use action='discover_servers' first or provide a port.", |
| 129 | + }) |
| 130 | + } |
| 131 | + args.port = discovered.port |
| 132 | + } |
| 133 | + |
| 134 | + if (!args.toolName) { |
| 135 | + return JSON.stringify({ |
| 136 | + success: false, |
| 137 | + error: |
| 138 | + "toolName is required for 'call_tool' action. Use action='list_tools' to discover available tool names.", |
| 139 | + }) |
| 140 | + } |
| 141 | + |
| 142 | + const result = await callNextJsTool(args.port, args.toolName, args.args || {}) |
| 143 | + |
| 144 | + return JSON.stringify({ |
| 145 | + success: true, |
| 146 | + port: args.port, |
| 147 | + toolName: args.toolName, |
| 148 | + result, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + default: |
| 153 | + return JSON.stringify({ |
| 154 | + success: false, |
| 155 | + error: `Unknown action: ${args.action}`, |
| 156 | + }) |
| 157 | + } |
| 158 | + } catch (error) { |
| 159 | + const errorMessage = error instanceof Error ? error.message : String(error) |
| 160 | + return JSON.stringify({ |
| 161 | + success: false, |
| 162 | + error: errorMessage, |
| 163 | + action: args.action, |
| 164 | + }) |
| 165 | + } |
| 166 | + }, |
| 167 | +}) |
0 commit comments