Skip to content

Commit 54d9001

Browse files
chore(internal): support type annotations when running MCP in local execution mode
1 parent a75ecde commit 54d9001

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 { Isaacus, ClientOptions } from 'isaacus';
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;
@@ -241,7 +245,9 @@ const fetch = async (req: Request): Promise<Response> => {
241245

242246
const log_lines: string[] = [];
243247
const err_lines: string[] = [];
244-
const console = {
248+
const originalConsole = globalThis.console;
249+
globalThis.console = {
250+
...originalConsole,
245251
log: (...args: unknown[]) => {
246252
log_lines.push(util.format(...args));
247253
},
@@ -251,7 +257,7 @@ const fetch = async (req: Request): Promise<Response> => {
251257
};
252258
try {
253259
let run_ = async (client: any) => {};
254-
eval(`${code}\nrun_ = run;`);
260+
run_ = (await tseval(`${code}\nexport default run;`)).default;
255261
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
256262
return Response.json({
257263
is_error: false,
@@ -269,6 +275,8 @@ const fetch = async (req: Request): Promise<Response> => {
269275
} satisfies WorkerOutput,
270276
{ status: 400, statusText: 'Code execution error' },
271277
);
278+
} finally {
279+
globalThis.console = originalConsole;
272280
}
273281
};
274282

0 commit comments

Comments
 (0)