Skip to content

Commit f8cc9b0

Browse files
feat(mcp): return logs on code tool errors
1 parent 5ae2a33 commit f8cc9b0

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
@@ -146,6 +146,8 @@ const fetch = async (req: Request): Promise<Response> => {
146146
{
147147
message:
148148
'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```',
149+
logLines: [],
150+
errLines: [],
149151
} satisfies WorkerError,
150152
{ status: 400, statusText: 'Code execution error' },
151153
);
@@ -157,6 +159,8 @@ const fetch = async (req: Request): Promise<Response> => {
157159
{
158160
message:
159161
'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```',
162+
logLines: [],
163+
errLines: [],
160164
} satisfies WorkerError,
161165
{ status: 400, statusText: 'Code execution error' },
162166
);
@@ -189,6 +193,8 @@ const fetch = async (req: Request): Promise<Response> => {
189193
return Response.json(
190194
{
191195
message: parseError(code, e),
196+
logLines,
197+
errLines,
192198
} satisfies WorkerError,
193199
{ status: 400, statusText: 'Code execution error' },
194200
);

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)