index.ts•3.34 kB
#!/usr/bin/env node
import readline from "readline";
import { MCPServer } from "mcp-framework";
import ExampleTool from "./tools/ExampleTool.js";
import QuoteTool from "./tools/QuoteTool.js";
async function interactiveMode(toolName: string) {
let tool;
if (toolName === "example_tool") {
tool = new ExampleTool();
} else if (toolName === "quote_tool") {
tool = new QuoteTool();
} else {
console.error("알 수 없는 툴 이름:", toolName);
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '입력(JSON) > '
});
rl.prompt();
rl.on('line', async (line) => {
const trimmed = line.trim();
if (trimmed === 'exit' || trimmed === 'quit') {
rl.close();
return;
}
try {
const input = JSON.parse(trimmed);
const result = await tool.execute(input);
console.log("결과:", result);
} catch (e) {
if (e instanceof Error) {
console.error("입력 오류 혹은 실행 실패:", e.message);
} else {
console.error("입력 오류 혹은 실행 실패:", String(e));
}
}
rl.prompt();
});
rl.on('close', () => {
console.log("종료합니다.");
process.exit(0);
});
}
async function cliMode() {
const argv = process.argv.slice(2);
if (argv[0] === "list-tools") {
console.log("example_tool");
console.log("quote_tool"); // 예시로 추가한 툴 이름
// 필요한 다른 툴 이름도 추가 가능
process.exit(0);
}
if (argv[0] === "run-tool") {
const toolName = argv[1];
const inputIndex = argv.indexOf("--input");
if (inputIndex === -1 || !argv[inputIndex + 1]) {
console.error("Error: --input 옵션에 JSON 문자열을 넣어주세요.");
process.exit(1);
}
let input;
try {
input = JSON.parse(argv[inputIndex + 1]);
} catch (e) {
console.error("Error: 입력 JSON 파싱 실패", e);
process.exit(1);
}
let tool;
if (toolName === "example_tool") {
tool = new ExampleTool();
} else if (toolName === "quote_tool") {
tool = new QuoteTool();
} else {
console.error("알 수 없는 툴 이름:", toolName);
process.exit(1);
}
try {
const result = await tool.execute(input);
console.log(result);
process.exit(0);
} catch (err) {
console.error("툴 실행 오류:", err);
process.exit(1);
}
}
if (argv[0] === "interactive") {
const toolName = argv[1];
if (!toolName) {
console.error("사용법: node dist/index.js interactive <tool_name>");
process.exit(1);
}
await interactiveMode(toolName);
return;
}
// 그 외엔 서버 실행
await startServer();
}
async function startServer() {
const server = new MCPServer();
await server.start();
}
cliMode().catch(err => {
console.error("오류 발생:", err);
process.exit(1);
});