byterover-cli icon indicating copy to clipboard operation
byterover-cli copied to clipboard

[BUG] MCP mode doesn't load .env file causing environment variable errors

Open butmasaru opened this issue 1 year ago • 3 comments

Description

When running cipher in MCP mode (--mode mcp), the .env file is not loaded, causing environment variables to be undefined. This issue is different from #159 (Ollama connection error) and affects all operating systems.

Environment

  • cipher version: 0.2.0
  • OS: Windows 11 (but affects all OS)
  • Node.js version: v20+
  • Usage: As MCP server with Claude Code

Steps to Reproduce

  1. Install cipher globally: npm install -g @byterover/cipher
  2. Create .env file in cipher installation directory with required variables:
    OLLAMA_BASEURL=http://localhost:11434/v1
    AZURE_OPENAI_API_KEY=your-key
    AZURE_OPENAI_ENDPOINT=your-endpoint
    
  3. Run cipher in MCP mode: cipher --mode mcp
  4. Environment variables are not loaded, causing connection failures

Root Cause

In dist/src/app/index.cjs (lines 4167-4171), the code skips loading .env in MCP mode:

isMcpMode = process.argv.includes("--mode") && process.argv[process.argv.indexOf("--mode") + 1] === "mcp";
if (isMcpMode) {
  // dotenv.config() is NOT called here - this is the bug
} else {
  (0, import_dotenv.config)();
}

Solution

I've tested the following fix which resolves the issue:

isMcpMode = process.argv.includes("--mode") && process.argv[process.argv.indexOf("--mode") + 1] === "mcp";
// Always load .env file, even in MCP mode
const path = require("path");
const envPath = path.resolve(__dirname, "../../../.env");
(0, import_dotenv.config)({ path: envPath });

Impact

  • MCP mode is unusable without this fix when using environment variables
  • Affects integration with tools like Claude Code that rely on MCP
  • Users must manually edit the compiled JavaScript file as a workaround

Additional Context

This bug was discovered while integrating cipher with Claude Code's MCP feature. The fix has been tested and confirms that:

  • Environment variables are properly loaded in MCP mode
  • The cipher MCP server functions correctly after the fix
  • No side effects on non-MCP mode operation

Would be happy to submit a PR with this fix if helpful!

butmasaru avatar Aug 05 '25 06:08 butmasaru

Hi, this is about the default settings behind of stdio so that it cannot use .env. You have to send a json config to it or you have to export key in your terminal before run mcp mode. We will update guidance in READ.me now

billcache avatar Aug 05 '25 09:08 billcache

Oh, I see! So it was a security design decision. I didn't fully understand that at first - thanks for clarifying!

I got it working by passing the environment variables through .claude.json instead of .env, and now it makes sense why you designed it this way. It's actually better for keeping different API keys separate for different projects.

I checked the README but didn't see this MCP-specific behavior mentioned yet - maybe the update is still in progress? It would be really helpful to add a note that MCP mode doesn't read .env files and requires environment variables to be passed through the MCP client's config.

Thanks again for the quick response!

butmasaru avatar Aug 05 '25 15:08 butmasaru

yea, we will update right now. Thank you for your contribution.

billcache avatar Aug 05 '25 15:08 billcache