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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
"""
Renderer MCP Server

This MCP server provides tools for rendering animations using Manim and
processing videos with FFmpeg.
"""

import asyncio
import base64
import json
import logging
import os
import subprocess
import tempfile
from pathlib import Path
from typing import Any, Dict, List, Optional

from blaxel.core.sandbox import SandboxInstance
from mcp.server import NotificationOptions, Server
from mcp.server.models import InitializationOptions
from mcp.server.stdio import stdio_server
from mcp.types import (
    CallToolRequest,
    CallToolResult,
    ListToolsRequest,
    ListToolsResult,
    TextContent,
    Tool,
)
from pydantic import BaseModel

logger = logging.getLogger(__name__)

# Create MCP server
server = Server("neuroanim-renderer")


class AnimationConfig(BaseModel):
    """Configuration for Manim animations."""

    scene_name: str
    code: str
    output_file: Optional[str] = None
    quality: str = "medium"  # low, medium, high, production_quality
    format: str = "mp4"  # mp4, gif, png
    resolution: Optional[str] = None
    frame_rate: int = 30


class RendererTool:
    """Base class for renderer tools."""

    @staticmethod
    def create_temp_dir() -> Path:
        """Create a temporary directory for rendering."""
        return Path(tempfile.mkdtemp(prefix="neuroanim_"))

    @staticmethod
    def cleanup_temp_dir(temp_dir: Path):
        """Clean up temporary directory."""
        import shutil

        shutil.rmtree(temp_dir, ignore_errors=True)

    @staticmethod
    async def execute_sandbox_process(
        sandbox, process_config: dict, logger, operation_name: str
    ):
        """Execute a sandbox process with retry logic for connection timeouts and process conflicts."""
        import uuid

        # Store original name for retries
        original_name = process_config.get("name", "unnamed-process")

        # Check for existing processes with the same name and clean them up
        try:
            existing_processes = await sandbox.process.list()
            for proc in existing_processes:
                if proc.name == original_name:
                    logger.warning(
                        f"Found existing process '{original_name}' (status: {proc.status}), terminating it..."
                    )
                    try:
                        await sandbox.process.kill(original_name)
                        logger.info(
                            f"Successfully terminated process '{original_name}'"
                        )
                        # Wait a moment for cleanup
                        await asyncio.sleep(1)
                    except Exception as kill_error:
                        logger.warning(
                            f"Failed to kill process '{original_name}': {kill_error}"
                        )
                        # Continue anyway, might still work
        except Exception as list_error:
            logger.debug(f"Could not list existing processes: {list_error}")

        # For the first attempt, use the original name
        try:
            result = await sandbox.process.exec(process_config)
            return result
        except Exception as exec_error:
            error_str = str(exec_error).lower()
            # Check if it's a duplicate process error
            if "already exists" in error_str or "already running" in error_str:
                logger.warning(
                    f"Process {original_name} already exists, creating unique variant..."
                )
                # Create a unique name and retry
                unique_name = f"{original_name}-{uuid.uuid4().hex[:8]}"
                process_config["name"] = unique_name
                try:
                    result = await sandbox.process.exec(process_config)
                    return result
                except Exception as unique_error:
                    logger.error(
                        f"Unique process {unique_name} also failed: {unique_error}"
                    )
                    raise exec_error  # Raise original error
            elif "timeout" in error_str or "connecttimeout" in error_str:
                logger.warning(
                    f"{operation_name} connection timed out, retrying after delay..."
                )
                await asyncio.sleep(3)  # Wait before retry
                # For timeout, also try with a unique name to avoid conflicts
                unique_name = f"{original_name}-{uuid.uuid4().hex[:8]}-retry"
                process_config["name"] = unique_name
                try:
                    result = await sandbox.process.exec(process_config)
                    logger.info(f"Retry successful for {operation_name}")
                    return result
                except Exception as retry_error:
                    logger.error(f"Retry failed for {operation_name}: {retry_error}")
                    raise exec_error  # Raise original error
            else:
                logger.error(
                    f"{operation_name} failed with non-timeout error: {exec_error}"
                )
                raise exec_error

    @staticmethod
    async def read_sandbox_file(sandbox, file_path: str, logger):
        """Read a file from sandbox with retry logic for connection timeouts."""
        try:
            content = await sandbox.fs.read(file_path)
            return content
        except Exception as read_error:
            error_str = str(read_error).lower()
            if "timeout" in error_str or "connecttimeout" in error_str:
                logger.warning(
                    f"File read from sandbox timed out, retrying after delay..."
                )
                await asyncio.sleep(3)  # Wait before retry
                try:
                    content = await sandbox.fs.read(file_path)
                    logger.info(f"Retry successful for file read: {file_path}")
                    return content
                except Exception as retry_error:
                    logger.error(f"Retry failed for file read: {retry_error}")
                    raise read_error  # Raise original error
            else:
                logger.error(f"File read failed with non-timeout error: {read_error}")
                raise read_error

    @staticmethod
    async def write_sandbox_file(sandbox, file_path: str, content: str, logger):
        """Write a file to sandbox with retry logic for connection timeouts."""
        try:
            await sandbox.fs.write(file_path, content)
            return
        except Exception as write_error:
            error_str = str(write_error).lower()
            if "timeout" in error_str or "connecttimeout" in error_str:
                logger.warning(
                    f"File write to sandbox timed out, retrying after delay..."
                )
                await asyncio.sleep(3)  # Wait before retry
                try:
                    await sandbox.fs.write(file_path, content)
                    logger.info(f"Retry successful for file write: {file_path}")
                    return
                except Exception as retry_error:
                    logger.error(f"Retry failed for file write: {retry_error}")
                    raise write_error  # Raise original error
            else:
                logger.error(f"File write failed with non-timeout error: {write_error}")
                raise write_error


