|
| 1 | +/** |
| 2 | + * MCP tool for getting all routes that become entry points in a Next.js application. |
| 3 | + * |
| 4 | + * This tool discovers routes by scanning the filesystem directly. It finds all route |
| 5 | + * files in the app/ and pages/ directories and converts them to route paths. |
| 6 | + * |
| 7 | + * Returns routes grouped by router type: |
| 8 | + * - appRouter: App Router pages and route handlers |
| 9 | + * - pagesRouter: Pages Router pages and API routes |
| 10 | + * |
| 11 | + * Dynamic route segments appear as [id], [slug], or [...slug] patterns. This tool |
| 12 | + * does NOT expand getStaticParams - it only shows the route patterns as defined in |
| 13 | + * the filesystem. |
| 14 | + */ |
| 15 | +import type { McpServer } from 'next/dist/compiled/@modelcontextprotocol/sdk/server/mcp' |
| 16 | +import { mcpTelemetryTracker } from '../mcp-telemetry-tracker' |
| 17 | +import { |
| 18 | + collectAppFiles, |
| 19 | + collectPagesFiles, |
| 20 | + processAppRoutes, |
| 21 | + processPageRoutes, |
| 22 | + createPagesMapping, |
| 23 | +} from '../../../build/entries' |
| 24 | +import { createValidFileMatcher } from '../../lib/find-page-file' |
| 25 | +import { PAGE_TYPES } from '../../../lib/page-types' |
| 26 | +import type { NextConfigComplete } from '../../../server/config-shared' |
| 27 | + |
| 28 | +interface RouteInfo { |
| 29 | + route: string |
| 30 | + type: 'app' | 'page' | 'api' |
| 31 | +} |
| 32 | + |
| 33 | +export function registerGetRoutesTool( |
| 34 | + server: McpServer, |
| 35 | + options: { |
| 36 | + projectPath: string |
| 37 | + nextConfig: NextConfigComplete |
| 38 | + pagesDir: string | undefined |
| 39 | + appDir: string | undefined |
| 40 | + } |
| 41 | +) { |
| 42 | + server.registerTool( |
| 43 | + 'get_routes', |
| 44 | + { |
| 45 | + description: |
| 46 | + 'Get all routes that will become entry points in the Next.js application by scanning the filesystem. Returns routes grouped by router type (appRouter, pagesRouter). Dynamic segments appear as [param] or [...slug] patterns. Optional parameters: includeApi (boolean, default true) - whether to include API routes; routerType (string, default "all") - filter by "all", "app", or "pages".', |
| 47 | + }, |
| 48 | + async (request) => { |
| 49 | + // Track telemetry |
| 50 | + mcpTelemetryTracker.recordToolCall('mcp/get_routes') |
| 51 | + |
| 52 | + try { |
| 53 | + const includeApi = |
| 54 | + request.params?.includeApi !== undefined |
| 55 | + ? request.params.includeApi |
| 56 | + : true |
| 57 | + const routerType = |
| 58 | + (request.params?.routerType as 'all' | 'app' | 'pages') || 'all' |
| 59 | + |
| 60 | + const routes: RouteInfo[] = [] |
| 61 | + |
| 62 | + const { projectPath, nextConfig, pagesDir, appDir } = options |
| 63 | + |
| 64 | + // Check if we have any directories to scan |
| 65 | + if (!pagesDir && !appDir) { |
| 66 | + return { |
| 67 | + content: [ |
| 68 | + { |
| 69 | + type: 'text', |
| 70 | + text: 'No pages or app directory found in the project.', |
| 71 | + }, |
| 72 | + ], |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const isSrcDir = |
| 77 | + (pagesDir && pagesDir.includes('/src/')) || |
| 78 | + (appDir && appDir.includes('/src/')) |
| 79 | + |
| 80 | + // Create valid file matcher for filtering |
| 81 | + const validFileMatcher = createValidFileMatcher( |
| 82 | + nextConfig.pageExtensions, |
| 83 | + appDir |
| 84 | + ) |
| 85 | + |
| 86 | + // Collect and process App Router routes if requested |
| 87 | + if (appDir && (routerType === 'all' || routerType === 'app')) { |
| 88 | + try { |
| 89 | + const { appPaths } = await collectAppFiles(appDir, validFileMatcher) |
| 90 | + |
| 91 | + if (appPaths.length > 0) { |
| 92 | + const mappedAppPages = await createPagesMapping({ |
| 93 | + pagePaths: appPaths, |
| 94 | + isDev: true, |
| 95 | + pagesType: PAGE_TYPES.APP, |
| 96 | + pageExtensions: nextConfig.pageExtensions, |
| 97 | + pagesDir, |
| 98 | + appDir, |
| 99 | + appDirOnly: pagesDir ? false : true, |
| 100 | + }) |
| 101 | + |
| 102 | + const { appRoutes, appRouteHandlers } = processAppRoutes( |
| 103 | + mappedAppPages, |
| 104 | + validFileMatcher, |
| 105 | + projectPath, |
| 106 | + isSrcDir || false |
| 107 | + ) |
| 108 | + |
| 109 | + // Add app page routes |
| 110 | + for (const { route } of appRoutes) { |
| 111 | + routes.push({ |
| 112 | + route, |
| 113 | + type: 'app', |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + // Add app route handlers |
| 118 | + for (const { route } of appRouteHandlers) { |
| 119 | + routes.push({ |
| 120 | + route, |
| 121 | + type: 'app', |
| 122 | + }) |
| 123 | + } |
| 124 | + } |
| 125 | + } catch (error) { |
| 126 | + // Error collecting app routes - continue anyway |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Collect and process Pages Router routes if requested |
| 131 | + if (pagesDir && (routerType === 'all' || routerType === 'pages')) { |
| 132 | + try { |
| 133 | + const pagePaths = await collectPagesFiles( |
| 134 | + pagesDir, |
| 135 | + validFileMatcher |
| 136 | + ) |
| 137 | + |
| 138 | + if (pagePaths.length > 0) { |
| 139 | + const mappedPages = await createPagesMapping({ |
| 140 | + pagePaths, |
| 141 | + isDev: true, |
| 142 | + pagesType: PAGE_TYPES.PAGES, |
| 143 | + pageExtensions: nextConfig.pageExtensions, |
| 144 | + pagesDir, |
| 145 | + appDir, |
| 146 | + appDirOnly: false, |
| 147 | + }) |
| 148 | + |
| 149 | + const { pageRoutes, pageApiRoutes } = processPageRoutes( |
| 150 | + mappedPages, |
| 151 | + projectPath, |
| 152 | + isSrcDir || false |
| 153 | + ) |
| 154 | + |
| 155 | + // Add page routes |
| 156 | + for (const { route } of pageRoutes) { |
| 157 | + routes.push({ |
| 158 | + route, |
| 159 | + type: 'page', |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + // Add API routes if requested |
| 164 | + if (includeApi) { |
| 165 | + for (const { route } of pageApiRoutes) { |
| 166 | + routes.push({ |
| 167 | + route, |
| 168 | + type: 'api', |
| 169 | + }) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } catch (error) { |
| 174 | + // Error collecting pages routes - continue anyway |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if (routes.length === 0) { |
| 179 | + return { |
| 180 | + content: [ |
| 181 | + { |
| 182 | + type: 'text', |
| 183 | + text: 'No routes found in the project.', |
| 184 | + }, |
| 185 | + ], |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // Group routes by router type |
| 190 | + const appRoutes = routes |
| 191 | + .filter((r) => r.type === 'app') |
| 192 | + .map((r) => r.route) |
| 193 | + .sort() |
| 194 | + const pageRoutes = routes |
| 195 | + .filter((r) => r.type === 'page' || r.type === 'api') |
| 196 | + .map((r) => r.route) |
| 197 | + .sort() |
| 198 | + |
| 199 | + // Format the output with grouped routes |
| 200 | + const output = { |
| 201 | + appRouter: appRoutes.length > 0 ? appRoutes : undefined, |
| 202 | + pagesRouter: pageRoutes.length > 0 ? pageRoutes : undefined, |
| 203 | + } |
| 204 | + |
| 205 | + return { |
| 206 | + content: [ |
| 207 | + { |
| 208 | + type: 'text', |
| 209 | + text: JSON.stringify(output, null, 2), |
| 210 | + }, |
| 211 | + ], |
| 212 | + } |
| 213 | + } catch (error) { |
| 214 | + return { |
| 215 | + content: [ |
| 216 | + { |
| 217 | + type: 'text', |
| 218 | + text: `Error: ${error instanceof Error ? error.message : String(error)}`, |
| 219 | + }, |
| 220 | + ], |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + ) |
| 225 | +} |
0 commit comments