Skip to content

Commit 3983ff2

Browse files
committed
feat: mcp server
1 parent 6ecad9c commit 3983ff2

File tree

7 files changed

+10061
-1
lines changed

7 files changed

+10061
-1
lines changed

src/codegen/cli/api/client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PR_LOOKUP_ENDPOINT,
1616
RUN_ENDPOINT,
1717
RUN_ON_PR_ENDPOINT,
18+
IMPROVE_ENDPOINT,
1819
)
1920
from codegen.cli.api.schemas import (
2021
AskExpertInput,
@@ -36,12 +37,15 @@
3637
RunCodemodOutput,
3738
RunOnPRInput,
3839
RunOnPRResponse,
40+
ImproveCodemodInput,
41+
ImproveCodemodResponse,
3942
)
4043
from codegen.cli.auth.session import CodegenSession
4144
from codegen.cli.codemod.convert import convert_to_ui
4245
from codegen.cli.env.global_env import global_env
4346
from codegen.cli.errors import InvalidTokenError, ServerError
4447
from codegen.cli.utils.codemods import Codemod
48+
from codegen.cli.utils.constants import ProgrammingLanguage
4549
from codegen.cli.utils.function_finder import DecoratedFunction
4650

4751
InputT = TypeVar("InputT", bound=BaseModel)
@@ -55,7 +59,7 @@ class RestAPI:
5559

5660
auth_token: str | None = None
5761

58-
def __init__(self, auth_token: str):
62+
def __init__(self, auth_token: str | None = None):
5963
self.auth_token = auth_token
6064

6165
def _get_headers(self) -> dict[str, str]:
@@ -248,3 +252,13 @@ def lookup_pr(self, repo_full_name: str, github_pr_number: int) -> PRSchema:
248252
PRLookupInput(input=PRLookupInput.BasePRLookupInput(repo_full_name=repo_full_name, github_pr_number=github_pr_number)),
249253
PRLookupResponse,
250254
)
255+
256+
257+
def improve_codemod(self, codemod: str, task: str, concerns: list[str], context: dict[str, str], language: ProgrammingLanguage) -> ImproveCodemodResponse:
258+
"""Improve a codemod."""
259+
return self._make_request(
260+
"GET",
261+
IMPROVE_ENDPOINT,
262+
ImproveCodemodInput(input=ImproveCodemodInput.BaseImproveCodemodInput(codemod=codemod, task=task, concerns=concerns, context=context, language=language)),
263+
ImproveCodemodResponse
264+
)

src/codegen/cli/api/endpoints.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
RUN_ON_PR_ENDPOINT = f"https://{MODAL_PREFIX}--cli-run-on-pull-request.modal.run"
1111
PR_LOOKUP_ENDPOINT = f"https://{MODAL_PREFIX}--cli-pr-lookup.modal.run"
1212
CODEGEN_SYSTEM_PROMPT_URL = "https://gist.githubusercontent.com/jayhack/15681a2ceaccd726f19e6fdb3a44738b/raw/17c08054e3931b3b7fdf424458269c9e607541e8/codegen-system-prompt.txt"
13+
IMPROVE_ENDPOINT = f"https://{MODAL_PREFIX}--cli-improve.modal.run"

src/codegen/cli/api/schemas.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,22 @@ class RunOnPRResponse(BaseModel):
234234
codemod_id: int = Field(..., description="ID of the codemod")
235235
codemod_run_id: int = Field(..., description="ID of the codemod run")
236236
web_url: str = Field(..., description="URL to view the test results")
237+
238+
239+
###########################################################################
240+
# IMPROVE
241+
###########################################################################
242+
243+
class ImproveCodemodInput(BaseModel):
244+
class BaseImproveCodemodInput(BaseModel):
245+
codemod: str = Field(..., description="Source code of the codemod to improve")
246+
task: str = Field(..., description="Task to which the codemod should implement to solve")
247+
concerns: list[str] = Field(..., description="A list of issues that were discovered with the current codemod that need to be considered in the next iteration")
248+
context: dict[str, str] = Field(..., description="Additional context for the codemod this can be a list of files that are related, additional information about the task, etc.")
249+
language: ProgrammingLanguage = Field(..., description="Language of the codemod")
250+
251+
input: BaseImproveCodemodInput = Field(..., description="Input data for improvement")
252+
253+
class ImproveCodemodResponse(BaseModel):
254+
success: bool = Field(..., description="Whether the improvement was successful")
255+
codemod_source: str = Field(..., description="Source code of the improved codemod")

src/codegen/cli/mcp/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Codegen MCP server
2+
3+
A MCP server implementation that provides tools and resources for using and working with the Codegen CLI and SDK, enabling AI agents to iterate quickly on writing codemods with the codegen sdk.
4+
5+
### Dependencies
6+
7+
- [fastmcp](https://github.com/codegen-sh/fastmcp)
8+
9+
## Usage
10+
11+
Most AI Agents that support MCP will have some way to configure the server startup.
12+
### Cline
13+
Add this to your `cline_mcp_settings.json` file to get started:
14+
```
15+
{
16+
"mcpServers": {
17+
"codegen-cli": {
18+
"command": "uv",
19+
"args": [
20+
"--directory",
21+
"<path to codegen installation>/codegen-sdk/src/codegen/cli/mcp",
22+
"run",
23+
"server.py"
24+
]
25+
}
26+
}
27+
}
28+
```
29+
30+
Cursor:
31+
Under the `Settings` > `Feature` > `MCP Servers` section, click "Add New MCP Server" and add the following:
32+
33+
```
34+
Name: codegen-mcp
35+
Type: Command
36+
Command: uv --directory <path to codegen installation>/codegen-sdk/src/codegen/cli/mcp run server.py
37+
```

0 commit comments

Comments
 (0)