@cyanheads/git-mcp-server 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ /**
2
+ * Git MCP Server
3
+ * =============
4
+ *
5
+ * Main implementation of the Git MCP server.
6
+ */
7
+ import { execSync } from 'child_process';
8
+ import fs from 'fs';
9
+ import path from 'path';
10
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
11
+ // Get the directory path of the current module
12
+ import { fileURLToPath } from 'url';
13
+ import { registerAllResources } from './resources/index.js';
14
+ import { registerAllTools } from './tools/index.js';
15
+ /**
16
+ * Git MCP Server class
17
+ *
18
+ * This class creates and manages an MCP server that exposes Git functionality
19
+ * through the Model Context Protocol, making it accessible to AI assistants
20
+ * and other MCP clients.
21
+ */
22
+ export class GitMcpServer {
23
+ server;
24
+ /**
25
+ * Reads the package.json file to get metadata
26
+ */
27
+ getPackageInfo() {
28
+ // Get current file's directory and navigate to the project root
29
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
30
+ const packagePath = path.resolve(__dirname, '../package.json');
31
+ const packageContent = fs.readFileSync(packagePath, 'utf8');
32
+ return JSON.parse(packageContent);
33
+ }
34
+ /**
35
+ * Creates a new GitMcpServer instance
36
+ */
37
+ constructor() {
38
+ // Set up git config with global user settings
39
+ this.setupGitConfig();
40
+ // Get package info
41
+ const pkg = this.getPackageInfo();
42
+ // Initialize MCP server
43
+ this.server = new McpServer({
44
+ name: pkg.name,
45
+ version: pkg.version,
46
+ description: pkg.description
47
+ });
48
+ // Register all resources and tools
49
+ this.registerHandlers();
50
+ // Set up error handling
51
+ this.setupErrorHandling();
52
+ }
53
+ /**
54
+ * Sets up git config by setting environment variables for consistent author identity
55
+ * This ensures all git operations use the global git configuration
56
+ */
57
+ setupGitConfig() {
58
+ try {
59
+ // Get global git config values
60
+ const globalUserName = execSync('git config --global user.name').toString().trim();
61
+ const globalUserEmail = execSync('git config --global user.email').toString().trim();
62
+ // Set environment variables for git to use
63
+ // These variables will override any other configuration
64
+ process.env.GIT_AUTHOR_NAME = globalUserName;
65
+ process.env.GIT_AUTHOR_EMAIL = globalUserEmail;
66
+ process.env.GIT_COMMITTER_NAME = globalUserName;
67
+ process.env.GIT_COMMITTER_EMAIL = globalUserEmail;
68
+ console.error(`[Git MCP Server] Setting up git author identity: ${globalUserName} <${globalUserEmail}>`);
69
+ }
70
+ catch (error) {
71
+ console.error('Failed to set up git config:', error);
72
+ }
73
+ }
74
+ /**
75
+ * Registers all resource and tool handlers with the server
76
+ */
77
+ registerHandlers() {
78
+ // Register all resources (for providing Git data)
79
+ registerAllResources(this.server);
80
+ // Register all tools (for executing Git commands)
81
+ registerAllTools(this.server);
82
+ // Register resource descriptions
83
+ this.registerResourceDescriptions();
84
+ }
85
+ /**
86
+ * Registers resource descriptions for better client displays
87
+ */
88
+ registerResourceDescriptions() {
89
+ // This is a placeholder for resource descriptions
90
+ // In MCP SDK, descriptions need to be specified at resource registration time
91
+ // The actual descriptions are now defined in descriptors.ts and can be used
92
+ // by the individual resource registration methods
93
+ console.error('Resource descriptions are provided during resource registration');
94
+ }
95
+ /**
96
+ * Sets up global error handling for the server
97
+ */
98
+ setupErrorHandling() {
99
+ // Error handling will be done with try-catch in methods that can fail
100
+ process.on('uncaughtException', (error) => {
101
+ console.error(`[Git MCP Server Uncaught Exception] ${error.message}`);
102
+ console.error(error.stack);
103
+ });
104
+ process.on('unhandledRejection', (reason) => {
105
+ console.error(`[Git MCP Server Unhandled Rejection] ${reason instanceof Error ? reason.message : String(reason)}`);
106
+ if (reason instanceof Error && reason.stack) {
107
+ console.error(reason.stack);
108
+ }
109
+ });
110
+ }
111
+ /**
112
+ * Connects the server to a transport
113
+ *
114
+ * @param transport - MCP transport to connect to
115
+ * @returns Promise that resolves when connected
116
+ */
117
+ async connect(transport) {
118
+ await this.server.connect(transport);
119
+ }
120
+ }
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Error Handling Service
3
+ * ======================
4
+ *
5
+ * Standardized error handling for Git operations and MCP server.
6
+ */
7
+ /**
8
+ * Standardized error category classification
9
+ */
10
+ export const ErrorCategoryType = {
11
+ CATEGORY_VALIDATION: 'VALIDATION',
12
+ CATEGORY_GIT: 'GIT',
13
+ CATEGORY_MCP: 'MCP',
14
+ CATEGORY_SYSTEM: 'SYSTEM',
15
+ CATEGORY_UNKNOWN: 'UNKNOWN'
16
+ };
17
+ /**
18
+ * Error severity classification
19
+ */
20
+ export const ErrorSeverityLevel = {
21
+ SEVERITY_DEBUG: 0,
22
+ SEVERITY_INFO: 1,
23
+ SEVERITY_WARN: 2,
24
+ SEVERITY_ERROR: 3,
25
+ SEVERITY_FATAL: 4
26
+ };
27
+ /**
28
+ * Creates a standardized success result
29
+ */
30
+ export function createSuccessResult(data) {
31
+ return { resultSuccessful: true, resultData: data };
32
+ }
33
+ /**
34
+ * Creates a standardized failure result
35
+ */
36
+ export function createFailureResult(error) {
37
+ return { resultSuccessful: false, resultError: error };
38
+ }
39
+ /**
40
+ * Creates a standardized error object
41
+ */
42
+ export function createStandardizedError(message, code, category, severity, context = {}) {
43
+ return {
44
+ errorMessage: message,
45
+ errorCode: code,
46
+ errorCategory: category,
47
+ errorSeverity: severity,
48
+ errorTimestamp: new Date().toISOString(),
49
+ errorContext: context
50
+ };
51
+ }
52
+ /**
53
+ * Converts an exception to a standardized error object
54
+ */
55
+ export function wrapExceptionAsStandardizedError(exception, defaultMessage) {
56
+ const errorMessage = exception instanceof Error ? exception.message : defaultMessage;
57
+ const errorStack = exception instanceof Error ? exception.stack : undefined;
58
+ return {
59
+ errorMessage,
60
+ errorCode: 'UNEXPECTED_ERROR',
61
+ errorCategory: ErrorCategoryType.CATEGORY_UNKNOWN,
62
+ errorSeverity: ErrorSeverityLevel.SEVERITY_ERROR,
63
+ errorTimestamp: new Date().toISOString(),
64
+ errorContext: { originalException: exception },
65
+ errorStack
66
+ };
67
+ }
68
+ /**
69
+ * Handles Git-specific errors and converts them to standardized format
70
+ */
71
+ export function createGitError(message, code, context = {}) {
72
+ return createStandardizedError(message, code, ErrorCategoryType.CATEGORY_GIT, ErrorSeverityLevel.SEVERITY_ERROR, context);
73
+ }