feat: mcp serve preview

This commit is contained in:
2026-03-18 14:47:56 +08:00
parent 1d2b4b3e1e
commit 5389345a00
2 changed files with 271 additions and 7 deletions
+20 -7
View File
@@ -3,7 +3,7 @@ import { generateCardDeck, type GenerateCardDeckParams, type CardField } from '.
import { readFrontmatter, type ReadFrontmatterParams, type DeckFrontmatter } from '../tools/frontmatter/read-frontmatter.js';
import { writeFrontmatter, type WriteFrontmatterParams } from '../tools/frontmatter/write-frontmatter.js';
import { cardCrud, type CardCrudParams, type CardData } from '../tools/card/card-crud.js';
import { ensureDeckPreview, type EnsureDeckPreviewParams } from '../tools/ensure-deck-preview.js';
import { previewDeck, type PreviewDeckParams } from '../tools/preview-deck.js';
import {
designCardGame,
getDesignCardGamePrompt,
@@ -24,6 +24,7 @@ import {
readResource,
type DocResource
} from '../resources/docs.js';
import { serveCommand } from './serve.js';
/**
* MCP 服务器命令
@@ -68,6 +69,13 @@ async function mcpServeAction(host: string, options: MCPOptions) {
process.chdir(cwd);
console.error(`MCP 服务器工作目录:${cwd}`);
// 启动 serve 服务器(在后台运行)
console.error('启动预览服务器...');
serveCommand(cwd, { port: '3000' });
// 等待服务器启动
await new Promise(resolve => setTimeout(resolve, 1000));
// 动态导入 MCP SDK
const { Server } = await import('@modelcontextprotocol/sdk/server/index.js');
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
@@ -196,8 +204,8 @@ async function mcpServeAction(host: string, options: MCPOptions) {
},
},
{
name: 'deck_ensure_preview',
description: '确保 CSV 对应的 Markdown 预览文件存在',
name: 'deck_preview',
description: '保存 CSV 对应的 Markdown 预览文件并打开浏览器',
inputSchema: {
type: 'object',
properties: {
@@ -346,8 +354,8 @@ async function mcpServeAction(host: string, options: MCPOptions) {
};
}
case 'deck_ensure_preview': {
const params = args as unknown as EnsureDeckPreviewParams;
case 'deck_preview': {
const params = args as unknown as PreviewDeckParams;
if (!params.csv_file) {
return {
@@ -356,10 +364,15 @@ async function mcpServeAction(host: string, options: MCPOptions) {
};
}
const result = ensureDeckPreview(params);
const result = previewDeck(params);
return {
content: [{ type: 'text', text: result.success ? `✅ ${result.message}` : `❌ ${result.message}` }],
content: [{
type: 'text',
text: result.success
? `✅ ${result.message}\n\n预览 URL: ${result.preview_url}`
: `❌ ${result.message}`
}],
isError: !result.success,
};
}