|
| 1 | +# Codegen MCP (Model-Controller-Protocol) Server |
| 2 | + |
| 3 | +This module provides a standardized interface for AI models to interact with codebases. It implements a set of operations for manipulating symbols, functions, classes, imports, and AI-related functionality. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +The MCP server follows a Model-Controller-Protocol architecture: |
| 8 | + |
| 9 | +- **Models**: Data structures representing code elements (Symbol, Function, Class, Import, etc.) |
| 10 | +- **Controllers**: Business logic for operations on code elements |
| 11 | +- **Protocol**: Request/response handling and routing |
| 12 | + |
| 13 | +## Features |
| 14 | + |
| 15 | +The MCP server provides the following operations: |
| 16 | + |
| 17 | +### Symbol Operations |
| 18 | + |
| 19 | +- Get symbols by name or pattern |
| 20 | +- Check if a symbol exists |
| 21 | +- Get all symbols, functions, classes, imports, etc. |
| 22 | +- Get symbol usages |
| 23 | +- Move symbols between files |
| 24 | +- Rename symbols |
| 25 | +- Remove symbols |
| 26 | + |
| 27 | +### Function Operations |
| 28 | + |
| 29 | +- Get/set function return types |
| 30 | +- Get/modify function parameters |
| 31 | +- Check if a function is async |
| 32 | +- Get/add function decorators |
| 33 | +- Get function calls |
| 34 | +- Generate/set function docstrings |
| 35 | +- Rename local variables |
| 36 | +- Get function call sites and dependencies |
| 37 | + |
| 38 | +### Class Operations |
| 39 | + |
| 40 | +- Get class methods, properties, and attributes |
| 41 | +- Check if a class is abstract |
| 42 | +- Get parent class names |
| 43 | +- Check if a class is a subclass of another |
| 44 | +- Add/remove methods and attributes |
| 45 | +- Convert a class to a protocol |
| 46 | + |
| 47 | +### Import Operations |
| 48 | + |
| 49 | +- Get import sources |
| 50 | +- Update import sources |
| 51 | +- Remove imports |
| 52 | +- Rename imports |
| 53 | + |
| 54 | +### AI Operations |
| 55 | + |
| 56 | +- Set AI API keys |
| 57 | +- Configure AI session options |
| 58 | +- Call AI with prompts and context |
| 59 | +- Get AI client configuration |
| 60 | +- Use reflection to organize thoughts and plan next steps |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +### Starting the Server |
| 65 | + |
| 66 | +```bash |
| 67 | +python -m mcp.server --host localhost --port 8000 --repo /path/to/your/repo |
| 68 | +``` |
| 69 | + |
| 70 | +### Using the Client |
| 71 | + |
| 72 | +```python |
| 73 | +from mcp.client import MCPClient |
| 74 | + |
| 75 | +# Initialize the client |
| 76 | +client = MCPClient(host="localhost", port=8000) |
| 77 | + |
| 78 | +# Get all symbols in the codebase |
| 79 | +symbols = client.symbols() |
| 80 | + |
| 81 | +# Get a specific symbol |
| 82 | +symbol = client.get_symbol("MyClass") |
| 83 | + |
| 84 | +# Rename a symbol |
| 85 | +client.symbol_rename("old_name", "new_name") |
| 86 | + |
| 87 | +# Call AI to generate code |
| 88 | +response = client.ai("Generate a function to calculate factorial", |
| 89 | + target={"name": "math_utils.py"}) |
| 90 | + |
| 91 | +# Use reflection to organize thoughts and plan next steps |
| 92 | +reflection = client.reflection( |
| 93 | + context_summary="Refactoring a large codebase", |
| 94 | + findings_so_far="Found many circular dependencies", |
| 95 | + current_challenges="How to break dependencies safely?", |
| 96 | + reflection_focus="architecture" |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +## AI Tools |
| 101 | + |
| 102 | +The MCP server provides powerful AI tools to assist with code generation, analysis, and planning: |
| 103 | + |
| 104 | +### Codebase AI |
| 105 | + |
| 106 | +The `codebase.ai` tool provides the following capabilities: |
| 107 | + |
| 108 | +- Code generation based on natural language prompts |
| 109 | +- Code analysis and improvement suggestions |
| 110 | +- Contextual modifications to existing code |
| 111 | +- Documentation generation |
| 112 | + |
| 113 | +Parameters: |
| 114 | +- `prompt`: The instruction or query for the AI |
| 115 | +- `target` (optional): The code to modify or analyze |
| 116 | +- `context` (optional): Additional information to help the AI understand the task |
| 117 | +- `model` (optional): The specific AI model to use |
| 118 | + |
| 119 | +### Reflection |
| 120 | + |
| 121 | +The `reflection` tool helps organize thoughts and plan next steps when working on complex problems. It's particularly useful for: |
| 122 | + |
| 123 | +- Consolidating information from multiple sources |
| 124 | +- Identifying knowledge gaps |
| 125 | +- Creating a strategic plan |
| 126 | +- Breaking down complex problems into manageable steps |
| 127 | + |
| 128 | +Parameters: |
| 129 | +- `context_summary`: Summary of the current context and problem being solved |
| 130 | +- `findings_so_far`: Key information and insights gathered so far |
| 131 | +- `current_challenges` (optional): Current obstacles or questions that need to be addressed |
| 132 | +- `reflection_focus` (optional): Specific aspect to focus reflection on (e.g., 'architecture', 'performance', 'next steps') |
| 133 | + |
| 134 | +See the [AI Tools Examples](examples/ai_tools/) for detailed usage examples. |
| 135 | + |
| 136 | +## API Reference |
| 137 | + |
| 138 | +See the docstrings in the code for detailed API documentation. |
| 139 | + |
| 140 | +## License |
| 141 | + |
| 142 | +This project is licensed under the same license as the Codegen project. |
0 commit comments