Skip to content

Auto-parse .env and .env.mci files from project root and ./mci directory using python-dotenv#31

Merged
MaestroError merged 5 commits intomainfrom
copilot/auto-parse-env-files
Nov 12, 2025
Merged

Auto-parse .env and .env.mci files from project root and ./mci directory using python-dotenv#31
MaestroError merged 5 commits intomainfrom
copilot/auto-parse-env-files

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 11, 2025

Automatically loads environment variables from .env and .env.mci files in the project root and ./mci directory when initializing MCI configurations. Uses the industry-standard python-dotenv library for parsing. Implements exclusive loading behavior where .env.mci files take complete precedence over .env files for clean separation of MCI-specific configs.

Changes

Core Implementation

  • src/mci/utils/dotenv.py: Refactored to use python-dotenv library for robust .env file parsing
  • src/mci/core/config.py: Added auto_load_dotenv parameter to MCIConfig.load() and validate_schema()
  • src/mci/core/mci_client.py: Extended MCIClientWrapper to auto-load .env files on initialization
  • pyproject.toml: Added python-dotenv dependency

File Priority & Exclusive Loading Behavior

Important: MCI implements exclusive loading - when .env.mci files exist, .env files are completely ignored.

Loading Behavior:

  • If .env.mci files exist: Only .env.mci files are loaded
    1. ./mci/.env.mci - Library MCI-specific configs
    2. Root .env.mci - Project MCI-specific configs (overrides library)
  • If NO .env.mci files exist: Only .env files are loaded
    1. ./mci/.env - Library general defaults
    2. Root .env - Project-level configs (overrides library)
  • Then: System environment variables and explicit env_vars override all file-based configs

Behavior

  • Silent success if .env files don't exist
  • Can be disabled via auto_load_dotenv=False
  • Works with all CLI commands (run, list, validate) automatically
  • Uses python-dotenv for industry-standard parsing with proper quote handling

Example

Scenario 1: Using .env.mci (MCI-specific)

# Project structure:
# ./mci/.env.mci     → API_KEY=mci-library-default
# .env.mci           → API_KEY=my-mci-key
# .env               → (ignored - .env.mci takes precedence)

from mci.core.config import MCIConfig

config = MCIConfig()
client = config.load("mci.json")
# API_KEY=my-mci-key (from root .env.mci)
# .env file is NOT loaded

Scenario 2: Using .env (no .env.mci)

# Project structure:
# ./mci/.env         → DATABASE_URL=postgres://default
# .env               → DATABASE_URL=postgres://prod, API_KEY=key123

from mci.core.config import MCIConfig

config = MCIConfig()
client = config.load("mci.json")
# DATABASE_URL=postgres://prod (from root .env)
# API_KEY=key123 (from root .env)

Testing

  • 22 unit tests covering parsing, exclusive loading, and precedence
  • All 357 tests passing
  • Tests verify that .env files are ignored when .env.mci exists

Documentation

  • Added "Automatic .env File Loading" section to README.md
  • Documented exclusive loading behavior and precedence rules
  • Updated MCP client configuration examples with two scenarios
  • Updated docstrings in config.py and mci_client.py

Fixes #30

Original prompt

This section details on the original issue you should resolve

<issue_title>Auto-parse .env files at project root and ./mci directory, pass as env_vars</issue_title>
<issue_description>## Goal
Enable automatic detection and parsing of .env files for environment variable configuration when loading the main entry file (mci.json or mci.yaml). The CLI/tool should check for .env in both:

  • The project root (same location as the entry file)
  • The ./mci library directory
    If found, all environment variables from these files should be parsed and passed to MCIClient as the env_vars argument.

Acceptance Criteria

  • When initializing the CLI or loading an MCI file, the system searches for .env in both the project root and ./mci directory.
  • If .env is found, parses environment variables (supports standard dotenv format: KEY=VALUE, comments, blank lines, ignores export).
  • All parsed variables are merged (with the convention: root .env overrides ./mci/.env if keys conflict) and passed as env_vars to MCIClient.
  • Works for both mci.json and mci.yaml schema entry files.
  • No error if .env is missing in either location;
  • If both files exist, document precedence (root > ./mci) in README.md.
  • Tests verify resolution/merging behavior and correct passing to MCIClient.

