diagnostics
Identify and report Java code errors and warnings in specific files or across entire projects using Eclipse JDT.LS diagnostics.
Instructions
Get diagnostics (errors, warnings) for a file or all files.
Args: file_path: Optional path to get diagnostics for a specific file. If not provided, returns diagnostics for all files.
Returns: Dictionary with 'diagnostics' array containing formatted diagnostic info
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | No |
Implementation Reference
- The main handler for the 'diagnostics' MCP tool, decorated with @mcp.tool(). It retrieves diagnostics from the manager for a specific file or all files and formats them.@mcp.tool() async def diagnostics( file_path: str | None = None, ) -> dict: """ Get diagnostics (errors, warnings) for a file or all files. Args: file_path: Optional path to get diagnostics for a specific file. If not provided, returns diagnostics for all files. Returns: Dictionary with 'diagnostics' array containing formatted diagnostic info """ manager = get_manager() if manager is None: return {"status": "error", "message": "Server not initialized"} if file_path: # Get diagnostics for specific file raw_diagnostics = manager.get_diagnostics(Path(file_path)) formatted = _format_diagnostics(file_path, raw_diagnostics) return {"diagnostics": formatted} else: # Get all diagnostics all_raw = manager.get_all_diagnostics() all_formatted = [] for uri, diags in all_raw.items(): try: path = str(uri_to_path(uri)) except ValueError: path = uri all_formatted.extend(_format_diagnostics(path, diags)) return {"diagnostics": all_formatted}
- src/jons_mcp_java/server.py:64-65 (registration)Import of the diagnostics tool (along with others) in the FastMCP server, which registers the tools due to the @mcp.tool() decorators.# Import tools to register them from jons_mcp_java.tools import navigation, symbols, diagnostics, info # noqa: E402, F401
- Helper function to format raw LSP diagnostics into a standardized dictionary format used by the tool.def _format_diagnostics(file_path: str, diagnostics: list) -> list[dict]: """Format LSP diagnostics to a user-friendly format.""" result = [] for diag in diagnostics: range_obj = diag.get("range", {}) start = range_obj.get("start", {}) severity = diag.get("severity", 1) severity_name = { 1: "error", 2: "warning", 3: "information", 4: "hint", }.get(severity, "unknown") result.append({ "file": file_path, "line": start.get("line", 0), "character": start.get("character", 0), "severity": severity_name, "message": diag.get("message", ""), "source": diag.get("source", "jdtls"), "code": diag.get("code"), }) return result