@cyanheads/git-mcp-server 1.2.4 → 2.0.2

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.
Files changed (105) hide show
  1. package/README.md +172 -285
  2. package/dist/config/index.js +69 -0
  3. package/dist/index.js +135 -0
  4. package/dist/mcp-server/server.js +572 -0
  5. package/dist/mcp-server/tools/gitAdd/index.js +7 -0
  6. package/dist/mcp-server/tools/gitAdd/logic.js +118 -0
  7. package/dist/mcp-server/tools/gitAdd/registration.js +73 -0
  8. package/dist/mcp-server/tools/gitBranch/index.js +7 -0
  9. package/dist/mcp-server/tools/gitBranch/logic.js +180 -0
  10. package/dist/mcp-server/tools/gitBranch/registration.js +72 -0
  11. package/dist/mcp-server/tools/gitCheckout/index.js +6 -0
  12. package/dist/mcp-server/tools/gitCheckout/logic.js +165 -0
  13. package/dist/mcp-server/tools/gitCheckout/registration.js +78 -0
  14. package/dist/mcp-server/tools/gitCherryPick/index.js +7 -0
  15. package/dist/mcp-server/tools/gitCherryPick/logic.js +115 -0
  16. package/dist/mcp-server/tools/gitCherryPick/registration.js +69 -0
  17. package/dist/mcp-server/tools/gitClean/index.js +7 -0
  18. package/dist/mcp-server/tools/gitClean/logic.js +110 -0
  19. package/dist/mcp-server/tools/gitClean/registration.js +98 -0
  20. package/dist/mcp-server/tools/gitClearWorkingDir/index.js +7 -0
  21. package/dist/mcp-server/tools/gitClearWorkingDir/logic.js +35 -0
  22. package/dist/mcp-server/tools/gitClearWorkingDir/registration.js +73 -0
  23. package/dist/mcp-server/tools/gitClone/index.js +7 -0
  24. package/dist/mcp-server/tools/gitClone/logic.js +136 -0
  25. package/dist/mcp-server/tools/gitClone/registration.js +44 -0
  26. package/dist/mcp-server/tools/gitCommit/index.js +7 -0
  27. package/dist/mcp-server/tools/gitCommit/logic.js +129 -0
  28. package/dist/mcp-server/tools/gitCommit/registration.js +100 -0
  29. package/dist/mcp-server/tools/gitDiff/index.js +6 -0
  30. package/dist/mcp-server/tools/gitDiff/logic.js +114 -0
  31. package/dist/mcp-server/tools/gitDiff/registration.js +74 -0
  32. package/dist/mcp-server/tools/gitFetch/index.js +6 -0
  33. package/dist/mcp-server/tools/gitFetch/logic.js +116 -0
  34. package/dist/mcp-server/tools/gitFetch/registration.js +71 -0
  35. package/dist/mcp-server/tools/gitInit/index.js +7 -0
  36. package/dist/mcp-server/tools/gitInit/logic.js +117 -0
  37. package/dist/mcp-server/tools/gitInit/registration.js +44 -0
  38. package/dist/mcp-server/tools/gitLog/index.js +6 -0
  39. package/dist/mcp-server/tools/gitLog/logic.js +148 -0
  40. package/dist/mcp-server/tools/gitLog/registration.js +71 -0
  41. package/dist/mcp-server/tools/gitMerge/index.js +7 -0
  42. package/dist/mcp-server/tools/gitMerge/logic.js +160 -0
  43. package/dist/mcp-server/tools/gitMerge/registration.js +77 -0
  44. package/dist/mcp-server/tools/gitPull/index.js +6 -0
  45. package/dist/mcp-server/tools/gitPull/logic.js +144 -0
  46. package/dist/mcp-server/tools/gitPull/registration.js +81 -0
  47. package/dist/mcp-server/tools/gitPush/index.js +6 -0
  48. package/dist/mcp-server/tools/gitPush/logic.js +188 -0
  49. package/dist/mcp-server/tools/gitPush/registration.js +81 -0
  50. package/dist/mcp-server/tools/gitRebase/index.js +7 -0
  51. package/dist/mcp-server/tools/gitRebase/logic.js +171 -0
  52. package/dist/mcp-server/tools/gitRebase/registration.js +72 -0
  53. package/dist/mcp-server/tools/gitRemote/index.js +7 -0
  54. package/dist/mcp-server/tools/gitRemote/logic.js +158 -0
  55. package/dist/mcp-server/tools/gitRemote/registration.js +76 -0
  56. package/dist/mcp-server/tools/gitReset/index.js +6 -0
  57. package/dist/mcp-server/tools/gitReset/logic.js +116 -0
  58. package/dist/mcp-server/tools/gitReset/registration.js +71 -0
  59. package/dist/mcp-server/tools/gitSetWorkingDir/index.js +7 -0
  60. package/dist/mcp-server/tools/gitSetWorkingDir/logic.js +91 -0
  61. package/dist/mcp-server/tools/gitSetWorkingDir/registration.js +78 -0
  62. package/dist/mcp-server/tools/gitShow/index.js +7 -0
  63. package/dist/mcp-server/tools/gitShow/logic.js +99 -0
  64. package/dist/mcp-server/tools/gitShow/registration.js +83 -0
  65. package/dist/mcp-server/tools/gitStash/index.js +7 -0
  66. package/dist/mcp-server/tools/gitStash/logic.js +161 -0
  67. package/dist/mcp-server/tools/gitStash/registration.js +84 -0
  68. package/dist/mcp-server/tools/gitStatus/index.js +7 -0
  69. package/dist/mcp-server/tools/gitStatus/logic.js +215 -0
  70. package/dist/mcp-server/tools/gitStatus/registration.js +77 -0
  71. package/dist/mcp-server/tools/gitTag/index.js +7 -0
  72. package/dist/mcp-server/tools/gitTag/logic.js +142 -0
  73. package/dist/mcp-server/tools/gitTag/registration.js +84 -0
  74. package/dist/types-global/errors.js +68 -0
  75. package/dist/types-global/mcp.js +59 -0
  76. package/dist/types-global/tool.js +1 -0
  77. package/dist/utils/errorHandler.js +237 -0
  78. package/dist/utils/idGenerator.js +148 -0
  79. package/dist/utils/index.js +11 -0
  80. package/dist/utils/jsonParser.js +78 -0
  81. package/dist/utils/logger.js +266 -0
  82. package/dist/utils/rateLimiter.js +177 -0
  83. package/dist/utils/requestContext.js +49 -0
  84. package/dist/utils/sanitization.js +371 -0
  85. package/dist/utils/tokenCounter.js +124 -0
  86. package/package.json +62 -17
  87. package/build/index.js +0 -54
  88. package/build/resources/descriptors.js +0 -77
  89. package/build/resources/diff.js +0 -241
  90. package/build/resources/file.js +0 -222
  91. package/build/resources/history.js +0 -242
  92. package/build/resources/index.js +0 -99
  93. package/build/resources/repository.js +0 -286
  94. package/build/server.js +0 -120
  95. package/build/services/error-service.js +0 -73
  96. package/build/services/git-service.js +0 -965
  97. package/build/tools/advanced.js +0 -526
  98. package/build/tools/branch.js +0 -296
  99. package/build/tools/index.js +0 -29
  100. package/build/tools/remote.js +0 -279
  101. package/build/tools/repository.js +0 -170
  102. package/build/tools/workdir.js +0 -445
  103. package/build/types/git.js +0 -7
  104. package/build/utils/global-settings.js +0 -64
  105. package/build/utils/validation.js +0 -108
