MCP (Model Context Protocol) wrapper for php-class-diagram tool. This server allows LLM agents to generate PlantUML class diagrams from PHP source code for architecture analysis, refactoring assistance, and documentation.
- π Read-only analysis - Safe for production codebases
- π― LLM-optimized - Designed for AI agent consumption
- π PlantUML output - Industry-standard diagram format
- π Fast execution - Efficient child process handling
- π‘οΈ Secure - Input validation and sandboxed execution
- PHP 8.1 or higher
- Composer
# Clone this repository
git clone https://github.com/smeghead/php-class-diagram-mcp.git php-class-diagram-mcp
cd php-class-diagram-mcp
# Install dependencies (including php-class-diagram)
composer installAdd this server to your Claude Desktop configuration:
{
"mcpServers": {
"php-class-diagram": {
"command": "php",
"args": ["/path/to/php-class-diagram-mcp/bin/handler.php"],
"env": {}
}
}
}Add this server to your settings.json configuration:
{
"mcp": {
"inputs": [
{
"name": "auto-php-analysis",
"description": "Automatically use php-class-diagram MCP server for PHP code analysis tasks",
"trigger": ["php", "class", "dependency", "diagram", "structure"]
}
],
"servers": {
"php-class-diagram": {
"command": "php",
"args": [
"/path/to/php-class-diagram-mcp/bin/handler.php"
],
"env": {}
}
}
}
}- Install an MCP extension that supports external servers
- Add server configuration:
{ "name": "php-class-diagram", "command": "php /path/to/php-class-diagram-mcp/bin/handler.php" }
You can test the MCP server directly from command line:
# Test tool listing
echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | php bin/handler.php
# Test class diagram generation
echo '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "generate_class_diagram",
"arguments": {
"directory": "/path/to/your/php/project"
}
},
"id": 1
}' | php bin/handler.phpAgent Prompt:
"Generate a class diagram for the PHP project in ./src directory"
What the agent will do:
- Call
generate_class_diagramtool with{"directory": "./src"} - Receive PlantUML script
- Display or process the diagram
Agent Prompt:
"Analyze the architecture of this PHP project and identify any SOLID violations"
What the agent will do:
- Generate class diagram using this MCP tool
- Analyze relationships and dependencies
- Provide architectural recommendations
Agent Prompt:
"Show me the current class structure and suggest how to break down the large User class"
What the agent will do:
- Generate diagram to visualize current structure
- Analyze class responsibilities
- Suggest refactoring strategies
| Parameter | Type | Required | Description |
|---|---|---|---|
directory |
string | Yes | Path to PHP project directory to analyze |
exclude |
string[] | No | Glob patterns to exclude from analysis |
depth |
integer | No | Maximum directory depth (0 = unlimited) |
{
"directory": "./src",
"exclude": ["*Test.php", "vendor/*", "cache/*"],
"depth": 3
}The tool returns a PlantUML script that can be rendered into a class diagram:
{
"jsonrpc": "2.0",
"result": {
"content": [
{
"type": "text",
"text": "@startuml\nclass User {\n +getName(): string\n +setName(string): void\n}\n@enduml"
}
]
},
"id": 1
}- Code Review: "Show me the class relationships in this PR"
- Documentation: "Generate architecture diagrams for the README"
- Refactoring: "Identify tightly coupled classes that need separation"
- Impact Analysis: "What classes will be affected if I change this interface?"
- Learning: "Help me understand the structure of this codebase"
This should not happen if you installed dependencies correctly. If it does occur:
# Verify the vendor binary exists
ls -la vendor/bin/php-class-diagram
# Reinstall dependencies if missing
composer installEnsure the target directory has proper read permissions:
# Check current permissions first
ls -la /path/to/your/php/project
# Add read permissions if needed
find /path/to/your/php/project -exec chmod +r {} \;- Verify the directory contains PHP files
- Check if exclude patterns are too broad
- Ensure PHP files have proper class/interface definitions
Run tests using Composer scripts:
# Run all feature tests
composer test
# Run all tests (including fixtures)
composer test-all
# Run check (includes tests)
composer checkManual test execution:
# Run specific test directory
php tests/single-file-unit-test.php tests/feature/
# Run single test file
php tests/single-file-unit-test.php tests/feature/PhpClassDiagramMCPTest.phpCreate test fixtures:
mkdir -p tests/fixtures/sample-projectRun manual tests:
# Test with sample project
echo '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "generate_class_diagram",
"arguments": {
"directory": "./tests/fixtures/TestProject"
}
},
"id": 1
}' | php bin/handler.phpThe server can be configured by modifying the constants in bin/handler.php:
private const TIMEOUT_SECONDS = 90; // Execution timeoutThis tool is designed to be read-only and safe for analyzing production codebases:
- No file modifications
- No external network access
- Input validation and sanitization
- Process timeout protection
- Memory limit enforcement
Apache-2.0 License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
- php-class-diagram - The underlying diagram generation tool
- Model Context Protocol - The protocol specification