Deliverables

  • Update config/file discovery logic (likely in src/mci/core/config.py and/or supporting utility modules):
    • Detect and parse .env files with dotenv rules
    • Merge and assemble the environment variable dict
    • Pass to MCIClient upon config/schema load
  • Update documentation:
    • README.md: Explain automatic .env parsing, override rules, and user workflow
    • Mention in relevant CLI command docs (install, run, validate, etc.) as needed
  • Add/modify tests:
    • Unit tests for .env detection and parsing
    • Feature tests for correct env_vars assignment in tool execution

README.md Note

  • Add a section describing the auto-detection behavior:
    • "If .env is present in the project root or ./mci directory, environment variables will be automatically loaded and passed to all CLI commands and tool execution. If a variable is defined in both locations, the project root takes precedence."

Files to Create/Modify

  • src/mci/core/config.py (main logic)
  • src/mci/utils/validation.py and/or new utility for dotenv parsing
  • README.md and relevant CLI docs
  • Tests: tests/unit/core/test_config.py, tests/test_config_loading.py, etc.

Classes/Functions

  • parse_dotenv_files() utility to detect/merge .env files and return dict
  • Extend MCIConfig class to call dotenv parser on load

Tests

  • Add tests for .env detection, merging, and parsing
  • Add feature tests for config initialization with env_vars

Acceptance Reminder

  • This feature must be silent if no .env files are present.
  • All changes must update README.md and user-facing docs for uvx mcix, especially in the configuration and environment variable sections.

Reference: mci-py docs (example only)

client = MCIClient(schema_file_path="example.mci.json", env_vars={
    "API_KEY": "key-value"
})

The above example is just for reference, real implementation is up to you.


Please ensure these changes maintain compatibility with the current mci-py and CLI workflow.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 11, 2025 00:51
- Add dotenv parsing utility with support for standard .env format
- Implement automatic .env file discovery from project root and ./mci directory
- Update MCIConfig and MCIClientWrapper to auto-load .env files
- Add comprehensive tests for dotenv parsing and loading
- Update README.md with .env auto-loading documentation
- Maintain precedence order: ./mci/.env < root .env < system env < explicit env_vars

Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
- Create comprehensive manual test for dotenv auto-loading
- Test all precedence scenarios
- Verify integration with CLI commands
- All 352 automated tests passing
- Linting passed successfully

Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
Copilot AI changed the title [WIP] Add automatic parsing of .env files for environment variables Auto-parse .env files from project root and ./mci directory Nov 11, 2025
Copilot AI requested a review from MaestroError November 11, 2025 00:58
@MaestroError MaestroError requested a review from Copilot November 11, 2025 14:58
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements automatic .env file loading for the MCI Python adapter, enabling seamless environment variable configuration without manual setup. When loading MCI schemas, the system now automatically discovers and parses .env files from both the project root and ./mci directory, merging them with defined precedence rules.

Key Changes:

  • New dotenv parser: Implements standard .env format support (KEY=VALUE, comments, export keyword, quoted values) in src/mci/utils/dotenv.py
  • Auto-loading integration: Modified MCIConfig and MCIClientWrapper to automatically load .env files on initialization with configurable auto_load_dotenv parameter
  • Comprehensive testing: Added 23 new tests covering parsing, merging, precedence, and integration scenarios

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/mci/utils/dotenv.py New module providing dotenv file parsing and merging utilities with precedence support
src/mci/core/config.py Enhanced MCIConfig.load() and validate_schema() to auto-load .env files with configurable behavior
src/mci/core/mci_client.py Extended MCIClientWrapper initialization to support auto-loading .env files from project directories
tests/unit/utils/test_dotenv.py Comprehensive unit tests for dotenv parsing, including edge cases like comments, quotes, and malformed lines
tests/unit/core/test_config.py Integration tests verifying .env auto-loading in config loading and precedence rules
tests/test_dotenv_loading.py Feature tests for end-to-end .env file loading behavior
testsManual/test_dotenv_autoload.py Manual tests for visual verification of .env auto-loading with rich console output
README.md Added extensive documentation on automatic .env file loading, precedence rules, and MCP client configuration
Comments suppressed due to low confidence (2)

testsManual/test_dotenv_autoload.py:12

  • Import of 'os' is not used.
import os

testsManual/test_dotenv_autoload.py:13

  • Import of 'shutil' is not used.
import shutil

Comment on lines +12 to +13
import os
import shutil
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused imports detected. The os and shutil modules are imported but never used in this manual test file. These should be removed to keep the code clean.