@server.list_tools()
async def list_tools() -> ListToolsResult:
    """List available renderer tools."""
    tools = [
        Tool(
            name="write_manim_file",
            description="Write a Manim Python file to the filesystem",
            inputSchema={
                "type": "object",
                "properties": {
                    "filepath": {
                        "type": "string",
                        "description": "Path where to write the Manim file",
                    },
                    "code": {
                        "type": "string",
                        "description": "Manim Python code to write",
                    },
                },
                "required": ["filepath", "code"],
            },
        ),
        Tool(
            name="render_manim_animation",
            description="Render a Manim animation using subprocess",
            inputSchema={
                "type": "object",
                "properties": {
                    "scene_name": {
                        "type": "string",
                        "description": "Name of the Manim scene to render",
                    },
                    "file_path": {
                        "type": "string",
                        "description": "Path to the Manim Python file",
                    },
                    "output_dir": {
                        "type": "string",
                        "description": "Directory to save the output animation",
                    },
                    "quality": {
                        "type": "string",
                        "enum": ["low", "medium", "high", "production_quality"],
                        "description": "Rendering quality (default: medium)",
                    },
                    "format": {
                        "type": "string",
                        "enum": ["mp4", "gif", "png"],
                        "description": "Output format (default: mp4)",
                    },
                    "frame_rate": {
                        "type": "integer",
                        "description": "Frame rate (default: 30)",
                    },
                },
                "required": ["scene_name", "file_path", "output_dir"],
            },
        ),
        Tool(
            name="process_video_with_ffmpeg",
            description="Process video using FFmpeg for merging, conversion, etc.",
            inputSchema={
                "type": "object",
                "properties": {
                    "input_files": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "List of input video/audio files",
                    },
                    "output_file": {
                        "type": "string",
                        "description": "Output file path",
                    },
                    "ffmpeg_args": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Additional FFmpeg arguments",
                    },
                },
                "required": ["input_files", "output_file"],
            },
        ),
        Tool(
            name="merge_video_audio",
            description="Merge video and audio files using FFmpeg",
            inputSchema={
                "type": "object",
                "properties": {
                    "video_file": {
                        "type": "string",
                        "description": "Path to the video file",
                    },
                    "audio_file": {
                        "type": "string",
                        "description": "Path to the audio file",
                    },
                    "output_file": {
                        "type": "string",
                        "description": "Path to the output merged file",
                    },
                },
                "required": ["video_file", "audio_file", "output_file"],
            },
        ),
        Tool(
            name="check_file_exists",
            description="Check if a file exists and return its metadata",
            inputSchema={
                "type": "object",
                "properties": {
                    "filepath": {
                        "type": "string",
                        "description": "Path to the file to check",
                    }
                },
                "required": ["filepath"],
            },
        ),
    ]

    return ListToolsResult(tools=tools)


@server.call_tool()
async def call_tool(tool_name: str, arguments: Dict[str, Any]) -> CallToolResult:
    """Dispatch renderer tool calls.

    As with the creative server, the low-level MCP server passes
    `(tool_name, arguments)` into this handler.
    """

    try:
        if tool_name == "write_manim_file":
            return await write_manim_file(arguments)
        elif tool_name == "render_manim_animation":
            return await render_manim_animation(arguments)
        elif tool_name == "process_video_with_ffmpeg":
            return await process_video_with_ffmpeg(arguments)
        elif tool_name == "merge_video_audio":
            return await merge_video_audio(arguments)
        elif tool_name == "check_file_exists":
            return await check_file_exists(arguments)
        else:
            return CallToolResult(
                content=[TextContent(type="text", text=f"Unknown tool: {tool_name}")],
                isError=True,
            )
    except Exception as e:
        logger.error(f"Error in tool {tool_name}: {e}")
        return CallToolResult(
            content=[TextContent(type="text", text=f"Error: {str(e)}")],
            isError=True,
        )


async def write_manim_file(arguments: Dict[str, Any]) -> CallToolResult:
    """Write a Manim Python file."""
    filepath = arguments["filepath"]
    code = arguments["code"]

    try:
        # Ensure directory exists
        Path(filepath).parent.mkdir(parents=True, exist_ok=True)

        # Write the file
        with open(filepath, "w") as f:
            f.write(code)

        logger.info(f"Manim file written to: {filepath}")

        return CallToolResult(
            content=[
                TextContent(
                    type="text", text=f"Successfully wrote Manim file to {filepath}"
                )
            ]
        )
    except Exception as e:
        return CallToolResult(
            content=[TextContent(type="text", text=f"Failed to write file: {str(e)}")],
            isError=True,
        )


async def render_manim_animation(arguments: Dict[str, Any]) -> CallToolResult:
    """Render a Manim animation using Blaxel sandbox execution with local fallback."""
    scene_name = arguments["scene_name"]
    file_path = arguments["file_path"]
    output_dir = arguments["output_dir"]
    quality = arguments.get("quality", "medium")
    format_type = arguments.get("format", "mp4")
    frame_rate = arguments.get("frame_rate", 30)

    # Skip sandbox rendering and use local rendering directly with .venv
    logger.info("Using local Manim rendering with .venv environment...")

    local_result = await _render_manim_locally(
        scene_name, file_path, output_dir, quality, format_type, frame_rate
    )

    return CallToolResult(
        content=[TextContent(type="text", text=local_result["text"])],
        isError=local_result.get("isError", False),
    )


