feat: mcp server!

This commit is contained in:
hyper
2026-03-17 22:49:47 +08:00
parent 834db72ec4
commit 7b68504b1e
2 changed files with 231 additions and 2 deletions
+9 -2
View File
@@ -9,6 +9,7 @@ import { generateCardDeck, type GenerateCardDeckParams, type CardField } from '.
export interface MCPOptions {
port?: string;
cwd?: string;
}
export const mcpCommand = new Command('mcp')
@@ -18,6 +19,7 @@ export const mcpCommand = new Command('mcp')
.description('启动 MCP 服务器')
.argument('[host]', '服务器地址', 'stdio')
.option('-p, --port <port>', 'HTTP 端口(仅 HTTP 传输)', '3001')
.option('--cwd <dir>', '工作目录(工具调用的相对路径基准)', process.cwd())
.action(mcpServeAction)
)
.addCommand(
@@ -37,6 +39,11 @@ export const mcpCommand = new Command('mcp')
* MCP 服务器启动处理函数
*/
async function mcpServeAction(host: string, options: MCPOptions) {
// 切换到指定的工作目录
const cwd = options.cwd || process.cwd();
process.chdir(cwd);
console.error(`MCP 服务器工作目录:${cwd}`);
// 动态导入 MCP SDK
const { Server } = await import('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
@@ -73,7 +80,7 @@ async function mcpServeAction(host: string, options: MCPOptions) {
},
output_dir: {
type: 'string',
description: '输出目录路径',
description: '输出目录路径(相对路径相对于 MCP 服务器工作目录)',
},
card_count: {
type: 'number',
@@ -191,7 +198,7 @@ async function mcpServeAction(host: string, options: MCPOptions) {
};
}
// 生成卡牌组
// 生成卡牌组(使用当前工作目录)
const result = generateCardDeck(params);
return {