"""Simple test client for MCP Code Refiner Server.
NOTE: This is just for testing!
For actual use, configure Claude Desktop to use this MCP server.
See claude_desktop_config.md for setup instructions.
"""
import asyncio
import json
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt, Confirm
from rich.syntax import Syntax
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
console = Console()
async def test_server():
"""Test the MCP server."""
console.print(Panel.fit(
"[bold yellow]⚠️ TEST CLIENT ONLY[/bold yellow]\n\n"
"[bold cyan]For actual use:[/bold cyan]\n"
"Configure Claude Desktop to connect to this MCP server\n"
"See claude_desktop_config.md for instructions\n\n"
"[dim]This client is just for testing the server[/dim]",
border_style="yellow"
))
console.print("\n[bold blue]Connecting to MCP server...[/bold blue]")
server_params = StdioServerParameters(
command="python",
args=["mcp_server.py"],
env=None
)
try:
stdio_context = stdio_client(server_params)
read_stream, write_stream = await stdio_context.__aenter__()
session = ClientSession(read_stream, write_stream)
await session.__aenter__()
await session.initialize()
console.print("[bold green]✓ Connected![/bold green]\n")
while True:
console.print("[bold cyan]Test refine_code_tool[/bold cyan]")
user_request = Prompt.ask("What to improve").strip()
if not user_request or user_request.lower() in ['exit', 'quit']:
break
file_path = Prompt.ask("File path").strip()
if not file_path:
continue
console.print("\n[bold yellow]Calling refine_code_tool...[/bold yellow]\n")
try:
result = await session.call_tool(
"refine_code_tool",
arguments={
"user_request": user_request,
"file_path": file_path,
"ai_provider": "gemini"
}
)
content = result.content[0].text if result.content else "{}"
data = json.loads(content)
if data.get("status") == "error":
print("\n" + "="*80)
print(f"ERROR: {data.get('error')}")
# Show debug info if available
if "debug_file" in data:
print(f"\nDebug file saved to: {data['debug_file']}")
print("You can inspect this file to see the raw LLM response")
if "debug_info" in data:
print("\nDebug Information:")
for key, value in data['debug_info'].items():
print(f" {key}: {value}")
print("="*80 + "\n")
continue
console.print(Panel(data.get("explanation", ""), title="[bold green]Changes[/bold green]", border_style="green"))
diff = data.get("diff", "")
if diff:
console.print("\n[bold yellow]Diff:[/bold yellow]")
console.print(Syntax(diff, "diff", theme="monokai"))
console.print()
if Confirm.ask("[bold]Apply?[/bold]", default=False):
apply_result = await session.call_tool(
"apply_refinement_tool",
arguments={
"file_path": file_path,
"refined_code": data.get("refined_code", "")
}
)
apply_data = json.loads(apply_result.content[0].text if apply_result.content else "{}")
console.print(f"\n[bold green]{apply_data.get('message', 'Done')}[/bold green]\n")
else:
console.print("\n[dim]Discarded[/dim]\n")
except Exception as e:
console.print(f"[bold red]Error: {e}[/bold red]\n")
if not Confirm.ask("\n[dim]Continue?[/dim]", default=True):
break
await session.__aexit__(None, None, None)
await stdio_context.__aexit__(None, None, None)
except Exception as e:
console.print(f"[bold red]Failed: {e}[/bold red]")
if __name__ == "__main__":
asyncio.run(test_server())