async def _render_manim_with_sandbox(
    scene_name: str,
    file_path: str,
    output_dir: str,
    quality: str,
    format_type: str,
    frame_rate: int,
) -> Dict[str, Any]:
    """Render a Manim animation using Blaxel sandbox execution."""
    # Map quality to manim flags
    quality_flags = {
        "low": "-ql",
        "medium": "-qm",
        "high": "-qh",
        "production_quality": "-qp",
    }
    quality_flag = quality_flags.get(quality, "-qm")

    try:
        # Ensure output directory exists
        Path(output_dir).mkdir(parents=True, exist_ok=True)

        # Read the Manim code file
        with open(file_path, "r") as f:
            manim_code = f.read()

        logger.info(f"Creating Blaxel sandbox for scene: {scene_name}")

        # Sanitize scene name for valid sandbox name
        sanitized_scene_name = scene_name.lower().replace(" ", "-").replace("_", "-")
        # Ensure name is not too long and only contains valid characters
        import re

        sanitized_scene_name = re.sub(r"[^a-z0-9\-]", "", sanitized_scene_name)[:20]
        if not sanitized_scene_name:
            sanitized_scene_name = "default"

        try:
            # Create or get sandbox using Blaxel SDK
            # Uses BL_WORKSPACE and BL_API_KEY from environment or .env file
            logger.info(f"Creating Blaxel sandbox: manim-render-{sanitized_scene_name}")
            try:
                # Create sandbox with proper virtual environment
                sandbox = await SandboxInstance.create(
                    {
                        "name": f"manim-render-{sanitized_scene_name}",
                        "image": "blaxel/py-app:latest",
                        "memory": 4096,
                        # Use virtual environment instead of system
                        "virtual": True,
                    }
                )
                logger.info(f"Successfully created sandbox: {sandbox.metadata.name}")

                # Wait a moment for sandbox to fully initialize
                logger.info("Waiting for sandbox to initialize...")
                await asyncio.sleep(2)

            except Exception as create_error:
                # Handle connection timeouts by retrying
                error_str = str(create_error).lower()
                if "timeout" in error_str or "connecttimeout" in error_str:
                    logger.warning(
                        "Sandbox creation connection timed out, retrying after delay..."
                    )
                    await asyncio.sleep(5)  # Wait longer before retry
                    try:
                        # Retry once
                        sandbox = await SandboxInstance.create(
                            {
                                "name": f"manim-render-{sanitized_scene_name}",
                                "image": "blaxel/py-app:latest",
                                "memory": 4096,
                            }
                        )
                        logger.info(
                            f"Retry successful: Created sandbox: {sandbox.metadata.name}"
                        )

                        # Wait for sandbox to initialize
                        logger.info("Waiting for sandbox to initialize after retry...")
                        await asyncio.sleep(3)

                    except Exception as retry_error:
                        logger.error(f"Retry failed: {retry_error}")
                        raise create_error  # Raise original error
                else:
                    logger.error(
                        f"Sandbox creation failed with non-timeout error: {create_error}"
                    )
                    raise create_error
        except Exception as sandbox_error:
            error_msg = f"Failed to create Blaxel sandbox: {str(sandbox_error)}"
            logger.error(error_msg)
            return {"text": error_msg, "isError": True}

        try:
            # Write the Manim code to the sandbox
            sandbox_file_path = f"/tmp/{scene_name}.py"
            logger.info(f"Writing Manim code to sandbox: {sandbox_file_path}")
            await RendererTool.write_sandbox_file(
                sandbox, sandbox_file_path, manim_code, logger
            )
            logger.info(
                f"Successfully wrote Manim code to sandbox: {sandbox_file_path}"
            )

            # Initialize flag for Manim installation check
            manim_already_installed = False

            # Test what's available in the sandbox
            logger.info("Testing sandbox environment...")
            try:
                test_result = await RendererTool.execute_sandbox_process(
                    sandbox,
                    {
                        "name": "test-environment",
                        "command": "which python3 && python3 --version && which pip && pip --version",
                        "wait_for_completion": True,
                    },
                    logger,
                    "Environment test",
                )
                logger.info(f"Environment test result: {test_result}")

                # Get test logs
                try:
                    test_logs = await sandbox.process.logs("test-environment", "all")
                    logger.info(f"Environment test logs: {test_logs}")
                except Exception as log_error:
                    logger.warning(f"Could not retrieve test logs: {log_error}")
            except Exception as test_error:
                logger.warning(f"Environment test failed: {test_error}")

            # Test if apt-get is available
            logger.info("Testing if apt-get is available...")
            try:
                apt_test_result = await RendererTool.execute_sandbox_process(
                    sandbox,
                    {
                        "name": "test-apt",
                        "command": "which apt-get || echo 'apt-get not found'",
                        "wait_for_completion": True,
                    },
                    logger,
                    "Apt availability test",
                )
                logger.info(f"Apt test result: {apt_test_result}")

                # Get apt test logs
                try:
                    apt_test_logs = await sandbox.process.logs("test-apt", "all")
                    logger.info(f"Apt test logs: {apt_test_logs}")
                except Exception as log_error:
                    logger.warning(f"Could not retrieve apt test logs: {log_error}")
            except Exception as apt_test_error:
                logger.warning(f"Apt test failed: {apt_test_error}")

            # Try a simple pip install first to see if it works
            logger.info("Testing pip install...")
            try:
                pip_test_result = await RendererTool.execute_sandbox_process(
                    sandbox,
                    {
                        "name": "test-pip",
                        "command": "pip install --dry-run manim",
                        "wait_for_completion": True,
                    },
                    logger,
                    "Pip test",
                )
                logger.info(f"Pip test result: {pip_test_result}")

                # Get pip test logs
                try:
                    pip_test_logs = await sandbox.process.logs("test-pip", "all")
                    logger.info(f"Pip test logs: {pip_test_logs}")
                except Exception as log_error:
                    logger.warning(f"Could not retrieve pip test logs: {log_error}")
            except Exception as pip_test_error:
                logger.warning(f"Pip test failed: {pip_test_error}")

            # Check if Manim is already installed
            logger.info("Checking if Manim is already installed...")
            try:
                manim_check_result = await RendererTool.execute_sandbox_process(
                    sandbox,
                    {
                        "name": "check-manim",
                        "command": "python3 -c \"import manim; print('Manim version:', manim.__version__)\" || echo 'Manim not found'",
                        "wait_for_completion": True,
                    },
                    logger,
                    "Manim check",
                )
                logger.info(f"Manim check result: {manim_check_result}")

                # Get manim check logs
                try:
                    manim_check_logs = await sandbox.process.logs("check-manim", "all")
                    logger.info(f"Manim check logs: {manim_check_logs}")

                    # Check if Manim is installed
                    if "Manim version:" in str(manim_check_logs):
                        logger.info("Manim is already installed, skipping installation")
                        manim_already_installed = True
                    else:
                        logger.info(
                            "Manim is not installed, proceeding with installation"
                        )
                        manim_already_installed = False
                except Exception as log_error:
                    logger.warning(f"Could not retrieve manim check logs: {log_error}")
                    manim_already_installed = False
            except Exception as manim_check_error:
                logger.warning(f"Manim check failed: {manim_check_error}")
                manim_already_installed = False

            # Install manim and its dependencies in the sandbox
            logger.info("Installing manim and dependencies in the sandbox...")

            # Check if ffmpeg is already available
            logger.info("Checking if ffmpeg is available...")
            try:
                ffmpeg_check_result = await RendererTool.execute_sandbox_process(
                    sandbox,
                    {
                        "name": "check-ffmpeg",
                        "command": "which ffmpeg || echo 'ffmpeg not found'",
                        "wait_for_completion": True,
                    },
                    logger,
                    "FFmpeg availability check",
                )
                logger.info(f"Ffmpeg check result: {ffmpeg_check_result}")

                # Get ffmpeg check logs
                try:
                    ffmpeg_check_logs = await sandbox.process.logs(
                        "check-ffmpeg", "all"
                    )
                    logger.info(f"Ffmpeg check logs: {ffmpeg_check_logs}")
                except Exception as log_error:
                    logger.warning(f"Could not retrieve ffmpeg check logs: {log_error}")
            except Exception as ffmpeg_check_error:
                logger.warning(f"Ffmpeg check failed: {ffmpeg_check_error}")

            # Skip installation if Manim is already installed
            if manim_already_installed:
                logger.info(
                    "Skipping dependencies installation as Manim is already installed"
                )
                manim_installed = True
            else:
                # Try to install system dependencies step-by-step for better reliability
                logger.info("Installing system dependencies step-by-step...")

                # First update package lists
                try:
                    logger.info("Updating package lists...")
                    update_result = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "apt-update",
                            "command": "apt-get update",
                            "wait_for_completion": True,
                            "timeout": 120,
                        },
                        logger,
                        "Package list update",
                    )
                    logger.info(f"Package update result: {update_result}")

                    if update_result.status != "exited" or (
                        hasattr(update_result, "exit_code")
                        and update_result.exit_code != 0
                    ):
                        logger.warning("Package update failed, but continuing...")
                except Exception as update_error:
                    logger.warning(
                        f"Package update failed: {update_error}, continuing..."
                    )

                # Install ffmpeg
                try:
                    logger.info("Installing ffmpeg...")
                    ffmpeg_result = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "install-ffmpeg",
                            "command": "apt-get install -y ffmpeg",
                            "wait_for_completion": True,
                            "timeout": 180,
                        },
                        logger,
                        "FFmpeg installation",
                    )
                    logger.info(f"FFmpeg installation result: {ffmpeg_result}")

                    if ffmpeg_result.status != "exited" or (
                        hasattr(ffmpeg_result, "exit_code")
                        and ffmpeg_result.exit_code != 0
                    ):
                        logger.warning("FFmpeg installation failed, but continuing...")
                except Exception as ffmpeg_error:
                    logger.warning(
                        f"FFmpeg installation failed: {ffmpeg_error}, continuing..."
                    )

                # Install libcairo2-dev
                try:
                    logger.info("Installing libcairo2-dev...")
                    cairo_result = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "install-cairo",
                            "command": "apt-get install -y libcairo2-dev",
                            "wait_for_completion": True,
                            "timeout": 180,
                        },
                        logger,
                        "Cairo installation",
                    )
                    logger.info(f"Cairo installation result: {cairo_result}")

                    if cairo_result.status != "exited" or (
                        hasattr(cairo_result, "exit_code")
                        and cairo_result.exit_code != 0
                    ):
                        logger.warning("Cairo installation failed, but continuing...")
                except Exception as cairo_error:
                    logger.warning(
                        f"Cairo installation failed: {cairo_error}, continuing..."
                    )

                # Install Python dependencies - try lighter alternatives first
                logger.info("Installing Python dependencies...")
                manim_installed = False

                # Try installing manim Community Edition (lighter than full manim)
                install_commands = [
                    ("pip install manimlib", "manimlib installation"),
                    ("pip install manim", "full manim installation"),
                    (
                        "pip install --no-deps manim && pip install numpy scipy matplotlib",
                        "minimal manim with deps",
                    ),
                ]

                for install_cmd, description in install_commands:
                    if manim_installed:
                        break

                    try:
                        logger.info(f"Trying {description}: {install_cmd}")
                        install_result = await RendererTool.execute_sandbox_process(
                            sandbox,
                            {
                                "name": "install-manim-attempt",
                                "command": install_cmd,
                                "wait_for_completion": True,
                                "timeout": 600,  # 10 minute timeout
                            },
                            logger,
                            description,
                        )
                        logger.info(f"{description} result: {install_result}")

                        if install_result.status == "exited" and (
                            not hasattr(install_result, "exit_code")
                            or install_result.exit_code == 0
                        ):
                            logger.info(f"Successfully installed with: {install_cmd}")
                            manim_installed = True

                            # Verify installation
                            try:
                                verify_result = await RendererTool.execute_sandbox_process(
                                    sandbox,
                                    {
                                        "name": "verify-manim",
                                        "command": "python3 -c \"import manim; print('Manim version:', getattr(manim, '__version__', 'unknown'))\"",
                                        "wait_for_completion": True,
                                        "timeout": 30,
                                    },
                                    logger,
                                    "Manim verification",
                                )
                                logger.info(
                                    f"Manim verification result: {verify_result}"
                                )
                            except Exception as verify_error:
                                logger.warning(
                                    f"Manim verification failed: {verify_error}"
                                )
                        else:
                            logger.warning(
                                f"{description} failed, trying next option..."
                            )

                        # Get installation logs for debugging (for the last attempt)
                        try:
                            install_logs = await sandbox.process.logs(
                                "install-manim-attempt", "all"
                            )
                            logger.info(f"Manim installation logs: {install_logs}")
                        except Exception as log_error:
                            logger.warning(
                                f"Could not retrieve installation logs: {log_error}"
                            )

                        # Check if the last installation attempt was successful
                        if install_result.status != "exited" or (
                            hasattr(install_result, "exit_code")
                            and install_result.exit_code != 0
                        ):
                            error_msg = f"Manim installation failed with status: {install_result.status}"
                            if hasattr(install_result, "exit_code"):
                                error_msg += f", exit code: {install_result.exit_code}"

                            # Try to get more detailed logs
                            try:
                                install_logs = await sandbox.process.logs(
                                    "install-manim-attempt", "all"
                                )
                                error_msg += f"\nLogs: {install_logs}"
                            except Exception as log_error:
                                error_msg += f"\nCould not retrieve logs: {log_error}"

                            logger.error(error_msg)
                            # Don't return error here, continue to check if any installation worked

                    except Exception as install_error:
                        # Handle timeout specifically
                        error_str = str(install_error).lower()
                        if "timeout" in error_str or "readtimeout" in error_str:
                            logger.warning(
                                "Pip install manim timed out - this might be OK if packages were already installed or partially installed"
                            )
                            # Try to check if manim was actually installed despite timeout
                            try:
                                manim_check_after = await RendererTool.execute_sandbox_process(
                                    sandbox,
                                    {
                                        "name": "check-manim-after-install",
                                        "command": "python3 -c \"import manim; print('Manim available after install timeout')\" || echo 'Manim not available after install timeout'",
                                        "wait_for_completion": True,
                                    },
                                    logger,
                                    "Post-install Manim check",
                                )
                                logger.info(
                                    f"Post-install Manim check result: {manim_check_after}"
                                )

                                # Get logs
                                try:
                                    check_logs = await sandbox.process.logs(
                                        "check-manim-after-install", "all"
                                    )
                                    logger.info(
                                        f"Post-install check logs: {check_logs}"
                                    )

                                    if "manim available" in str(check_logs).lower():
                                        logger.info(
                                            "Manim appears to be installed despite timeout, continuing..."
                                        )
                                        manim_installed = True
                                    else:
                                        logger.warning(
                                            "Manim not available after install timeout, may cause render failure"
                                        )
                                except Exception as log_error:
                                    logger.warning(
                                        f"Could not check post-install logs: {log_error}"
                                    )
                            except Exception as check_error:
                                logger.warning(
                                    f"Could not verify Manim installation after timeout: {check_error}"
                                )
                        else:
                            import traceback

                            error_details = traceback.format_exc()
                            error_msg = f"Error during pip install manim: {str(install_error)}\nDetails: {error_details}"

                            # Try to get installation logs for debugging
                            try:
                                install_logs = await sandbox.process.logs(
                                    "install-manim-attempt", "all"
                                )
                                error_msg += f"\nInstallation logs: {install_logs}"
                            except Exception as log_error:
                                error_msg += f"\nCould not retrieve installation logs: {log_error}"

                            logger.error(error_msg)
                            # Don't return error here, continue to try other installation methods

                        logger.warning(f"{description} failed: {install_error}")
                        continue

            # Final check: ensure Manim is actually installed before proceeding to render
            if not manim_already_installed and not manim_installed:
                logger.warning(
                    "Manim installation appears to have failed, attempting final verification..."
                )

                # Final verification attempt
                try:
                    final_check = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "final-manim-check",
                            "command": "python3 -c \"import manim; print('SUCCESS: Manim is available')\" || echo 'FAILED: Manim not available'",
                            "wait_for_completion": True,
                            "timeout": 30,
                        },
                        logger,
                        "Final Manim availability check",
                    )

                    # Get logs to check result
                    try:
                        check_logs = await sandbox.process.logs(
                            "final-manim-check", "all"
                        )
                        if "SUCCESS" in str(check_logs):
                            logger.info(
                                "Final check confirms Manim is available, proceeding with render"
                            )
                            manim_installed = True
                        else:
                            error_msg = f"Final verification shows Manim is not available. Installation appears to have failed.\nCheck logs: {check_logs}"
                            logger.error(error_msg)
                            return {"text": error_msg, "isError": True}
                    except Exception as log_error:
                        logger.warning(
                            f"Could not retrieve final check logs: {log_error}"
                        )

                except Exception as final_check_error:
                    error_msg = f"Cannot verify Manim installation: {final_check_error}"
                    logger.error(error_msg)
                    return {"text": error_msg, "isError": True}

            # Run the Manim render command - try different possible commands
            render_commands = [
                f"manim {quality_flag} --fps {frame_rate} -o {scene_name}.{format_type} {sandbox_file_path} {scene_name}",
                f"python3 -m manim {quality_flag} --fps {frame_rate} -o {scene_name}.{format_type} {sandbox_file_path} {scene_name}",
                f"manimce {quality_flag} --fps {frame_rate} -o {scene_name}.{format_type} {sandbox_file_path} {scene_name}",
            ]

            render_success = False
            render_result = None

            for cmd in render_commands:
                if render_success:
                    break

                logger.info(f"Trying render command: {cmd}")
                try:
                    render_result = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "render-manim",
                            "command": cmd,
                            "wait_for_completion": True,
                            "timeout": 600,  # 10 minute timeout for rendering
                        },
                        logger,
                        f"Manim rendering with '{cmd}'",
                    )
                    logger.info(f"Render result: {render_result}")

                    if render_result.status == "exited" and (
                        not hasattr(render_result, "exit_code")
                        or render_result.exit_code == 0
                    ):
                        logger.info(f"Successfully rendered with: {cmd}")
                        render_success = True
                    else:
                        logger.warning(
                            f"Render failed with command '{cmd}', trying next option..."
                        )

                except Exception as render_error:
                    logger.warning(f"Render failed with '{cmd}': {render_error}")
                    continue
                # Check if rendering was successful
                if not render_success:
                    error_msg = "All render command attempts failed."

                    # Try to get logs for debugging from the last attempt
                    try:
                        logs = await sandbox.process.logs("render-manim", "all")
                        error_msg += f"\nLast render logs: {logs}"
                    except Exception as log_error:
                        error_msg += f"\nCould not retrieve logs: {log_error}"

                    logger.error(error_msg)
                    return {"text": error_msg, "isError": True}
            # If we get here and render wasn't successful, it's an error
            if not render_success:
                error_msg = "Manim rendering failed - no working render command found"
                logger.error(error_msg)
                return {"text": error_msg, "isError": True}

        except Exception as render_error:
            # Handle timeout specifically
            error_str = str(render_error).lower()
            if "timeout" in error_str or "readtimeout" in error_str:
                logger.warning(
                    "Manim render timed out - this indicates a long-running render process"
                )
                # Try to continue and check if output was generated
            else:
                error_msg = f"Error during manim rendering: {str(render_error)}"
                logger.error(error_msg)
                return {"text": error_msg, "isError": True}

            # Find the output file in the sandbox
            # Manim typically outputs to media/videos/{scene_name}/{quality}/
            possible_paths = [
                f"/tmp/media/videos/{scene_name}/{quality}/{scene_name}.{format_type}",
                f"/tmp/media/videos/{scene_name.lower()}/{quality}/{scene_name}.{format_type}",
                f"/tmp/{scene_name}.{format_type}",
                f"/root/media/videos/{scene_name}/{quality}/{scene_name}.{format_type}",
            ]

            output_content = None
            found_path = None

            for sandbox_path in possible_paths:
                try:
                    output_content = await RendererTool.read_sandbox_file(
                        sandbox, sandbox_path, logger
                    )
                    found_path = sandbox_path
                    logger.info(f"Found output at: {sandbox_path}")
                    break
                except Exception:
                    continue

            if not output_content:
                # List files to debug
                try:
                    ls_result = await RendererTool.execute_sandbox_process(
                        sandbox,
                        {
                            "name": "find-output",
                            "command": "find /tmp -name '*.mp4' -o -name '*.gif' 2>/dev/null || true",
                            "wait_for_completion": True,
                        },
                        logger,
                        "Find output files",
                    )
                    find_logs = await sandbox.process.logs("find-output", "stdout")
                    logger.info(f"Found video files: {find_logs}")
                except Exception:
                    pass

                error_msg = f"Could not find rendered output file. Searched paths: {possible_paths}"
                logger.error(error_msg)
                return {"text": error_msg, "isError": True}

            # Write the output to local filesystem
            output_path = Path(output_dir) / f"{scene_name}.{format_type}"

            # Handle the content - it may be base64 encoded or bytes
            if isinstance(output_content, str):
                try:
                    decoded_content = base64.b64decode(output_content)
                    with open(output_path, "wb") as f:
                        f.write(decoded_content)
                except Exception:
                    with open(output_path, "w") as f:
                        f.write(output_content)
            elif isinstance(output_content, (bytes, bytearray)):
                with open(output_path, "wb") as f:
                    f.write(output_content)
            else:
                with open(output_path, "wb") as f:
                    f.write(output_content)

            result_msg = (
                f"Successfully rendered animation using Blaxel sandbox!\n"
                f"Scene: {scene_name}\n"
                f"Output file: {output_path}\n"
                f"Quality: {quality}\n"
                f"Format: {format_type}\n"
                f"File size: {output_path.stat().st_size if output_path.exists() else 'Unknown'} bytes"
            )

            logger.info(result_msg)
            return {"text": result_msg, "isError": False}

        finally:
            # Clean up sandbox
            try:
                await SandboxInstance.delete(sandbox.metadata.name)
                logger.info(f"Deleted sandbox: {sandbox.metadata.name}")
            except Exception as cleanup_error:
                logger.warning(f"Failed to delete sandbox: {cleanup_error}")

    except asyncio.TimeoutError:
        error_msg = "Blaxel sandbox execution timed out"
        logger.error(error_msg)
        return {"text": error_msg, "isError": True}
    except Exception as e:
        # Get detailed exception information
        import traceback

        error_details = traceback.format_exc()
        error_msg = (
            f"Error during Blaxel sandbox rendering: {str(e)}\nDetails: {error_details}"
        )
        logger.error(error_msg)
        return {"text": error_msg, "isError": True}


