33import { dirname } from 'node:path' ;
44import { pathToFileURL } from 'node:url' ;
55import Stainless , { ClientOptions } from '@stainless-api/sdk' ;
6- import { Endpoint , ContentBlock , Metadata } from './tools/types' ;
6+ import { ContentBlock , Endpoint , Metadata , ToolCallResult } from './tools/types' ;
77
88import { Tool } from '@modelcontextprotocol/sdk/types.js' ;
99
@@ -31,7 +31,7 @@ export async function codeTool(): Promise<Endpoint> {
3131 const { newDenoHTTPWorker } = await import ( '@valtown/deno-http-worker' ) ;
3232 const { workerPath } = await import ( './code-tool-paths.cjs' ) ;
3333
34- const handler = async ( client : Stainless , args : unknown ) => {
34+ const handler = async ( client : Stainless , args : unknown ) : Promise < ToolCallResult > => {
3535 const baseURLHostname = new URL ( client . baseURL ) . hostname ;
3636 const { code } = args as { code : string } ;
3737
@@ -98,7 +98,7 @@ export async function codeTool(): Promise<Endpoint> {
9898 } satisfies WorkerInput ) ;
9999
100100 req . write ( body , ( err ) => {
101- if ( err !== null && err !== undefined ) {
101+ if ( err != null ) {
102102 reject ( err ) ;
103103 }
104104 } ) ;
@@ -109,12 +109,12 @@ export async function codeTool(): Promise<Endpoint> {
109109 if ( resp . status === 200 ) {
110110 const { result, logLines, errLines } = ( await resp . json ( ) ) as WorkerSuccess ;
111111 const returnOutput : ContentBlock | null =
112- result === null ? null
113- : result === undefined ? null
114- : {
112+ result == null ? null : (
113+ {
115114 type : 'text' ,
116- text : typeof result === 'string' ? ( result as string ) : JSON . stringify ( result ) ,
117- } ;
115+ text : typeof result === 'string' ? result : JSON . stringify ( result ) ,
116+ }
117+ ) ;
118118 const logOutput : ContentBlock | null =
119119 logLines . length === 0 ?
120120 null
@@ -134,10 +134,11 @@ export async function codeTool(): Promise<Endpoint> {
134134 } ;
135135 } else {
136136 const { message } = ( await resp . json ( ) ) as WorkerError ;
137- throw new Error ( message ) ;
137+ return {
138+ content : message == null ? [ ] : [ { type : 'text' , text : message } ] ,
139+ isError : true ,
140+ } ;
138141 }
139- } catch ( e ) {
140- throw e ;
141142 } finally {
142143 worker . terminate ( ) ;
143144 }
0 commit comments