Skip to content

Add uvx mcix envs command to list environment variables in MCI schemas#33

Merged
MaestroError merged 7 commits intomainfrom
copilot/add-uvx-mcix-envs-command
Nov 12, 2025
Merged

Add uvx mcix envs command to list environment variables in MCI schemas#33
MaestroError merged 7 commits intomainfrom
copilot/add-uvx-mcix-envs-command

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 12, 2025

Implementation of uvx mcix envs Command ✅

This PR successfully implements a new CLI command uvx mcix envs that scans MCI schemas and reports all referenced environment variables.

✅ Completed Tasks:

  • Create environment variable extraction utility
    • Implement function to recursively scan dictionaries/strings for {{env.VAR}} patterns
    • Add function to extract env vars from all tool definitions
    • Handle both JSON and YAML schemas
  • Implement envs command CLI
    • Create src/mci/cli/envs.py with Click command
    • Support --file option to specify custom schema
    • Support --format option (table, env)
    • Add warning messages for failed toolset loads
  • Create env formatter for .env.example.mci output
    • Create src/mci/cli/formatters/env_formatter.py
    • Generate .env.example.mci file with empty values and location comments
    • Refactored to eliminate code duplication (DRY principle)
  • Add comprehensive tests
    • Unit tests for env extraction utility (14 tests)
    • Unit tests for envs command (9 tests)
    • Feature tests for both table and env formats (6 tests)
    • Test with JSON and YAML schemas
    • Manual demo test showing realistic e-commerce example
  • Update documentation
    • Add envs command to README.md with detailed examples
    • Provide usage examples in Quick Start section
    • Document both output formats (table and env)
    • Add to workflow examples
  • Security scan with CodeQL ✅ - 0 alerts
  • Final verification and testing ✅ - All 386 tests passing
  • Address code review feedback ✅
    • Refactored EnvFormatter to eliminate duplication between format_to_string and format_to_file
    • format_to_file now reuses format_to_string for DRY code

📦 Files Created:

  • src/mci/cli/envs.py - Main envs command (255 lines)
  • src/mci/cli/formatters/env_formatter.py - Env file formatter (84 lines, refactored)
  • src/mci/utils/env_scanner.py - Environment variable scanner (77 lines)
  • tests/unit/cli/test_envs.py - Unit tests (296 lines)
  • tests/test_envs_command.py - Feature tests (363 lines)
  • tests/unit/utils/test_env_scanner.py - Scanner tests (149 lines)
  • testsManual/test_envs_demo.py - Manual demonstration (251 lines)

🔧 Files Modified:

  • src/mci/mci.py - Registered envs command
  • README.md - Added comprehensive documentation (55 new lines)

🎯 Features Implemented:

✅ Scans entire MCI schema: main file, toolsets, and MCP server configurations
✅ De-duplicates variables and shows all locations in one row
✅ Table format shows variable names and where they're used (main/toolset/mcp:server)
✅ Env format generates .env.example.mci file with empty values and usage comments
✅ Handles missing toolset files gracefully with warning messages
✅ Works with both JSON and YAML schemas
✅ Recursive scanning of nested structures
✅ MCP server configurations are scanned separately from main schema
✅ Clean, DRY code following best practices

📊 Test Results:

  • Total Tests: 386 (29 new tests added)
  • Pass Rate: 100%
  • Linting: ✅ No errors, no warnings
  • Security: ✅ CodeQL found 0 alerts
  • Test Coverage: Comprehensive unit and feature tests

🎉 Implementation Complete

All requirements from the issue have been met. Code review feedback has been addressed. The command is fully functional, tested, documented, and ready for use.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add command: uvx mcix envs – List all used environment variables in MCI configuration</issue_title>
<issue_description>## Goal
Introduce a new CLI command uvx mcix envs which scans the entire MCI schema (Like the "list" command: main file, all registered tools from toolsets, MCP servers), detects every instance where an environment variable (env.VARNAME or {{env.VARNAME}}) is referenced, and outputs a distinct list of all required environment variables. The main purpose: help users quickly see what environment variables, secrets, and credentials are needed for the context.

