Skip to content

Add Python code injection system for remote execution#1

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/update-codecarbon-requirements
Draft

Add Python code injection system for remote execution#1
Copilot wants to merge 3 commits intomainfrom
copilot/update-codecarbon-requirements

Conversation

Copy link

Copilot AI commented Feb 2, 2026

Implements AST-based code injection system from codecarbon/pull/1030 to enable dynamic modification of Python scripts before remote execution.

Changes

  • Injector class (src/mcp_remote_run/injector.py)

    • Variable injection with type coercion (str, int, float, bool, None)
    • Dependency injection via os.system("pip install ...")
    • Function body replacement via AST rewriting
    • Temporary file management with context manager support
    • Method chaining API
  • Package structure

    • pyproject.toml with libcst dependency
    • Test suite (14 tests, 90% coverage)
    • Usage examples

Example

from mcp_remote_run import Injector

code = """
def train_model():
    pass
"""

with Injector(code=code) as injector:
    injector.inject_variables({
        "epochs": 10,
        "batch_size": 32
    }).add_dependency([
        "codecarbon",
        "torch"
    ]).inject_function("""
from codecarbon import EmissionsTracker
tracker = EmissionsTracker()
tracker.start()
# training code here
tracker.stop()
""", "train_model")
    
    # Modified script at injector.get_temp_file_path()
    # Ready for remote execution

All operations use libcst for safe AST manipulation. Temporary files auto-cleanup on context exit or explicit destroy().

Original prompt

Use mlco2/codecarbon#1030 as a starting point.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits February 2, 2026 11:04
Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>
Co-authored-by: benoit-cty <6603048+benoit-cty@users.noreply.github.com>
Copilot AI changed the title [WIP] Update requirements for CodeCarbon package Add Python code injection system for remote execution Feb 2, 2026
Copilot AI requested a review from benoit-cty February 2, 2026 11:07
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.

2 participants