Skip to content

Commit 5bfdeef

Browse files
committed
feat: implement RustMCPServer class with tools for compiling and fixing Rust code
1 parent 310b751 commit 5bfdeef

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

app/mcp_server.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,52 @@
77

88
# Use FastMCP instead of direct Server inheritance
99
from mcp.server.fastmcp import FastMCP
10-
from mcp.server.models import Status
1110

1211
from app.compiler import RustCompiler
1312
from app.response_parser import ResponseParser
1413
from app.vector_store import QdrantStore
1514
from app.llm_client import LlamaEdgeClient
1615
from app.mcp_service import RustCompilerMCP
1716

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
1956
mcp = FastMCP("Rust Compiler")
2057
mcp_service = None # Will be initialized in main
2158

0 commit comments

Comments
 (0)