"""MCP Code Refiner Server - Second Layer LLM for Code Improvement.
This server works with any MCP client (Claude Desktop, etc.).
Just tell Claude what you want to improve and it will automatically use these tools.
"""
from fastmcp import FastMCP
from tools.code_refinement import refine_code, apply_refinement
from tools.code_review import review_code
from dotenv import load_dotenv
load_dotenv()
mcp = FastMCP("CodeRefiner")
@mcp.tool()
def refine_code_tool(user_request: str, file_path: str, ai_provider: str = "gemini") -> dict:
"""Second layer LLM - refines code based on natural language feedback.
This tool takes existing code and improves it based on your request.
Perfect for refining code generated by another LLM or improving human-written code.
Args:
user_request: What you want to improve (e.g., "make it more logical", "add error handling")
file_path: Path to the code file to refine
ai_provider: AI provider to use (gemini, claude, openai) - default is gemini
Returns:
JSON with:
- status: "success" or "error"
- explanation: What was changed and why
- diff: Unified diff showing changes
- refined_code: The improved code (use this with apply_refinement_tool)
- file_path: Original file path
"""
return refine_code(user_request, file_path, ai_provider)
@mcp.tool()
def review_code_tool(file_path: str, ai_provider: str = "gemini") -> dict:
"""AI-powered code review - analyze code for bugs, security, performance, and quality.
This tool performs comprehensive code analysis to identify issues and suggest improvements.
Args:
file_path: Path to the code file to review
ai_provider: AI provider to use - options:
- "gemini" (default, fast and free with API key)
- "claude" or "claude-sonnet" (high quality)
- "openai" or "gpt-4o" (balanced)
- "claude-haiku" (fast)
- "gemini-pro" (more capable)
Returns:
JSON with:
- status: "success" or "error"
- issues: List of found issues with severity, category, and suggestions
- strengths: Positive aspects of the code
- overall_assessment: Summary of code quality
- score: Quality score from 1-10
"""
return review_code(file_path, ai_provider)
@mcp.tool()
def apply_refinement_tool(file_path: str, refined_code: str) -> dict:
"""Apply refined code to the file after user approval.
IMPORTANT: Only call this after the user has reviewed and approved the changes from refine_code_tool.
Args:
file_path: Path to the file to update
refined_code: The refined code from refine_code_tool
Returns:
JSON with:
- status: "success" or "error"
- message: Status message
"""
return apply_refinement(file_path, refined_code)
if __name__ == "__main__":
mcp.run()