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')