async def _render_manim_locally(
    scene_name: str,
    file_path: str,
    output_dir: str,
    quality: str,
    format_type: str,
    frame_rate: int,
) -> Dict[str, Any]:
    """Render a Manim animation using local Manim installation."""
    try:
        # Ensure output directory exists
        Path(output_dir).mkdir(parents=True, exist_ok=True)

        # Map quality to manim flags
        quality_flags = {
            "low": "-ql",
            "medium": "-qm",
            "high": "-qh",
            "production_quality": "-qp",
        }
        quality_flag = quality_flags.get(quality, "-qm")

        # Find the project root and .venv
        # Assume the project root contains .venv directory
        project_root = Path(__file__).resolve().parent.parent
        venv_python = project_root / ".venv" / "bin" / "python"
        venv_manim = project_root / ".venv" / "bin" / "manim"

        # Use venv manim if it exists, otherwise fall back to system manim
        if venv_manim.exists():
            manim_cmd = str(venv_manim)
            logger.info(f"Using .venv manim at: {manim_cmd}")
        else:
            manim_cmd = "manim"
            logger.warning(f".venv manim not found at {venv_manim}, using system manim")

        # Build the manim command
        cmd = [
            manim_cmd,
            quality_flag,
            "--fps",
            str(frame_rate),
            "-o",
            f"{scene_name}.{format_type}",
            file_path,
            scene_name,
        ]

        logger.info(f"Running local Manim command: {' '.join(cmd)}")

        # Execute the command with .venv in PATH
        env = os.environ.copy()
        if venv_manim.exists():
            venv_bin = project_root / ".venv" / "bin"
            env["PATH"] = f"{venv_bin}:{env.get('PATH', '')}"
            env["VIRTUAL_ENV"] = str(project_root / ".venv")

        # Execute the command
        process = await asyncio.create_subprocess_exec(
            *cmd,
            stdout=asyncio.subprocess.PIPE,
            stderr=asyncio.subprocess.PIPE,
            cwd=output_dir,  # Run in output directory
            env=env,
        )

        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            error_msg = f"Local Manim rendering failed:\nSTDOUT: {stdout.decode()}\nSTDERR: {stderr.decode()}"
            logger.error(error_msg)
            return {"text": error_msg, "isError": True}

        # Log the stdout for debugging
        logger.info(f"Manim stdout: {stdout.decode()}")
        logger.info(f"Manim stderr: {stderr.decode()}")

        # Find the output file
        # Manim typically outputs to media/videos/{filename}/{quality}/
        import glob

        # First, let's see what files actually exist in the output directory
        logger.info(f"Listing all files in output directory: {output_dir}")
        try:
            all_files = list(Path(output_dir).rglob("*"))
            logger.info(f"Found {len(all_files)} files/dirs:")
            for f in all_files[:50]:  # Log first 50 to avoid spam
                logger.info(f"  - {f}")
        except Exception as list_error:
            logger.warning(f"Could not list files: {list_error}")

        # Manim outputs to paths like: media/videos/{filename}/{resolution}/SceneName.mp4
        # where resolution is like: 720p30, 480p15, 1080p60, 2160p60
        # Quality flags map to resolutions:
        # -ql (low): 480p15
        # -qm (medium): 720p30
        # -qh (high): 1080p60
        # -qp (production): 2160p60

        # Map quality to likely resolution folder names
        quality_to_resolution = {
            "low": ["480p15", "854x480", "480p"],
            "medium": ["720p30", "1280x720", "720p"],
            "high": ["1080p60", "1920x1080", "1080p"],
            "production_quality": ["2160p60", "3840x2160", "2160p"],
        }

        resolutions = quality_to_resolution.get(quality, ["720p30"])

        output_patterns = []

        # Search with specific resolutions
        for res in resolutions:
            output_patterns.extend(
                [
                    f"{output_dir}/media/videos/*/{res}/{scene_name}.{format_type}",
                    f"{output_dir}/media/videos/**/{res}/{scene_name}.{format_type}",
                ]
            )

        # Fallback: search all resolution patterns
        output_patterns.extend(
            [
                f"{output_dir}/media/videos/*/*/{scene_name}.{format_type}",
                f"{output_dir}/media/videos/**/{scene_name}.{format_type}",
                f"{output_dir}/videos/*/*/{scene_name}.{format_type}",
                f"{output_dir}/**/{scene_name}.{format_type}",
                f"{output_dir}/{scene_name}.{format_type}",
            ]
        )

        output_files = []
        for pattern in output_patterns:
            logger.info(f"Trying pattern: {pattern}")
            matches = glob.glob(pattern, recursive=True)
            if matches:
                logger.info(f"  Found matches: {matches}")
                output_files.extend(matches)
                break

        if not output_files:
            error_msg = f"Could not find rendered output file.\nSearched patterns: {output_patterns}\nStdout: {stdout.decode()}\nStderr: {stderr.decode()}"
            logger.error(error_msg)
            return {"text": error_msg, "isError": True}

        output_file = output_files[0]  # Take the first match
        final_output = Path(output_dir) / f"{scene_name}.{format_type}"

        # Move the output file to the expected location
        import shutil

        shutil.move(output_file, final_output)

        result_msg = (
            f"Successfully rendered animation locally!\n"
            f"Scene: {scene_name}\n"
            f"Output file: {final_output}\n"
            f"Quality: {quality}\n"
            f"Format: {format_type}\n"
            f"File size: {final_output.stat().st_size if final_output.exists() else 'Unknown'} bytes"
        )

        logger.info(result_msg)
        return {"text": result_msg, "isError": False}

    except Exception as e:
        import traceback

        error_details = traceback.format_exc()
        error_msg = (
            f"Error during local Manim rendering: {str(e)}\nDetails: {error_details}"
        )
        logger.error(error_msg)
        return {"text": error_msg, "isError": True}


async def process_video_with_ffmpeg(arguments: Dict[str, Any]) -> CallToolResult:
    """Process video using FFmpeg."""
    input_files = arguments["input_files"]
    output_file = arguments["output_file"]
    ffmpeg_args = arguments.get("ffmpeg_args", [])

    try:
        # Ensure output directory exists
        Path(output_file).parent.mkdir(parents=True, exist_ok=True)

        # Build FFmpeg command
        cmd = ["ffmpeg"]

        # Add input files
        for input_file in input_files:
            cmd.extend(["-i", input_file])

        # Add additional arguments
        cmd.extend(ffmpeg_args)

        # Add output file
        cmd.append(output_file)

        logger.info(f"Running FFmpeg command: {' '.join(cmd)}")

        process = await asyncio.create_subprocess_exec(
            *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )

        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            error_msg = f"FFmpeg processing failed: {stderr.decode()}"
            logger.error(error_msg)
            return CallToolResult(
                content=[TextContent(type="text", text=error_msg)], isError=True
            )

        result_msg = f"Successfully processed video with FFmpeg: {output_file}"
        logger.info(result_msg)

        return CallToolResult(content=[TextContent(type="text", text=result_msg)])

    except Exception as e:
        error_msg = f"Error during FFmpeg processing: {str(e)}"
        logger.error(error_msg)
        return CallToolResult(
            content=[TextContent(type="text", text=error_msg)], isError=True
        )


