Skip to content

mcpland/mcp-env

Repository files navigation

mcp-env

Node CI npm license

A zero-dependency Node.js module that loads MCP (Model Context Protocol) environment variables and executes commands with those variables set. This tool creates temporary scripts to run commands with specific environment variables, making it easy to manage different configurations for your MCP applications.

Features

  • Zero Dependencies: Only uses Node.js built-in modules (except for dotenv)
  • Cross-Platform Support: Works on Windows, macOS, and Linux
  • Environment Variable Management: Loads variables from .env.mcp(in your user directory) file or custom env file
  • Script Caching: Reuses generated scripts for identical commands to improve performance
  • Secure: Uses MD5 hashing for script naming to avoid conflicts

Installation

npm install mcp-env

Or use it directly with npx:

npx mcp-env

Usage

Basic Syntax

npx mcp-env VAR1=VALUE1 VAR2=VALUE2 -- COMMAND_TO_RUN

Examples

Running a Node.js script with custom environment variables:

npx mcp-env NODE_ENV=production API_KEY=your-api-key -- node app.js

Running a Python script with multiple environment variables:

npx mcp-env PYTHONPATH=/usr/local/lib/python3.9 DATABASE_URL=postgresql://localhost:5432/mydb DEBUG=true -- python script.py

Running a shell command with environment variables:

npx mcp-env AWS_REGION=us-west-2 AWS_PROFILE=production -- aws s3 ls

Using with Docker:

npx mcp-env DOCKER_IMAGE=myapp:latest PORT=3000 -- docker run -p $PORT:3000 $DOCKER_IMAGE

Environment File Support

The tool automatically loads environment variables from .env.mcp file in your user directory. You can specify a custom environment file using the MCP_ENV_FILE environment variable:

MCP_ENV_FILE=.env.custom npx mcp-env VAR1=VALUE1 -- command

How It Works

  1. Parsing: The tool parses environment variables and the command from the command line arguments
  2. Script Generation: Creates a temporary script file with the environment variables and command
  3. Caching: Uses MD5 hash of variables and command to create unique script names
  4. Execution: Runs the generated script with the specified environment variables

Generated Scripts

The tool creates platform-specific scripts:

  • Windows: .bat files using set commands
  • Unix-like systems: .sh files using export commands

Scripts are stored in the same directory as the executable and are reused for identical commands.

Requirements

  • Node.js >= 16.0.0
  • Cross-platform support (Windows, macOS, Linux)

Development

Building

npm run build

Testing

This project uses Vitest for testing.

# Run all tests
npm run test:run

# Run tests in watch mode
npm run test

# Run tests with UI
npm run test:ui

See test/README.md for detailed testing information.

Dependencies

  • Runtime: dotenv for environment file loading
  • Development: TypeScript, @types/node, tsx, vitest

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

Author

Created by unadlib

Repository

https://github.com/mcpland/mcp-env

License

MIT License - see LICENSE file for details.

About

A zero-dependency module that loads MCP environment variables

Resources

License

Stars

Watchers

Forks

Packages

No packages published