|
| 1 | +import asyncio |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from typing import Annotated, Optional, Dict, Any, List |
| 4 | +from mcp.server.fastmcp import FastMCP |
| 5 | +from codegen import Codebase |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class CodebaseState: |
| 10 | + """Class to manage codebase state and parsing.""" |
| 11 | + |
| 12 | + parse_task: Optional[asyncio.Task] = None |
| 13 | + parsed_codebase: Optional[Codebase] = None |
| 14 | + log_buffer: List[str] = field(default_factory=list) |
| 15 | + |
| 16 | + async def parse(self, path: str) -> Codebase: |
| 17 | + """Parse the codebase at the given path.""" |
| 18 | + codebase = Codebase(path) |
| 19 | + self.parsed_codebase = codebase |
| 20 | + return codebase |
| 21 | + |
| 22 | + def reset(self) -> None: |
| 23 | + """Reset the state.""" |
| 24 | + if self.parsed_codebase: |
| 25 | + self.parsed_codebase.reset() |
| 26 | + self.log_buffer.clear() |
| 27 | + |
| 28 | + |
| 29 | +# Initialize FastMCP server |
| 30 | +mcp = FastMCP( |
| 31 | + "codegen-mcp-server", |
| 32 | + instructions="""This server provides tools to parse and modify a codebase using codemods. |
| 33 | + It can initiate parsing, check parsing status, and execute codemods.""", |
| 34 | +) |
| 35 | + |
| 36 | +# Initialize state |
| 37 | +state = CodebaseState() |
| 38 | + |
| 39 | + |
| 40 | +def capture_output(*args, **kwargs) -> None: |
| 41 | + """Capture and log output messages.""" |
| 42 | + for arg in args: |
| 43 | + state.log_buffer.append(str(arg)) |
| 44 | + |
| 45 | + |
| 46 | +@mcp.tool(name="parse_codebase", description="Initiate codebase parsing") |
| 47 | +async def parse_codebase(codebase_path: Annotated[str, "path to the codebase to be parsed"]) -> Dict[str, str]: |
| 48 | + if not state.parse_task or state.parse_task.done(): |
| 49 | + state.parse_task = asyncio.create_task(state.parse(codebase_path)) |
| 50 | + return {"message": "Codebase parsing initiated, this may take some time depending on the size of the codebase. Use the `check_parsing_status` tool to check if the parse has completed."} |
| 51 | + return {"message": "Codebase is already being parsed."} |
| 52 | + |
| 53 | + |
| 54 | +@mcp.tool(name="check_parse_status", description="Check if codebase parsing has completed") |
| 55 | +async def check_parse_status() -> Dict[str, str]: |
| 56 | + if not state.parse_task: |
| 57 | + return {"message": "No codebase provided to parse."} |
| 58 | + if state.parse_task.done(): |
| 59 | + return {"message": "Codebase parsing completed."} |
| 60 | + return {"message": "Codebase parsing in progress."} |
| 61 | + |
| 62 | + |
| 63 | +@mcp.tool(name="execute_codemod", description="Execute a codemod on the codebase") |
| 64 | +async def execute_codemod(codemod: Annotated[str, "The python codemod code to execute on the codebase"]) -> Dict[str, Any]: |
| 65 | + if not state.parse_task or not state.parse_task.done(): |
| 66 | + return {"error": "Codebase is not ready for codemod execution."} |
| 67 | + |
| 68 | + try: |
| 69 | + await state.parse_task |
| 70 | + # TODO: Implement proper sandboxing for code execution |
| 71 | + context = { |
| 72 | + "codebase": state.parsed_codebase, |
| 73 | + "print": capture_output, |
| 74 | + } |
| 75 | + exec(codemod, context) |
| 76 | + |
| 77 | + logs = "\n".join(state.log_buffer) |
| 78 | + |
| 79 | + state.reset() |
| 80 | + return {"message": "Codemod executed and codebase reset.", "logs": logs} |
| 81 | + except Exception as e: |
| 82 | + return {"error": f"Error executing codemod: {str(e)}", "details": {"type": type(e).__name__, "message": str(e)}} |
| 83 | + |
| 84 | + |
| 85 | +def main(): |
| 86 | + print("starting codegen-mcp-server") |
| 87 | + run = mcp.run_stdio_async() |
| 88 | + print("codegen-mcp-server started") |
| 89 | + asyncio.run(run) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments