UNPKG

@nvhien20vn/git-mcp-server

Version:
68 lines (67 loc) 3.56 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 }); };