11// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22
3- import { McpTool , Metadata , ToolCallResult , asTextContentResult } from './types' ;
3+ import { McpTool , Metadata , ToolCallResult , asErrorResult , asTextContentResult } from './types' ;
44import { Tool } from '@modelcontextprotocol/sdk/types.js' ;
55import { readEnv } from './server' ;
6- import { WorkerSuccess } from './code-tool-types' ;
6+ import { WorkerInput , WorkerOutput } from './code-tool-types' ;
77/**
88 * A tool that runs code against a copy of the SDK.
99 *
@@ -44,9 +44,9 @@ export function codeTool(): McpTool {
4444 } ,
4545 body : JSON . stringify ( {
4646 project_name : 'imagekit' ,
47- client_opts : { } ,
4847 code,
49- } ) ,
48+ client_opts : { } ,
49+ } satisfies WorkerInput ) ,
5050 } ) ;
5151
5252 if ( ! res . ok ) {
@@ -57,7 +57,17 @@ export function codeTool(): McpTool {
5757 ) ;
5858 }
5959
60- return asTextContentResult ( ( await res . json ( ) ) as WorkerSuccess ) ;
60+ const { is_error, result, log_lines, err_lines } = ( await res . json ( ) ) as WorkerOutput ;
61+ const hasLogs = log_lines . length > 0 || err_lines . length > 0 ;
62+ const output = {
63+ result,
64+ ...( log_lines . length > 0 && { log_lines } ) ,
65+ ...( err_lines . length > 0 && { err_lines } ) ,
66+ } ;
67+ if ( is_error ) {
68+ return asErrorResult ( typeof result === 'string' && ! hasLogs ? result : JSON . stringify ( output , null , 2 ) ) ;
69+ }
70+ return asTextContentResult ( output ) ;
6171 } ;
6272
6373 return { metadata, tool, handler } ;
0 commit comments