File size: 10,193 Bytes
ba52031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MCP (Model Context Protocol) request handler for debug-env.

Implements the MCP JSON-RPC 2024-11-05 protocol, exposing the three debug
tools (run_tests, read_file, edit_file) and six advanced analysis tools to any
MCP-compatible client.

Session model: a single shared DebugEnvironment is used for MCP sessions.
Call 'initialize' to set up a session (optionally passing {"task": "task2"} in
params.meta), then use 'tools/call' to invoke tools across turns.
"""

import logging
from typing import Any, Dict, Optional

from fastapi import Request

logger = logging.getLogger(__name__)

# ── MCP tool definitions ───────────────────────────────────────────────────────

MCP_TOOLS = [
    {
        "name": "run_tests",
        "description": """Execute the pytest test suite against the current code.

        Runs all tests in the task working directory and returns detailed results
        including pass rate and full test output logs.

        Response Structure:
          - pass_rate: Float (0.0–1.0) indicating fraction of tests passed
          - logs: Test output including failures, errors, and summary
          - reward: Scalar reward based on pass_rate (0.0 at fail, >0.0 at pass)
          - done: Boolean, true when all tests pass (pass_rate = 1.0)

        Status Codes:
          - 200: Success - Tests executed (may include failures)
          - 500: Internal error running tests
        """,
        "inputSchema": {"type": "object", "properties": {}, "required": []},
    },
    {
        "name": "read_file",
        "description": """Read the current content of a source file in the task working directory.

        Allows inspection of broken code and helper files before deciding what to edit.
        Reading a file does not affect the test pass rate or episode score.

        Required Parameters:
          - path: Filename to read (relative to workdir), e.g. 'broken_code.py'

        Response Structure:
          - pass_rate: 0.0 (reading doesn't affect score)
          - logs: File content prefixed with filename header
          - success: true if file was read successfully

        Status Codes:
          - 200: Success - File read and returned
          - 400: Bad Request - Invalid filename or path traversal attempt
          - 404: Not Found - File does not exist in task
          - 500: Internal error reading file
        """,
        "inputSchema": {
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Filename to read (e.g. 'broken_code.py', 'helper.py')",
                }
            },
            "required": ["path"],
        },
    },
    {
        "name": "edit_file",
        "description": """Overwrite a source file with corrected content and run tests.

        Replaces the entire file content and immediately executes the test suite
        to evaluate the fix. Partial writes are not supportedβ€”always provide the
        complete file content.

        Required Parameters:
          - path: Filename to edit (e.g. 'broken_code.py')
          - content: Complete new file content (full replacement)

        Response Structure:
          - pass_rate: Float (0.0–1.0) from subsequent test run
          - logs: Test output showing impact of the edit
          - reward: Scalar reward based on new pass_rate
          - done: Boolean, true when all tests pass
          - success: true if file was written and tests executed

        Status Codes:
          - 200: Success - File written and tests executed
          - 400: Bad Request - Invalid filename or path traversal attempt
          - 404: Not Found - File does not exist in task
          - 500: Internal error writing or testing
        """,
        "inputSchema": {
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Filename to edit (e.g. 'broken_code.py')",
                },
                "content": {
                    "type": "string",
                    "description": "Complete new file content (full file replacement)",
                },
            },
            "required": ["path", "content"],
        },
    },
]

# Advanced debugging tools will be added at runtime (in _handle_tools_list) to avoid circular imports

# ── Shared environment instance ────────────────────────────────────────────────

_env = None  # lazily initialised on first tool call


def _get_env():
    """Return the shared DebugEnvironment, creating it if needed."""
    global _env
    if _env is None:
        from debug_env.server.debug_env_environment import DebugEnvironment
        _env = DebugEnvironment()
    return _env


# ── JSON-RPC helpers ───────────────────────────────────────────────────────────


def _ok(request_id: Any, result: Dict[str, Any]) -> Dict[str, Any]:
    return {"jsonrpc": "2.0", "id": request_id, "result": result}


def _error(request_id: Any, code: int, message: str) -> Dict[str, Any]:
    return {"jsonrpc": "2.0", "id": request_id, "error": {"code": code, "message": message}}


# ── Handler ────────────────────────────────────────────────────────────────────


async def handle_mcp_request(request: Request) -> Optional[Dict[str, Any]]:
    """
    Dispatch an incoming MCP JSON-RPC request to the appropriate handler.

    Returns None for notifications (caller should respond with 204 No Content).
    """
    try:
        body = await request.json()
    except Exception:
        return _error(None, -32700, "Parse error: request body is not valid JSON")

    method: str = body.get("method", "")
    params: Dict[str, Any] = body.get("params") or {}
    request_id = body.get("id")

    # Notifications have no 'id' β€” we return None so the router sends 204
    if request_id is None:
        logger.debug(f"MCP notification received: {method}")
        return None

    logger.info(f"MCP request: method={method} id={request_id}")

    if method == "initialize":
        return _handle_initialize(request_id, params)

    if method == "tools/list":
        return _handle_tools_list(request_id)

    if method == "tools/call":
        return await _handle_tools_call(request_id, params)

    return _error(request_id, -32601, f"Method not found: {method}")


# ── Method handlers ────────────────────────────────────────────────────────────


def _handle_initialize(request_id: Any, params: Dict[str, Any]) -> Dict[str, Any]:
    """
    Perform the MCP handshake and optionally load a task.

    Clients may pass {"meta": {"task": "task2"}} to select a task up front.
    """
    task = (params.get("meta") or {}).get("task")
    env = _get_env()
    env.reset(task=task)
    logger.info(f"MCP session initialised β€” task={env.current_task}")

    return _ok(request_id, {
        "protocolVersion": "2024-11-05",
        "capabilities": {"tools": {}},
        "serverInfo": {"name": "debug-env", "version": "0.1.0"},
    })


def _handle_tools_list(request_id: Any) -> Dict[str, Any]:
    """Return the list of available debug tools in MCP format."""
    # Add advanced tools on first call (lazy loading to avoid circular imports)
    if len(MCP_TOOLS) == 3:  # Only have the 3 base tools
        from debug_env.server.tools.tool_handlers import get_mcp_tools_list
        advanced_tools = get_mcp_tools_list()
        MCP_TOOLS.extend(advanced_tools)

    return _ok(request_id, {"tools": MCP_TOOLS})


async def _handle_tools_call(request_id: Any, params: Dict[str, Any]) -> Dict[str, Any]:
    """Execute a debug tool via generic dispatcher and return the result as MCP content."""
    from debug_env.models import DebugAction
    from debug_env.server.tools.tool_handlers import get_tool_handlers

    tool_name: str = params.get("name", "")
    tool_args: Dict[str, Any] = params.get("arguments") or {}

    if not tool_name:
        return _error(request_id, -32602, "Invalid params: 'name' is required")

    env = _get_env()
    if env.workdir is None:
        env.reset()

    # Get tool handlers (lazy-loaded to avoid circular imports)
    tool_handlers = get_tool_handlers()

    # Try advanced tools first via generic dispatcher
    if tool_name in tool_handlers:
        try:
            logger.debug(f"Executing advanced tool {tool_name} via generic dispatcher")
            result = await tool_handlers[tool_name](tool_name, tool_args, env.workdir)
            text = result.get("text", "")
            return _ok(request_id, {
                "content": [{"type": "text", "text": text}],
                "isError": result.get("isError", False),
            })
        except Exception as e:
            logger.error(f"Tool {tool_name} execution failed: {e}", exc_info=True)
            return _error(request_id, -32603, f"Internal error executing tool '{tool_name}'")

    # Fall back to environment tools (run_tests, read_file, edit_file)
    try:
        action = DebugAction(tool=tool_name, args=tool_args)
        obs = env.step(action)
    except Exception as e:
        logger.error(f"MCP tool call failed β€” tool={tool_name}: {e}", exc_info=True)
        return _error(request_id, -32603, f"Internal error executing tool '{tool_name}'")

    text = (
        f"pass_rate={obs.pass_rate:.2f}  reward={obs.reward:.2f}  done={obs.done}"
        f"\n\n{obs.logs}"
    )
    return _ok(request_id, {
        "content": [{"type": "text", "text": text}],
        "isError": obs.pass_rate == 0.0 and not obs.done,
    })