|
| 1 | +import * as readline from "node:readline/promises"; |
| 2 | +import { stdin, stdout } from "node:process"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { homedir } from "node:os"; |
| 5 | +import { Agent } from "./src"; |
| 6 | +import type { StdioServerParameters } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 7 | +import { ANSI } from "./src/utils"; |
| 8 | +import type { InferenceProvider } from "@huggingface/inference"; |
| 9 | + |
| 10 | +const MODEL_ID = process.env.MODEL_ID ?? "Qwen/Qwen2.5-72B-Instruct"; |
| 11 | +const PROVIDER = (process.env.PROVIDER as InferenceProvider) ?? "nebius"; |
| 12 | + |
| 13 | +const SERVERS: StdioServerParameters[] = [ |
| 14 | + { |
| 15 | + // Filesystem "official" mcp-server with access to your Desktop |
| 16 | + command: "npx", |
| 17 | + args: ["-y", "@modelcontextprotocol/server-filesystem", join(homedir(), "Desktop")], |
| 18 | + }, |
| 19 | + { |
| 20 | + // Playwright MCP |
| 21 | + command: "npx", |
| 22 | + args: ["@playwright/mcp@latest"], |
| 23 | + }, |
| 24 | +]; |
| 25 | + |
| 26 | +if (process.env.EXPERIMENTAL_HF_MCP_SERVER) { |
| 27 | + SERVERS.push({ |
| 28 | + // Early version of a HF-MCP server |
| 29 | + // you can download it from gist.github.com/julien-c/0500ba922e1b38f2dc30447fb81f7dc6 |
| 30 | + command: "node", |
| 31 | + args: ["--disable-warning=ExperimentalWarning", join(homedir(), "Desktop/hf-mcp/index.ts")], |
| 32 | + env: { |
| 33 | + HF_TOKEN: process.env.HF_TOKEN ?? "", |
| 34 | + }, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +async function main() { |
| 39 | + if (!process.env.HF_TOKEN) { |
| 40 | + console.error(`a valid HF_TOKEN must be present in the env`); |
| 41 | + process.exit(1); |
| 42 | + } |
| 43 | + |
| 44 | + const agent = new Agent({ |
| 45 | + provider: PROVIDER, |
| 46 | + model: MODEL_ID, |
| 47 | + apiKey: process.env.HF_TOKEN, |
| 48 | + servers: SERVERS, |
| 49 | + }); |
| 50 | + |
| 51 | + const rl = readline.createInterface({ input: stdin, output: stdout }); |
| 52 | + let abortController = new AbortController(); |
| 53 | + let waitingForInput = false; |
| 54 | + async function waitForInput() { |
| 55 | + waitingForInput = true; |
| 56 | + const input = await rl.question("> "); |
| 57 | + waitingForInput = false; |
| 58 | + return input; |
| 59 | + } |
| 60 | + rl.on("SIGINT", async () => { |
| 61 | + if (waitingForInput) { |
| 62 | + // close the whole process |
| 63 | + await agent.cleanup(); |
| 64 | + stdout.write("\n"); |
| 65 | + rl.close(); |
| 66 | + } else { |
| 67 | + // otherwise, it means a request is underway |
| 68 | + abortController.abort(); |
| 69 | + abortController = new AbortController(); |
| 70 | + stdout.write("\n"); |
| 71 | + stdout.write(ANSI.GRAY); |
| 72 | + stdout.write("Ctrl+C a second time to exit"); |
| 73 | + stdout.write(ANSI.RESET); |
| 74 | + stdout.write("\n"); |
| 75 | + } |
| 76 | + }); |
| 77 | + process.on("uncaughtException", (err) => { |
| 78 | + stdout.write("\n"); |
| 79 | + rl.close(); |
| 80 | + throw err; |
| 81 | + }); |
| 82 | + |
| 83 | + await agent.loadTools(); |
| 84 | + |
| 85 | + stdout.write(ANSI.BLUE); |
| 86 | + stdout.write(`Agent loaded with ${agent.availableTools.length} tools:\n`); |
| 87 | + stdout.write(agent.availableTools.map((t) => `- ${t.function.name}`).join("\n")); |
| 88 | + stdout.write(ANSI.RESET); |
| 89 | + stdout.write("\n"); |
| 90 | + |
| 91 | + while (true) { |
| 92 | + const input = await waitForInput(); |
| 93 | + for await (const chunk of agent.run(input, { abortSignal: abortController.signal })) { |
| 94 | + if ("choices" in chunk) { |
| 95 | + const delta = chunk.choices[0]?.delta; |
| 96 | + if (delta.content) { |
| 97 | + stdout.write(delta.content); |
| 98 | + } |
| 99 | + if (delta.tool_calls) { |
| 100 | + stdout.write(ANSI.GRAY); |
| 101 | + for (const deltaToolCall of delta.tool_calls) { |
| 102 | + if (deltaToolCall.id) { |
| 103 | + stdout.write(`<Tool ${deltaToolCall.id}>\n`); |
| 104 | + } |
| 105 | + if (deltaToolCall.function.name) { |
| 106 | + stdout.write(deltaToolCall.function.name + " "); |
| 107 | + } |
| 108 | + if (deltaToolCall.function.arguments) { |
| 109 | + stdout.write(deltaToolCall.function.arguments); |
| 110 | + } |
| 111 | + } |
| 112 | + stdout.write(ANSI.RESET); |
| 113 | + } |
| 114 | + } else { |
| 115 | + /// Tool call info |
| 116 | + stdout.write("\n\n"); |
| 117 | + stdout.write(ANSI.GREEN); |
| 118 | + stdout.write(`Tool[${chunk.name}] ${chunk.tool_call_id}\n`); |
| 119 | + stdout.write(chunk.content); |
| 120 | + stdout.write(ANSI.RESET); |
| 121 | + stdout.write("\n\n"); |
| 122 | + } |
| 123 | + } |
| 124 | + stdout.write("\n"); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +main(); |
0 commit comments