File size: 2,339 Bytes
cce8120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
        return {"jsonrpc": "2.0", "result": "pong", "id": req_id}

    # JSON-RPC Tool Discovery
    if method in ("tools/list", "mcp.list_tools"):
        return {
            "jsonrpc": "2.0",
            "result": {
                "tools": [
                    {"name": "generate_api", "description": "Generate backend API endpoints"},
                    {"name": "modify_code", "description": "Modify code files in workspace"},
                    {"name": "backend_run_tests", "description": "Run pytest test suite"}
                ]
            },
            "id": req_id
        }

    # JSON-RPC Tool Execution
    if method == "tools/call":
        tool_name = params.get("name")
        args = params.get("arguments", {})

        if tool_name == "generate_api":
            return {
                "jsonrpc": "2.0",
                "result": {
                    "status": "success",
                    "content": [{"type": "text", "text": "API code generated successfully"}]
                },
                "id": req_id
            }

        elif tool_name == "modify_code":
            filepath = args.get("filepath")
            search_text = args.get("search_text")
            replace_text = args.get("replace_text")
            if filepath and search_text and replace_text:
                builder.apply_patch(filepath, search_text, replace_text)
            return {
                "jsonrpc": "2.0",
                "result": {
                    "status": "success",
                    "content": [{"type": "text", "text": "Code modified successfully"}]
                },
                "id": req_id
            }

        elif tool_name == "backend_run_tests":
            test_path = args.get("test_path", "tests")
            try:
                proc = await asyncio.create_subprocess_exec(
                    "python3", "-m", "pytest", test_path, "-v",
                    stdout=asyncio.subprocess.PIPE,
                    stderr=asyncio.subprocess.PIPE
                )
                stdout, stderr = await proc.communicate()
                out_str = stdout.decode(errors="replace")
                err_str = stderr.decode(errors="replace")
                return {
                    "jsonrpc": "2.0",
                    "result": {
                        "exit_code": proc.returncode,