GreenCodeMCP
An MCP server that detects energy anti-patterns in Python code, retrieves optimization examples, suggests refactoring, validates correctness, and benchmarks resource gains, integrating with VS Code, Cursor, and Windsurf.
README
GreenCodeMCP
An MCP-based tool for sustainable software maintenance and resource-aware refactoring.
Accepted at IEEE ICSME 2026 — Tool Demonstration and Data Showcase Track
Overview
GreenCodeMCP is a developer-facing tool that connects modern AI coding assistants to a sustainable refactoring workflow via the Model Context Protocol (MCP). Given Python code, it:
- Detects energy anti-patterns using 20 AST-based rules
- Retrieves optimization evidence from an 800-example knowledge base
- Suggests refactored code (deterministic auto-fix + LLM-assisted)
- Validates behavioral preservation (syntax, signature, tests, output hash)
- Benchmarks resource gains under a controlled protocol (time, memory, energy, CO2)
- Reports results as structured JSON or Markdown
It integrates directly into VS Code, Cursor, and Windsurf via MCP.
Installation
Prerequisites
- Python >= 3.10
- No GPU required
- (Optional) NVIDIA API key for LLM-assisted refactoring — get one free
Setup
# Clone the repository
git clone https://github.com/DaoudiAmir/GreenCodeMCP.git
cd GreenCodeMCP
# Create and activate a virtual environment
python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux / macOS
source .venv/bin/activate
# Install the package
pip install -e .
# (Optional) Configure LLM access
cp .env.example .env
# Edit .env and set NVIDIA_API_KEY=your_key_here
Note: Without an API key, all features work except LLM-assisted generation. The deterministic auto-fix (7 rules), KB retrieval, validation, and benchmarking are fully offline.
Running the MCP Server
Start the server with:
python -m src.greencode_mcp.mcp_server
The server communicates over stdio (standard input/output), which is the default MCP transport for IDE integration.
IDE Integration
Cursor
Add to your Cursor MCP settings (.cursor/mcp.json in your project, or global settings):
{
"mcpServers": {
"greencode-mcp": {
"command": "python",
"args": ["-m", "src.greencode_mcp.mcp_server"],
"cwd": "/absolute/path/to/GreenCodeMCP"
}
}
}
Then in Cursor's chat, the 9 GreenCodeMCP tools become available to the AI agent.
VS Code (with Copilot MCP support)
Add to your VS Code settings (.vscode/mcp.json):
{
"servers": {
"greencode-mcp": {
"command": "python",
"args": ["-m", "src.greencode_mcp.mcp_server"],
"cwd": "/absolute/path/to/GreenCodeMCP"
}
}
}
Windsurf
Add to your Windsurf MCP configuration (~/.codeium/windsurf/mcp_config.json):
{
"mcpServers": {
"greencode-mcp": {
"command": "python",
"args": ["-m", "src.greencode_mcp.mcp_server"],
"cwd": "/absolute/path/to/GreenCodeMCP"
}
}
}
Tips
- Replace
/absolute/path/to/GreenCodeMCPwith the actual absolute path to this repository. - On Windows, use the full path to the Python executable inside the venv:
"command": "C:/path/to/GreenCodeMCP/.venv/Scripts/python.exe" - After configuring, restart the IDE or reload MCP servers.
- Verify the tools are loaded by asking the agent: "List available MCP tools".
MCP Tools
| # | Tool | Description |
|---|---|---|
| 1 | analyze_code |
Detect energy anti-patterns (20 AST rules) |
| 2 | retrieve_green_practices |
Query KB for relevant before/after examples |
| 3 | suggest_refactoring |
Generate optimized code (auto-fix + LLM) |
| 4 | validate_equivalence |
Verify functional correctness preservation |
| 5 | benchmark_resource_gain |
Measure time, memory, energy, CO2 gains |
| 6 | run_full_green_refactor_pipeline |
End-to-end pipeline (recommended) |
| 7 | generate_green_report |
Produce JSON or Markdown report |
| 8 | list_demo_workloads |
List available demo scenarios |
| 9 | run_demo_workload |
Run a complete demo scenario |
Typical Usage (via AI agent in IDE)
"Analyze this Python file for energy anti-patterns and suggest an optimized version"
The agent will call run_full_green_refactor_pipeline, which orchestrates all stages and returns a full report.
Demo
Run the built-in demo without IDE integration:
# Single workload
python scripts/run_demo.py --workload transaction_analytics
# All demo workloads
python scripts/run_demo.py --all
Available Demo Workloads
| Workload | Category | Anti-patterns | Expected Gain |
|---|---|---|---|
statistics_sorting |
Statistics & Sorting | Multiple sorts, list in sum, string concat | 20–50% |
numerical_loops |
Numerical Computation | Generator misuse, redundant computation | 15–40% |
transaction_analytics |
Data Processing | Deep copy, repeated iteration, O(n²) grouping | 30–60% |
Project Structure
GreenCodeMCP/
├── src/greencode_mcp/ # Core MCP tool package
│ ├── mcp_server.py # MCP server entry point (9 tools)
│ ├── config.py # Centralized configuration
│ ├── schemas.py # Data schemas
│ ├── analysis/ # 20 AST-based energy anti-pattern rules
│ ├── kb/ # TF-IDF retrieval over 800-entry KB
│ ├── generation/ # Refactoring (auto-fix + LLM)
│ ├── validation/ # 4-level functional equivalence gate
│ ├── benchmark/ # Controlled benchmarking (CodeCarbon)
│ ├── pipelines/ # End-to-end pipeline orchestration
│ └── reporting/ # JSON + Markdown report generation
├── data/
│ ├── knowledge_base/ # 800 curated energy-optimization samples
│ └── demo_workloads/ # 3 controlled demo scenarios
├── scripts/
│ └── run_demo.py # CLI demo runner
├── tests/ # Test suite (91 tests)
├── pyproject.toml # Package metadata and dependencies
├── requirements.txt # Python dependencies
├── .env.example # Environment variable template
└── LICENSE # MIT License
Knowledge Base
The data/knowledge_base/ directory contains 800 curated Python energy-optimization examples derived from 7,056 candidates through a 10-step filtering pipeline. Each entry includes:
- Original (inefficient) code
- Optimized code
- Functional test assertions
- CodeCarbon-estimated energy measurements (before/after)
- Quality tier (Gold / Silver / Bronze)
Sources: MBPP, Mercury (NeurIPS 2024), HumanEval.
Configuration
All parameters are configurable via environment variables or .env file:
| Variable | Default | Description |
|---|---|---|
NVIDIA_API_KEY |
(empty) | API key for LLM-assisted refactoring |
LLM_MODEL |
qwen/qwen2.5-coder-32b-instruct |
LLM model identifier |
LLM_BASE_URL |
https://integrate.api.nvidia.com/v1 |
LLM API endpoint |
BENCHMARK_WARMUP |
3 |
Warm-up iterations before measurement |
BENCHMARK_RUNS |
10 |
Number of measurement runs |
BENCHMARK_TIMEOUT |
60 |
Timeout per benchmark (seconds) |
LOG_LEVEL |
INFO |
Logging verbosity |
Running Tests
pytest tests/ -v
Limitations
- Energy/CO2 values are software-level estimates (CodeCarbon), not hardware measurements
- Validation depends on the completeness of available tests
- Currently supports Python only
- LLM-assisted refactoring requires internet access and API key
- The 20 AST rules detect local, single-file patterns only
Citation
If you use GreenCodeMCP in your research, please cite:
@inproceedings{greencodemcp2026,
title = {GreenCodeMCP: An MCP-based Tool for Sustainable Software
Maintenance and Resource-Aware Refactoring},
author = {Oubelmouh, Youssef and Daoudi, Amir Salah Eddine and Chhieng, Pierre},
booktitle = {Proceedings of the 42nd IEEE International Conference on
Software Maintenance and Evolution (ICSME), Tool Demonstration Track},
year = {2026}
}
License
MIT — see LICENSE.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。