这是一个使用Osmosis-Apply-1.7B模型来合并代码编辑的Model Context Protocol (MCP)服务器。
ollama pull Osmosis/Osmosis-Apply-1.7Bgit clone <repository-url>
cd codemerge
uv sync
uv run python codemerge.py
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 = """def hello():
print('Hello, World!')
print('Some code')
print('Some more code')
return 'success'"""
# 要应用的编辑
edit_snippet = """def hello():
print('Hello, Universe!')
print('Some code')
# ... existing code ...
return 'success'"""
# 应用编辑
result = apply_code_edit(original_code, edit_snippet)
print(result)
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:Model Context Protocol实现ollama:Ollama的Python客户端(单独安装)CodeMerge由几个组件组成:
codemerge.py:主要的MCP服务器实现call_snippet.py:具有流支持的直接使用接口test_codemerge.py:全面的测试套件pyproject.toml:项目配置和依赖项