SoftwareEnergyEfficiency-MCP-Server

mmeinhar/SoftwareEnergyEfficiency-MCP-Server

3.2

If you are the rightful owner of SoftwareEnergyEfficiency-MCP-Server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

The Software Energy Efficiency MCP Server provides tools for suggesting energy efficiency improvements in computer code, currently supporting Java.

Software Energy Efficiency MCP Server

The MCP (Model Context Protocol) Server enables developers to write energy-efficient code, minimizing software’s environmental and operational costs. Its research dataset currently supports Java but is architected to support additional languages by expanding the dataset. It integrates with AI agents like GitHub Copilot to suggest sustainable optimizations.

The server enhances LLM-based code optimization by delivering customized, high-quality energy-efficiency research data to an AI agent. This agent incorporates the data into LLM prompts, prioritizing effective code optimization for reduced energy consumption. Using semantic search, the server analyzes energy efficiency observations, providing research-backed suggestions (e.g., from energy optimization studies; see Acknowledgments).

Written in Python with FastMCP 2.x, it supports local or remote operation and scalable data storage (JSON or cloud-hosted Qdrant). Configurable authentication and Docker-based deployment ensure secure, flexible operation.

White Paper Supplement

This software project supplements the white paper "Energy-Efficiency Tactics in Software Architecture and Implementation" (author: Max Meinhardt), which is a software energy-efficiency tactic reference guide for software architecture and implementation of cloud and non-cloud Web and IoT software applications.

Features

  • Supports any computer language. Current available data sets: Java.
  • Semantic search for relevant energy efficiency suggestions based on code snippets.
  • Conditional storage: JSON for simple setups and small energy efficiency datasets, Qdrant with scalable vector search for large data sets.
  • Scripted migration from JSON to Qdrant vector database
  • Configurable levels of server authentication depending on the deployment environment (Production: JWKS-based JWT verification, Staging: Static public key JWT verification, Development: Static token verification, and None)).
  • Centralized configuration in '.env' file for server settings.
  • Unit tests for core functionality.
  • Containerized for easy deployment and development.

Tested with the following MCP Clients

  • GitHub Copilot in Visual Studio Code

Project Structure

  • .devcontainer/: GitHub Codespaces configuration (devcontainer.json).
  • data/: Software energy efficiency dataset JSON file.
  • src/: Source code for the server.
  • src/tools/: Scripts for the JSON to database migration and test token generation.
  • tests/: Unit tests.
  • example_code_for_testing/: Examples of energy-inefficient code in multiple languages that is used for testing the MCP server.
  • Dockerfile: Defines the Docker image for the server.
  • .env.example: Example environment configuration file. Renamed to '.env' to use it.
  • LICENSE: MIT License for the project.

Settings

The MCP server is configured via the following environment variables that are set in the '.env' file or passed in as arguments when running the Docker container. Example values are provided in the '.env.example' file. The server can be run in different environments (Development, Staging, Production) with varying configurations for database usage, transport method, and authentication.:

Variable NameDescriptionEnvironment
USE_DBSet to true to use Qdrant vector DB, false for JSON file in data/efficiency-data.jsonDevelopment, Staging, Production
TRANSPORTTransport: stdio for local, sse for remoteDevelopment, Staging, Production
QDRANT_URLFor Qdrant (required if USE_DB=true)Development, Staging, Production
QDRANT_API_KEYFor Qdrant (required if USE_DB=true)Development, Staging, Production
MCP_SERVER_LOG_LEVELLogging level: DEBUG, INFO, WARNING, ERROR, CRITICALDevelopment, Staging, Production
FASTMCP_SERVER_AUTHAuthentication type: JWT (production), STATIC_JWT (staging), STATIC_TOKEN (development), or none (no auth)Development, Staging, Production
FASTMCP_SERVER_AUTH_JWT_JWKS_URIJWKS endpoint URL for fetching public keys (for JWT authentication in production)Production
FASTMCP_SERVER_AUTH_JWT_ISSUERToken issuer for JWT authentication (production)Production
FASTMCP_SERVER_AUTH_JWT_AUDIENCEExpected audience for JWT tokens (production)Production
FASTMCP_SERVER_AUTH_JWT_REQUIRED_SCOPESComma-separated list of required scopes for JWT authentication (production)Production
FASTMCP_SERVER_AUTH_JWT_PUBLIC_KEYStatic public key in PEM format for JWT verification (for STATIC_JWT authentication in staging)Staging
FASTMCP_SERVER_AUTH_JWT_ISSUERToken issuer for static JWT authentication (staging)Staging
FASTMCP_SERVER_AUTH_JWT_AUDIENCEExpected audience for static JWT authentication (staging)Staging
FASTMCP_SERVER_AUTH_JWT_REQUIRED_SCOPESComma-separated list of required scopes for static JWT authentication (staging)Staging

