Skip to main content
Glama
code_review_example.py3.18 kB
#!/usr/bin/env python3 """ Example: Code Review This example demonstrates how to use the review_code tool to analyze code against best practices. """ import sys import os # Add parent directory to path to import modules sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from bestpractices_mcp_server import _review_code_impl def main(): """Demonstrate code review functionality.""" print("=" * 60) print("Example: Code Review") print("=" * 60) print() # Example 1: Review code with issues print("1. Reviewing code with common issues:") print("-" * 60) bad_code = """ def calculate(x, y): try: result = x / y except: return None return result """ print("Code to review:") print(bad_code) print() result = _review_code_impl(bad_code, "general") print(f"Overall Score: {result['overall_score']}/100") print(f"Summary: {result['summary']}") print() if result.get('violations'): print("Violations found:") for v in result['violations']: print(f" [{v['severity']}] {v['issue']}") print(f" Recommendation: {v['recommendation']}") print(f" Example: {v['example'][:60]}...") print() # Example 2: Review good code print("\n2. Reviewing well-written code:") print("-" * 60) good_code = """ def calculate_total(price: float, quantity: int) -> float: ''' Calculate the total cost. Args: price: Unit price quantity: Number of items Returns: Total cost ''' try: total = price * quantity return total except TypeError as e: raise ValueError(f"Invalid input types: {e}") """ print("Code to review:") print(good_code) print() result = _review_code_impl(good_code, "general") print(f"Overall Score: {result['overall_score']}/100") print(f"Summary: {result['summary']}") print() if result.get('good_patterns'): print("Good patterns identified:") for pattern in result['good_patterns']: print(f" ✓ {pattern}") print() # Example 3: Review FastAPI code print("\n3. Reviewing FastAPI endpoint:") print("-" * 60) fastapi_code = """ from fastapi import FastAPI app = FastAPI() @app.get("/users/{user_id}") def get_user(user_id: str): return {"user_id": user_id} """ print("Code to review:") print(fastapi_code) print() result = _review_code_impl(fastapi_code, "fastapi") print(f"Overall Score: {result['overall_score']}/100") print(f"Detected Context: {result.get('detected_context', 'N/A')}") print() if result.get('violations'): print("FastAPI-specific issues:") for v in result['violations']: print(f" [{v['severity']}] {v['issue']}") print(f" Recommendation: {v['recommendation']}") print() print("=" * 60) print("Code review examples complete!") print("=" * 60) if __name__ == "__main__": main()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/n-ochs/best-practice-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server