MCP 文本编辑器是一款基于文本的协作工具,借助 HTTP API 可实现文件的读取、写入和更新操作。它专为实时协作场景打造,支持多用户同时编辑同一文件,还具备冲突检测与解决机制,为协作编辑提供了可靠保障。
MCP 文本编辑器为需要实时协作编辑文件的场景提供了便捷的解决方案。通过 HTTP API,用户可以轻松地进行文件内容的获取、编辑等操作。
contents = await get_text_file_contents({
"files": [
{
"file_path": "file.txt",
"ranges": [{"start": 1, "end": null}] # 读取整个文件
}
]
})
result = await edit_text_file_contents({
"files": [
{
"path": "file.txt",
"hash": contents["file.txt"][0]["hash"],
"encoding": "utf-8", # 可选,默认为 "utf-8"
"patches": [
{
"line_start": 5,
"line_end": 8,
"contents": "新内容\n"
}
]
}
]
})
if result["file.txt"]["result"] == "error":
if "hash mismatch" in result["file.txt"]["reason"]:
# 文件被其他进程修改,获取最新内容并重试
pass
服务端会处理多种错误情况:
pip install requests
python tests.py
mcp-text-editor/
├── README.md # 项目说明文档
├── requirements.txt # 依赖管理文件
├── src/
│ ├── __init__.py # 包根目录初始化
│ ├── client.py # HTTP 客户端实现
│ └── server.py # HTTP 服务器实现
└── tests/
├── test_client.py # 客户端单元测试
└── test_server.py # 服务端集成测试
该项目遵循 MIT 许可证。请查看 LICENSE 文件获取详细信息。
git clone https://github.com/yourusername/mcp-text-editor.git
cd mcp-text-editor
git branch -b feature/new-feature
git add .
git commit -m "添加新功能"
git push origin feature/new-feature
请通过 GitHub Issues 提交 bug 或功能请求。
black . # 格式化代码
flake8 . # 检查代码风格
mypy . # 静态类型检查
import unittest
from src.client import MCPClient
class TestMCPClient(unittest.TestCase):
def setUp(self):
self.client = MCPClient('http://localhost:8000')
def test_get_file_contents(self):
result = self.client.get_file_contents('file.txt')
self.assertIsInstance(result, dict)
if __name__ == '__main__':
unittest.main()
import pytest
from src.server import MCPServer
def test_server_response():
server = MCPServer()
server.start()
assert 200 in server.responses