NOTES:

  • QDRANT_URL and QDRANT_API_KEY are required only if USE_DB is set to 'true'.
  • FASTMCP_SERVER_AUTH_JWT_* variables apply based on the FASTMCP_SERVER_AUTH setting: JWT for production, STATIC_JWT for staging, and STATIC_TOKEN for development.
  • The FASTMCP_SERVER_AUTH_JWT_ISSUER and FASTMCP_SERVER_AUTH_JWT_AUDIENCE variables appear twice, as they are used for both JWT (production) and STATIC_JWT (staging) authentication with potentially different values.

Container Setup

To build and run locally, follow these steps:

  1. Clone the repository (if not already done):
    git clone https://github.com/mmeinhar/SoftwareEnergyEfficiency-MCP-Server.git
    cd SoftwareEnergyEfficiency-MCP-Server
    
  2. Copy the '.env.example' file to '.env'. Customize the environment variables in it to fit your requirements. These will be the default settings for the server:
    cp .env.example .env
    
  3. If you have set FASTMCP_SERVER_AUTH = STATIC_JWT for the Staging environment's authentication setup, you can create a public key for testing.
    docker run --rm agile7-software-energy-efficiency-mcp-server python src/tools/generate_mcp_server_test_token.py
    
    Assign the generated public key to the FASTMCP_SERVER_AUTH_JWT_PUBLIC_KEY environment variable in the '.env' file or as its assigned argument when running the container:
  4. Change the permissions of the '.env' file to ensure it is only readable and writable by the owner:
    chmod 600 .env
    
  5. Build the Docker image:
    docker build -t agile7-software-energy-efficiency-mcp-server .
    
  6. Check that the Docker image has been created successfully:
    docker images
    
  7. If you want to use Qdrant instead of the efficiency-data.json file to store the energy optimization data, migrate it from the JSON file to Qdrant by running the data migration script:
       docker run --rm agile7-software-energy-efficiency-mcp-server python src/tools/migrate_data_from_json_to_db.py
    
  8. Run the Docker container:
    docker run -it -p 8000:8000 agile7-software-energy-efficiency-mcp-server
    
    To run the Docker container in Debug mode, you can use the following command:
    docker run -it -e MCP_SERVER_LOG_LEVEL=DEBUG -p 8000:8000 agile7-software-energy-efficiency-mcp-server
    

Integration with GitHub Copilot

Configure GitHub Copilot to use this MCP server as a custom provider. When a Copilot user selects some code in their IDE and prompts CoPilot to optimize the code to be energy efficient, Copilot will associate the prompt to the MCP server's 'optimize_energy_efficiency' tool and send the selected code as strings to its input arguments. The tool then returns suggestions from its energy optimization database which the LLM used to apply to apply changes or generate new code, depending on the context which Copilot utilizes. When the MCP server's tool is invoked by Copilot, Copilot will ask the user to authorize the tool's use before applying the prompt's result.

Prompts to Ensure Copilot Uses the MCP Server

If a prompt is not created properly, the MCP server's tool may not be invoked by Copilot and will instead use the LLM to answer the prompt which may result in incomplete or innaccurate energy optimization results. To force a call to the optimize_energy_efficiency tool from GitHub Copilot, select some code in the IDE and then open the Copilot inline chat and enter "#optimize_energy_efficiency: analyze this Java code".

Dataset

The dataset is in data/efficiency_data.json. Add more observations as needed. Any language can be added, but it currently contains only Java data. Format:

[
  {"lang": "java", "component": "Variables", lang_keywords": "keyword1, keyword2", "observation": "..."}
]

Acknowledgments

This project's Java dataset in the efficiency-data.json file uses some data from the following academic research papers and industry publications:

License

This project is licensed under the MIT License. See the file for details. The efficiency-data.json file contains some data derived from third-party research papers, which may be subject to separate licensing terms. See the Acknowledgments section for details.