@@ -0,0 +1,69 @@
1
+ import dotenv from "dotenv";
2
+ import { readFileSync } from "fs";
3
+ import { dirname, join } from "path";
4
+ import { fileURLToPath } from "url";
5
+ import { logger } from "../utils/logger.js"; // Import McpLogLevel and logger
6
+ dotenv.config(); // Load environment variables from .env file
7
+ // Determine the directory name of the current module
8
+ const __dirname = dirname(fileURLToPath(import.meta.url));
9
+ // Construct the path to package.json relative to the current file
10
+ const pkgPath = join(__dirname, '../../package.json');
11
+ // Default package information in case package.json is unreadable
12
+ let pkg = { name: 'mcp-ts-template', version: '0.0.0' };
13
+ try {
14
+ // Read and parse package.json to get server name and version
15
+ pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
16
+ }
17
+ catch (error) {
18
+ // Silently use default pkg info if reading fails. Error will be logged later if needed.
19
+ }
20
+ /**
21
+ * Main application configuration object.
22
+ * Aggregates settings from environment variables and package.json.
23
+ */
24
+ export const config = {
25
+ /** The name of the MCP server, derived from package.json. */
26
+ mcpServerName: pkg.name,
27
+ /** The version of the MCP server, derived from package.json. */
28
+ mcpServerVersion: pkg.version,
29
+ /** Logging level for the application (e.g., "debug", "info", "warning", "error"). Defaults to "info". */
30
+ logLevel: process.env.MCP_LOG_LEVEL || "info", // Use MCP_LOG_LEVEL consistently
31
+ /** The runtime environment (e.g., "development", "production"). Defaults to "development". */
32
+ environment: process.env.NODE_ENV || "development",
33
+ /** Security-related configurations. */
34
+ security: {
35
+ // Placeholder for security settings
36
+ // Example: authRequired: process.env.AUTH_REQUIRED === 'true'
37
+ /** Indicates if authentication is required for server operations. */
38
+ authRequired: false,
39
+ }
40
+ // Note: mcpClient configuration is now loaded separately from mcp-config.json
41
+ };
42
+ /**
43
+ * The configured logging level for the application.
44
+ * Exported separately for convenience (e.g., logger initialization).
45
+ * @type {string}
46
+ */
47
+ export const logLevel = config.logLevel;
48
+ /**
49
+ * The configured runtime environment for the application.
50
+ * Exported separately for convenience.
51
+ * @type {string}
52
+ */
53
+ export const environment = config.environment;
54
+ // Define valid MCP log levels based on the logger's type definition
55
+ const validMcpLogLevels = ['debug', 'info', 'notice', 'warning', 'error', 'crit', 'alert', 'emerg'];
56
+ // Validate the configured log level
57
+ let validatedMcpLogLevel = 'info'; // Default to 'info'
58
+ if (validMcpLogLevels.includes(logLevel)) {
59
+ validatedMcpLogLevel = logLevel;
60
+ }
61
+ else {
62
+ // Silently default to 'info' if the configured level is invalid.
63
+ // The logger initialization message will show the actual level being used.
64
+ }
65
+ // Initialize the logger with the validated MCP level AFTER config is defined.
66
+ logger.initialize(validatedMcpLogLevel);
67
+ // Log initialization message using the logger itself (will go to file and potentially MCP)
68
+ logger.info(`Logger initialized. MCP logging level: ${validatedMcpLogLevel}`);
69
+ logger.debug("Configuration loaded successfully", { config }); // Log loaded config at debug level
package/dist/index.js ADDED
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env node
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; // Import McpServer type
3
+ import { config, environment } from "./config/index.js";
4
+ import { initializeAndStartServer } from "./mcp-server/server.js"; // Updated import
5
+ import { logger } from "./utils/logger.js";
6
+ // Import the service instance instead of the standalone function
7
+ import { requestContextService } from "./utils/requestContext.js";
8
+ /**
9
+ * The main MCP server instance.
10
+ * @type {McpServer | undefined}
11
+ */
12
+ let server;
13
+ /**
14
+ * Gracefully shuts down the main MCP server.
15
+ * Handles process termination signals (SIGTERM, SIGINT) and critical errors.
16
+ *
17
+ * @param signal - The signal or event name that triggered the shutdown (e.g., "SIGTERM", "uncaughtException").
18
+ */
19
+ const shutdown = async (signal) => {
20
+ // Define context for the shutdown operation
21
+ const shutdownContext = {
22
+ operation: 'Shutdown',
23
+ signal,
24
+ };
25
+ logger.info(`Received ${signal}. Starting graceful shutdown...`, shutdownContext);
26
+ try {
27
+ // Close the main MCP server
28
+ if (server) {
29
+ logger.info("Closing main MCP server...", shutdownContext);
30
+ await server.close();
31
+ logger.info("Main MCP server closed successfully", shutdownContext);
32
+ }
33
+ else {
34
+ logger.warning("Server instance not found during shutdown.", shutdownContext);
35
+ }
36
+ logger.info("Graceful shutdown completed successfully", shutdownContext);
37
+ process.exit(0);
38
+ }
39
+ catch (error) {
40
+ // Handle any errors during shutdown
41
+ logger.error("Critical error during shutdown", {
42
+ ...shutdownContext,
43
+ error: error instanceof Error ? error.message : String(error),
44
+ stack: error instanceof Error ? error.stack : undefined
45
+ });
46
+ process.exit(1); // Exit with error code if shutdown fails
47
+ }
48
+ };
49
+ /**
50
+ * Initializes and starts the main MCP server.
51
+ * Sets up request context, initializes the server instance, starts the transport,
52
+ * and registers signal handlers for graceful shutdown and error handling.
53
+ */
54
+ const start = async () => {
55
+ // Create application-level request context using the service instance
56
+ const transportType = (process.env.MCP_TRANSPORT_TYPE || 'stdio').toLowerCase();
57
+ const startupContext = requestContextService.createRequestContext({
58
+ operation: `ServerStartup_${transportType}`, // Include transport in operation name
59
+ appName: config.mcpServerName,
60
+ appVersion: config.mcpServerVersion,
61
+ environment: environment
62
+ });
63
+ logger.info(`Starting ${config.mcpServerName} v${config.mcpServerVersion} (Transport: ${transportType})...`, startupContext);
64
+ try {
65
+ // Initialize the server instance and start the selected transport
66
+ logger.debug("Initializing and starting MCP server transport", startupContext);
67
+ // Start the server transport. For stdio, this returns the server instance.
68
+ // For http, it sets up the listener and returns void (or potentially the http.Server).
69
+ // We only need to store the instance for stdio shutdown.
70
+ const potentialServerInstance = await initializeAndStartServer();
71
+ if (transportType === 'stdio' && potentialServerInstance instanceof McpServer) {
72
+ server = potentialServerInstance; // Store only for stdio
73
+ }
74
+ else {
75
+ // For HTTP, server instances are managed per-session in server.ts
76
+ // The main http server listener keeps the process alive.
77
+ // Shutdown for HTTP needs to handle closing the main http server.
78
+ // We might need to return the httpServer from initializeAndStartServer if
79
+ // we want to close it explicitly here during shutdown.
80
+ // For now, we don't store anything globally for HTTP transport.
81
+ }
82
+ // If initializeAndStartServer failed, it would have thrown an error,
83
+ // and execution would jump to the outer catch block.
84
+ logger.info(`${config.mcpServerName} is running with ${transportType} transport`, {
85
+ ...startupContext,
86
+ startTime: new Date().toISOString(),
87
+ });
88
+ // --- Signal and Error Handling Setup ---
89
+ // Handle process signals for graceful shutdown
90
+ process.on("SIGTERM", () => shutdown("SIGTERM"));
91
+ process.on("SIGINT", () => shutdown("SIGINT"));
92
+ // Handle uncaught exceptions
93
+ process.on("uncaughtException", async (error) => {
94
+ const errorContext = {
95
+ ...startupContext, // Include base context for correlation
96
+ event: 'uncaughtException',
97
+ error: error instanceof Error ? error.message : String(error),
98
+ stack: error instanceof Error ? error.stack : undefined
99
+ };
100
+ logger.error("Uncaught exception detected. Initiating shutdown...", errorContext);
101
+ // Attempt graceful shutdown; shutdown() handles its own errors.
102
+ await shutdown("uncaughtException");
103
+ // If shutdown fails internally, it will call process.exit(1).
104
+ // If shutdown succeeds, it calls process.exit(0).
105
+ // If shutdown itself throws unexpectedly *before* exiting, this process might terminate abruptly,
106
+ // but the core shutdown logic is handled within shutdown().
107
+ });
108
+ // Handle unhandled promise rejections
109
+ process.on("unhandledRejection", async (reason) => {
110
+ const rejectionContext = {
111
+ ...startupContext, // Include base context for correlation
112
+ event: 'unhandledRejection',
113
+ reason: reason instanceof Error ? reason.message : String(reason),
114
+ stack: reason instanceof Error ? reason.stack : undefined
115
+ };
116
+ logger.error("Unhandled promise rejection detected. Initiating shutdown...", rejectionContext);
117
+ // Attempt graceful shutdown; shutdown() handles its own errors.
118
+ await shutdown("unhandledRejection");
119
+ // Similar logic as uncaughtException: shutdown handles its exit codes.
120
+ });
121
+ }
122
+ catch (error) {
123
+ // Handle critical startup errors (already logged by ErrorHandler or caught above)
124
+ // Log the final failure context, including error details, before exiting
125
+ logger.error("Critical error during startup, exiting.", {
126
+ ...startupContext,
127
+ finalErrorContext: 'Startup Failure',
128
+ error: error instanceof Error ? error.message : String(error),
129
+ stack: error instanceof Error ? error.stack : undefined,
130
+ });
131
+ process.exit(1);
132
+ }
133
+ };
134
+ // Start the application
135
+ start();