Skip to content

fix: report why nextjs_index cannot reach an explicit port - #162

Open
vishalhabib99 wants to merge 1 commit into
vercel:mainfrom
vishalhabib99:fix/nextjs-index-connect-error
Open

vishalhabib99 wants to merge 1 commit into
vercel:mainfrom
vishalhabib99:fix/nextjs-index-connect-error

Conversation

@vishalhabib99

Copy link
Copy Markdown

What's wrong

1. nextjs_index with an explicit port hides "cannot connect". With nothing listening on port 4999:

  • nextjs_index {"port": 4999} returns No MCP tools found on port 4999. The server may not be running Next.js 16+ or MCP may not be enabled.
  • nextjs_call {"port": 4999, ...} on the same port correctly returns Cannot connect to Next.js dev server on port 4999. Make sure the dev server is running. ...

So the agent is told to check the Next.js version when the dev server simply isn't running. A 404, an MCP Error: ... or an invalid SSE response get the same "No MCP tools found" text.

2. Auto-discovery ignores NEXT_DEVTOOLS_HOST in its output. With NEXT_DEVTOOLS_HOST=127.0.0.1, nextjs_index {} reports "url": "http://localhost:3007" for a server on 3007, while nextjs_index {"port": 4555} reports "url": "http://127.0.0.1:4555".

Cause

  1. listNextJsTools (src/_internal/nextjs-runtime-manager.ts:403-415) catches every error and returns []. probeAndListTools (src/tools/nextjs_index.ts:86-92) reads [] as "not Next.js 16+", so its own catch ("Failed to connect to port ...") never runs.
  2. src/tools/nextjs_index.ts:157 hard-codes localhost. The explicit-port path (line 99) uses MCP_HOST.

Fix

  1. Split listNextJsTools into fetchNextJsTools (rejects with the reason) and listNextJsTools (same as before: logs and returns []). probeAndListTools calls fetchNextJsTools, so an explicit port now returns Failed to connect to port 4999: Cannot connect to Next.js dev server on port 4999. .... Auto-discovery still uses listNextJsTools, so one failing candidate still drops out without failing the whole listing (fix: exclude servers without a valid MCP tool listing #154).
  2. Use MCP_HOST in the auto-discovery URL.

Includes a patch changeset.

Tests

  • New test/unit/nextjs-index-errors.test.ts: calls nextjs_index on a port with nothing listening and expects the "Cannot connect" reason. No mocks, no network beyond loopback.
  • test/unit/nextjs-index-validation.test.ts: the module mock now sets MCP_HOST to 127.0.0.1 and provides fetchNextJsTools. A new case checks that an auto-discovered server's URL uses that host and matches the explicit-port URL. The existing explicit-port case now mocks fetchNextJsTools.

Both new tests fail on main (Received: "No MCP tools found on port ..." and Received: "http://localhost:3001") and pass with the fix.

pnpm build and pnpm typecheck pass. pnpm test: 50 passed, 1 skipped (main: 48 passed, 1 skipped). In both runs test/unit/nextjs-runtime-discovery.test.ts fails in setup locally because it shells out to pnpm install for its fixture app and pnpm isn't on my PATH. That's unrelated to this change. I didn't run the e2e tests.

listNextJsTools catches every request error and returns [], so
nextjs_index {port} said "No MCP tools found ... may not be running
Next.js 16+" when nothing was listening on the port. The explicit-port
path now uses fetchNextJsTools, which rejects with the real reason
(e.g. "Cannot connect to Next.js dev server on port N"). Auto-discovery
keeps listNextJsTools, so one failing candidate still doesn't fail the
whole listing.

Auto-discovered servers also reported http://localhost:<port> even when
NEXT_DEVTOOLS_HOST was set; they now use MCP_HOST like the
explicit-port path.
@vishalhabib99

Copy link
Copy Markdown
Author

Hi @gaojude, just checking this one is still on your radar. It's still mergeable, and both new tests still fail on main: an explicit port with nothing listening reports No MCP tools found on port ... instead of why the connection failed. They pass here. Happy to change anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant