File size: 3,329 Bytes
57c34dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from mcp.server.fastmcp import FastMCP
import json
import sys
import io
import time
from gradio_client import Client

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')

mcp = FastMCP("Gradio MCP Server")

clients = {}

BUILD_SERVER_SPACE_ID = "https://agents-mcp-hackathon-gradio-motioncanvas-mcp-server.hf.space/"
DOCS_SERVER_SPACE_ID = "https://agents-mcp-hackathon-gradio-motioncanvas-docs-mcp-server.hf.space/"

def get_client(space_id: str) -> Client:
    """Get or create a Gradio client for the specified space."""
    if space_id not in clients:
        clients[space_id] = Client(space_id)
    return clients[space_id]

@mcp.tool()
async def build_project(code: str) -> str:
    """Build a Motion Canvas project using the Gradio Motion MCP server.
    
    Args:
        code: TypeScript code for the scene to build
    """

    try:
        result = get_client(BUILD_SERVER_SPACE_ID).predict(
            code,
            api_name="/build"
        )
        
        if isinstance(result, list) and len(result) >= 2:
            project_path = result[0]
            logs = result[1]

            if project_path:
                return json.dumps({
                    "type": "mc_project",
                    "project_path": project_path,
                    "message": f"Built project at {project_path}",
                    "logs": result[1]
                })
            else:
                return json.dumps({
                    "type": "error",
                    "message": f"Error building project:\n" + logs
                })
        else:
            return json.dumps({
                "type": "error",
                "message": f"Error building project"
            })

    except Exception as e:
        return json.dumps({
            "type": "error",
            "message": f"Error building project: {str(e)}"
        })

@mcp.tool()
async def build_project(code: str) -> str:
    """Build a Motion Canvas project using the Gradio Motion MCP server.
    
    Args:
        code: TypeScript code for the scene to build
    """
    client = Client("https://agents-mcp-hackathon-gradio-motioncanvas-mcp-server.hf.space/")




    try:
        result = client.predict(
            code,
            api_name="/build"
        )
        
        if isinstance(result, list) and len(result) >= 2:
            project_path = result[0]
            logs = result[1]

            if project_path:
                return json.dumps({
                    "type": "mc_project",
                    "project_path": project_path,
                    "message": f"Built project at {project_path}",
                    "logs": result[1]
                })
            else:
                return json.dumps({
                    "type": "error",
                    "message": f"Error building project:\n" + logs
                })
        else:
            return json.dumps({
                "type": "error",
                "message": f"Error building project"
            })

    except Exception as e:
        return json.dumps({
            "type": "error",
            "message": f"Error building project: {str(e)}"
        })

if __name__ == "__main__":
    mcp.run(transport='stdio')