Analysis Tools¶
Analysis tools help AI assistants understand code structure without guessing. They provide accurate information about functions, classes, imports, and code organization.
Tools in This Category¶
| Tool | Description | Tier |
|---|---|---|
analyze_code | Parse code structure (functions, classes, imports) | Community |
analyze_code¶
Parse code and extract its structure with zero hallucination.
What It Does¶
analyze_code parses source code using real AST (Abstract Syntax Tree) analysis—not regex pattern matching. It returns structured information about:
- Functions: Names, parameters, return types, docstrings, line numbers
- Classes: Names, methods, inheritance, attributes
- Imports: All import statements with their targets
- Complexity: Cyclomatic complexity metrics
When AI Agents Use This¶
- Before making any code modifications
- To understand file structure before extracting code
- To find all functions/classes in a file
- To check code complexity
Quick Reference¶
| Property | Value |
|---|---|
| Tier | Community |
| Languages | Python, JavaScript, TypeScript, Java, JSX, TSX |
| Token Cost | ~100-500 tokens |
Parameters¶
| Parameter | Type | Required | Description |
|---|---|---|---|
code | string | ✓* | Source code to analyze |
file_path | string | ✓* | Path to file to analyze |
language | string | Language hint (auto-detected if omitted) |
*Provide either code or file_path, not both.
Example Usage¶
Response Format¶
{
"data": {
"functions": [
{
"name": "authenticate_user",
"line_start": 15,
"line_end": 32,
"parameters": [
{"name": "username", "type": "str"},
{"name": "password", "type": "str"}
],
"return_type": "Optional[User]",
"docstring": "Authenticate a user by username and password.",
"complexity": 4,
"is_async": false
}
],
"classes": [
{
"name": "AuthService",
"line_start": 5,
"line_end": 50,
"docstring": "Service for authentication operations.",
"methods": ["__init__", "authenticate_user", "logout"],
"bases": ["BaseService"],
"attributes": ["db", "cache"]
}
],
"imports": [
{"module": "typing", "names": ["Optional"], "line": 1},
{"module": "hashlib", "names": null, "line": 2}
],
"global_variables": [],
"total_lines": 50,
"total_functions": 3,
"total_classes": 1
},
"tier_applied": "community",
"duration_ms": 45
}
Why This Matters¶
| Without analyze_code | With analyze_code |
|---|---|
| AI guesses function names | Exact function list with signatures |
| "I think there's a class..." | Precise class structure and methods |
| Reads entire file (~10K tokens) | Returns only structure (~200 tokens) |
| May hallucinate non-existent code | Zero hallucination—AST parsed |
Tier Differences¶
| Feature | Community | Pro | Enterprise |
|---|---|---|---|
| Basic structure parsing | ✅ | ✅ | ✅ |
| Complexity metrics | ✅ | ✅ | ✅ |
| Docstring extraction | ✅ | ✅ | ✅ |
| Type hint extraction | ✅ | ✅ | ✅ |
| Max file size | 1 MB | 10 MB | Unlimited |
Common Patterns¶
Pattern: Analyze → Extract → Modify
1. analyze_code(file_path="src/auth.py")
→ Learn that authenticate_user exists at line 15
2. extract_code(file_path="src/auth.py", target_name="authenticate_user")
→ Get exact function code
3. update_symbol(file_path="src/auth.py", target_name="authenticate_user", new_code=...)
→ Replace with improved version
Troubleshooting¶
Returns empty results
- Check that the file exists and is readable
- Verify the file contains valid syntax for the language
- Use the
languageparameter if auto-detection fails
Syntax error in response
- The file may have invalid syntax
- Try running a linter on the file first
- Check for encoding issues (non-UTF-8 files)
Related Categories¶
- Context Tools - Navigate and understand codebases
- Extraction Tools - Extract specific code elements
- Security Tools - Find vulnerabilities in analyzed code