11#!/usr/bin/env node
2- import * as readline from "node:readline/promises" ;
3- import { stdin , stdout } from "node:process" ;
42import { dirname , join } from "node:path" ;
5- import { fileURLToPath } from "node:url" ;
63import { parseArgs } from "node:util" ;
4+ import { readFile } from "node:fs/promises" ;
75import { z } from "zod" ;
8- import type { InferenceProviderOrPolicy } from "@huggingface/inference" ;
96import { PROVIDERS_OR_POLICIES } from "@huggingface/inference" ;
10- import { version as packageVersion } from "../package.json" ;
11- import { readFile } from "node:fs/promises" ;
7+ import { Agent } from "@huggingface/mcp-client" ;
128import type { ServerConfig } from "@huggingface/mcp-client" ;
9+ import { version as packageVersion } from "../package.json" ;
1310
1411const USAGE_HELP = `
1512Usage:
@@ -35,6 +32,22 @@ const FILENAME_CONFIG = "agent.json";
3532// eslint-disable-next-line @typescript-eslint/no-unused-vars
3633const FILENAME_PROMPT = "PROMPT.md" ;
3734
35+ async function loadConfigFrom ( loadFrom : string ) : Promise < string > {
36+ try {
37+ /// First try it as a local directory path, then we will try as a path inside the repo itself
38+ return await readFile ( loadFrom, { encoding : "utf8" } ) ;
39+ } catch {
40+ const srcDir = dirname ( __filename ) ;
41+ const configPath = join ( srcDir , "agents" , loadFrom , FILENAME_CONFIG ) ;
42+ try {
43+ return await readFile ( configPath , { encoding : "utf8" } ) ;
44+ } catch {
45+ console . error ( `Config file not found! Loading from the HF Hub is not implemented yet` ) ;
46+ process . exit ( 1 ) ;
47+ }
48+ }
49+ }
50+
3851async function main ( ) {
3952 const {
4053 values : { help, version } ,
@@ -72,18 +85,11 @@ async function main() {
7285 console . error ( `Serve is not implemented yet, coming soon!` ) ;
7386 process . exit ( 1 ) ;
7487 } else {
75- const srcDir = dirname ( fileURLToPath ( import . meta. url ) ) ;
76- const configPath = join ( srcDir , "agents" , loadFrom , FILENAME_CONFIG ) ;
77- let configJson : string ;
78- try {
79- configJson = await readFile ( configPath , { encoding : "utf8" } ) ;
80- } catch {
81- console . error ( `Config file not found!` ) ;
82- process . exit ( 1 ) ;
83- }
88+ const configJson = await loadConfigFrom ( loadFrom ) ;
89+
8490 const ConfigSchema = z . object ( {
8591 model : z . string ( ) ,
86- provider : z . enum < InferenceProviderOrPolicy > ( PROVIDERS_OR_POLICIES ) ,
92+ provider : z . enum ( PROVIDERS_OR_POLICIES ) ,
8793 servers : z . array ( z . custom < ServerConfig > ( ) ) ,
8894 } ) ;
8995
@@ -98,10 +104,12 @@ async function main() {
98104 const agent = new Agent ( {
99105 provider : config . provider ,
100106 model : config . model ,
101- apiKey : process . env . HF_TOKEN ,
107+ apiKey : process . env . HF_TOKEN ?? "" ,
102108 servers : config . servers ,
103109 } ) ;
110+
104111 console . log ( agent ) ;
112+
105113 // TODO: hook main loop from mcp-client/cli.ts
106114 }
107115}
0 commit comments