Suggested change
import os
import shutil

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ebda5c0. Removed unused imports os and shutil.

# If line doesn't match format, silently skip it
# (could be malformed or a directive we don't support)

except Exception:
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overly broad exception handler silently suppresses all errors. The bare except Exception: clause on line 81 catches all exceptions during file parsing, including unexpected errors like UnicodeDecodeError, MemoryError, or programming errors. This makes debugging difficult since all failures are silently ignored.

Consider catching only specific exceptions related to file reading (e.g., OSError, IOError) and letting other exceptions propagate, or at minimum log the error for debugging purposes.

Suggested change
except Exception:
except (OSError, IOError):

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please fix it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ebda5c0. Updated exception handling to catch specific exceptions: OSError, IOError, and UnicodeDecodeError instead of bare Exception.

Comment on lines +72 to +75
if (value.startswith('"') and value.endswith('"')) or (
value.startswith("'") and value.endswith("'")
):
value = value[1:-1]
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quote removal logic has a potential bug when handling mismatched quotes. If a value starts with a double quote but ends with a single quote (e.g., KEY="value'), the current implementation will incorrectly strip both characters, resulting in value instead of preserving the original "value'.

Consider adding validation to ensure matching quote types:

if len(value) >= 2:
    if value.startswith('"') and value.endswith('"'):
        value = value[1:-1]
    elif value.startswith("'") and value.endswith("'"):
        value = value[1:-1]
Suggested change
if (value.startswith('"') and value.endswith('"')) or (
value.startswith("'") and value.endswith("'")
):
value = value[1:-1]
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Why do we parse it manually? Isn't there any package as community standard in python for parsing .env files?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question! I initially chose to implement a simple parser to:

  1. Avoid adding external dependencies (keep the package lightweight)
  2. Only support the minimal dotenv features needed for this use case
  3. Maintain full control over behavior (silent failures, specific error handling)

However, if you prefer using a standard library like python-dotenv, we can switch to that. It's the most popular dotenv library in Python with excellent community support. Would you like me to refactor to use python-dotenv instead?

For now, I've fixed the quote handling bug in commit ebda5c0 to properly validate matching quote types.


# Load config
config = MCIConfig()
client = config.load(str(schema_file))
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable client is not used.

Suggested change
client = config.load(str(schema_file))
config.load(str(schema_file))

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ebda5c0. Removed unused client variable.


# Load config
config = MCIConfig()
client = config.load(str(schema_file))
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable client is not used.

Copilot uses AI. Check for mistakes.
# Load config with explicit env vars
config = MCIConfig()
explicit_env = {"OVERRIDE_VAR": "from-explicit", "EXPLICIT_ONLY": "explicit-value"}
client = config.load(str(schema_file), env_vars=explicit_env)
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable client is not used.

Suggested change
client = config.load(str(schema_file), env_vars=explicit_env)
config.load(str(schema_file), env_vars=explicit_env)

Copilot uses AI. Check for mistakes.
import tempfile
from pathlib import Path

import pytest
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import of 'pytest' is not used.

Suggested change
import pytest

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Remove unused imports

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit ebda5c0. Removed unused pytest import.

Copy link
Copy Markdown
Contributor

@MaestroError MaestroError left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please check comments and make fixes.

Also, let's add the .env.mci file parsing and make it a priority. So, at first, in both locations let's check the .env.mci file and if it doesn't exists, than check for .env file

Update documentation with change about ".env.mci" priority

# If line doesn't match format, silently skip it
# (could be malformed or a directive we don't support)

except Exception:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot please fix it

import tempfile
from pathlib import Path

import pytest
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Remove unused imports


# Load config
config = MCIConfig()
client = config.load(str(schema_file))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix it

Comment on lines +72 to +75
if (value.startswith('"') and value.endswith('"')) or (
value.startswith("'") and value.endswith("'")
):
value = value[1:-1]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Why do we parse it manually? Isn't there any package as community standard in python for parsing .env files?

- Remove unused imports (os, shutil, pytest)
- Fix exception handling to catch specific exceptions (OSError, IOError, UnicodeDecodeError)
- Fix quote removal logic to ensure matching quote types
- Remove unused client variables in manual tests
- Add support for .env.mci files with priority over .env files
- Update documentation with .env.mci precedence
- Add 5 new tests for .env.mci functionality
- All 357 tests passing

Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 11, 2025

@copilot Please check comments and make fixes.

Also, let's add the .env.mci file parsing and make it a priority. So, at first, in both locations let's check the .env.mci file and if it doesn't ex...

All feedback addressed in commit ebda5c0:

✅ Removed unused imports
✅ Fixed exception handling to be more specific
✅ Fixed quote removal logic
✅ Removed unused variables

Added .env.mci file support with the following priority (lowest to highest):

  1. ./mci/.env
  2. ./mci/.env.mci
  3. Root .env
  4. Root .env.mci
  5. System environment variables
  6. Explicit env_vars

Updated documentation in README.md with comprehensive .env.mci coverage, including precedence rules and examples.

Added 5 new tests for .env.mci functionality. All 357 tests passing.

Copilot AI changed the title Auto-parse .env files from project root and ./mci directory Auto-parse .env and .env.mci files from project root and ./mci directory Nov 11, 2025
Copilot AI requested a review from MaestroError November 11, 2025 15:19
@MaestroError MaestroError requested a review from Copilot November 12, 2025 14:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Comment on lines +72 to +77
# Remove quotes if present (ensure matching quote types)
if len(value) >= 2:
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quote handling logic doesn't properly validate matching quotes. A value like \"hello' (starting with double quote, ending with single quote) will not have quotes stripped as expected, but it also won't raise an error. This could lead to unexpected behavior where mismatched quotes are silently kept in the value. Consider either: (1) validating that quotes match and raising an error if they don't, or (2) documenting this behavior as intentional. The PR description mentions 'quoted values with matching validation' but this validation is missing.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please refactor this method (parse_dotenv_file) with "python-dotenv", we don't need to reinvent the wheel.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored in commit d762f44 to use python-dotenv library instead of manual parsing. This provides robust quote handling and follows industry standards.

Comment on lines +38 to +43
from .env files in the project root and ./mci directory. The precedence order is:
1. ./mci/.env (lowest priority)
2. Project root .env
3. System environment variables
4. env_vars argument (highest priority)

Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The precedence documentation is incomplete and doesn't match the actual implementation. According to the code in get_env_with_dotenv() and find_and_merge_dotenv_files(), there are actually 6 levels of precedence including .env.mci files. The correct order should be: 1. ./mci/.env, 2. ./mci/.env.mci, 3. Project root .env, 4. Project root .env.mci, 5. System environment variables, 6. env_vars argument. This discrepancy could confuse users about the actual behavior.

Suggested change
from .env files in the project root and ./mci directory. The precedence order is:
1. ./mci/.env (lowest priority)
2. Project root .env
3. System environment variables
4. env_vars argument (highest priority)
from .env and .env.mci files in both the project root and ./mci directory.
The precedence order (lowest to highest priority) is:
1. ./mci/.env
2. ./mci/.env.mci
3. Project root .env
4. Project root .env.mci
5. System environment variables
6. env_vars argument

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit d762f44. Updated documentation to reflect the new exclusive loading behavior:

  • If .env.mci files exist: only ./mci/.env.mci and root .env.mci are loaded
  • If NO .env.mci files: only ./mci/.env and root .env are loaded
  • .env files are completely ignored when .env.mci files exist

Comment on lines +45 to +48
1. ./mci/.env (lowest priority)
2. Project root .env
3. System environment variables
4. env_vars argument (highest priority)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The precedence documentation is incomplete and doesn't match the actual implementation. According to the code in get_env_with_dotenv() and find_and_merge_dotenv_files(), there are actually 6 levels of precedence including .env.mci files. The correct order should be: 1. ./mci/.env, 2. ./mci/.env.mci, 3. Project root .env, 4. Project root .env.mci, 5. System environment variables, 6. env_vars argument. This discrepancy could confuse users about the actual behavior.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit d762f44. Updated config.py documentation with the new exclusive loading behavior and correct priority order.

Comment on lines +96 to +103
This function looks for .env files in two locations with the following priority:
1. {project_root}/.env.mci (highest priority for MCI-specific configs)
2. {project_root}/.env (project-level configs)
3. {project_root}/mci/.env.mci (MCI library-level MCI-specific configs)
4. {project_root}/mci/.env (lowest priority - MCI library defaults)

Files are checked in order at each location (.env.mci first, then .env).
Variables from higher priority files override those from lower priority files.
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation incorrectly states the priority order. The listed order shows root files as highest priority (1-2) and mci directory files as lower priority (3-4), but the actual implementation in lines 122-145 loads files in the opposite order: mci directory files first (lowest priority), then root files (higher priority). The implementation is correct, but the documentation should list them in the order they are applied (lowest to highest): 1. ./mci/.env, 2. ./mci/.env.mci, 3. root .env, 4. root .env.mci.

Suggested change
This function looks for .env files in two locations with the following priority:
1. {project_root}/.env.mci (highest priority for MCI-specific configs)
2. {project_root}/.env (project-level configs)
3. {project_root}/mci/.env.mci (MCI library-level MCI-specific configs)
4. {project_root}/mci/.env (lowest priority - MCI library defaults)
Files are checked in order at each location (.env.mci first, then .env).
Variables from higher priority files override those from lower priority files.
This function looks for .env files in two locations and merges them in the following order (lowest to highest priority):
1. {project_root}/mci/.env (lowest priority - MCI library defaults)
2. {project_root}/mci/.env.mci (MCI library-level MCI-specific configs)
3. {project_root}/.env (project-level configs)
4. {project_root}/.env.mci (highest priority for MCI-specific configs)
Files are loaded in this order, and variables from higher priority files override those from lower priority files.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit d762f44. Updated dotenv.py documentation to correctly describe the exclusive loading behavior:

  • Checks for .env.mci files first in both locations
  • If found, only loads .env.mci files (ignores .env)
  • If not found, loads .env files instead

Copy link
Copy Markdown
Contributor

@MaestroError MaestroError left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix the order for env files, current comments are mainly correct, but code doesn't follow the priority order.

Also, refactor env parsing with python-dotenv

Comment on lines +45 to +48
1. ./mci/.env (lowest priority)
2. Project root .env
3. System environment variables
4. env_vars argument (highest priority)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

Comment on lines +38 to +43
from .env files in the project root and ./mci directory. The precedence order is:
1. ./mci/.env (lowest priority)
2. Project root .env
3. System environment variables
4. env_vars argument (highest priority)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

Comment on lines +72 to +77
# Remove quotes if present (ensure matching quote types)
if len(value) >= 2:
if value.startswith('"') and value.endswith('"'):
value = value[1:-1]
elif value.startswith("'") and value.endswith("'"):
value = value[1:-1]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please refactor this method (parse_dotenv_file) with "python-dotenv", we don't need to reinvent the wheel.

Comment on lines +96 to +103
This function looks for .env files in two locations with the following priority:
1. {project_root}/.env.mci (highest priority for MCI-specific configs)
2. {project_root}/.env (project-level configs)
3. {project_root}/mci/.env.mci (MCI library-level MCI-specific configs)
4. {project_root}/mci/.env (lowest priority - MCI library defaults)

Files are checked in order at each location (.env.mci first, then .env).
Variables from higher priority files override those from lower priority files.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please fix it and make sure it first checks for ".env.mci" files in both locations and if they doesn't exists, than check for .env file at first in ./mci directory than in root.

- Add python-dotenv dependency for robust .env parsing
- Implement new priority logic: .env.mci files take precedence
- When .env.mci files exist, .env files are NOT loaded (exclusive)
- Priority: ./mci/.env.mci, then root .env.mci (if .env.mci exist)
- OR: ./mci/.env, then root .env (if no .env.mci files)
- Update all documentation to reflect new behavior
- Update tests to match new exclusive loading logic
- All 357 tests passing

Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 12, 2025

@copilot Please fix the order for env files, current comments are mainly correct, but code doesn't follow the priority order.

Also, refactor env parsing with python-dotenv

All changes completed in commit d762f44:

Refactored with python-dotenv - Using industry-standard library for parsing
Fixed priority order - Implemented exclusive loading:

  • If .env.mci files exist: only .env.mci files loaded
  • If no .env.mci files: only .env files loaded
  • Priority: ./mci directory first, then root (for whichever file type exists)

Updated documentation in README.md, config.py, and mci_client.py
Updated tests to match new behavior
All 357 tests passing

Copilot AI changed the title Auto-parse .env and .env.mci files from project root and ./mci directory Auto-parse .env and .env.mci files from project root and ./mci directory using python-dotenv Nov 12, 2025
Copilot AI requested a review from MaestroError November 12, 2025 17:41
@MaestroError MaestroError marked this pull request as ready for review November 12, 2025 17:50
@MaestroError MaestroError merged commit 32fe4b5 into main Nov 12, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-parse .env files at project root and ./mci directory, pass as env_vars

3 participants