UNPKG

@cyanheads/git-mcp-server

Version:

An MCP (Model Context Protocol) server enabling LLMs and AI agents to interact with Git repositories. Provides tools for comprehensive Git operations including clone, commit, branch, diff, log, status, push, pull, merge, rebase, worktree, tag management,

71 lines (70 loc) 3.61 kB
import { logger, ErrorHandler, requestContextService, } from "../../../utils/index.js"; import { BaseErrorCode } from "../../../types-global/errors.js"; import { GitWorktreeBaseSchema, gitWorktreeLogic, } from "./logic.js"; let _getWorkingDirectory; let _getSessionId; /** * Initializes the state accessors needed by the git_worktree tool registration. * @param getWdFn - Function to get the working directory for a session. * @param getSidFn - Function to get the session ID from context. */ export function initializeGitWorktreeStateAccessors(getWdFn, getSidFn) { _getWorkingDirectory = getWdFn; _getSessionId = getSidFn; logger.info("State accessors initialized for git_worktree tool registration."); } const TOOL_NAME = "git_worktree"; const TOOL_DESCRIPTION = "Manages Git worktrees. Supports listing, adding, removing, moving, and pruning worktrees. Returns results as a JSON object."; /** * Registers the git_worktree tool with the MCP server. * * @param {McpServer} server - The McpServer instance to register the tool with. * @returns {Promise<void>} * @throws {Error} If registration fails or state accessors are not initialized. */ export const registerGitWorktreeTool = async (server) => { if (!_getWorkingDirectory || !_getSessionId) { throw new Error("State accessors for git_worktree must be initialized before registration."); } const operation = "registerGitWorktreeTool"; const context = requestContextService.createRequestContext({ operation }); await ErrorHandler.tryCatch(async () => { server.tool(TOOL_NAME, TOOL_DESCRIPTION, GitWorktreeBaseSchema.shape, async (validatedArgs, callContext) => { const toolInput = validatedArgs; // Cast for use const toolOperation = `tool:${TOOL_NAME}:${toolInput.mode}`; const requestContext = requestContextService.createRequestContext({ operation: toolOperation, parentContext: callContext, }); const sessionId = _getSessionId(requestContext); const getWorkingDirectoryForSession = () => _getWorkingDirectory(sessionId); const logicContext = { ...requestContext, sessionId: sessionId, getWorkingDirectory: getWorkingDirectoryForSession, }; logger.info(`Executing tool: ${TOOL_NAME} (mode: ${toolInput.mode})`, logicContext); return await ErrorHandler.tryCatch(async () => { const worktreeResult = await gitWorktreeLogic(toolInput, logicContext); const resultContent = { type: "text", text: JSON.stringify(worktreeResult, null, 2), // Pretty-print JSON contentType: "application/json", }; if (worktreeResult.success) { logger.info(`Tool ${TOOL_NAME} (mode: ${toolInput.mode}) executed successfully, returning JSON`, logicContext); } else { logger.warning(`Tool ${TOOL_NAME} (mode: ${toolInput.mode}) failed: ${worktreeResult.message}`, { ...logicContext, errorDetails: worktreeResult.error }); } return { content: [resultContent] }; }, { operation: toolOperation, context: logicContext, input: validatedArgs, errorCode: BaseErrorCode.INTERNAL_ERROR, }); }); logger.info(`Tool registered: ${TOOL_NAME}`, context); }, { operation, context, critical: true }); };