33import sys
44from typing import Dict , Any , Optional , List
55
6-
76sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
87
9- # Update these imports with the correct paths
10- from mcp .server import Server
11- from mcp .server .models import Response , Status
12- from mcp .server .method import MethodCall
13- from mcp .runner import MCPRunner
8+ # Use FastMCP instead of direct Server inheritance
9+ from mcp .server .fastmcp import FastMCP
10+ from mcp .server .models import Status
1411
1512from app .compiler import RustCompiler
1613from app .response_parser import ResponseParser
1714from app .vector_store import QdrantStore
1815from app .llm_client import LlamaEdgeClient
1916from app .mcp_service import RustCompilerMCP
2017
18+ # Initialize MCP service globally for use in tools
19+ mcp = FastMCP ("Rust Compiler" )
20+ mcp_service = None # Will be initialized in main
2121
22- class RustMCPServer (Server ):
23- def __init__ (self , mcp_service ):
24- self .mcp_service = mcp_service
25- super ().__init__ ()
22+ @mcp .tool ()
23+ def compile_and_fix (code : str , description : str , max_attempts : int = 3 ) -> Dict [str , Any ]:
24+ """
25+ Compile and fix Rust code
26+
27+ Args:
28+ code: Multi-file Rust code with [filename:] markers
29+ description: Project description for context
30+ max_attempts: Maximum number of attempts to fix errors
2631
27- def compile_and_fix (self , call : MethodCall ) -> Response :
28- """Compile and fix Rust code according to MCP standard"""
29- code = call .params .get ("code" , "" )
30- description = call .params .get ("description" , "" )
31- max_attempts = int (call .params .get ("max_attempts" , 3 ))
32+ Returns:
33+ Fixed code or error details
34+ """
35+ global mcp_service
36+
37+ if not code or not description :
38+ return {"status" : "error" , "message" : "Missing required parameters" }
3239
33- if not code or not description :
34- return Response (status = Status .ERROR , result = "Missing required parameters" )
35-
36- try :
37- result = self .mcp_service .compile_and_fix_rust_code (code , description , max_attempts )
40+ try :
41+ result = mcp_service .compile_and_fix_rust_code (code , description , max_attempts )
42+
43+ if result ["success" ]:
44+ # Format fixed files as raw text with filename markers
45+ output_text = ""
46+ for filename , content in result ["final_files" ].items ():
47+ output_text += f"[filename: { filename } ]\n { content } \n \n "
3848
39- if result ["success" ]:
40- # Format fixed files as raw text with filename markers
41- output_text = ""
42- for filename , content in result ["final_files" ].items ():
43- output_text += f"[filename: { filename } ]\n { content } \n \n "
44-
45- # Return raw text instead of JSON
46- return Response (status = Status .SUCCESS , result = output_text .strip ())
47- else :
48- # For errors, return error message
49- return Response (status = Status .ERROR , result = f"Failed to fix code: { result .get ('build_output' , '' )} " )
50- except Exception as e :
51- return Response (status = Status .ERROR , result = str (e ))
52-
49+ return output_text .strip ()
50+ else :
51+ # For errors, return error message
52+ return {"status" : "error" , "message" : f"Failed to fix code: { result .get ('build_output' , '' )} " }
53+ except Exception as e :
54+ return {"status" : "error" , "message" : str (e )}
5355
5456if __name__ == "__main__" :
5557 # Initialize required components
@@ -67,20 +69,23 @@ def compile_and_fix(self, call: MethodCall) -> Response:
6769
6870 # Initialize components
6971 llm_client = LlamaEdgeClient (api_key = api_key )
70- parser = ResponseParser ()
71- compiler = RustCompiler ()
72-
73- # Initialize vector store
7472 vector_store = QdrantStore (embedding_size = llm_embed_size )
7573 vector_store .create_collection ("project_examples" )
7674 vector_store .create_collection ("error_examples" )
7775
7876 # Initialize MCP service
7977 mcp_service = RustCompilerMCP (vector_store = vector_store , llm_client = llm_client )
8078
81- # Create and run the MCP server
82- server = RustMCPServer ( mcp_service )
79+ # Determine transport mode from arguments or environment
80+ transport = os . getenv ( "MCP_TRANSPORT" , "stdio" )
8381
84- # Run the server with STDIO communication
85- runner = MCPRunner (server )
86- runner .run (sys .stdin , sys .stdout )
82+ # Run server with appropriate transport
83+ if transport == "stdio" :
84+ # Run in STDIO mode
85+ mcp .run (transport = "stdio" )
86+ else :
87+ # Run in HTTP/SSE mode
88+ host = os .getenv ("MCP_HOST" , "0.0.0.0" )
89+ port = int (os .getenv ("MCP_PORT" , "3001" ))
90+ print (f"Starting MCP server on { host } :{ port } " )
91+ mcp .run (host = host , port = port )
0 commit comments