How to Configure Environment Variables in Claude Desktop for MatlabMCP
Add an env object to the MatlabMCP server configuration in claude_desktop_config.json to inject custom environment variables that the server reads via standard os.getenv calls.
The MatlabMCP server from the jigarbhoye04/matlabmcp repository runs as a subprocess managed by Claude Desktop, allowing you to execute MATLAB code through the Model Context Protocol (MCP). When you need to pass API keys, file system paths, or debugging flags to this server, you must configure environment variables directly in Claude Desktop's server configuration file.
Locating the Claude Desktop Configuration File
To begin configuring environment variables for MatlabMCP, open Claude Desktop and navigate to Settings → Developer → Edit Config. This action opens the claude_desktop_config.json file, which stores all MCP server definitions and their respective settings.
Adding Environment Variables to the MatlabMCP Server
According to the configuration template shown in README.md at lines 71-75, the MatlabMCP entry uses the uv Python package manager to launch main.py. You can extend this configuration by adding an env object at the same level as the command and args properties.
Structure of the env Object
The env object contains key-value pairs where each key represents the variable name and each value is a JSON string. This structure adheres to the standard MCP server configuration schema supported by Claude Desktop.
Example configuration snippet:
{
"mcpServers": {
"MatlabMCP": {
"command": "C:\\Users\\username\\.local\\bin\\uv.exe",
"args": [
"--directory",
"C:\\Users\\username\\Desktop\\MatlabMCP\\",
"run",
"main.py"
],
"env": {
"MATLAB_ROOT": "C:/Program Files/MATLAB/R2023a",
"CUSTOM_LOG_LEVEL": "debug"
}
}
}
}
Configuration Requirements
- All values must be JSON strings, even for numeric or boolean settings.
- Variable names follow standard shell conventions (uppercase with underscores recommended).
- The
envobject is optional; omit it entirely if no custom variables are required.
Reading Environment Variables in Your Code
Once configured, Claude Desktop injects these variables into the server process environment before launching the Python interpreter. Both the Python server code and subsequent MATLAB executions can access these values.
Python Implementation in main.py
The server script main.py imports the os module at line 8 and reads environment variables using os.getenv at line 9 and throughout the codebase. This standard Python pattern allows flexible configuration without hard-coded defaults.
import os
# Retrieve variable with default fallback
matlab_root = os.getenv("MATLAB_ROOT", "C:/Program Files/MATLAB/R2023a")
log_level = os.getenv("CUSTOM_LOG_LEVEL", "info")
MATLAB Implementation
When the server executes MATLAB code via the engine API, those scripts inherit the same process environment. Use MATLAB's built-in getenv function to retrieve values:
% Inside MATLAB scripts executed by runMatlabCode
rootPath = getenv('MATLAB_ROOT');
fprintf('MATLAB installation: %s\n', rootPath);
Understanding Environment Propagation
The variable injection follows a clear process chain. Claude Desktop spawns the MCP server process with the specified env mapping, uv forwards this environment to the Python interpreter when running main.py, and the MATLAB Engine API inherits these values when starting MATLAB sessions. This architecture ensures that variables defined in claude_desktop_config.json are available at every execution layer.
Summary
- Configure environment variables for MatlabMCP by adding an
envobject to the server definition inclaude_desktop_config.json. - Reference the configuration template in
README.md(lines 71-75) for the correct JSON structure. - Access variables in Python using
os.getenvas implemented inmain.py(lines 8-9). - Access the same variables in MATLAB using the
getenvfunction. - Restart Claude Desktop after saving configuration changes to reload the environment.
Frequently Asked Questions
Where is the claude_desktop_config.json file located?
Claude Desktop manages this file internally. Access it through the application menu by selecting Settings → Developer → Edit Config, which opens the file in your default text editor regardless of operating system.
Can I use environment variables to specify the MATLAB installation path?
Yes. Set a variable like MATLAB_ROOT in the env object, then reference it in your Python code using os.getenv("MATLAB_ROOT") or in MATLAB scripts using getenv('MATLAB_ROOT') to dynamically locate the MATLAB executable.
Do environment variables support boolean or numeric values?
All values in the JSON env object must be strings. If you need to represent booleans or numbers, use string literals like "true", "false", or "8080", then parse them in your Python code using bool() or int() as needed.
Why do my environment variable changes not take effect immediately?
Claude Desktop reads claude_desktop_config.json only when launching the MCP server process. You must fully restart Claude Desktop after editing the configuration file to reload the environment variables into the server process.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →