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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/env python3
"""
LibreChat Custom Code Interpreter - HuggingFace Space
A secure code execution server using Gradio + MCP with SSE support
"""
 
import json
import subprocess
import tempfile
import os
import sys
import uuid
import time
import base64
import io
from pathlib import Path
from typing import Dict, Any, Optional, List
import gradio as gr
import matplotlib
matplotlib.use('Agg')  # Use non-interactive backend
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
class SecureCodeExecutor:
    def __init__(self):
        self.sessions = {}
        self.max_execution_time = 30
        self.max_output_length = 10000
        self.allowed_languages = ["python", "javascript", "bash"]
        
        # Security: List of blocked commands/imports
        self.blocked_imports = [
            'subprocess', 'os', 'sys', 'shutil', 'glob', 'pickle', 
            'marshal', 'imp', 'importlib', '__import__'
        ]
        self.blocked_bash_commands = [
            'rm', 'sudo', 'chmod', 'chown', 'dd', 'mkfs', 'fdisk',
            'curl', 'wget', 'ssh', 'scp', 'nc', 'netcat'
        ]
 
    def create_session(self) -> str:
        """Create a new execution session"""
        session_id = str(uuid.uuid4())[:8]  # Shorter ID for HF
        self.sessions[session_id] = {
            'created_at': time.time(),
            'variables': {},
            'history': [],
            'files': {}
        }
        return session_id
 
    def cleanup_old_sessions(self):
        """Remove sessions older than 1 hour"""
        current_time = time.time()
        old_sessions = [
            sid for sid, session in self.sessions.items()
            if current_time - session['created_at'] > 3600
        ]
        for sid in old_sessions:
            del self.sessions[sid]
 
    def is_code_safe(self, code: str, language: str) -> tuple[bool, str]:
        """Check if code is safe to execute"""
        if language == "python":
            # Check for blocked imports
            for blocked in self.blocked_imports:
                if blocked in code:
                    return False, f"Blocked import/function: {blocked}"
            
            # Check for dangerous patterns
            dangerous_patterns = ['exec(', 'eval(', 'open(', 'file(', '__']
            for pattern in dangerous_patterns:
                if pattern in code:
                    return False, f"Dangerous pattern detected: {pattern}"
        
        elif language == "bash":
            # Check for blocked commands
            for blocked in self.blocked_bash_commands:
                if blocked in code.lower():
                    return False, f"Blocked command: {blocked}"
        
        return True, ""
 
    def execute_python_code(self, code: str, session_id: Optional[str] = None) -> Dict[str, Any]:
        """Execute Python code with visualization support"""
        # Security check
        is_safe, reason = self.is_code_safe(code, "python")
        if not is_safe:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"Security violation: {reason}",
                "execution_time": time.time()
            }
 
        # Prepare execution environment
        setup_code = '''
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import json
import math
import random
import base64
import io
from datetime import datetime, timedelta
 
# Custom print function to capture output
_output_buffer = []
_original_print = print
def print(*args, **kwargs):
    _output_buffer.append(' '.join(str(arg) for arg in args))
 
# Function to save plots as base64
def save_current_plot():
    if plt.get_fignums():  # Check if there are any figures
        buffer = io.BytesIO()
        plt.savefig(buffer, format='png', bbox_inches='tight', dpi=100)
        buffer.seek(0)
        plot_data = buffer.getvalue()
        buffer.close()
        return base64.b64encode(plot_data).decode()
    return None
'''
 
        # Combine setup and user code
        full_code = setup_code + "\n" + code + "\n"
        
        # Add plot capture if plotting commands detected
        if any(cmd in code for cmd in ['plt.', 'plot(', 'scatter(', 'bar(', 'hist(']):
            full_code += "\n_plot_data = save_current_plot()\nif _plot_data: _output_buffer.append('PLOT_DATA:' + _plot_data)\n"
 
        try:
            # Create temporary file
            with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
                f.write(full_code)
                temp_file = f.name
 
            # Execute with timeout
            result = subprocess.run(
                [sys.executable, temp_file],
                capture_output=True,
                text=True,
                timeout=self.max_execution_time,
                cwd=tempfile.gettempdir()
            )
 
            # Process output
            stdout = result.stdout
            stderr = result.stderr
            plot_data = None
 
            # Extract plot data if present
            if 'PLOT_DATA:' in stdout:
                lines = stdout.split('\n')
                clean_lines = []
                for line in lines:
                    if line.startswith('PLOT_DATA:'):
                        plot_data = line.replace('PLOT_DATA:', '')
                    else:
                        clean_lines.append(line)
                stdout = '\n'.join(clean_lines)
 
            # Limit output length
            if len(stdout) > self.max_output_length:
                stdout = stdout[:self.max_output_length] + "\n... (output truncated)"
 
            execution_result = {
                "success": result.returncode == 0,
                "stdout": stdout.strip(),
                "stderr": stderr.strip() if stderr else "",
                "execution_time": time.time(),
                "return_code": result.returncode
            }
 
            if plot_data:
                execution_result["plot"] = plot_data
 
            return execution_result
 
        except subprocess.TimeoutExpired:
            return {
                "success": False,
                "stdout": "",
                "stderr": "Execution timed out (30s limit)",
                "execution_time": time.time()
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": str(e),
                "execution_time": time.time()
            }
        finally:
            if 'temp_file' in locals():
                try:
                    os.unlink(temp_file)
                except:
                    pass
 
    def execute_javascript_code(self, code: str, session_id: Optional[str] = None) -> Dict[str, Any]:
        """Execute JavaScript code using Node.js"""
        # Security check
        is_safe, reason = self.is_code_safe(code, "javascript")
        if not is_safe:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"Security violation: {reason}",
                "execution_time": time.time()
            }
 
        try:
            with tempfile.NamedTemporaryFile(mode='w', suffix='.js', delete=False) as f:
                f.write(code)
                temp_file = f.name
 
            result = subprocess.run(
                ['node', temp_file],
                capture_output=True,
                text=True,
                timeout=self.max_execution_time
            )
 
            stdout = result.stdout
            if len(stdout) > self.max_output_length:
                stdout = stdout[:self.max_output_length] + "\n... (output truncated)"
 
            return {
                "success": result.returncode == 0,
                "stdout": stdout.strip(),
                "stderr": result.stderr.strip() if result.stderr else "",
                "execution_time": time.time(),
                "return_code": result.returncode
            }
 
        except subprocess.TimeoutExpired:
            return {
                "success": False,
                "stdout": "",
                "stderr": "Execution timed out (30s limit)",
                "execution_time": time.time()
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": str(e),
                "execution_time": time.time()
            }
        finally:
            if 'temp_file' in locals():
                try:
                    os.unlink(temp_file)
                except:
                    pass
 
    def execute_bash_command(self, command: str, session_id: Optional[str] = None) -> Dict[str, Any]:
        """Execute bash commands with security restrictions"""
        # Security check
        is_safe, reason = self.is_code_safe(command, "bash")
        if not is_safe:
            return {
                "success": False,
                "stdout": "",
                "stderr": f"Security violation: {reason}",
                "execution_time": time.time()
            }
 
        try:
            result = subprocess.run(
                command,
                shell=True,
                capture_output=True,
                text=True,
                timeout=self.max_execution_time,
                cwd=tempfile.gettempdir()
            )
 
            stdout = result.stdout
            if len(stdout) > self.max_output_length:
                stdout = stdout[:self.max_output_length] + "\n... (output truncated)"
 
            return {
                "success": result.returncode == 0,
                "stdout": stdout.strip(),
                "stderr": result.stderr.strip() if result.stderr else "",
                "execution_time": time.time(),
                "return_code": result.returncode
            }
 
        except subprocess.TimeoutExpired:
            return {
                "success": False,
                "stdout": "",
                "stderr": "Command timed out (30s limit)",
                "execution_time": time.time()
            }
        except Exception as e:
            return {
                "success": False,
                "stdout": "",
                "stderr": str(e),
                "execution_time": time.time()
            }
 
    def execute_code(self, code: str, language: str = "python", session_id: Optional[str] = None) -> str:
        """Main execution function - returns JSON for MCP compatibility"""
        # Cleanup old sessions periodically
        if len(self.sessions) > 10:
            self.cleanup_old_sessions()
 
        if language not in self.allowed_languages:
            return json.dumps({
                "success": False,
                "error": f"Language '{language}' not supported. Allowed: {', '.join(self.allowed_languages)}"
            })
 
        # Create session if needed
        if session_id and session_id not in self.sessions:
            session_id = self.create_session()
        elif not session_id:
            session_id = self.create_session()
 
        # Execute based on language
        if language == "python":
            result = self.execute_python_code(code, session_id)
        elif language == "javascript":
            result = self.execute_javascript_code(code, session_id)
        elif language == "bash":
            result = self.execute_bash_command(code, session_id)
        else:
            result = {
                "success": False,
                "error": f"Execution handler for {language} not implemented"
            }
 
        # Store in session history
        if session_id in self.sessions:
            self.sessions[session_id]['history'].append({
                'code': code,
                'language': language,
                'result': result,
                'timestamp': time.time()
            })
 
        result['session_id'] = session_id
        return json.dumps(result, indent=2)
 
