@@ -5,6 +5,7 @@ import { homedir } from "os";
55import { join } from "path" ;
66import type { InferenceProvider } from "./types" ;
77import type {
8+ ChatCompletionInputMessage ,
89 ChatCompletionInputTool ,
910 ChatCompletionOutput ,
1011} from "@huggingface/tasks/src/tasks/chat-completion/inference" ;
@@ -28,7 +29,7 @@ export class McpClient {
2829 const transport = new StdioClientTransport ( {
2930 command,
3031 args,
31- env,
32+ env : { ... env , PATH : process . env . PATH ?? "" } ,
3233 } ) ;
3334 const mcp = new Client ( { name : "@huggingface/mcp-client" , version : "1.0.0" } ) ;
3435 await mcp . connect ( transport ) ;
@@ -58,7 +59,56 @@ export class McpClient {
5859 }
5960
6061 async processQuery ( query : string ) : Promise < ChatCompletionOutput > {
61- /// TODO
62+ const messages : ChatCompletionInputMessage [ ] = [
63+ {
64+ role : "user" ,
65+ content : query ,
66+ } ,
67+ ] ;
68+
69+ const response = await this . client . chatCompletion ( {
70+ provider : this . provider ,
71+ model : this . model ,
72+ messages,
73+ tools : this . availableTools ,
74+ tool_choice : "auto" ,
75+ } ) ;
76+
77+ const toolCalls = response . choices [ 0 ] . message . tool_calls ;
78+ if ( ! toolCalls || toolCalls . length === 0 ) {
79+ return response ;
80+ }
81+ for ( const toolCall of toolCalls ) {
82+ const toolName = toolCall . function . name ;
83+ const toolArgs = JSON . parse ( `${ toolCall . function . arguments } ` ) ;
84+
85+ /// Get the appropriate session for this tool
86+ const client = this . clients . get ( toolName ) ;
87+ if ( client ) {
88+ const result = await client . callTool ( { name : toolName , arguments : toolArgs } ) ;
89+ messages . push ( {
90+ tool_call_id : toolCall . id ,
91+ role : "tool" ,
92+ name : toolName ,
93+ content : ( result . content as Array < { text : string } > ) [ 0 ] . text ,
94+ } ) ;
95+ } else {
96+ messages . push ( {
97+ tool_call_id : toolCall . id ,
98+ role : "tool" ,
99+ name : toolName ,
100+ content : `Error: No session found for tool: ${ toolName } ` ,
101+ } ) ;
102+ }
103+ }
104+
105+ const enrichedResponse = await this . client . chatCompletion ( {
106+ provider : this . provider ,
107+ model : this . model ,
108+ messages,
109+ } ) ;
110+
111+ return enrichedResponse ;
62112 }
63113
64114 async cleanup ( ) : Promise < void > {
@@ -99,6 +149,4 @@ async function main() {
99149 }
100150}
101151
102- if ( require . main === module ) {
103- main ( ) ;
104- }
152+ main ( ) ;
0 commit comments