Skip to content

Commit 451f306

Browse files
chore: use structured error when code execution tool errors
1 parent 71e22a3 commit 451f306

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { dirname } from 'node:path';
44
import { pathToFileURL } from 'node:url';
55
import ImageKit, { ClientOptions } from '@imagekit/nodejs';
6-
import { Endpoint, ContentBlock, Metadata } from './tools/types';
6+
import { ContentBlock, Endpoint, Metadata, ToolCallResult } from './tools/types';
77

88
import { Tool } from '@modelcontextprotocol/sdk/types.js';
99

@@ -31,7 +31,7 @@ export async function codeTool(): Promise<Endpoint> {
3131
const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
3232
const { workerPath } = await import('./code-tool-paths.cjs');
3333

34-
const handler = async (client: ImageKit, args: unknown) => {
34+
const handler = async (client: ImageKit, args: unknown): Promise<ToolCallResult> => {
3535
const baseURLHostname = new URL(client.baseURL).hostname;
3636
const { code } = args as { code: string };
3737

@@ -99,7 +99,7 @@ export async function codeTool(): Promise<Endpoint> {
9999
} satisfies WorkerInput);
100100

101101
req.write(body, (err) => {
102-
if (err !== null && err !== undefined) {
102+
if (err != null) {
103103
reject(err);
104104
}
105105
});
@@ -110,12 +110,12 @@ export async function codeTool(): Promise<Endpoint> {
110110
if (resp.status === 200) {
111111
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
112112
const returnOutput: ContentBlock | null =
113-
result === null ? null
114-
: result === undefined ? null
115-
: {
113+
result == null ? null : (
114+
{
116115
type: 'text',
117-
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
118-
};
116+
text: typeof result === 'string' ? result : JSON.stringify(result),
117+
}
118+
);
119119
const logOutput: ContentBlock | null =
120120
logLines.length === 0 ?
121121
null
@@ -135,10 +135,11 @@ export async function codeTool(): Promise<Endpoint> {
135135
};
136136
} else {
137137
const { message } = (await resp.json()) as WorkerError;
138-
throw new Error(message);
138+
return {
139+
content: message == null ? [] : [{ type: 'text', text: message }],
140+
isError: true,
141+
};
139142
}
140-
} catch (e) {
141-
throw e;
142143
} finally {
143144
worker.terminate();
144145
}

0 commit comments

Comments
 (0)