# Global executor instance
executor = SecureCodeExecutor()
 
# MCP Functions
def execute_python_code(code: str, session_id: str = None) -> str:
    """
    Execute Python code safely with visualization support.
    
    Args:
        code (str): Python code to execute
        session_id (str, optional): Session ID for persistent context
    
    Returns:
        str: JSON string with execution results
    """
    return executor.execute_code(code, "python", session_id)
 
def execute_javascript_code(code: str, session_id: str = None) -> str:
    """
    Execute JavaScript code using Node.js.
    
    Args:
        code (str): JavaScript code to execute
        session_id (str, optional): Session ID for persistent context
    
    Returns:
        str: JSON string with execution results
    """
    return executor.execute_code(code, "javascript", session_id)
 
def execute_bash_command(command: str, session_id: str = None) -> str:
    """
    Execute bash commands with security restrictions.
    
    Args:
        command (str): Bash command to execute
        session_id (str, optional): Session ID for persistent context
    
    Returns:
        str: JSON string with execution results
    """
    return executor.execute_code(command, "bash", session_id)
 
def create_execution_session() -> str:
    """
    Create a new execution session for maintaining state.
    
    Returns:
        str: JSON string containing new session ID
    """
    session_id = executor.create_session()
    return json.dumps({"session_id": session_id, "created_at": time.time()})
 
