@yqg/mcp-cli
Version:
统一的MCP服务器脚手架工具,支持TypeScript和Python
102 lines • 3.72 kB
JavaScript
import chalk from "chalk";
import { Command } from "commander";
import inquirer from "inquirer";
import ora from "ora";
import { createTypeScriptServer } from "./generators/typescript.js";
import { createPythonServer } from "./generators/python.js";
import { validateProjectName, checkDirectoryExists, } from "./utils/validation.js";
async function createServer(directory, options = {}) {
// 检查目录是否已存在
if (await checkDirectoryExists(directory)) {
console.log(chalk.red(`错误: 目录 '${directory}' 已存在。`));
process.exit(1);
}
// 交互式问题
const questions = [
{
type: "input",
name: "name",
message: "请输入MCP服务器的名称:",
default: directory,
when: !options.name,
validate: validateProjectName,
},
{
type: "input",
name: "description",
message: "请输入服务器描述:",
default: "一个Model Context Protocol服务器",
when: !options.description,
},
{
type: "list",
name: "language",
message: "选择开发语言:",
choices: [
{ name: "TypeScript", value: "typescript" },
{ name: "Python", value: "python" },
],
when: !options.language,
},
{
type: "list",
name: "transport",
message: "选择传输方式:",
default: "stdio",
choices: [
{ name: "stdio", value: "stdio" },
{ name: "sse", value: "sse" },
{ name: "streamable-http", value: "streamable-http" },
],
when: !options.transport,
},
];
const answers = await inquirer.prompt(questions);
const config = {
name: options.name || answers.name,
description: options.description || answers.description,
language: options.language || answers.language,
transport: options.transport || answers.transport,
};
const spinner = ora("正在创建MCP服务器...").start();
try {
// 根据选择的语言创建项目
if (config.language === "typescript") {
await createTypeScriptServer(directory, config);
}
else {
await createPythonServer(directory, config);
}
spinner.succeed(chalk.green("MCP服务器创建成功!"));
// 显示下一步操作
console.log("\n下一步操作:");
console.log(chalk.cyan(` cd ${directory}`));
if (config.language === "typescript") {
console.log(chalk.cyan(" npm install"));
console.log(chalk.cyan(` npm run build ${chalk.reset("# 或者: npm run watch")}`));
}
else {
console.log(chalk.cyan(" uv sync --dev"));
console.log(chalk.cyan(` uv run ${config.name}`));
}
console.log();
}
catch (error) {
spinner.fail(chalk.red("创建MCP服务器失败"));
console.error(error);
process.exit(1);
}
}
const program = new Command()
.name("mcp-scaffold")
.description("统一的MCP服务器脚手架工具")
.version("1.0.0")
.argument("<directory>", "创建服务器的目录")
.option("-n, --name <name>", "服务器名称")
.option("-d, --description <description>", "服务器描述")
.option("-l, --language <language>", "开发语言 (typescript|python)")
.option("-t, --transport <transport>", "传输方式 (stdio|sse)")
.action(createServer);
program.parse();
//# sourceMappingURL=index.js.map