analyze_python_file
Analyze Python files to identify missing type annotations, helping improve code documentation and type safety.
Instructions
Analyze a Python file for missing type annotations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Path to the Python file to analyze | |
| detailed | No | Include detailed analysis information |
Implementation Reference
- The handler logic within the call_tool function that processes calls to 'analyze_python_file', validates input, runs the analysis via PyreflyAnalyzer, and returns formatted results.if name == "analyze_python_file": file_path = arguments.get("file_path") detailed = arguments.get("detailed", False) if not file_path: raise ValueError("Missing file_path argument") if not os.path.exists(file_path): raise ValueError(f"File not found: {file_path}") analysis = await pyrefly_analyzer.analyze_file(file_path) if detailed: result_text = f"""Detailed Pyrefly Analysis for {file_path}: Functions needing types ({len(analysis.get('functions_needing_types', []))}): {chr(10).join(f" - {func}" for func in analysis.get('functions_needing_types', []))} Variables needing types ({len(analysis.get('variables_needing_types', []))}): {chr(10).join(f" - {var}" for var in analysis.get('variables_needing_types', []))} Suggested types: {chr(10).join(f" - {name}: {type_hint}" for name, type_hint in analysis.get('suggested_types', {}).items())} Pyrefly Output: {analysis.get('pyrefly_output', 'No output available')}""" else: result_text = f"""Pyrefly Analysis for {file_path}: Functions needing types: {len(analysis.get('functions_needing_types', []))} Variables needing types: {len(analysis.get('variables_needing_types', []))}""" return [types.TextContent(type="text", text=result_text)]
- Schema definition for the 'analyze_python_file' tool, including input parameters file_path (required) and detailed (optional boolean).types.Tool( name="analyze_python_file", description="Analyze a Python file for missing type annotations", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Python file to analyze" }, "detailed": { "type": "boolean", "description": "Include detailed analysis information", "default": False } }, "required": ["file_path"], }, ),
- src/mcp_pyrefly_autotype/server.py:331-414 (registration)Registration of the 'analyze_python_file' tool via the MCP server.list_tools() handler, which lists all available tools including this one.@server.list_tools() async def handle_list_tools() -> list[types.Tool]: """ List available tools for Python type analysis and modification. """ return [ types.Tool( name="analyze_python_file", description="Analyze a Python file for missing type annotations", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Python file to analyze" }, "detailed": { "type": "boolean", "description": "Include detailed analysis information", "default": False } }, "required": ["file_path"], }, ), types.Tool( name="add_types_to_file", description="Add type annotations to a Python file using Pyrefly", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Python file to add types to" }, "backup": { "type": "boolean", "description": "Create a backup of the original file", "default": True }, "aggressive": { "type": "boolean", "description": "Use aggressive type inference", "default": False }, "safe_mode": { "type": "boolean", "description": "Use safe mode for type inference", "default": True } }, "required": ["file_path"], }, ), types.Tool( name="type_check_file", description="Run type checking on a Python file using Pyrefly", inputSchema={ "type": "object", "properties": { "file_path": { "type": "string", "description": "Path to the Python file to type check" } }, "required": ["file_path"], }, ), types.Tool( name="get_project_context", description="Get project-wide type information for better type inference", inputSchema={ "type": "object", "properties": { "project_path": { "type": "string", "description": "Path to the project directory" } }, "required": ["project_path"], }, ), ]
- Helper method in PyreflyAnalyzer class that runs the pyrefly autotype command on the file, parses the output, and returns structured analysis data used by the tool handler.async def analyze_file(self, file_path: str) -> Dict[str, Any]: """Analyze a Python file using Pyrefly's analysis capabilities.""" try: # Use Pyrefly to analyze the file result = await self.run_pyrefly_command([ "uv", "run", "pyrefly", "autotype", file_path ]) if result["success"]: # Parse Pyrefly's output for analysis information analysis = self._parse_pyrefly_analysis(result["stdout"], file_path) return analysis else: return { "error": result.get("error", result.get("stderr", "Unknown error")), "file_path": file_path } except Exception as e: return {"error": str(e), "file_path": file_path}