Command Details

  • Command name: uvx mcix envs
  • Output formats supported:
    • table (default): List of env var names, where used (main file/toolset/mcp-server), description if available, sorted alphabetically
    • env: Generates .env.example.mci file with all referenced env vars (exported with empty values)
  • For each env variable:
    • Show the file/section referencing it: either main schema, or name/path of toolset/MCP toolset
    • Do NOT repeat the same variable if used in multiple places: each unique VAR is listed, but all locations shown in one column
    • Warnings: If a referenced toolset file is missing, output a warning but continue
  • Parsing must cover:
    • All places where env.VARNAME template is resolved: execution configs (http, cli, file, text), MCP server configs, toolset filter specs, directoryAllowList, top-level libraryDir if templated, etc
    • Both JSON and YAML schemas, throughout parsing
  • The set of files to scan is identical to the files loaded for the list command (main + registered toolsets + registered MCP toolsets)
  • Only registered tools and toolsets referenced per current schema/filters are scanned (same context as list)

Acceptance Criteria

  • uvx mcix envs scans and reports all used env vars across main schema, referenced toolsets, and MCP caches
  • Output formats: rich table (default), .env.example.mci (env format with empty values)
  • Table shows variable name, locations used, optional descriptions (if from schema metadata)
  • Supports --file to pick custom schema; warns on missing toolset files
  • De-duplicates variables and shows all locations in one row
  • Detects all template references (env.VARNAME) for both direct and nested usage
  • Uses and adapts existing tool loading/parsing logic from list command (Stage 5)
  • Implements minimal, non-invasive changes for reuse
  • Unit/feature tests for both formats and both JSON/YAML
  • No error if variable is used but not set; this command does not read the real .env files

Deliverables

  • src/mci/cli/envs.py: CLI command implementation, structured like list.py
  • src/mci/cli/formatters/env_formatter.py: Formatter for env output
  • src/mci/cli/formatters/table_formatter.py: Reuse/enhance for env reporting if needed
  • Tests:
    • tests/unit/cli/test_envs.py: Unit tests
    • tests/test_envs_command.py: End-to-end feature tests
  • README.md:
    • Document new envs command: format, output, and purpose
    • Example table and env output; recommendation to commit .env.example.mci as template
    • Add to command reference section
    • Something like: "Run uvx mcix envs to preview all environment variables your project/toolset/MCP server will require – output formats allow reviewing required variables and auto-generating your .env.example.mci template."

Example Output

Table format:

┌─────────────┬───────────────────────────┐
│ Env Var     │ Locations                 │
├─────────────┼───────────────────────────┤
│ API_KEY     │ main, weather-toolset     │
│ DB_URL      │ main, database-toolset    │
│ GITHUB_PAT  │ github-mcp-toolset        │
└─────────────┴───────────────────────────┘

env format:

# .env.example.mci
API_KEY=
DB_URL=
GITHUB_PAT=

Files to Create/Modify (PLAN.md)

  • src/mci/cli/envs.py (new command)
  • src/mci/cli/formatters/env_formatter.py (new)
  • src/mci/cli/formatters/table_formatter.py (reuse/enhance)
  • tests/unit/cli/test_envs.py
  • tests/test_envs_command.py
  • README.md (add usage docs)

Classes/Functions

  • envs_command(file: str, format: str) – main Click command
  • EnvFormatter class – builds .env.example.mci file from detected variables
  • Extend existing TableFormatter for env reporting
  • Utility functions to scan dicts/strings for all env.VAR references

Tests

  • Add tests for both formats (JSON & YAML schemas)
  • Add feature tests to verify parsing of execution/config/mcp blocks
  • Test handling of missing toolset files (warn + continue)

Acceptance Reminder

  • Env vars are detected by parsing all places with env.VAR references via template engine (not just explicit appearance)
  • Table and env formats provide actionable context for users managing credentials/secrets
  • Underlying logic must be DRY and reuse list command s...

✨ 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 12, 2025 18:08
Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
Copilot AI changed the title [WIP] Add command to list used environment variables in MCI configuration Add uvx mcix envs command to list environment variables in MCI schemas Nov 12, 2025
Copilot AI requested a review from MaestroError November 12, 2025 18:16
@MaestroError MaestroError requested a review from Copilot November 12, 2025 19:01
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 adds a new uvx mcix envs command that scans MCI schemas to discover and report all environment variable references across the configuration, helping developers understand what credentials and settings are needed for their project.

