From d32c82bb69906955328894396176260c7c72b7d3 Mon Sep 17 00:00:00 2001 From: Brian Strauch Date: Mon, 10 Aug 2026 08:58:38 -0700 Subject: [PATCH 1/2] fix(ai-sdk): spawn the MCP server without requiring a build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- ai-sdk/src/mcp-server.ts | 3 ++- ai-sdk/src/worker.ts | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ai-sdk/src/mcp-server.ts b/ai-sdk/src/mcp-server.ts index 61f98c105..73c3cdcc8 100644 --- a/ai-sdk/src/mcp-server.ts +++ b/ai-sdk/src/mcp-server.ts @@ -46,7 +46,8 @@ server.registerTool( async function main() { const transport = new StdioServerTransport(); await server.connect(transport); - console.log('Pokemon MCP Server running on stdio'); + // Log to stderr: stdout is the JSON-RPC channel for a stdio MCP server. + console.error('Pokemon MCP Server running on stdio'); } main().catch((error) => { diff --git a/ai-sdk/src/worker.ts b/ai-sdk/src/worker.ts index 36d7425b1..90219f8a8 100644 --- a/ai-sdk/src/worker.ts +++ b/ai-sdk/src/worker.ts @@ -1,3 +1,4 @@ +import path from 'node:path'; import { NativeConnection, Worker } from '@temporalio/worker'; import * as activities from './activities'; import { AiSdkPlugin } from '@temporalio/ai-sdk'; @@ -12,12 +13,23 @@ async function run() { try { // This is only used by the MCP Sample // @@@SNIPSTART typescript-vercel-ai-sdk-mcp-client-factories + // Resolve the MCP server script to an absolute path so it's found regardless of the Worker's + // cwd, and run it with the same runtime as the Worker: ts-node when running the sources in + // `src`, plain node when running the compiled output in `lib`. + const mcpServerPath = require.resolve('./mcp-server'); + const mcpServerArgs = mcpServerPath.endsWith('.ts') + ? ['-r', require.resolve('ts-node/register'), mcpServerPath] + : [mcpServerPath]; + const mcpClientFactories = { testServer: () => createMCPClient({ transport: new StdioClientTransport({ - command: 'node', - args: ['lib/mcp-server.js'], + // The same Node binary that's running this Worker. + command: process.execPath, + args: mcpServerArgs, + // Run from the project root so ts-node picks up this project's tsconfig.json. + cwd: path.resolve(__dirname, '..'), }), }), }; From e6905a92e192f81efceb5eaf45ad2f7078cf3fee Mon Sep 17 00:00:00 2001 From: Brian Strauch Date: Mon, 10 Aug 2026 09:22:54 -0700 Subject: [PATCH 2/2] Remove verbose comments in snipsync area Co-authored-by: Brian Strauch --- ai-sdk/src/worker.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/ai-sdk/src/worker.ts b/ai-sdk/src/worker.ts index 90219f8a8..8d296ba88 100644 --- a/ai-sdk/src/worker.ts +++ b/ai-sdk/src/worker.ts @@ -13,9 +13,6 @@ async function run() { try { // This is only used by the MCP Sample // @@@SNIPSTART typescript-vercel-ai-sdk-mcp-client-factories - // Resolve the MCP server script to an absolute path so it's found regardless of the Worker's - // cwd, and run it with the same runtime as the Worker: ts-node when running the sources in - // `src`, plain node when running the compiled output in `lib`. const mcpServerPath = require.resolve('./mcp-server'); const mcpServerArgs = mcpServerPath.endsWith('.ts') ? ['-r', require.resolve('ts-node/register'), mcpServerPath] @@ -25,10 +22,8 @@ async function run() { testServer: () => createMCPClient({ transport: new StdioClientTransport({ - // The same Node binary that's running this Worker. command: process.execPath, args: mcpServerArgs, - // Run from the project root so ts-node picks up this project's tsconfig.json. cwd: path.resolve(__dirname, '..'), }), }),