ai-demo/git_mcp_server/git_mcp_server.py

61 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Git MCP 服务器
整合所有 Git 工具为 MCP Server
设计说明:
- GitServer 继承自 McpServer提供 Git 相关的工具
- 在 __init__ 中直接实例化并赋值工具列表
- 工具定义在各自的文件中git_server 负责组装
"""
import asyncio
import logging
from base_mcp_server import BaseMcpServer
from git_clone_tool import GitCloneTool
from git_checkout_tool import GitCheckoutTool
from git_add_tool import GitAddTool
from git_commit_tool import GitCommitTool
from git_push_tool import GitPushTool
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler() # 输出到 stderr
]
)
logger = logging.getLogger(__name__)
class GitMcpServer(BaseMcpServer):
"""
Git MCP 服务器
继承 McpServer在 __init__ 中直接赋值 Git 工具实例列表。
"""
def __init__(self):
# 初始化父类,设置服务器名称和版本
super().__init__(server_name="git-mcp", version="1.0.0")
# 直接赋值工具实例列表
self.tools = [
GitCloneTool(),
GitCheckoutTool(),
GitAddTool(),
GitCommitTool(),
GitPushTool(),
]
async def main():
"""启动 Git MCP 服务器"""
logger.info("正在启动 Git MCP 服务器...")
server = GitMcpServer()
await server.run_stdio()
if __name__ == "__main__":
asyncio.run(main())