analyze_code
Analyze code fragments with strict reviews powered by Groq LLM to identify bugs, vulnerabilities, and security issues.
Instructions
Strict analysis of a code fragment using Groq LLM.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Code fragment to review. | |
| language | No | Programming language (python, javascript, go, rust, ...). | python |
| context | No | Optional description - what the code does or where it came from. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/analyze.py:7-47 (handler)The async function `analyze_code` is the core handler for the MCP tool. It takes `code`, `language` (default python), and optional `context`, validates input, checks cache, calls the Groq LLM via the ANALYZE prompt, parses the JSON response, caches it, and returns a JSON string with issues, warnings, suggestions, and score.
async def analyze_code(code: str, language: str = "python", context: str = "") -> str: """ Strict analysis of a code fragment using Groq LLM. Args: code: Code fragment to review. language: Programming language (python, javascript, go, rust, ...). context: Optional description - what the code does or where it came from. Returns: JSON with fields: issues, warnings, suggestions, score. """ if not code.strip(): return error_response("Empty code provided.") key = cache.make_key("analyze_code", code, language, context) if hit := cache.get(key): return hit context_block = f"\nContext: {context}" if context else "" user = f"Language: {language}{context_block}\n\nCode:\n```{language}\n{code}\n```" try: raw = await call(ANALYZE, user) result = json.loads(raw) except httpx.HTTPStatusError as e: return error_response(f"Groq API error {e.response.status_code}", e.response.text[:300]) except json.JSONDecodeError as e: return error_response("Groq returned invalid JSON", str(e)) except ValueError as e: return error_response(str(e)) out = json.dumps(result, ensure_ascii=True, indent=2) cache.set(key, out) return out - tools/analyze.py:25-29 (helper)Cache helper integration: `cache.make_key()` creates a SHA-256 cache key, `cache.get()` retrieves cached results, and `cache.set()` stores results to avoid redundant API calls.
key = cache.make_key("analyze_code", code, language, context) if hit := cache.get(key): return hit context_block = f"\nContext: {context}" if context else ""