Skip to content

Commit 08b6d3f

Browse files
feat(mcp): return logs on code tool errors
1 parent 4292092 commit 08b6d3f

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
@@ -153,6 +153,8 @@ const fetch = async (req: Request): Promise<Response> => {
153153
{
154154
message:
155155
'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```',
156+
logLines: [],
157+
errLines: [],
156158
} satisfies WorkerError,
157159
{ status: 400, statusText: 'Code execution error' },
158160
);
@@ -164,6 +166,8 @@ const fetch = async (req: Request): Promise<Response> => {
164166
{
165167
message:
166168
'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```',
169+
logLines: [],
170+
errLines: [],
167171
} satisfies WorkerError,
168172
{ status: 400, statusText: 'Code execution error' },
169173
);
@@ -196,6 +200,8 @@ const fetch = async (req: Request): Promise<Response> => {
196200
return Response.json(
197201
{
198202
message: parseError(code, e),
203+
logLines,
204+
errLines,
199205
} satisfies WorkerError,
200206
{ status: 400, statusText: 'Code execution error' },
201207
);

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)