Skip to content

remote_mcp_execution

Class info

Classes

Name Children Inherits
RemoteMCPExecutor
llmling_agent.resource_providers.codemode.remote_mcp_execution
Provides secure code execution with tool access.

    🛈 DocStrings

    Code execution provider with secure tool isolation via FastAPI server.

    RemoteMCPExecutor dataclass

    Provides secure code execution with tool access.

    Code Generation mode (ctx-zip style): - Tool functions are generated as Python files inside sandbox - User code imports tools directly, no HTTP server needed - Better for cloud sandboxes (E2B, etc.) that can't reach localhost

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    @dataclass
    class RemoteMCPExecutor:
        """Provides secure code execution with tool access.
    
        Code Generation mode (ctx-zip style):
           - Tool functions are generated as Python files inside sandbox
           - User code imports tools directly, no HTTP server needed
           - Better for cloud sandboxes (E2B, etc.) that can't reach localhost
        """
    
        toolset_generator: ToolsetCodeGenerator
        """Code generator for tools."""
    
        execution_env: ExecutionEnvironment
        """Execution environment for running code."""
    
        use_code_generation: bool = False
        """If True, use code generation approach instead of HTTP server."""
    
        @classmethod
        def from_tools(
            cls,
            tools: Sequence[Tool],
            env_config: ExecutionEnvironmentConfig,
            include_docstrings: bool = True,
        ) -> RemoteMCPExecutor:
            """Create provider from tools and environment configuration.
    
            Args:
                tools: Tools to make available for code execution
                env_config: Execution environment configuration
                include_docstrings: Include function docstrings in documentation
    
            Returns:
                RemoteMCPExecutor instance
            """
            from llmling_agent.resource_providers.codemode.helpers import tools_to_codegen
    
            toolset_gen = tools_to_codegen(tools, include_docstrings)
            execution_env = env_config.get_provider()
            return cls(toolset_gen, execution_env)
    
        def get_tool_description(self) -> str:
            """Get comprehensive description of available tools."""
            # For code generation mode, provide import-based usage instructions
            tool_names = [gen.name for gen in self.toolset_generator.generators]
            desc = self.toolset_generator.generate_tool_description()
            desc += "Usage:\n"
            for name in tool_names:
                desc += f"  from tools.{name} import {name}\n"
                desc += f"  result = await {name}(...)  # or {name}(...) if sync\n"
            return desc
    
        async def execute_code(self, code: str) -> Any:
            """Execute code with tools available.
    
            Args:
                code: Python code to execute
    
            Returns:
                Execution result from the environment
            """
            return await self.execution_env.execute(code)
    
        async def __aenter__(self):
            """Async context manager entry."""
            await self.execution_env.__aenter__()
            return self
    
        async def __aexit__(self, exc_type, exc_val, exc_tb):
            """Async context manager exit."""
            return await self.execution_env.__aexit__(exc_type, exc_val, exc_tb)
    

    execution_env instance-attribute

    execution_env: ExecutionEnvironment
    

    Execution environment for running code.

    toolset_generator instance-attribute

    toolset_generator: ToolsetCodeGenerator
    

    Code generator for tools.

    use_code_generation class-attribute instance-attribute

    use_code_generation: bool = False
    

    If True, use code generation approach instead of HTTP server.

    __aenter__ async

    __aenter__()
    

    Async context manager entry.

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    89
    90
    91
    92
    async def __aenter__(self):
        """Async context manager entry."""
        await self.execution_env.__aenter__()
        return self
    

    __aexit__ async

    __aexit__(exc_type, exc_val, exc_tb)
    

    Async context manager exit.

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    94
    95
    96
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        """Async context manager exit."""
        return await self.execution_env.__aexit__(exc_type, exc_val, exc_tb)
    

    execute_code async

    execute_code(code: str) -> Any
    

    Execute code with tools available.

    Parameters:

    Name Type Description Default
    code str

    Python code to execute

    required

    Returns:

    Type Description
    Any

    Execution result from the environment

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    async def execute_code(self, code: str) -> Any:
        """Execute code with tools available.
    
        Args:
            code: Python code to execute
    
        Returns:
            Execution result from the environment
        """
        return await self.execution_env.execute(code)
    

    from_tools classmethod

    from_tools(
        tools: Sequence[Tool],
        env_config: ExecutionEnvironmentConfig,
        include_docstrings: bool = True,
    ) -> RemoteMCPExecutor
    

    Create provider from tools and environment configuration.

    Parameters:

    Name Type Description Default
    tools Sequence[Tool]

    Tools to make available for code execution

    required
    env_config ExecutionEnvironmentConfig

    Execution environment configuration

    required
    include_docstrings bool

    Include function docstrings in documentation

    True

    Returns:

    Type Description
    RemoteMCPExecutor

    RemoteMCPExecutor instance

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    @classmethod
    def from_tools(
        cls,
        tools: Sequence[Tool],
        env_config: ExecutionEnvironmentConfig,
        include_docstrings: bool = True,
    ) -> RemoteMCPExecutor:
        """Create provider from tools and environment configuration.
    
        Args:
            tools: Tools to make available for code execution
            env_config: Execution environment configuration
            include_docstrings: Include function docstrings in documentation
    
        Returns:
            RemoteMCPExecutor instance
        """
        from llmling_agent.resource_providers.codemode.helpers import tools_to_codegen
    
        toolset_gen = tools_to_codegen(tools, include_docstrings)
        execution_env = env_config.get_provider()
        return cls(toolset_gen, execution_env)
    

    get_tool_description

    get_tool_description() -> str
    

    Get comprehensive description of available tools.

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    def get_tool_description(self) -> str:
        """Get comprehensive description of available tools."""
        # For code generation mode, provide import-based usage instructions
        tool_names = [gen.name for gen in self.toolset_generator.generators]
        desc = self.toolset_generator.generate_tool_description()
        desc += "Usage:\n"
        for name in tool_names:
            desc += f"  from tools.{name} import {name}\n"
            desc += f"  result = await {name}(...)  # or {name}(...) if sync\n"
        return desc
    

    add_numbers

    add_numbers(x: int, y: int) -> int
    

    Add two numbers.

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    104
    105
    106
    def add_numbers(x: int, y: int) -> int:
        """Add two numbers."""
        return x + y
    

    demo_code_generation_approach async

    demo_code_generation_approach()
    

    Demo new code generation approach (ctx-zip style, works with remote envs).

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
        async def demo_code_generation_approach():
            """Demo new code generation approach (ctx-zip style, works with remote envs)."""
            print("\n=== Code Generation Approach (ctx-zip style) ===")
            tools = [Tool.from_callable(add_numbers), Tool.from_callable(multiply_numbers)]
            config = LocalExecutionEnvironmentConfig()  # Could be E2B, etc.
            provider = RemoteMCPExecutor.from_tools(tools, config)
            async with provider:
                print("Tool description:")
                print(provider.get_tool_description())
                print("\nExecuting code with direct imports...")
    
                # Code that imports tools directly (no HTTP calls)
                code = """
    from tools.add_numbers import add_numbers
    from tools.multiply_numbers import multiply_numbers
    
    # Direct function calls - no HTTP server needed!
    result1 = add_numbers(5, 3)
    result2 = multiply_numbers(4, 7)
    
    _result = {"addition": result1, "multiplication": result2}
    print(f"Addition: {result1}, Multiplication: {result2}")
    """
                result = await provider.execute_code(code)
                print(f"Result: {result.result}")
    

    multiply_numbers

    multiply_numbers(x: int, y: int) -> int
    

    Multiply two numbers.

    Source code in src/llmling_agent/resource_providers/codemode/remote_mcp_execution.py
    108
    109
    110
    def multiply_numbers(x: int, y: int) -> int:
        """Multiply two numbers."""
        return x * y