Key Changes

  • Environment variable scanner: Regex-based recursive scanner extracts {{env.VARIABLE}} patterns from nested data structures
  • Dual output formats: Table view for quick inspection and .env.example.mci file generation for team templates
  • Comprehensive scanning: Processes main schema, toolsets from libraryDir, and MCP server configurations with proper deduplication

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/mci/utils/env_scanner.py New recursive scanner utility using regex to extract environment variable references from strings, dicts, and lists
src/mci/cli/envs.py Command implementation that loads schema files, scans all referenced toolsets and MCP servers, and manages output formatting
src/mci/cli/formatters/env_formatter.py Formatter for generating .env.example.mci files with usage comments showing where each variable is referenced
src/mci/mci.py Registers the new envs command in the CLI
tests/unit/utils/test_env_scanner.py 14 unit tests covering scanner functionality including edge cases and invalid patterns
tests/unit/cli/test_envs.py 9 unit tests for command logic including schema loading, toolset processing, and both output formats
tests/test_envs_command.py 6 feature tests covering end-to-end workflows with JSON/YAML schemas and mixed toolsets
testsManual/test_envs_demo.py Manual demonstration script creating realistic e-commerce schema with 16 environment variables
README.md Documentation for new command including examples, Quick Start updates, and workflow integration

Comment on lines +68 to +103
def format_to_string(env_vars: dict[str, list[str]]) -> str:
"""
Format environment variables as a string in .env format.

Args:
env_vars: Dictionary mapping variable names to list of locations where used

Returns:
Formatted string in .env format

Example:
>>> env_vars = {"API_KEY": ["main"], "DB_URL": ["database-toolset"]}
>>> print(EnvFormatter.format_to_string(env_vars))
# .env.example.mci
...
"""
# Sort variables alphabetically
sorted_vars = sorted(env_vars.keys())

# Generate content
lines: list[str] = []
lines.append("# .env.example.mci")
lines.append("# Environment variables used in MCI configuration")
lines.append("#")
lines.append("# Copy this file to .env.mci and fill in your values")
lines.append("")

for var_name in sorted_vars:
locations = env_vars[var_name]
# Add comment showing where the variable is used
location_str = ", ".join(sorted(locations))
lines.append(f"# Used in: {location_str}")
lines.append(f"{var_name}=")
lines.append("")

return "\n".join(lines)
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 format_to_string method duplicates the formatting logic from format_to_file. This violates the DRY (Don't Repeat Yourself) principle and makes maintenance harder. Consider refactoring to reuse the formatting logic:

@staticmethod
def format_to_string(env_vars: dict[str, list[str]]) -> str:
    """Format environment variables as a string in .env format."""
    # Generate content using shared logic
    sorted_vars = sorted(env_vars.keys())
    
    lines: list[str] = []
    lines.append("# .env.example.mci")
    lines.append("# Environment variables used in MCI configuration")
    lines.append("#")
    lines.append("# Copy this file to .env.mci and fill in your values")
    lines.append("")
    
    for var_name in sorted_vars:
        locations = env_vars[var_name]
        location_str = ", ".join(sorted(locations))
        lines.append(f"# Used in: {location_str}")
        lines.append(f"{var_name}=")
        lines.append("")
    
    return "\n".join(lines)

@staticmethod
def format_to_file(env_vars: dict[str, list[str]], output_path: str | None = None) -> str:
    """Format environment variables and write to .env.example.mci file."""
    if output_path is None:
        output_path = ".env.example.mci"
    
    content = EnvFormatter.format_to_string(env_vars)
    Path(output_path).write_text(content)
    
    return output_path

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 apply changes based on this feedback

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 EnvFormatter to eliminate code duplication. The format_to_file method now reuses format_to_string, following the DRY principle. Fixed in commit 9fa2ea1.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: MaestroError <46760939+MaestroError@users.noreply.github.com>
@MaestroError MaestroError marked this pull request as ready for review November 12, 2025 20:28
@MaestroError MaestroError merged commit bf2db80 into main Nov 12, 2025
3 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.

Add command: uvx mcix envs – List all used environment variables in MCI configuration

3 participants