"""
API endpoints module.
This module contains additional REST API endpoints that complement the MCP tools.
These endpoints provide direct HTTP access to some functionality for testing and monitoring.
"""
from pathlib import Path
from starlette.responses import JSONResponse
from starlette.routing import Route
from ..utils import running_processes
async def get_status(request):
"""Get server status and statistics."""
workspace_dir = (
Path("/workspace") if Path("/workspace").exists() else Path("./workspace")
)
return JSONResponse(
{
"status": "running",
"workspace": str(workspace_dir),
"workspace_exists": workspace_dir.exists(),
"running_processes": len(running_processes),
"available_tools": ["shell", "execute_code", "install_package"],
"supported_languages": ["python", "node", "javascript", "js", "go"],
"supported_package_managers": ["pip", "npm", "go"],
}
)
async def list_workspace_files(request):
"""List files in the workspace directory."""
workspace_dir = (
Path("/workspace") if Path("/workspace").exists() else Path("./workspace")
)
if not workspace_dir.exists():
return JSONResponse({"error": "Workspace directory not found"}, status_code=404)
try:
files = []
for item in workspace_dir.iterdir():
files.append(
{
"name": item.name,
"type": "directory" if item.is_dir() else "file",
"size": item.stat().st_size if item.is_file() else None,
}
)
return JSONResponse(
{
"workspace": str(workspace_dir),
"files": sorted(files, key=lambda x: (x["type"], x["name"])),
"total_files": len([f for f in files if f["type"] == "file"]),
"total_directories": len(
[f for f in files if f["type"] == "directory"]
),
}
)
except Exception as e:
return JSONResponse(
{"error": f"Error listing files: {str(e)}"}, status_code=500
)
async def list_running_processes(request):
"""List currently running processes managed by the server."""
processes = []
for pid, process in running_processes.items():
processes.append(
{
"pid": pid,
"returncode": process.returncode,
"is_running": process.returncode is None,
}
)
return JSONResponse(
{
"running_processes": processes,
"total_count": len(processes),
}
)
# Create Starlette routes
router = [
Route("/status", get_status),
Route("/workspace/files", list_workspace_files),
Route("/processes", list_running_processes),
]