generate_diagram
Transform natural language descriptions into detailed draw.io architecture diagrams or HTML files, aiding in visualizing system components, services, databases, and tech stacks with precision.
Instructions
根据用户的描述,结合专业提示词模板,生成详细的draw.io架构图或对应的HTML文件
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | Yes | 系统架构描述,包括组件、服务、数据库、技术栈等详细信息 | |
| diagram_name | No | 架构图名称 | 系统架构图 |
| file_type | Yes | 生成的文件保存的格式 | |
| output_file | Yes | 用于保存生成架构图的文件路径,请注意macos与windows都不应该放在根目录 | |
| prompt_id | Yes | 要生成的图表类型ID。可以通过 list_support_diagram_types 工具获取可用ID。 |
Implementation Reference
- mcp_server.py:844-938 (handler)The primary handler for the 'generate_diagram' tool. It processes arguments, validates inputs, loads the appropriate prompt template, calls the relevant generator function based on file_type (draw.io, html, svg, json), handles content generation via LLM, and saves the output file.if name == "generate_diagram": prompt_id = arguments.get("prompt_id") file_type = arguments.get("file_type") description = arguments.get("description") diagram_name = arguments.get("diagram_name") output_file = arguments.get("output_file") logger.info(f"MCP调用:生成架构图 - {diagram_name}") logger.info(f"描述长度: {len(description)}") if not description: return [TextContent(type="text", text="错误:请提供系统架构描述")] if not prompt_id: return [ TextContent( type="text", text="错误:请提供使用的提示词ID,你可以通过 list_supported_tools 获取支持ID", ) ] if not file_type: return [ TextContent( type="text", text="错误:请提供生成的文件保存的格式,你可以通过 list_supported_tools 获取对应的文件格式", ) ] # 1. 查找并加载精确的Prompt,同时完成组合验证 try: prompt_template = load_prompt_template(prompt_id, file_type) except KeyError: return [ TextContent( type="text", text=f"❌ 错误:不支持的 '{prompt_id}' 与 '{file_type}' 的组合。", ) ] if not prompt_template: return [ TextContent(type="text", text=f"❌ 错误:无法加载对应的提示词文件。") ] # 2. 根据file_type决定调用哪个生成器 # 这里的逻辑和你最初的实现一样,是正确的! content = "" if file_type == "draw.io": logger.info("调用 Draw.io XML 生成器") content = await generate_xml_with_llm( description, diagram_name, prompt_template ) elif file_type == "html": logger.info("调用 HTML 生成器") content = await generate_html_with_llm( description, diagram_name, prompt_template ) elif file_type == "svg": logger.info("调用 SVG 生成器") content = await generate_svg_with_llm( description, diagram_name, prompt_template ) elif file_type == "json": logger.info("调用 PPT 生成器") content = await generate_ppt_with_llm(description, prompt_template) else: # 理论上不会到这里,因为load_prompt_template已经校验过了,但作为防御性编程加上 return [ TextContent( type="text", text=f"❌ 内部错误:未知的有效文件类型 '{file_type}'。" ) ] if file_type != "json": try: output_path = Path(output_file) output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, "w", encoding="utf-8") as f: f.write(content) return [ TextContent( type="text", text=f"✅ {file_type.upper()} 文件已生成并保存到: {output_file}", ) ] except Exception as e: return [TextContent(type="text", text=f"❌ 保存文件失败: {e}")] else: return [ TextContent( type="text", text=f"✅ PPT 计划书已生成: {content}", ) ]
- mcp_server.py:771-797 (schema)JSON schema defining the input parameters for the generate_diagram tool, including prompt_id, file_type, description, diagram_name, and output_file.inputSchema={ "type": "object", "properties": { "prompt_id": { "type": "string", "description": "要生成的图表或者文件类型ID。可以通过 list_supported_tools 工具获取可用ID。", }, "file_type": { "type": "string", "description": "生成的文件保存的格式, json为特殊模式,仅用于交互不会保存文件,仅供PPT规划使用", }, "description": { "type": "string", "description": "系统架构描述,包括组件、服务、数据库、技术栈、PPT内容等详细信息", }, "diagram_name": { "type": "string", "description": "架构图名称", "default": "系统架构图", }, "output_file": { "type": "string", "description": "用于保存生成架构图的文件路径,请注意macos与windows都不应该放在根目录", }, }, "required": ["prompt_id", "file_type", "description", "output_file"], },
- mcp_server.py:769-798 (registration)Registration of the 'generate_diagram' tool in the list_tools handler, including name, description, and reference to inputSchema.name="generate_diagram", description="当用户的核心意图是【绘制、画出、创建、生成】一个【图表、架构图、流程图、原型图、产品设计所需的各种图表】时,调用此工具。它能生成draw.io文件、HTML原型、svg图表等。提示,如果用户需要生成svg图表,文件生成后,务必告诉用户可以通过 https://www.jyshare.com/more/svgeditor/ 网站对刚才生成的文件进行编辑修改。", inputSchema={ "type": "object", "properties": { "prompt_id": { "type": "string", "description": "要生成的图表或者文件类型ID。可以通过 list_supported_tools 工具获取可用ID。", }, "file_type": { "type": "string", "description": "生成的文件保存的格式, json为特殊模式,仅用于交互不会保存文件,仅供PPT规划使用", }, "description": { "type": "string", "description": "系统架构描述,包括组件、服务、数据库、技术栈、PPT内容等详细信息", }, "diagram_name": { "type": "string", "description": "架构图名称", "default": "系统架构图", }, "output_file": { "type": "string", "description": "用于保存生成架构图的文件路径,请注意macos与windows都不应该放在根目录", }, }, "required": ["prompt_id", "file_type", "description", "output_file"], }, ),