代码合并
一种模型上下文协议(MCP)服务器,使用Osmosis-Apply-1.7B模型合并代码编辑。
- 代码合并:使用Osmosi/Osmosis-Apply-1.7B模型应用编辑
- MCP集成:作为模型上下文协议服务器,集成到现有的AI IDE解决方案中
- 文件操作:可以直接就地编辑文件或返回编辑后的代码
安装
先决条件
- Python 3.10或更高版本
- 没有 已安装并正在运行
- Osmosis模型得出:
ollama pull Osmosis/Osmosis-Apply-1.7B
设置
- 克隆存储库:
git clone
cd codemerge- 使用uv安装依赖项:
uv sync- 运行:
uv run python codemerge.py用法
作为MCP服务器
CodeMerge可以用作MCP服务器。在MCP客户端中配置它:
{
"mcpServers": {
"codemerge": {
"command": "uv",
"args": [
"--directory",
"/path/to/codemerge",
"run",
"codemerge.py"
]
}
}
}直接使用
你也可以在Python中直接使用CodeMerge:
from call_snippet import apply_code_edit
# Original code
original_code = """def hello():
print('Hello, World!')
print('Some code')
print('Some more code')
return 'success'"""
# Edit to apply
edit_snippet = """def hello():
print('Hello, Universe!')
print('Some code')
# ... existing code ...
return 'success'"""
# Apply the edit
result = apply_code_edit(original_code, edit_snippet)
print(result)MCP工具使用
MCP服务器提供 edit_snippet 工具具有以下参数:
original_code:要编辑的确切原始代码edit_snippet:要应用的编辑,使用// ... existing code ...标记物file_path:要就地更新的文件的绝对路径
示例工具调用:
{
"name": "edit_snippet",
"arguments": {
"original_code": "def hello():\n print('Hello, World!')",
"edit_snippet": "def hello():\n print('Hello, Universe!')\n # ... existing code ...",
"file_path": "/path/to/your/file.py"
}
}编辑格式
CodeMerge使用特定的编辑格式,在保持简洁的同时保留上下文:
// ... existing code ...
FIRST_EDIT
// ... existing code ...
SECOND_EDIT
// ... existing code ...
THIRD_EDIT
// ... existing code ...这 // ... existing code ... 标记指示应保留原始代码的位置。这允许您仅指定要更改的部分,同时保持原始代码的确切结构和格式。
依赖项
httpx>=0.28.1:API调用的HTTP客户端mcp[cli]>=1.10.1:模型上下文协议实现ollama:Ollama的Python客户端(单独安装)
建筑
CodeMerge由几个组件组成:
codemerge.py:主MCP服务器实现call_snippet.py:具有流媒体支持的直接使用界面test_codemerge.py:全面的测试套件pyproject.toml:项目配置和依赖关系
