Skip to content

Commit 1b6f120

Browse files
chore(internal): support type annotations when running MCP in local execution mode
1 parent 2a5884c commit 1b6f120

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

packages/mcp-server/src/code-tool-worker.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import ts from 'typescript';
77
import { WorkerOutput } from './code-tool-types';
88
import { ContextDev, ClientOptions } from 'context.dev';
99

10+
async function tseval(code: string) {
11+
return import('data:application/typescript;charset=utf-8;base64,' + Buffer.from(code).toString('base64'));
12+
}
13+
1014
function getRunFunctionSource(code: string): {
1115
type: 'declaration' | 'expression';
1216
client: string | undefined;
@@ -256,7 +260,9 @@ const fetch = async (req: Request): Promise<Response> => {
256260

257261
const log_lines: string[] = [];
258262
const err_lines: string[] = [];
259-
const console = {
263+
const originalConsole = globalThis.console;
264+
globalThis.console = {
265+
...originalConsole,
260266
log: (...args: unknown[]) => {
261267
log_lines.push(util.format(...args));
262268
},
@@ -266,7 +272,7 @@ const fetch = async (req: Request): Promise<Response> => {
266272
};
267273
try {
268274
let run_ = async (client: any) => {};
269-
eval(`${code}\nrun_ = run;`);
275+
run_ = (await tseval(`${code}\nexport default run;`)).default;
270276
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
271277
return Response.json({
272278
is_error: false,
@@ -284,6 +290,8 @@ const fetch = async (req: Request): Promise<Response> => {
284290
} satisfies WorkerOutput,
285291
{ status: 400, statusText: 'Code execution error' },
286292
);
293+
} finally {
294+
globalThis.console = originalConsole;
287295
}
288296
};
289297

0 commit comments

Comments
 (0)