33import { dirname } from 'node:path' ;
44import { pathToFileURL } from 'node:url' ;
55import CasParser , { ClientOptions } from 'cas-parser-node' ;
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 : CasParser , args : unknown ) => {
34+ const handler = async ( client : CasParser , args : unknown ) : Promise < ToolCallResult > => {
3535 const baseURLHostname = new URL ( client . baseURL ) . hostname ;
3636 const { code } = args as { code : string } ;
3737
@@ -97,7 +97,7 @@ export async function codeTool(): Promise<Endpoint> {
9797 } satisfies WorkerInput ) ;
9898
9999 req . write ( body , ( err ) => {
100- if ( err !== null && err !== undefined ) {
100+ if ( err != null ) {
101101 reject ( err ) ;
102102 }
103103 } ) ;
@@ -108,12 +108,12 @@ export async function codeTool(): Promise<Endpoint> {
108108 if ( resp . status === 200 ) {
109109 const { result, logLines, errLines } = ( await resp . json ( ) ) as WorkerSuccess ;
110110 const returnOutput : ContentBlock | null =
111- result === null ? null
112- : result === undefined ? null
113- : {
111+ result == null ? null : (
112+ {
114113 type : 'text' ,
115- text : typeof result === 'string' ? ( result as string ) : JSON . stringify ( result ) ,
116- } ;
114+ text : typeof result === 'string' ? result : JSON . stringify ( result ) ,
115+ }
116+ ) ;
117117 const logOutput : ContentBlock | null =
118118 logLines . length === 0 ?
119119 null
@@ -133,10 +133,11 @@ export async function codeTool(): Promise<Endpoint> {
133133 } ;
134134 } else {
135135 const { message } = ( await resp . json ( ) ) as WorkerError ;
136- throw new Error ( message ) ;
136+ return {
137+ content : message == null ? [ ] : [ { type : 'text' , text : message } ] ,
138+ isError : true ,
139+ } ;
137140 }
138- } catch ( e ) {
139- throw e ;
140141 } finally {
141142 worker . terminate ( ) ;
142143 }
0 commit comments