Skip to content

Commit 6118fe4

Browse files
feat(mcp): return logs on code tool errors
1 parent 1e866f8 commit 6118fe4

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
@@ -186,6 +186,8 @@ const fetch = async (req: Request): Promise<Response> => {
186186
{
187187
message:
188188
'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```',
189+
logLines: [],
190+
errLines: [],
189191
} satisfies WorkerError,
190192
{ status: 400, statusText: 'Code execution error' },
191193
);
@@ -197,6 +199,8 @@ const fetch = async (req: Request): Promise<Response> => {
197199
{
198200
message:
199201
'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```',
202+
logLines: [],
203+
errLines: [],
200204
} satisfies WorkerError,
201205
{ status: 400, statusText: 'Code execution error' },
202206
);
@@ -229,6 +233,8 @@ const fetch = async (req: Request): Promise<Response> => {
229233
return Response.json(
230234
{
231235
message: parseError(code, e),
236+
logLines,
237+
errLines,
232238
} satisfies WorkerError,
233239
{ status: 400, statusText: 'Code execution error' },
234240
);

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

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

0 commit comments

Comments
 (0)