Skip to content

Commit 4b5b5ea

Browse files
chore: use structured error when code execution tool errors
1 parent 26f73e0 commit 4b5b5ea

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

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

100100
req.write(body, (err) => {
101-
if (err !== null && err !== undefined) {
101+
if (err != null) {
102102
reject(err);
103103
}
104104
});
@@ -109,12 +109,12 @@ export async function codeTool(): Promise<Endpoint> {
109109
if (resp.status === 200) {
110110
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
111111
const returnOutput: ContentBlock | null =
112-
result === null ? null
113-
: result === undefined ? null
114-
: {
112+
result == null ? null : (
113+
{
115114
type: 'text',
116-
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
117-
};
115+
text: typeof result === 'string' ? result : JSON.stringify(result),
116+
}
117+
);
118118
const logOutput: ContentBlock | null =
119119
logLines.length === 0 ?
120120
null
@@ -134,10 +134,11 @@ export async function codeTool(): Promise<Endpoint> {
134134
};
135135
} else {
136136
const { message } = (await resp.json()) as WorkerError;
137-
throw new Error(message);
137+
return {
138+
content: message == null ? [] : [{ type: 'text', text: message }],
139+
isError: true,
140+
};
138141
}
139-
} catch (e) {
140-
throw e;
141142
} finally {
142143
worker.terminate();
143144
}

0 commit comments

Comments
 (0)