Skip to content

Commit d2a42f3

Browse files
feat(mcp): return logs on code tool errors
1 parent cbb54af commit d2a42f3

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ export type WorkerSuccess = {
1111
logLines: string[];
1212
errLines: string[];
1313
};
14-
export type WorkerError = { message: string | undefined };
14+
export type WorkerError = {
15+
message: string | undefined;
16+
logLines: string[];
17+
errLines: string[];
18+
};

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ const fetch = async (req: Request): Promise<Response> => {
189189
{
190190
message:
191191
'The code param is missing. Provide one containing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
192+
logLines: [],
193+
errLines: [],
192194
} satisfies WorkerError,
193195
{ status: 400, statusText: 'Code execution error' },
194196
);
@@ -200,6 +202,8 @@ const fetch = async (req: Request): Promise<Response> => {
200202
{
201203
message:
202204
'The code is missing a top-level `run` function. Write code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```',
205+
logLines: [],
206+
errLines: [],
203207
} satisfies WorkerError,
204208
{ status: 400, statusText: 'Code execution error' },
205209
);
@@ -232,6 +236,8 @@ const fetch = async (req: Request): Promise<Response> => {
232236
return Response.json(
233237
{
234238
message: parseError(code, e),
239+
logLines,
240+
errLines,
235241
} satisfies WorkerError,
236242
{ status: 400, statusText: 'Code execution error' },
237243
);

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,30 @@ export async function codeTool(): Promise<Endpoint> {
135135
content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
136136
};
137137
} else {
138-
const { message } = (await resp.json()) as WorkerError;
138+
const { message, logLines, errLines } = (await resp.json()) as WorkerError;
139+
const messageOutput: ContentBlock | null =
140+
message == null ? null : (
141+
{
142+
type: 'text',
143+
text: message,
144+
}
145+
);
146+
const logOutput: ContentBlock | null =
147+
logLines.length === 0 ?
148+
null
149+
: {
150+
type: 'text',
151+
text: logLines.join('\n'),
152+
};
153+
const errOutput: ContentBlock | null =
154+
errLines.length === 0 ?
155+
null
156+
: {
157+
type: 'text',
158+
text: 'Error output:\n' + errLines.join('\n'),
159+
};
139160
return {
140-
content: message == null ? [] : [{ type: 'text', text: message }],
161+
content: [messageOutput, logOutput, errOutput].filter((block) => block !== null),
141162
isError: true,
142163
};
143164
}

0 commit comments

Comments
 (0)