Skip to content

fix(ai-sdk): spawn the MCP server without requiring a build - #507

Merged
brianstrauch merged 2 commits into
mainfrom
fix/ai-sdk-mcp-server-spawn
Aug 10, 2026
Merged

fix(ai-sdk): spawn the MCP server without requiring a build#507
brianstrauch merged 2 commits into
mainfrom
fix/ai-sdk-mcp-server-spawn

Conversation

@brianstrauch

Copy link
Copy Markdown
Member

What

The ai-sdk sample's MCP client factory spawned the MCP server as node lib/mcp-server.js — a cwd-relative path into compiled output that neither npm start nor npm run start.watch produces, since both run the Worker straight from src via ts-node.

On a fresh clone, npm run workflow mcp failed on every attempt (retried forever) with:

MCPClientError: Connection closed
    at DefaultMCPClient.onClose (@ai-sdk/mcp/src/tool/mcp-client.ts:1263:19)
    at StdioClientTransport.DefaultMCPClient.transport.onclose (@ai-sdk/mcp/src/tool/mcp-client.ts:438:41)
    at ChildProcess.<anonymous> (@modelcontextprotocol/sdk/src/client/stdio.ts:152:31)
    at ChildProcess.emit (node:events:509:28)
    at maybeClose (node:internal/child_process:1124:16)

The child process exited immediately with MODULE_NOT_FOUND, the stdio transport fired onclose, and the pending initialize request was rejected. The message is generic enough that it reads like a broken MCP session rather than "the server script isn't there".

Changes

  • ai-sdk/src/worker.ts — resolve the server with require.resolve('./mcp-server'), so the path is absolute (cwd-independent) and follows whichever output is actually running: src/mcp-server.ts under ts-node, lib/mcp-server.js when compiled. Spawn it with process.execPath (the same Node binary running the Worker) plus -r ts-node/register for the TypeScript case, and set the child cwd to the project root so ts-node finds this project's tsconfig.json.
  • ai-sdk/src/mcp-server.ts — move the startup banner from console.log to console.error. stdout is the JSON-RPC channel of a stdio MCP server, so that line was being fed to the client's read buffer as a bogus message (swallowed as a parse error, but latent breakage).

No build step is required anymore, which matches the README's setup instructions.

Testing

temporal server start-dev + npm start + npm run workflow mcp against a fresh, unbuilt lib/:

Running mcp
Started workflow workflow-PdAVOTgNvQ1fHa0Bq9CQK
Lickitung is a Normal-type Pokémon known for its long tongue and playful nature. ...
  - **Own Tempo**: Prevents Lickitung from becoming confused.
  - **Oblivious**: Prevents Lickitung from being infatuated or taunted.
  - **Cloud Nine**: Negates the effects of weather during battle.
  • testServer-listTools and testServer-callTool both succeeded on the first attempt — no retries, no Connection closed.
  • Confirmed the spawned child was node -r .../ts-node/register .../ai-sdk/src/mcp-server.ts, and the banner landed on the Worker's stderr.
  • Verified the compiled path too: require.resolve('./mcp-server') from lib/worker.js resolves to lib/mcp-server.js and takes the plain-node branch.
  • Verified the ts-node branch works when the Worker is started from an unrelated cwd.
  • npm run build and prettier --check pass.

🤖 Generated with Claude Code

The MCP client factory spawned `node lib/mcp-server.js` — a cwd-relative path
into compiled output that neither `npm start` nor `npm run start.watch`
produces, since both run the Worker straight from `src` via ts-node. On a fresh
clone `npm run workflow mcp` then failed on every attempt with
`MCPClientError: Connection closed`: the child exited immediately with
MODULE_NOT_FOUND, the stdio transport fired `onclose`, and the pending
`initialize` request was rejected.

Resolve the server with `require.resolve('./mcp-server')` so the path is
absolute and follows whichever output is running (`src/*.ts` under ts-node,
`lib/*.js` when compiled), and run it with `process.execPath` plus
`-r ts-node/register` for the TypeScript case.

Also move the startup banner to stderr: stdout is the JSON-RPC channel of a
stdio MCP server, so the client was parsing that line as a message.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@brianstrauch
brianstrauch requested review from a team as code owners August 10, 2026 16:19
Comment thread ai-sdk/src/worker.ts Outdated
Comment thread ai-sdk/src/worker.ts Outdated
Comment thread ai-sdk/src/worker.ts Outdated
Co-authored-by: Brian Strauch <brian@brianstrauch.com>
@brianstrauch
brianstrauch enabled auto-merge (squash) August 10, 2026 16:24
@brianstrauch
brianstrauch merged commit 37b7f94 into main Aug 10, 2026
11 of 13 checks passed
@brianstrauch
brianstrauch deleted the fix/ai-sdk-mcp-server-spawn branch August 10, 2026 16:28
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.

2 participants