C++ UML Class Diagram Generator
效率与工作流by hydavinci
通过分析类结构、继承关系和成员,从 C++ 源码生成 UML 类图;输出兼容 PlantUML 的图表,便于可视化项目架构,并可作为脚本或模块集成到自动生成流程。
什么是 C++ UML Class Diagram Generator?
通过分析类结构、继承关系和成员,从 C++ 源码生成 UML 类图;输出兼容 PlantUML 的图表,便于可视化项目架构,并可作为脚本或模块集成到自动生成流程。
README
C++ UML Class Diagram Generator
A powerful Model Context Protocol (MCP) server that recursively analyzes C++ source and header files, extracts class definitions, inheritance relationships, and member information, then generates comprehensive UML class diagrams in PlantUML format.
Features
- 🔍 Multi-format C++ file support: .cpp, .hpp, .h, .cc, .cxx files
- 🏗️ Class & struct extraction: Automatically detects class/struct definitions
- 🔗 Inheritance mapping: Captures inheritance relationships and generates proper UML arrows
- 👁️ Member visibility: Handles public, private, and protected member access levels
- 📁 Directory scanning: Recursively processes entire project folders
- 📄 Content-based processing: Generate UML from provided file contents directly
- 🛡️ Error handling: Robust error handling with diagnostic UML output
- 🌐 MCP server support: Works as both HTTP and stdio MCP server
- 🐳 Docker ready: Containerized deployment support
- ✨ PlantUML output: Clean, valid PlantUML format for easy rendering
Installation
Quick Start
# Clone the repository
git clone https://github.com/hydavinci/uml-diagram.git
cd uml-diagram
# Install dependencies
pip install -r requirements.txt
# or using uv
uv sync
Docker Deployment
# Build the container
docker build -t uml-diagram-mcp .
# Run as HTTP server
docker run -p 8081:8081 -e TRANSPORT=http uml-diagram-mcp
# Run as stdio server
docker run -e TRANSPORT=stdio uml-diagram-mcp
Usage
As a Standalone Script
# Navigate to the src directory
cd src
# Generate UML from a C++ project directory
python uml_generate.py /path/to/your/cpp/project
# The UML diagram will be written to 'uml_output.puml' in the current directory
Example:
python uml_generate.py "C:\Projects\MyProject\src"
# Output: uml_output.puml
As a Python Module
Directory-based Generation
from src.uml_generate import generate_cpp_uml_from_path
# Generate UML from a directory
uml_diagram = generate_cpp_uml_from_path('/path/to/cpp/project')
print(uml_diagram)
# Save to file
with open('my_diagram.puml', 'w') as f:
f.write(uml_diagram)
Content-based Generation
from src.uml_generate import generate_cpp_uml_from_content
# Dictionary where keys are file names and values are file contents
file_contents = {
"animal.h": """
class Animal {
public:
virtual void makeSound() = 0;
virtual ~Animal() = default;
protected:
std::string name_;
private:
int age_;
};
""",
"dog.h": """
#include "animal.h"
class Dog : public Animal {
public:
void makeSound() override;
void bark();
private:
std::string breed_;
bool isTrained_;
};
""",
"cat.h": """
#include "animal.h"
class Cat : public Animal {
public:
void makeSound() override;
void purr();
private:
bool isIndoor_;
};
"""
}
uml_diagram = generate_cpp_uml_from_content(file_contents)
print(uml_diagram)
As an MCP Server
This tool provides a complete Model Context Protocol (MCP) server implementation with two available tools:
Available MCP Tools
-
generate_cpp_uml- Directory-based UML generation- Parameter:
path(string) - Path to directory containing C++ files - Returns: PlantUML diagram as string
- Example: Analyze entire C++ project directory
- Parameter:
-
generate_cpp_uml_from_content- Content-based UML generation- Parameter:
file_contents(Dict[str, str]) - Dictionary mapping file names to their contents - Returns: PlantUML diagram as string
- Example: Generate UML from provided source code snippets
- Parameter:
Starting the MCP Server
HTTP Mode (recommended for web clients):
# Set transport mode and start server
export TRANSPORT=http
python src/server.py
# Server will start on port 8081 by default
# Access at: http://localhost:8081
Stdio Mode (for command-line clients):
# Default mode - no environment variable needed
python src/server.py
# Or explicitly set stdio mode
export TRANSPORT=stdio
python src/server.py
MCP Client Integration Example
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def use_uml_generator():
server_params = StdioServerParameters(
command="python",
args=["src/server.py"],
cwd="/path/to/uml-diagram"
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# Initialize the session
await session.initialize()
# Generate UML from directory
result = await session.call_tool(
"generate_cpp_uml",
{"path": "/path/to/cpp/project"}
)
print("Generated UML:")
print(result.content[0].text)
# Run the example
asyncio.run(use_uml_generator())
Requirements
System Requirements
- Python: 3.10 or higher
- Operating System: Windows, Linux, macOS
- Memory: Minimum 512MB RAM (more for large codebases)
Dependencies
Core Dependencies (included in pyproject.toml)
fastmcp>=0.2.0- For MCP server functionality
Python Standard Library (no installation needed)
os- File system operationsre- Regular expression parsingtyping- Type hintsjson- JSON handling (middleware)base64- Encoding (middleware)urllib.parse- URL parsing (middleware)
Development Dependencies (optional)
# For development and testing
pip install pytest black flake8
PlantUML Rendering (optional)
To view the generated .puml files:
- VS Code: Install PlantUML extension
- Online: Use PlantUML Online Server
- Local: Install PlantUML with Java runtime
Output Examples
Generated PlantUML Structure
The tool generates clean, well-formatted PlantUML diagrams:
@startuml
class Animal {
+ virtual makeSound() = 0
+ virtual ~Animal() = default
# std::string name_
- int age_
}
class Dog {
+ makeSound() override
+ bark()
- std::string breed_
- bool isTrained_
}
class Cat {
+ makeSound() override
+ purr()
- bool isIndoor_
}
Animal <|-- Dog
Animal <|-- Cat
@enduml
Symbol Meanings
+Public members-Private members#Protected members<|--Inheritance relationship (base <|-- derived)
File Output
- Script mode: Creates
uml_output.pumlin current directory - Module mode: Returns PlantUML string for programmatic use
- MCP mode: Returns PlantUML string via MCP protocol
Project Structure
uml-diagram/
├── src/
│ ├── server.py # MCP server implementation
│ ├── uml_generate.py # Core UML generation logic
│ └── middleware.py # HTTP request middleware
├── Dockerfile # Container configuration
├── pyproject.toml # Project dependencies and metadata
├── smithery.yaml # Smithery platform configuration
├── README.md # This file
└── LICENSE # MIT license
Supported C++ Features
Class Detection
- ✅ Class definitions
- ✅ Struct definitions
- ✅ Template classes (generics stripped for PlantUML compatibility)
- ✅ Nested classes (basic support)
Inheritance
- ✅ Single inheritance
- ✅ Multiple inheritance
- ✅ Public inheritance
- ✅ Protected inheritance
- ✅ Private inheritance
Member Detection
- ✅ Public, private, protected members
- ✅ Methods and functions
- ✅ Variables and fields
- ✅ Virtual and pure virtual methods
- ✅ Constructors and destructors
- ✅ Static members (basic support)
Limitations
- ⚠️ Complex template specializations may not be fully captured
- ⚠️ Preprocessor macros are not expanded
- ⚠️ Forward declarations without definitions are skipped
- ⚠️ Some complex C++ syntax may require manual review
Configuration
Environment Variables
TRANSPORT: Set to "http" for HTTP mode, "stdio" for stdio mode (default)PORT: HTTP server port (default: 8081)SERVER_TOKEN: Optional authentication token for stdio mode
Smithery Platform
This project includes smithery.yaml for deployment on the Smithery platform:
- Containerized runtime
- HTTP transport mode
- Auto-scaling support
Troubleshooting
Common Issues
"No C++ classes found" message:
- Verify the path contains .cpp, .hpp, .h, .cc, or .cxx files
- Check file encoding (UTF-8 recommended)
- Ensure class definitions use standard C++ syntax
Empty or malformed UML output:
- Review C++ syntax for unsupported constructs
- Check for balanced braces in class definitions
- Verify inheritance syntax follows standard format
MCP server connection issues:
- Ensure correct transport mode (http vs stdio)
- Check port availability (8081 by default)
- Verify dependencies are installed (
fastmcp)
Debug Mode
Enable verbose logging by modifying server.py:
# Add debug logging
import logging
logging.basicConfig(level=logging.DEBUG)
Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and add tests
- Commit:
git commit -am 'Add feature-name' - Push:
git push origin feature-name - Create a Pull Request
Development Setup
# Clone and setup development environment
git clone https://github.com/hydavinci/uml-diagram.git
cd uml-diagram
# Install in development mode
pip install -e .
# Run tests (if available)
python -m pytest
# Format code
black src/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
v0.1.0
- Initial release
- Directory-based UML generation
- Content-based UML generation
- MCP server with HTTP and stdio transport
- Docker containerization support
- PlantUML output format
- Class, inheritance, and member detection
Author: hydavinci
Repository: https://github.com/hydavinci/uml-diagram
常见问题
C++ UML Class Diagram Generator 是什么?
通过分析类结构、继承关系和成员,从 C++ 源码生成 UML 类图;输出兼容 PlantUML 的图表,便于可视化项目架构,并可作为脚本或模块集成到自动生成流程。
相关 Skills
表格处理
by anthropics
围绕 .xlsx、.xlsm、.csv、.tsv 做读写、修复、清洗、格式整理、公式计算与格式转换,适合修改现有表格、生成新报表或把杂乱数据整理成交付级电子表格。
✎ 做 Excel/CSV 相关任务很省心,能直接读写、修复、清洗和格式转换,尤其擅长把乱七八糟的表格整理成交付级文件。
PDF处理
by anthropics
遇到 PDF 读写、文本表格提取、合并拆分、旋转加水印、表单填写或加解密时直接用它,也能提取图片、生成新 PDF,并把扫描件通过 OCR 变成可搜索文档。
✎ PDF杂活别再来回切工具了,文本表格提取、合并拆分到OCR识别一次搞定,连扫描件也能变可搜索。
Word文档
by anthropics
覆盖Word/.docx文档的创建、读取、编辑与重排,适合生成报告、备忘录、信函和模板,也能处理目录、页眉页脚、页码、图片替换、查找替换、修订批注及内容提取整理。
✎ 搞定 .docx 的创建、改写与精排版,目录、批量替换、批注修订和图片更新都能自动化,做正式文档尤其省心。
相关 MCP Server
文件系统
编辑精选by Anthropic
Filesystem 是 MCP 官方参考服务器,让 LLM 安全读写本地文件系统。
✎ 这个服务器解决了让 Claude 直接操作本地文件的痛点,比如自动整理文档或生成代码文件。适合需要自动化文件处理的开发者,但注意它只是参考实现,生产环境需自行加固安全。
by wonderwhy-er
Desktop Commander 是让 AI 直接执行终端命令、管理文件和进程的 MCP 服务器。
✎ 这工具解决了 AI 无法直接操作本地环境的痛点,适合需要自动化脚本调试或文件批量处理的开发者。它能让你用自然语言指挥终端,但权限控制需谨慎,毕竟让 AI 执行 rm -rf 可不是闹着玩的。
EdgarTools
编辑精选by dgunning
EdgarTools 是无需 API 密钥即可解析 SEC EDGAR 财报的开源 Python 库。
✎ 这个工具解决了金融数据获取的痛点——直接让 AI 读取结构化财报,比如让 Claude 分析苹果的 10-K 文件。适合量化分析师或金融开发者快速构建数据管道。但注意,它依赖 SEC 网站稳定性,高峰期可能延迟。