Skip to content

Commit 709d568

Browse files
feat(mcp): return logs on code tool errors
1 parent 53a87cf commit 709d568

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
@@ -187,6 +187,8 @@ const fetch = async (req: Request): Promise<Response> => {
187187
{
188188
message:
189189
'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```',
190+
logLines: [],
191+
errLines: [],
190192
} satisfies WorkerError,
191193
{ status: 400, statusText: 'Code execution error' },
192194
);
@@ -198,6 +200,8 @@ const fetch = async (req: Request): Promise<Response> => {
198200
{
199201
message:
200202
'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```',
203+
logLines: [],
204+
errLines: [],
201205
} satisfies WorkerError,
202206
{ status: 400, statusText: 'Code execution error' },
203207
);
@@ -230,6 +234,8 @@ const fetch = async (req: Request): Promise<Response> => {
230234
return Response.json(
231235
{
232236
message: parseError(code, e),
237+
logLines,
238+
errLines,
233239
} satisfies WorkerError,
234240
{ status: 400, statusText: 'Code execution error' },
235241
);

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

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

0 commit comments

Comments
 (0)