@@ -10,7 +10,7 @@ type ToolSetConfig = {
1010 env ?: Record < string , string > ;
1111 } ;
1212 } ;
13- onToolCall ?: ( serverName : string , toolName : string , args : any , result : Promise < string > ) => void ;
13+ onToolCall ?: < Result > ( serverName : string , toolName : string , args : any , result : Result ) => void ;
1414} ;
1515
1616type ToolSet = {
@@ -23,12 +23,11 @@ type ToolSet = {
2323} ;
2424
2525export async function createToolSet ( config : ToolSetConfig ) : Promise < ToolSet > {
26- let toolset : ToolSet = {
26+ const toolset : ToolSet = {
2727 tools : { } ,
2828 clients : { }
2929 } ;
3030
31- // could probably speed this up by spinning these up in parallel
3231 for ( const [ serverName , serverConfig ] of Object . entries ( config . mcpServers ) ) {
3332 const transport = new StdioClientTransport ( {
3433 ...serverConfig ,
@@ -44,31 +43,26 @@ export async function createToolSet(config: ToolSetConfig): Promise<ToolSet> {
4443 capabilities : { }
4544 }
4645 ) ;
46+
4747 toolset . clients [ serverName ] = client ;
4848 await client . connect ( transport ) ;
4949
50- // Get list of tools and add them to the toolset
51- const toolList = await client . listTools ( ) ;
52- for ( const tool of toolList . tools ) {
53- let toolName = tool . name ;
54- if ( toolName !== serverName ) {
55- toolName = `${ serverName } _${ toolName } ` ;
56- }
50+ const { tools } = await client . listTools ( ) ;
51+ for ( const tool of tools ) {
52+ const toolName = tool . name === serverName ? tool . name : `${ serverName } _${ tool . name } ` ;
53+
5754 toolset . tools [ toolName ] = {
5855 description : tool . description || '' ,
5956 parameters : jsonSchema ( tool . inputSchema as any ) ,
6057 execute : async ( args ) => {
61- const resultPromise = ( async ( ) => {
62- const result = await client . callTool ( {
63- name : tool . name ,
64- arguments : args
65- } ) ;
66- return JSON . stringify ( result ) ;
67- } ) ( ) ;
58+ const result = await client . callTool ( {
59+ name : tool . name ,
60+ arguments : args
61+ } ) ;
6862
69- config . onToolCall ?.( serverName , toolName , args , resultPromise ) ;
63+ config . onToolCall ?.( serverName , toolName , args , result ) ;
7064
71- return resultPromise ;
65+ return JSON . stringify ( result ) ;
7266 }
7367 } ;
7468 }
0 commit comments