async def merge_video_audio(arguments: Dict[str, Any]) -> CallToolResult:
    """Merge video and audio files."""
    video_file = arguments["video_file"]
    audio_file = arguments["audio_file"]
    output_file = arguments["output_file"]

    try:
        # Ensure output directory exists
        Path(output_file).parent.mkdir(parents=True, exist_ok=True)

        # Build FFmpeg merge command
        cmd = [
            "ffmpeg",
            "-i",
            video_file,
            "-i",
            audio_file,
            "-c:v",
            "copy",
            "-c:a",
            "aac",
            "-shortest",
            "-y",  # Overwrite output file
            output_file,
        ]

        logger.info(f"Merging video and audio: {' '.join(cmd)}")

        process = await asyncio.create_subprocess_exec(
            *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )

        stdout, stderr = await process.communicate()

        if process.returncode != 0:
            error_msg = f"Video/audio merge failed: {stderr.decode()}"
            logger.error(error_msg)
            return CallToolResult(
                content=[TextContent(type="text", text=error_msg)], isError=True
            )

        result_msg = f"Successfully merged video and audio: {output_file}"
        logger.info(result_msg)

        return CallToolResult(content=[TextContent(type="text", text=result_msg)])

    except Exception as e:
        error_msg = f"Error during video/audio merge: {str(e)}"
        logger.error(error_msg)
        return CallToolResult(
            content=[TextContent(type="text", text=error_msg)], isError=True
        )


