Skip to content

Commit a85e9d8

Browse files
chore: use structured error when code execution tool errors
1 parent b6892f5 commit a85e9d8

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

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

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

102102
req.write(body, (err) => {
103-
if (err !== null && err !== undefined) {
103+
if (err != null) {
104104
reject(err);
105105
}
106106
});
@@ -111,12 +111,12 @@ export async function codeTool(): Promise<Endpoint> {
111111
if (resp.status === 200) {
112112
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
113113
const returnOutput: ContentBlock | null =
114-
result === null ? null
115-
: result === undefined ? null
116-
: {
114+
result == null ? null : (
115+
{
117116
type: 'text',
118-
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
119-
};
117+
text: typeof result === 'string' ? result : JSON.stringify(result),
118+
}
119+
);
120120
const logOutput: ContentBlock | null =
121121
logLines.length === 0 ?
122122
null
@@ -136,10 +136,11 @@ export async function codeTool(): Promise<Endpoint> {
136136
};
137137
} else {
138138
const { message } = (await resp.json()) as WorkerError;
139-
throw new Error(message);
139+
return {
140+
content: message == null ? [] : [{ type: 'text', text: message }],
141+
isError: true,
142+
};
140143
}
141-
} catch (e) {
142-
throw e;
143144
} finally {
144145
worker.terminate();
145146
}

0 commit comments

Comments
 (0)