UNPKG

chart-mcp-service

Version:

MCP服务,实现根据输入自动生成对应图表的功能

452 lines 19.3 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js"); const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js"); const types_js_1 = require("@modelcontextprotocol/sdk/types.js"); const rules_js_1 = require("./rules.js"); const chart_generator_js_1 = require("./chart-generator.js"); class ChartMCPServer { constructor() { this.server = new index_js_1.Server({ name: "chart-mcp-service", version: "1.0.0", }, { capabilities: { tools: {}, }, }); this.chartRules = new rules_js_1.ChartRules(); this.chartGenerator = new chart_generator_js_1.ChartGenerator(); this.setupToolHandlers(); } setupToolHandlers() { this.server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => { return { tools: [ { name: "identify_chart_type", description: "🎯 智能识别图表类型 - 输入任何图表需求描述,自动识别最适合的图表类型并提供实现建议", inputSchema: { type: "object", properties: { description: { type: "string", description: '图表需求描述,例如:"显示销售趋势"、"创建一个饼图"、"用户分布可视化"等', }, }, required: ["description"], }, }, { name: "generate_chart_code", description: "⚡ 生成图表代码 - 基于@alife/bi-material-center-chart生成完整的React组件代码", inputSchema: { type: "object", properties: { type: { type: "string", enum: [ "line", "bar", "pie", "scatter", "radar", "combo", "data", "wordcloud", "area", "strip", "rose", "nightingale", "treemap", "hierarchy", "heatmap", "sankey", "flow", "ranking", "rank", ], description: "图表类型:line(折线图), bar(柱状图), pie(饼图), radar(雷达图), wordcloud(词云图)等", }, }, required: ["type"], }, }, { name: "create_chart", description: "📊 创建图表实例 - 创建并存储一个新的图表配置,包含完整的代码和配置信息", inputSchema: { type: "object", properties: { id: { type: "string", description: '图表唯一标识符,例如:"sales_chart_2024"', }, type: { type: "string", enum: [ "line", "bar", "pie", "scatter", "radar", "combo", "data", "wordcloud", "area", "strip", "rose", "nightingale", "treemap", "hierarchy", "heatmap", "sankey", "flow", "ranking", "rank", ], description: "图表类型", }, title: { type: "string", description: '图表标题,例如:"2024年销售数据分析"', }, config: { type: "object", description: "图表配置选项,包含数据字段映射、样式设置等", }, }, required: ["id", "type", "title"], }, }, { name: "get_chart", description: "🔍 获取图表详情 - 根据ID获取图表的完整信息,包括配置和代码", inputSchema: { type: "object", properties: { id: { type: "string", description: "图表ID", }, }, required: ["id"], }, }, { name: "list_charts", description: "📋 列出所有图表 - 获取当前存储的所有图表列表和基本信息", inputSchema: { type: "object", properties: { random_string: { type: "string", description: "Dummy parameter for no-parameter tools", }, }, required: ["random_string"], }, }, { name: "update_chart", description: "✏️ 更新图表配置 - 修改现有图表的配置或属性", inputSchema: { type: "object", properties: { id: { type: "string", description: "图表ID", }, updates: { type: "object", description: '要更新的字段,例如:{"title": "新标题", "config": {...}}', }, }, required: ["id", "updates"], }, }, { name: "delete_chart", description: "🗑️ 删除图表 - 从存储中删除指定的图表", inputSchema: { type: "object", properties: { id: { type: "string", description: "要删除的图表ID", }, }, required: ["id"], }, }, { name: "get_chart_rules", description: "📚 获取图表规则 - 查看完整的图表组件规则、触发关键词和模板", inputSchema: { type: "object", properties: { type: { type: "string", enum: [ "line", "bar", "pie", "scatter", "radar", "combo", "data", "wordcloud", "area", "strip", "rose", "nightingale", "treemap", "hierarchy", "heatmap", "sankey", "flow", "ranking", "rank", ], description: "特定图表类型的规则(可选),不填则返回所有规则", }, }, }, }, { name: "get_best_practices", description: "💡 获取最佳实践 - 获取特定图表类型的开发最佳实践和注意事项", inputSchema: { type: "object", properties: { type: { type: "string", enum: [ "line", "bar", "pie", "scatter", "radar", "combo", "data", "wordcloud", "area", "strip", "rose", "nightingale", "treemap", "hierarchy", "heatmap", "sankey", "flow", "ranking", "rank", ], description: "图表类型", }, }, required: ["type"], }, }, { name: "validate_chart_config", description: "✅ 验证图表配置 - 检查图表配置是否符合最佳实践和规则要求", inputSchema: { type: "object", properties: { type: { type: "string", enum: [ "line", "bar", "pie", "scatter", "radar", "combo", "data", "wordcloud", "area", "strip", "rose", "nightingale", "treemap", "hierarchy", "heatmap", "sankey", "flow", "ranking", "rank", ], description: "图表类型", }, config: { type: "object", description: "要验证的图表配置", }, }, required: ["type", "config"], }, }, ], }; }); this.server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { switch (name) { case "identify_chart_type": return await this.handleIdentifyChartType(args); case "generate_chart_code": return await this.handleGenerateChartCode(args); case "create_chart": return await this.handleCreateChart(args); case "get_chart": return await this.handleGetChart(args); case "list_charts": return await this.handleListCharts(); case "update_chart": return await this.handleUpdateChart(args); case "delete_chart": return await this.handleDeleteChart(args); case "get_chart_rules": return await this.handleGetChartRules(args); case "get_best_practices": return await this.handleGetBestPractices(args); case "validate_chart_config": return await this.handleValidateChartConfig(args); default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { return { content: [ { type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }); } async handleIdentifyChartType(args) { const result = await this.chartRules.identifyChartType(args.description); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleGenerateChartCode(args) { const result = await this.chartGenerator.generateChartCode(args.type); return { content: [ { type: "text", text: result, }, ], }; } async handleCreateChart(args) { const result = await this.chartGenerator.createChart(args.id, args.type, args.title, args.config); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleGetChart(args) { const result = await this.chartGenerator.getChart(args.id); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleListCharts() { const result = await this.chartGenerator.listCharts(); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleUpdateChart(args) { const result = await this.chartGenerator.updateChart(args.id, args.updates); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleDeleteChart(args) { const result = await this.chartGenerator.deleteChart(args.id); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleGetChartRules(args) { const result = await this.chartRules.getChartRules(args.type); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleGetBestPractices(args) { const result = await this.chartRules.getBestPractices(args.type); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async handleValidateChartConfig(args) { const result = await this.chartRules.validateChartConfig(args.type, args.config); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; } async run() { const transport = new stdio_js_1.StdioServerTransport(); await this.server.connect(transport); console.error("Chart MCP Server running on stdio"); } } const server = new ChartMCPServer(); server.run().catch(console.error); //# sourceMappingURL=index.js.map