async def check_file_exists(arguments: Dict[str, Any]) -> CallToolResult:
    """Check if a file exists and return its metadata."""
    filepath = arguments["filepath"]

    try:
        path = Path(filepath)

        if not path.exists():
            return CallToolResult(
                content=[
                    TextContent(type="text", text=f"File does not exist: {filepath}")
                ],
                isError=True,
            )

        stat = path.stat()

        metadata = {
            "filepath": str(path.absolute()),
            "exists": True,
            "is_file": path.is_file(),
            "is_directory": path.is_dir(),
            "size_bytes": stat.st_size,
            "created": stat.st_ctime,
            "modified": stat.st_mtime,
        }

        return CallToolResult(
            content=[
                TextContent(
                    type="text", text=f"File metadata: {json.dumps(metadata, indent=2)}"
                )
            ]
        )

    except Exception as e:
        return CallToolResult(
            content=[TextContent(type="text", text=f"Error checking file: {str(e)}")],
            isError=True,
        )


async def main():
    """Main entry point for the renderer MCP server."""
    # Set up logging
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
    )

    async with stdio_server() as (read_stream, write_stream):
        await server.run(
            read_stream,
            write_stream,
            InitializationOptions(
                server_name="neuroanim-renderer",
                server_version="0.1.0",
                capabilities=server.get_capabilities(
                    notification_options=NotificationOptions(),
                    experimental_capabilities={},
                ),
            ),
        )


if __name__ == "__main__":
    asyncio.run(main())