Skip to content

Commit 9dd22ea

Browse files
authored
fix: remove codebase reset call, and fix async bug (#541)
# Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed --------- Co-authored-by: rushilpatel0 <[email protected]>
1 parent d0c5349 commit 9dd22ea

File tree

1 file changed

+24
-14
lines changed
  • codegen-examples/examples/codegen-mcp-server

1 file changed

+24
-14
lines changed

codegen-examples/examples/codegen-mcp-server/server.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,16 @@ class CodebaseState:
1111

1212
parse_task: Optional[asyncio.Future] = None
1313
parsed_codebase: Optional[Codebase] = None
14-
codebase_path: Optional[str] = None
1514
log_buffer: List[str] = field(default_factory=list)
1615

17-
async def parse(self, path: str) -> Codebase:
16+
def parse(self, path: str) -> Codebase:
1817
"""Parse the codebase at the given path."""
1918
codebase = Codebase(path)
2019
self.parsed_codebase = codebase
21-
self.codebase_path = path
2220
return codebase
2321

2422
def reset(self) -> None:
2523
"""Reset the state."""
26-
if self.parsed_codebase:
27-
self.parsed_codebase.reset()
2824
self.log_buffer.clear()
2925

3026

@@ -45,12 +41,24 @@ def capture_output(*args, **kwargs) -> None:
4541
state.log_buffer.append(str(arg))
4642

4743

44+
def update_codebase(future: asyncio.Future):
45+
try:
46+
result = future.result()
47+
if result is not None:
48+
state.parsed_codebase = result
49+
else:
50+
state.parsed_codebase = None
51+
except Exception:
52+
pass
53+
54+
4855
@mcp.tool(name="parse_codebase", description="Initiate codebase parsing")
4956
async def parse_codebase(codebase_path: Annotated[str, "path to the codebase to be parsed"]) -> Dict[str, str]:
5057
if not state.parse_task or state.parse_task.done():
5158
state.parse_task = asyncio.get_event_loop().run_in_executor(None, lambda: state.parse(codebase_path))
59+
state.parse_task.add_done_callback(update_codebase)
5260
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."}
53-
return {"message": "Codebase is already being parsed."}
61+
return {"message": "Codebase is already being parsed.", "status": "error"}
5462

5563

5664
@mcp.tool(name="check_parse_status", description="Check if codebase parsing has completed")
@@ -69,17 +77,19 @@ async def execute_codemod(codemod: Annotated[str, "The python codemod code to ex
6977

7078
try:
7179
await state.parse_task
72-
# TODO: Implement proper sandboxing for code execution
73-
context = {
74-
"codebase": state.parsed_codebase,
75-
"print": capture_output,
76-
}
77-
exec(codemod, context)
80+
if state.parsed_codebase is None:
81+
return {"error": "Codebase path is not set."}
82+
else:
83+
# TODO: Implement proper sandboxing for code execution
84+
context = {
85+
"codebase": state.parsed_codebase,
86+
"print": capture_output,
87+
}
88+
exec(codemod, context)
7889

7990
logs = "\n".join(state.log_buffer)
80-
8191
state.reset()
82-
return {"message": "Codemod executed and codebase reset.", "logs": logs}
92+
return {"message": "Codemod executed, view the logs for any output and your source code for any resulting updates.", "logs": logs}
8393
except Exception as e:
8494
return {"error": f"Error executing codemod: {str(e)}", "details": {"type": type(e).__name__, "message": str(e)}}
8595

0 commit comments

Comments
 (0)