|
7 | 7 |
|
8 | 8 | # Use FastMCP instead of direct Server inheritance |
9 | 9 | from mcp.server.fastmcp import FastMCP |
10 | | -from mcp.server.models import Status |
11 | 10 |
|
12 | 11 | from app.compiler import RustCompiler |
13 | 12 | from app.response_parser import ResponseParser |
14 | 13 | from app.vector_store import QdrantStore |
15 | 14 | from app.llm_client import LlamaEdgeClient |
16 | 15 | from app.mcp_service import RustCompilerMCP |
17 | 16 |
|
18 | | -# Initialize MCP service globally for use in tools |
| 17 | +# Create a RustMCPServer class for the run_mcp_server.py script |
| 18 | +class RustMCPServer: |
| 19 | + def __init__(self, mcp_service): |
| 20 | + self.mcp = FastMCP("Rust Compiler") |
| 21 | + self.mcp_service = mcp_service |
| 22 | + |
| 23 | + # Register tools |
| 24 | + @self.mcp.tool() |
| 25 | + def compile(code: str) -> Dict[str, Any]: |
| 26 | + """Compile Rust code""" |
| 27 | + return self.mcp_service.compile_rust_code(code) |
| 28 | + |
| 29 | + @self.mcp.tool() |
| 30 | + def compileAndFix(code: str, description: str, max_attempts: int = 3) -> Dict[str, Any]: |
| 31 | + """Compile and fix Rust code""" |
| 32 | + result = self.mcp_service.compile_and_fix_rust_code(code, description, max_attempts) |
| 33 | + |
| 34 | + if result["success"]: |
| 35 | + # Format fixed files as raw text with filename markers |
| 36 | + output_text = "" |
| 37 | + for filename, content in result["final_files"].items(): |
| 38 | + output_text += f"[filename: {filename}]\n{content}\n\n" |
| 39 | + |
| 40 | + return output_text.strip() |
| 41 | + else: |
| 42 | + return {"status": "error", "message": f"Failed to fix code: {result.get('build_output', '')}"} |
| 43 | + |
| 44 | + @self.mcp.tool() |
| 45 | + def vectorSearch(query: str, collection: str, limit: int = 3) -> Dict[str, Any]: |
| 46 | + """Search vector database for similar examples""" |
| 47 | + embedding = self.mcp_service.llm_client.get_embeddings([query])[0] |
| 48 | + results = self.mcp_service.vector_store.search(collection, embedding, limit=limit) |
| 49 | + return {"results": results} |
| 50 | + |
| 51 | + def run(self, host="0.0.0.0", port=3001): |
| 52 | + """Run the MCP server""" |
| 53 | + self.mcp.run(host=host, port=port) |
| 54 | + |
| 55 | +# For direct invocation |
19 | 56 | mcp = FastMCP("Rust Compiler") |
20 | 57 | mcp_service = None # Will be initialized in main |
21 | 58 |
|
|
0 commit comments