def list_execution_sessions() -> str:
    """
    List all active execution sessions.
    
    Returns:
        str: JSON string containing session information
    """
    return json.dumps({
        "sessions": list(executor.sessions.keys()),
        "count": len(executor.sessions),
        "timestamp": time.time()
    })
 
def get_execution_history(session_id: str) -> str:
    """
    Get execution history for a specific session.
    
    Args:
        session_id (str): Session ID to get history for
    
    Returns:
        str: JSON string containing execution history
    """
    if session_id not in executor.sessions:
        return json.dumps({"error": "Session not found"})
    
    return json.dumps({
        "session_id": session_id,
        "history": executor.sessions[session_id]['history'],
        "created_at": executor.sessions[session_id]['created_at']
    })
 
def get_system_info() -> str:
    """
    Get system information and available packages.
    
    Returns:
        str: JSON string containing system information
    """
    return json.dumps({
        "python_version": sys.version,
        "available_packages": [
            "numpy", "pandas", "matplotlib", "json", "math", 
            "random", "datetime", "base64", "io"
        ],
        "execution_limits": {
            "max_time": executor.max_execution_time,
            "max_output": executor.max_output_length
        },
        "supported_languages": executor.allowed_languages
    })
 
# Gradio Interface
def gradio_execute_code(code: str, language: str, session_id: str = ""):
    """Gradio interface for code execution"""
    if not session_id:
        session_id = None
    
    result_json = executor.execute_code(code, language.lower(), session_id)
    result = json.loads(result_json)
    
    output = ""
    if result.get("success"):
        if result.get("stdout"):
            output += f"Output:\n{result['stdout']}\n\n"
        if result.get("stderr"):
            output += f"Warnings:\n{result['stderr']}\n\n"
        if result.get("plot"):
            output += f"Plot generated (base64): {result['plot'][:100]}...\n\n"
    else:
        output += f"Error:\n{result.get('stderr', result.get('error', 'Unknown error'))}\n\n"
    
    output += f"Session ID: {result.get('session_id', 'N/A')}"
    
    return output
 
# Create Gradio interface
with gr.Blocks(title="LibreChat Code Interpreter") as demo:
    gr.Markdown("# LibreChat Code Interpreter")
    gr.Markdown("Execute Python, JavaScript, and Bash code safely through MCP integration.")
    
    with gr.Row():
        with gr.Column():
            code_input = gr.Textbox(
                placeholder="Enter your code here...",
                lines=10,
                label="Code"
            )
            language_dropdown = gr.Dropdown(
                choices=["Python", "JavaScript", "Bash"],
                value="Python",
                label="Language"
            )
            session_input = gr.Textbox(
                placeholder="Optional: Session ID for persistent context",
                label="Session ID"
            )
            execute_btn = gr.Button("Execute Code", variant="primary")
        
        with gr.Column():
            output_display = gr.Textbox(
                lines=15,
                label="Execution Result",
                interactive=False
            )
    
    # Examples
    gr.Markdown("## Examples")
    
    example_python = gr.Code("""
import matplotlib.pyplot as plt
import numpy as np
 
# Generate sample data
x = np.linspace(0, 10, 100)
y = np.sin(x) * np.exp(-x/10)
 
# Create plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='Damped Sine Wave')
plt.title('Example Visualization')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
 
print("Visualization created successfully!")
""", language="python", label="Python Example with Visualization")
    
    example_js = gr.Code("""
// Data processing example
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 
const sum = data.reduce((acc, val) => acc + val, 0);
const mean = sum / data.length;
const variance = data.reduce((acc, val) => acc + Math.pow(val - mean, 2), 0) / data.length;
const stdDev = Math.sqrt(variance);
 
console.log(`Dataset: [${data.join(', ')}]`);
console.log(`Sum: ${sum}`);
console.log(`Mean: ${mean}`);
console.log(`Standard Deviation: ${stdDev.toFixed(3)}`);
 
// JSON processing
const result = {
    dataset: data,
    statistics: {
        sum, mean, variance, stdDev
    },
    timestamp: new Date().toISOString()
};
 
console.log('\\nResult:');
console.log(JSON.stringify(result, null, 2));
""", language="javascript", label="JavaScript Example")
    
    execute_btn.click(
        fn=gradio_execute_code,
        inputs=[code_input, language_dropdown, session_input],
        outputs=[output_display]
    )
 
if __name__ == "__main__":
    # Launch with MCP server enabled
    demo.launch(
        mcp_server=True,
        share=False,
        server_name="0.0.0.0",
        server_port=7860
    )