Optimist MCP服务器
智能代码优化MCP服务器,可跨多个维度分析和改进代码库
   
概述
Optimist是一个模型上下文协议(MCP)服务器,旨在与其他开发工具协同工作,提供全面的代码库优化。它分析代码的性能瓶颈、内存问题、代码异味和可维护性问题,提供可操作的改进建议。
主要特点
- 🚀 性能分析 -识别瓶颈和热点路径
- 💾 内存优化 -检测泄漏和低效分配
- 📊 代码质量度量 -复杂性分析和可维护性评分
- 🔍 死码检测 -查找并删除未使用的代码
- 📦 依赖管理 -优化和分析依赖关系图
- 🎯 智能重构 -基于人工智能的重构建议
- 🔗 MCP集成 -与其他MCP工具无缝集成
- ✅ 测试驱动 -使用TDD方法构建
快速开始
先决条件
- Node.js 18+
- npm或pnpm
- MCP兼容客户端(例如Claude Desktop)
- 要分析的代码库
安装
# Clone the repository
git clone https://github.com/Atomic-Germ/mcp-optimist.git
cd mcp-optimist
npm install
npm run build测试服务器
# Run tests to verify everything works
npm test
# Run with coverage
npm run test:coverage
# Verify build output
ls -la dist/配置MCP客户端
克劳德桌面
编辑配置文件:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 视窗:
%APPDATA%\Claude\claude_desktop_config.json - Linux:
~/.config/claude/claude_desktop_config.json
添加服务器:
{
"mcpServers": {
"optimist": {
"command": "node",
"args": ["/absolute/path/to/mcp-optimist/dist/index.js"],
"env": {}
}
}
}验证设置
- 重新启动MCP客户端
- 在可用工具中寻找“乐观主义者”
- 您应该看到8个可用的优化工具
首次代码分析
在您的MCP客户端中尝试以下示例:
分析代码复杂性
Use analyze_complexity tool on your project:
Path: "./src"
Max Complexity: 10
Report Format: "summary"检测性能问题
Use analyze_performance tool:
Path: "./src"
Include Tests: false
Threshold: "medium"查找代码气味
Use detect_code_smells tool:
Path: "./src"
Severity: "medium"内存分析
Use optimize_memory tool:
Path: "./src"
Detect Leaks: true
Suggest Fixes: true发展
开发命令
# Development
npm run dev # Run with ts-node (development mode)
npm run build:watch # Auto-rebuild on changes
# Testing
npm test # Run all tests
npm run test:watch # Watch mode for tests
npm run test:coverage # Generate coverage report
# Code Quality
npm run lint # Check code with ESLint
npm run lint:fix # Auto-fix linting issues
npm run format # Format code with Prettier
npm run format:check # Check formatting
# Build
npm run build # Compile to dist/
npm run clean # Remove dist/项目结构
mcp-optimist/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── server.ts # OptimistServer class
│ ├── types/ # TypeScript definitions
│ ├── tools/ # Tool implementations
│ ├── analyzers/ # Analysis engines
│ └── utils/ # Utility functions
│
├── tests/
│ ├── unit/ # Unit tests
│ ├── integration/ # Integration tests
│ └── fixtures/ # Test fixtures
│
├── docs/ # Documentation
├── archive/ # Archived documentation
├── README.md # This file
├── package.json # Dependencies and scripts
├── tsconfig.json # TypeScript configuration
├── jest.config.js # Test configuration
├── eslint.config.js # Linting rules
├── .prettierrc # Code formatting
└── dist/ # Compiled JavaScript示例
基本项目分析
对整个项目进行全面分析:
// Analyze overall code quality
{
tool: "detect_code_smells",
arguments: {
path: "./src",
severity: "medium"
}
}
// Check performance issues
{
tool: "analyze_performance",
arguments: {
path: "./src",
threshold: "medium",
includeTests: false
}
}
// Find complexity issues
{
tool: "analyze_complexity",
arguments: {
path: "./src",
maxComplexity: 8,
reportFormat: "detailed"
}
}单文件分析
分析一个特定的有问题的文件:
{
tool: "analyze_performance",
arguments: {
path: "./src/services/dataProcessor.ts",
threshold: "low",
profileHotPaths: true,
trackAsyncOperations: true
}
}内存优化
查找并修复React组件中的内存泄漏:
{
tool: "optimize_memory",
arguments: {
path: "./src/components",
detectLeaks: true,
analyzeClosures: true
}
}泄漏分析结果:
{
data: {
findings: [
{
type: 'event-listener-leak',
file: 'src/components/DataChart.tsx',
line: 23,
description: 'Event listeners not cleaned up in useEffect',
leakPotential: 'high',
},
{
type: 'closure-retention',
file: 'src/hooks/useDataFetch.ts',
line: 15,
description: 'Closure retaining large objects unnecessarily',
},
];
}
}内存泄漏修复:
问题-事件侦听器泄漏:
// Problematic - no cleanup
function DataChart() {
useEffect(() => {
window.addEventListener('resize', handleResize);
// Missing cleanup function
}, []);
}固定的:
// Fixed with proper cleanup
function DataChart() {
useEffect(() => {
const handleResize = () => {
// Handle resize
};
window.addEventListener('resize', handleResize);
// Cleanup function prevents leak
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
}性能优化
识别并解决性能瓶颈:
{
tool: "analyze_performance",
arguments: {
path: "./src/services/dataProcessor.ts",
threshold: "low",
profileHotPaths: true
}
}之前(有问题):
// O(n²) complexity - problematic
function processLargeDataset(items: Item[], lookup: LookupItem[]): ProcessedItem[] {
return items.map((item) => {
// Inner loop for each item - O(n²)
const match = lookup.find((l) => l.id === item.lookupId);
return { ...item, enrichedData: match?.data };
});
}之后(优化):
// O(n) complexity - optimized
function processLargeDataset(items: Item[], lookup: LookupItem[]): ProcessedItem[] {
// Create lookup map once - O(n)
const lookupMap = new Map(lookup.map((l) => [l.id, l.data]));
// Single pass through items - O(n)
return items.map((item) => ({
...item,
enrichedData: lookupMap.get(item.lookupId),
}));
}代码质量分析
分析函数复杂性和代码气味:
{
tool: "analyze_complexity",
arguments: {
path: "./src/utils/validation.ts",
maxComplexity: 6,
includeCognitive: true
}
}
{
tool: "detect_code_smells",
arguments: {
path: "./src/services/UserService.ts",
severity: "high"
}
}______________________________________________________________________
有关更多示例,请参阅 api参考.
