Skip to main content
Glama

execute_code

Execute Python code in a secure sandbox environment for real-time lab scenarios and testing. Run and evaluate code remotely via the Model Context Protocol.

Instructions

Execute code in a secure sandbox environment.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
payloadNo
filepathNo
latest_generatedNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • app.py:385-410 (handler)
    The entry point for the 'execute_code' MCP tool, which orchestrates code preparation and calls the execution handler.
    @mcp.tool(name="execute_code", description="Execute code in a secure sandbox environment.")
    async def execute_code(
        payload: Optional[str] = None,
        filepath: Optional[str] = None,
        latest_generated: Optional[str] = None
    ) -> str:
        """
        Executes code in a secure sandbox.
        Accepts:
        - payload: code directly
        - filepath: path to code file
        - latest_generated: fallback prompt-generated code
        """
        host_id=""
        sample_code = read_code_input(payload, filepath, latest_generated)
        
        user_access = await handle_code_execution(sample_code)
    
        if "error" in user_access:
            return f"Error: {user_access['error']}"
        
        user_access_list = json.loads(user_access['userAccess'])
    
        # # Extract the ServerIP
        server_ip = next(item['value'] for item in user_access_list if item['key'] == 'ServerIP')
  • app.py:317-321 (handler)
    The handler function 'handle_code_execution' that initializes the lab session and language detection for code execution.
    async def handle_code_execution(payload: str) -> str:
        username = create_lab_sessionInfo()
        detected_lang = detect_language(payload)
        user_access = await _create_lab(username, detected_lang)
        return user_access
  • app.py:325-340 (handler)
    The core logic 'run_code_in_sandbox' that performs the actual code execution by sending a request to the sandbox server.
    async def run_code_in_sandbox(host_id: str, code: str) -> dict:
        url = f"http://{host_id}:8000/run_code"
        headers = {"Content-Type": "application/json"}
        payload = {"code": code}
    
        try:
            async with httpx.AsyncClient() as client:
                response = await client.post(url, headers=headers, json=payload)
                response.raise_for_status()
                result = response.json()
            
            return json.dumps({
                "status": "success",
                "message": "Code executed successfully.",
                "result": result
            })
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Nuvepro-Technologies-Pvt-Ltd/genai-sandbox-nuvepro'

If you have feedback or need assistance with the MCP directory API, please join our Discord server