sessions_end
End a debug session and clean up resources to free memory and maintain system performance.
Instructions
End a debug session and clean up resources
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sessionId | Yes | The debug session ID |
Implementation Reference
- src/mcp_debug_tool/server.py:499-553 (handler)MCP tool handler for sessions_end: extracts session ID, invokes SessionManager.end_session_async, returns JSON response or structured error.async def _handle_sessions_end(self, arguments: dict) -> list[TextContent]: """ Handler for sessions_end tool. Ends a debug session and cleans up resources. """ try: session_id = arguments.get("sessionId") if not session_id: return [ TextContent( type="text", text=json.dumps({ "error": { "type": "ValueError", "message": "sessionId is required", } }), ) ] response = await self.session_manager.end_session_async(session_id) result = response.model_dump() return [ TextContent( type="text", text=json.dumps(result), ) ] except KeyError as e: return [ TextContent( type="text", text=json.dumps({ "error": { "type": "SessionNotFound", "message": str(e), } }), ) ] except Exception as e: logger.exception("Error ending session") return [ TextContent( type="text", text=json.dumps({ "error": { "type": type(e).__name__, "message": str(e), } }), ) ]
- src/mcp_debug_tool/server.py:148-161 (registration)Registers the sessions_end tool in the MCP server's list_tools() with name, description, and input schema requiring sessionId.Tool( name="sessions_end", description="End a debug session and clean up resources", inputSchema={ "type": "object", "properties": { "sessionId": { "type": "string", "description": "The debug session ID", }, }, "required": ["sessionId"], }, ),
- Pydantic model for the output response of sessions_end tool, confirming session ended.class EndSessionResponse(BaseModel): """Response confirming session ended.""" ended: bool = True
- Core logic in SessionManager to end a session: terminates DAP wrapper or subprocess gracefully/forcibly, updates status, removes from active sessions, returns EndSessionResponse.def end_session(self, session_id: str) -> EndSessionResponse: """ End a debug session and clean up resources. Args: session_id: Session ID Returns: End confirmation response """ session = self.get_session(session_id) # Clean up DAP wrapper if using DAP if session.dap_wrapper: try: session.dap_wrapper.terminate() except Exception: pass # Clean up subprocess if running (bdb mode) if session.process and session.process.poll() is None: # Try graceful termination first try: terminate_cmd = json.dumps({"command": "terminate"}) + '\n' session.process.stdin.write(terminate_cmd) session.process.stdin.flush() session.process.wait(timeout=5) except Exception: pass # Force terminate if still running if session.process.poll() is None: session.process.terminate() try: session.process.wait(timeout=5) except subprocess.TimeoutExpired: session.process.kill() session.process.wait() # Update status session.update_status(SessionStatus.COMPLETED) # Remove from active sessions del self.sessions[session_id] return EndSessionResponse(ended=True)