Skip to content

Commit 0b9eb86

Browse files
chore: use structured error when code execution tool errors
1 parent 0f7008c commit 0b9eb86

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 CasParser, { ClientOptions } from 'cas-parser-node';
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: CasParser, args: unknown) => {
34+
const handler = async (client: CasParser, args: unknown): Promise<ToolCallResult> => {
3535
const baseURLHostname = new URL(client.baseURL).hostname;
3636
const { code } = args as { code: string };
3737

@@ -97,7 +97,7 @@ export async function codeTool(): Promise<Endpoint> {
9797
} satisfies WorkerInput);
9898

9999
req.write(body, (err) => {
100-
if (err !== null && err !== undefined) {
100+
if (err != null) {
101101
reject(err);
102102
}
103103
});
@@ -108,12 +108,12 @@ export async function codeTool(): Promise<Endpoint> {
108108
if (resp.status === 200) {
109109
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
110110
const returnOutput: ContentBlock | null =
111-
result === null ? null
112-
: result === undefined ? null
113-
: {
111+
result == null ? null : (
112+
{
114113
type: 'text',
115-
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
116-
};
114+
text: typeof result === 'string' ? result : JSON.stringify(result),
115+
}
116+
);
117117
const logOutput: ContentBlock | null =
118118
logLines.length === 0 ?
119119
null
@@ -133,10 +133,11 @@ export async function codeTool(): Promise<Endpoint> {
133133
};
134134
} else {
135135
const { message } = (await resp.json()) as WorkerError;
136-
throw new Error(message);
136+
return {
137+
content: message == null ? [] : [{ type: 'text', text: message }],
138+
isError: true,
139+
};
137140
}
138-
} catch (e) {
139-
throw e;
140141
} finally {
141142
worker.terminate();
142143
}

0 commit comments

Comments
 (0)