Skip to content

feat: [CG-10632] mcp server #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/introduction/ide-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,46 @@ Codegen creates a custom Python environment in `.codegen/.venv`. Configure your
</AccordionGroup>


## MCP Server Setup
This is an optional step but highly recommended if your IDE supports MCP support and you use AI Agents.
The MCP server is a local server that allows your AI Agent to interact with the Codegen specific tools,
it will allow an agent to:
- ask an expert to create a codemod
- improve a codemod
- get setup instructions

### Configuration
#### Usage with Cline:
Add this to your cline_mcp_settings.json:
```
{
"mcpServers": {
"codegen-cli": {
"command": "uv",
"args": [
"--directory",
"<path to codegen installation>/codegen-sdk/src/codegen/cli/mcp",
"run",
"server.py"
]
}
}
}
```


#### Usage with Cursor:
Under the `Settings` > `Feature` > `MCP Servers` section, click "Add New MCP Server" and add the following:

```
Name: codegen-mcp
Type: Command
Command: uv --directory <path to codegen installation>/codegen-sdk/src/codegen/cli/mcp run server.py
```




## Create a New Codemod

Generate the boilerplate for a new code manipulation program using [codegen create](/cli/create):
Expand Down
15 changes: 14 additions & 1 deletion src/codegen/cli/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DOCS_ENDPOINT,
EXPERT_ENDPOINT,
IDENTIFY_ENDPOINT,
IMPROVE_ENDPOINT,
LOOKUP_ENDPOINT,
PR_LOOKUP_ENDPOINT,
RUN_ENDPOINT,
Expand All @@ -27,6 +28,8 @@
DocsInput,
DocsResponse,
IdentifyResponse,
ImproveCodemodInput,
ImproveCodemodResponse,
LookupInput,
LookupOutput,
PRLookupInput,
Expand All @@ -42,6 +45,7 @@
from codegen.cli.env.global_env import global_env
from codegen.cli.errors import InvalidTokenError, ServerError
from codegen.cli.utils.codemods import Codemod
from codegen.cli.utils.constants import ProgrammingLanguage
from codegen.cli.utils.function_finder import DecoratedFunction

InputT = TypeVar("InputT", bound=BaseModel)
Expand All @@ -55,7 +59,7 @@

auth_token: str | None = None

def __init__(self, auth_token: str):
def __init__(self, auth_token: str | None = None):
self.auth_token = auth_token

def _get_headers(self) -> dict[str, str]:
Expand Down Expand Up @@ -144,7 +148,7 @@

# Add template context if provided
if template_context:
base_input["template_context"] = template_context

Check failure on line 151 in src/codegen/cli/api/client.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible types in assignment (expression has type "dict[str, str]", target has type "str") [assignment]

input_data = RunCodemodInput(input=RunCodemodInput.BaseRunCodemodInput(**base_input))
return self._make_request(
Expand All @@ -157,7 +161,7 @@
def get_docs(self) -> dict:
"""Search documentation."""
session = CodegenSession()
return self._make_request(

Check failure on line 164 in src/codegen/cli/api/client.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "DocsResponse", expected "dict[Any, Any]") [return-value]
"GET",
DOCS_ENDPOINT,
DocsInput(docs_input=DocsInput.BaseDocsInput(repo_full_name=session.repo_name)),
Expand Down Expand Up @@ -242,9 +246,18 @@

def lookup_pr(self, repo_full_name: str, github_pr_number: int) -> PRSchema:
"""Look up a PR by repository and PR number."""
return self._make_request(

Check failure on line 249 in src/codegen/cli/api/client.py

View workflow job for this annotation

GitHub Actions / mypy

error: Incompatible return value type (got "PRLookupResponse", expected "PRSchema") [return-value]
"GET",
PR_LOOKUP_ENDPOINT,
PRLookupInput(input=PRLookupInput.BasePRLookupInput(repo_full_name=repo_full_name, github_pr_number=github_pr_number)),
PRLookupResponse,
)

def improve_codemod(self, codemod: str, task: str, concerns: list[str], context: dict[str, str], language: ProgrammingLanguage) -> ImproveCodemodResponse:
"""Improve a codemod."""
return self._make_request(

Check warning on line 258 in src/codegen/cli/api/client.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/cli/api/client.py#L258

Added line #L258 was not covered by tests
"GET",
IMPROVE_ENDPOINT,
ImproveCodemodInput(input=ImproveCodemodInput.BaseImproveCodemodInput(codemod=codemod, task=task, concerns=concerns, context=context, language=language)),
ImproveCodemodResponse,
)
1 change: 1 addition & 0 deletions src/codegen/cli/api/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
RUN_ON_PR_ENDPOINT = f"https://{MODAL_PREFIX}--cli-run-on-pull-request.modal.run"
PR_LOOKUP_ENDPOINT = f"https://{MODAL_PREFIX}--cli-pr-lookup.modal.run"
CODEGEN_SYSTEM_PROMPT_URL = "https://gist.githubusercontent.com/jayhack/15681a2ceaccd726f19e6fdb3a44738b/raw/17c08054e3931b3b7fdf424458269c9e607541e8/codegen-system-prompt.txt"
IMPROVE_ENDPOINT = f"https://{MODAL_PREFIX}--cli-improve.modal.run"
21 changes: 21 additions & 0 deletions src/codegen/cli/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ class RunOnPRResponse(BaseModel):
codemod_id: int = Field(..., description="ID of the codemod")
codemod_run_id: int = Field(..., description="ID of the codemod run")
web_url: str = Field(..., description="URL to view the test results")


###########################################################################
# IMPROVE
###########################################################################


class ImproveCodemodInput(BaseModel):
class BaseImproveCodemodInput(BaseModel):
codemod: str = Field(..., description="Source code of the codemod to improve")
task: str = Field(..., description="Task to which the codemod should implement to solve")
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")
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.")
language: ProgrammingLanguage = Field(..., description="Language of the codemod")

input: BaseImproveCodemodInput = Field(..., description="Input data for improvement")


class ImproveCodemodResponse(BaseModel):
success: bool = Field(..., description="Whether the improvement was successful")
codemod_source: str = Field(..., description="Source code of the improved codemod")
40 changes: 40 additions & 0 deletions src/codegen/cli/mcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Codegen MCP server

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.

### Dependencies

- [fastmcp](https://github.com/codegen-sh/fastmcp)

## Usage

Most AI Agents that support MCP will have some way to configure the server startup.

### Cline

Add this to your `cline_mcp_settings.json` file to get started:

```
{
"mcpServers": {
"codegen-cli": {
"command": "uv",
"args": [
"--directory",
"<path to codegen installation>/codegen-sdk/src/codegen/cli/mcp",
"run",
"server.py"
]
}
}
}
```

Cursor:
Under the `Settings` > `Feature` > `MCP Servers` section, click "Add New MCP Server" and add the following:

```
Name: codegen-mcp
Type: Command
Command: uv --directory <path to codegen installation>/codegen-sdk/src/codegen/cli/mcp run server.py
```
Loading
Loading