Skip to content

Commit 8c230cf

Browse files
chore(internal): support type annotations when running MCP in local execution mode
1 parent b1df6bc commit 8c230cf

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

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 { CasParser, ClientOptions } from 'cas-parser-node';
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;
@@ -257,7 +261,9 @@ const fetch = async (req: Request): Promise<Response> => {
257261

258262
const log_lines: string[] = [];
259263
const err_lines: string[] = [];
260-
const console = {
264+
const originalConsole = globalThis.console;
265+
globalThis.console = {
266+
...originalConsole,
261267
log: (...args: unknown[]) => {
262268
log_lines.push(util.format(...args));
263269
},
@@ -267,7 +273,7 @@ const fetch = async (req: Request): Promise<Response> => {
267273
};
268274
try {
269275
let run_ = async (client: any) => {};
270-
eval(`${code}\nrun_ = run;`);
276+
run_ = (await tseval(`${code}\nexport default run;`)).default;
271277
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
272278
return Response.json({
273279
is_error: false,
@@ -285,6 +291,8 @@ const fetch = async (req: Request): Promise<Response> => {
285291
} satisfies WorkerOutput,
286292
{ status: 400, statusText: 'Code execution error' },
287293
);
294+
} finally {
295+
globalThis.console = originalConsole;
288296
}
289297
};
290298

0 commit comments

Comments
 (0)