@cyanheads/git-mcp-server 2.0.1 → 2.0.3

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 (99) hide show
  1. package/README.md +55 -89
  2. package/{build → dist}/config/index.js +16 -18
  3. package/{build → dist}/index.js +80 -30
  4. package/dist/mcp-server/server.js +296 -0
  5. package/{build → dist}/mcp-server/tools/gitAdd/logic.js +9 -6
  6. package/{build → dist}/mcp-server/tools/gitAdd/registration.js +7 -4
  7. package/{build → dist}/mcp-server/tools/gitBranch/logic.js +23 -12
  8. package/{build → dist}/mcp-server/tools/gitBranch/registration.js +8 -5
  9. package/{build → dist}/mcp-server/tools/gitCheckout/logic.js +92 -44
  10. package/{build → dist}/mcp-server/tools/gitCheckout/registration.js +8 -5
  11. package/{build → dist}/mcp-server/tools/gitCherryPick/logic.js +10 -7
  12. package/{build → dist}/mcp-server/tools/gitCherryPick/registration.js +8 -5
  13. package/{build → dist}/mcp-server/tools/gitClean/logic.js +9 -6
  14. package/{build → dist}/mcp-server/tools/gitClean/registration.js +8 -5
  15. package/{build → dist}/mcp-server/tools/gitClearWorkingDir/logic.js +3 -2
  16. package/{build → dist}/mcp-server/tools/gitClearWorkingDir/registration.js +7 -4
  17. package/{build → dist}/mcp-server/tools/gitClone/logic.js +8 -5
  18. package/{build → dist}/mcp-server/tools/gitClone/registration.js +7 -4
  19. package/dist/mcp-server/tools/gitCommit/logic.js +207 -0
  20. package/{build → dist}/mcp-server/tools/gitCommit/registration.js +22 -15
  21. package/{build → dist}/mcp-server/tools/gitDiff/logic.js +9 -6
  22. package/{build → dist}/mcp-server/tools/gitDiff/registration.js +8 -5
  23. package/{build → dist}/mcp-server/tools/gitFetch/logic.js +10 -7
  24. package/{build → dist}/mcp-server/tools/gitFetch/registration.js +8 -5
  25. package/{build → dist}/mcp-server/tools/gitInit/index.js +2 -2
  26. package/{build → dist}/mcp-server/tools/gitInit/logic.js +9 -6
  27. package/dist/mcp-server/tools/gitInit/registration.js +98 -0
  28. package/{build → dist}/mcp-server/tools/gitLog/logic.js +53 -16
  29. package/{build → dist}/mcp-server/tools/gitLog/registration.js +8 -5
  30. package/{build → dist}/mcp-server/tools/gitMerge/logic.js +9 -6
  31. package/{build → dist}/mcp-server/tools/gitMerge/registration.js +8 -5
  32. package/{build → dist}/mcp-server/tools/gitPull/logic.js +11 -8
  33. package/{build → dist}/mcp-server/tools/gitPull/registration.js +7 -4
  34. package/{build → dist}/mcp-server/tools/gitPush/logic.js +12 -9
  35. package/{build → dist}/mcp-server/tools/gitPush/registration.js +7 -4
  36. package/{build → dist}/mcp-server/tools/gitRebase/logic.js +9 -6
  37. package/{build → dist}/mcp-server/tools/gitRebase/registration.js +8 -5
  38. package/{build → dist}/mcp-server/tools/gitRemote/logic.js +4 -5
  39. package/{build → dist}/mcp-server/tools/gitRemote/registration.js +2 -4
  40. package/{build → dist}/mcp-server/tools/gitReset/logic.js +5 -6
  41. package/{build → dist}/mcp-server/tools/gitReset/registration.js +2 -4
  42. package/{build → dist}/mcp-server/tools/gitSetWorkingDir/logic.js +5 -6
  43. package/{build → dist}/mcp-server/tools/gitSetWorkingDir/registration.js +22 -13
  44. package/{build → dist}/mcp-server/tools/gitShow/logic.js +5 -6
  45. package/{build → dist}/mcp-server/tools/gitShow/registration.js +3 -5
  46. package/{build → dist}/mcp-server/tools/gitStash/logic.js +5 -6
  47. package/{build → dist}/mcp-server/tools/gitStash/registration.js +3 -5
  48. package/{build → dist}/mcp-server/tools/gitStatus/logic.js +5 -6
  49. package/{build → dist}/mcp-server/tools/gitStatus/registration.js +2 -4
  50. package/{build → dist}/mcp-server/tools/gitTag/logic.js +3 -4
  51. package/{build → dist}/mcp-server/tools/gitTag/registration.js +2 -4
  52. package/dist/mcp-server/transports/authentication/authMiddleware.js +145 -0
  53. package/dist/mcp-server/transports/httpTransport.js +432 -0
  54. package/dist/mcp-server/transports/stdioTransport.js +87 -0
  55. package/{build → dist}/types-global/errors.js +2 -2
  56. package/dist/utils/index.js +12 -0
  57. package/{build/utils → dist/utils/internal}/errorHandler.js +18 -8
  58. package/dist/utils/internal/index.js +3 -0
  59. package/dist/utils/internal/logger.js +254 -0
  60. package/{build/utils → dist/utils/internal}/requestContext.js +2 -3
  61. package/dist/utils/metrics/index.js +1 -0
  62. package/{build/utils → dist/utils/metrics}/tokenCounter.js +3 -3
  63. package/dist/utils/parsing/dateParser.js +62 -0
  64. package/dist/utils/parsing/index.js +2 -0
  65. package/{build/utils → dist/utils/parsing}/jsonParser.js +3 -2
  66. package/{build/utils → dist/utils/security}/idGenerator.js +4 -5
  67. package/dist/utils/security/index.js +3 -0
  68. package/{build/utils → dist/utils/security}/rateLimiter.js +7 -10
  69. package/{build/utils → dist/utils/security}/sanitization.js +4 -3
  70. package/package.json +20 -16
  71. package/build/mcp-server/server.js +0 -572
  72. package/build/mcp-server/tools/gitCommit/logic.js +0 -129
  73. package/build/mcp-server/tools/gitInit/registration.js +0 -44
  74. package/build/types-global/mcp.js +0 -59
  75. package/build/types-global/tool.js +0 -1
  76. package/build/utils/index.js +0 -11
  77. package/build/utils/logger.js +0 -266
  78. /package/{build → dist}/mcp-server/tools/gitAdd/index.js +0 -0
  79. /package/{build → dist}/mcp-server/tools/gitBranch/index.js +0 -0
  80. /package/{build → dist}/mcp-server/tools/gitCheckout/index.js +0 -0
  81. /package/{build → dist}/mcp-server/tools/gitCherryPick/index.js +0 -0
  82. /package/{build → dist}/mcp-server/tools/gitClean/index.js +0 -0
  83. /package/{build → dist}/mcp-server/tools/gitClearWorkingDir/index.js +0 -0
  84. /package/{build → dist}/mcp-server/tools/gitClone/index.js +0 -0
  85. /package/{build → dist}/mcp-server/tools/gitCommit/index.js +0 -0
  86. /package/{build → dist}/mcp-server/tools/gitDiff/index.js +0 -0
  87. /package/{build → dist}/mcp-server/tools/gitFetch/index.js +0 -0
  88. /package/{build → dist}/mcp-server/tools/gitLog/index.js +0 -0
  89. /package/{build → dist}/mcp-server/tools/gitMerge/index.js +0 -0
  90. /package/{build → dist}/mcp-server/tools/gitPull/index.js +0 -0
  91. /package/{build → dist}/mcp-server/tools/gitPush/index.js +0 -0
  92. /package/{build → dist}/mcp-server/tools/gitRebase/index.js +0 -0
  93. /package/{build → dist}/mcp-server/tools/gitRemote/index.js +0 -0
  94. /package/{build → dist}/mcp-server/tools/gitReset/index.js +0 -0
  95. /package/{build → dist}/mcp-server/tools/gitSetWorkingDir/index.js +0 -0
  96. /package/{build → dist}/mcp-server/tools/gitShow/index.js +0 -0
  97. /package/{build → dist}/mcp-server/tools/gitStash/index.js +0 -0
  98. /package/{build → dist}/mcp-server/tools/gitStatus/index.js +0 -0
  99. /package/{build → dist}/mcp-server/tools/gitTag/index.js +0 -0
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Git MCP Server
2
2
 
3
3
  [![TypeScript](https://img.shields.io/badge/TypeScript-^5.8.3-blue.svg)](https://www.typescriptlang.org/)
4
- [![Model Context Protocol](https://img.shields.io/badge/MCP%20SDK-^1.10.2-green.svg)](https://modelcontextprotocol.io/)
5
- [![Version](https://img.shields.io/badge/Version-2.0.0-blue.svg)](./CHANGELOG.md)
4
+ [![Model Context Protocol](https://img.shields.io/badge/MCP%20SDK-^1.11.0-green.svg)](https://modelcontextprotocol.io/)
5
+ [![Version](https://img.shields.io/badge/Version-2.0.3-blue.svg)](./CHANGELOG.md)
6
6
  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
7
  [![Status](https://img.shields.io/badge/Status-Stable-green.svg)](https://github.com/cyanheads/git-mcp-server/issues)
8
8
  [![GitHub](https://img.shields.io/github/stars/cyanheads/git-mcp-server?style=social)](https://github.com/cyanheads/git-mcp-server)
@@ -11,74 +11,27 @@ An MCP (Model Context Protocol) server providing tools to interact with Git repo
11
11
 
12
12
  Built on the [`cyanheads/mcp-ts-template`](https://github.com/cyanheads/mcp-ts-template), this server follows a modular architecture:
13
13
 
14
- > **Note:** This version (v2.0.0) focuses on refactoring and updating the core Git tools based on the latest MCP SDK. MCP Resource capabilities are not implemented in this version. For resource access, please use [v1.2.4](https://github.com/cyanheads/git-mcp-server/releases/tag/v1.2.4).
15
-
16
- ```mermaid
17
- flowchart TB
18
- subgraph API["API Layer"]
19
- direction LR
20
- MCP["MCP Protocol"]
21
- Val["Validation (Zod)"]
22
- San["Sanitization"]
23
-
24
- MCP --> Val --> San
25
- end
26
-
27
- subgraph Core["Core Components"]
28
- direction LR
29
- Config["Configuration"]
30
- Logger["Logging System"]
31
- Error["Error Handling"]
32
- Server["MCP Server (SDK)"]
33
-
34
- Config --> Server
35
- Logger --> Server
36
- Error --> Server
37
- end
38
-
39
- subgraph Implementation["Implementation Layer"]
40
- direction LR
41
- Tool["Tools (Git Logic)"]
42
- Util["Utilities"]
43
-
44
- Tool --> Server
45
- Util --> Tool
46
- end
47
-
48
- San --> Config
49
- San --> Server
50
-
51
- classDef layer fill:#2d3748,stroke:#4299e1,stroke-width:3px,rx:5,color:#fff
52
- classDef component fill:#1a202c,stroke:#a0aec0,stroke-width:2px,rx:3,color:#fff
53
- class API,Core,Implementation layer
54
- class MCP,Val,San,Config,Logger,Error,Server,Tool,Util component
55
- ```
56
-
57
- Implemented as an MCP server, it allows LLM agents and other compatible clients to interact with local Git repositories using standardized commands.
58
-
59
14
  > **Developer Note**: This repository includes a [.clinerules](.clinerules) file that serves as a developer cheat sheet for your LLM coding agent with quick reference for the codebase patterns, file locations, and code snippets.
60
15
 
61
16
  ## Table of Contents
62
17
 
63
- - [Overview](#overview)
64
- - [Features](#features)
65
- - [Installation](#installation)
66
- - [Configuration](#configuration)
67
- - [Project Structure](#project-structure)
68
- - [Tools](#tools)
69
- - [Resources](#resources)
70
- - [Development](#development)
71
- - [License](#license)
18
+ | [Overview](#overview) | [Features](#features) | [Installation](#installation) |
19
+ | [Configuration](#configuration) | [Project Structure](#project-structure) |
20
+ | [Tools](#tools) | [Resources](#resources) | [Development](#development) | [License](#license) |
72
21
 
73
22
  ## Overview
74
23
 
75
- The Git MCP Server implements the Model Context Protocol (MCP), enabling standardized communication between LLMs and external systems through:
24
+ **Empower your AI agents and development tools with seamless Git integration!**
25
+
26
+ The Git MCP Server acts as a bridge, allowing applications (MCP Clients) that understand the Model Context Protocol (MCP) – like advanced AI assistants (LLMs), IDE extensions, or custom scripts – to interact directly and safely with local Git repositories.
76
27
 
77
- - **Clients**: Claude Desktop, IDEs, and other MCP-compatible clients.
78
- - **Servers**: This server provides tools for Git operations.
79
- - **LLM Agents**: AI models that leverage the server's Git capabilities.
28
+ Instead of complex scripting or manual CLI, your tools can leverage this server to:
80
29
 
81
- This server enables AI assistants and other MCP clients to interact with local Git repositories. It exposes specific Git commands as MCP tools, allowing for programmatic control over repository status checking, branching, staging, committing, fetching, pulling, pushing, and more.
30
+ - **Automate Git workflows**: Clone repositories, create branches, stage changes, commit work, push updates, and manage tags programmatically.
31
+ - **Gain repository insights**: Check status, view logs, diff changes, and inspect Git objects without leaving the host application.
32
+ - **Integrate Git into AI-driven development**: Enable LLMs to manage version control as part of their coding or refactoring tasks.
33
+
34
+ Built on the robust `mcp-ts-template`, this server provides a standardized, secure, and efficient way to expose Git functionality via the MCP standard. It achieves this by securely executing the standard `git` command-line tool installed on the system using Node.js's `child_process` module, ensuring compatibility and leveraging the full power of Git.
82
35
 
83
36
  ## Features
84
37
 
@@ -96,19 +49,28 @@ Leverages the robust utilities provided by the `mcp-ts-template`:
96
49
 
97
50
  ### Git Operations
98
51
 
52
+ - **Direct Git CLI Execution**: Interacts with Git by securely executing the standard `git` command-line tool via Node.js `child_process`, ensuring full compatibility and access to Git's features.
99
53
  - **Comprehensive Command Coverage**: Exposes a wide range of Git commands as MCP tools (see [Tools](#tools) section).
100
54
  - **Repository Interaction**: Supports status checking, branching, staging, committing, fetching, pulling, pushing, diffing, logging, resetting, tagging, and more.
101
55
  - **Working Directory Management**: Allows setting and clearing a session-specific working directory for context persistence across multiple Git operations.
102
56
  - **Safety Features**: Includes checks and requires explicit confirmation for potentially destructive operations like `git clean` and `git reset --hard`.
57
+ - **Commit Signing**: Supports GPG or SSH signing for verified commits, controlled via the `GIT_SIGN_COMMITS` environment variable and server-side Git configuration. Includes an optional tool parameter to fall back to unsigned commits on signing failure.
103
58
 
104
59
  ## Installation
105
60
 
106
61
  ### Prerequisites
107
62
 
108
- - [Node.js (v18+)](https://nodejs.org/)
63
+ - [Node.js (>=18.0.0)](https://nodejs.org/)
109
64
  - [npm](https://www.npmjs.com/) (comes with Node.js)
110
65
  - [Git](https://git-scm.com/) installed and accessible in the system PATH.
111
66
 
67
+ ### Install via npm
68
+
69
+ 1. Install the package globally:
70
+ ```bash
71
+ npm install git-mcp-server
72
+ ```
73
+
112
74
  ### Install from Source
113
75
 
114
76
  1. Clone the repository:
@@ -122,9 +84,9 @@ Leverages the robust utilities provided by the `mcp-ts-template`:
122
84
  ```
123
85
  3. Build the project:
124
86
  ```bash
125
- npm run build
87
+ npm run build (or `npm run rebuild`)
126
88
  ```
127
- This compiles the TypeScript code to JavaScript in the `build/` directory and makes the entry point executable.
89
+ This compiles the TypeScript code to JavaScript in the `dist/` directory and makes the entry point executable.
128
90
 
129
91
  ## Configuration
130
92
 
@@ -132,20 +94,14 @@ Leverages the robust utilities provided by the `mcp-ts-template`:
132
94
 
133
95
  Configure the server using environment variables. Create a `.env` file in the project root (copy from `.env.example`) or set them in your environment.
134
96
 
135
- | Variable | Description | Default |
136
- | ---------------------- | ---------------------------------------------------------------------------------------- | ------------------- |
137
- | `MCP_TRANSPORT_TYPE` | Transport mechanism: `stdio` or `http`. | `stdio` |
138
- | `MCP_HTTP_PORT` | Port for the HTTP server (if `MCP_TRANSPORT_TYPE=http`). Retries next ports if busy. | `3000` |
139
- | `MCP_HTTP_HOST` | Host address for the HTTP server (if `MCP_TRANSPORT_TYPE=http`). | `127.0.0.1` |
140
- | `MCP_ALLOWED_ORIGINS` | Comma-separated list of allowed origins for CORS (if `MCP_TRANSPORT_TYPE=http`). | (none) |
141
- | `MCP_SERVER_NAME` | Name reported during MCP initialization. | `git-mcp-server` |
142
- | `MCP_SERVER_VERSION` | Version reported during MCP initialization. | (from package.json) |
143
- | `LOG_LEVEL` | Logging level (`debug`, `info`, `notice`, `warning`, `error`, `crit`, `alert`, `emerg`). | `info` |
144
- | `LOG_REDACT_PATTERNS` | Comma-separated regex patterns for redacting sensitive data in logs. | (predefined) |
145
- | `LOG_FILE_PATH` | Path for log file output. If unset, logs only to console. | (none) |
146
- | `LOG_MAX_FILE_SIZE_MB` | Max size (MB) for log file rotation. | `10` |
147
- | `LOG_MAX_FILES` | Max number of rotated log files to keep. | `5` |
148
- | `LOG_ZIP_ARCHIVES` | Compress rotated log files (`true`/`false`). | `true` |
97
+ | Variable | Description | Default |
98
+ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
99
+ | `MCP_TRANSPORT_TYPE` | Transport mechanism: `stdio` or `http`. | `stdio` |
100
+ | `MCP_HTTP_PORT` | Port for the HTTP server (if `MCP_TRANSPORT_TYPE=http`). Retries next ports if busy. | `3000` |
101
+ | `MCP_HTTP_HOST` | Host address for the HTTP server (if `MCP_TRANSPORT_TYPE=http`). | `127.0.0.1` |
102
+ | `MCP_ALLOWED_ORIGINS` | Comma-separated list of allowed origins for CORS (if `MCP_TRANSPORT_TYPE=http`). | (none) |
103
+ | `MCP_LOG_LEVEL` | Logging level (`debug`, `info`, `notice`, `warning`, `error`, `crit`, `alert`, `emerg`). Inherited from template. | `info` |
104
+ | `GIT_SIGN_COMMITS` | Set to `"true"` to enable signing attempts for commits made by the `git_commit` tool. Requires server-side Git/key setup (see below). | `false` |
149
105
 
150
106
  ### MCP Client Settings
151
107
 
@@ -154,12 +110,14 @@ Add to your MCP client settings (e.g., `cline_mcp_settings.json`):
154
110
  ```json
155
111
  {
156
112
  "mcpServers": {
157
- "git": {
113
+ "git-mcp-server": {
114
+ // Use a descriptive name
158
115
  "command": "node", // Use node to run the script
159
- "args": ["/path/to/your/git-mcp-server/build/index.js"], // Absolute path to the built entry point
116
+ "args": ["/path/to/your/git-mcp-server/dist/index.js"], // Absolute path to the built entry point
160
117
  "env": {
161
118
  // "MCP_TRANSPORT_TYPE": "http", // Optional: if using http
162
- // "MCP_HTTP_PORT": "3001" // Optional: if using http and non-default port
119
+ // "MCP_HTTP_PORT": "3010", // Optional: if using http and non-default port
120
+ // "GIT_SIGN_COMMITS": "true" // Optional: Enable commit signing attempts if server is configured
163
121
  },
164
122
  "disabled": false,
165
123
  "autoApprove": [] // Configure auto-approval rules if desired
@@ -178,7 +136,8 @@ src/
178
136
  ├── config/ # Configuration loading (env vars, package info)
179
137
  │ └── index.ts
180
138
  ├── mcp-server/ # Core MCP server logic and capability registration
181
- │ ├── server.ts # Server setup, transport handling, tool registration
139
+ │ ├── server.ts # Server setup, capability registration
140
+ │ ├── transports/ # Transport handling (stdio, http)
182
141
  │ ├── resources/ # MCP Resource implementations (currently none)
183
142
  │ └── tools/ # MCP Tool implementations (subdirs per tool)
184
143
  ├── types-global/ # Shared TypeScript type definitions
@@ -200,7 +159,7 @@ The Git MCP Server provides a suite of tools for interacting with Git repositori
200
159
  | `git_clean` | Removes untracked files. **Requires `force: true`**. | `path?`, `force`, `dryRun?`, `directories?`, `ignored?` |
201
160
  | `git_clear_working_dir` | Clears the session-specific working directory. | (none) |
202
161
  | `git_clone` | Clones a repository into a specified absolute path. | `repositoryUrl`, `targetPath`, `branch?`, `depth?`, `quiet?` |
203
- | `git_commit` | Commits staged changes with a message. | `path?`, `message`, `author?`, `allowEmpty?`, `amend?` |
162
+ | `git_commit` | Commits staged changes. Supports author override, signing control. | `path?`, `message`, `author?`, `allowEmpty?`, `amend?`, `forceUnsignedOnFailure?` |
204
163
  | `git_diff` | Shows changes between commits, working tree, etc. | `path?`, `commit1?`, `commit2?`, `staged?`, `file?` |
205
164
  | `git_fetch` | Downloads objects and refs from other repositories. | `path?`, `remote?`, `prune?`, `tags?`, `all?` |
206
165
  | `git_init` | Initializes a new Git repository at the specified absolute path. | `path`, `initialBranch?`, `bare?`, `quiet?` |
@@ -221,25 +180,24 @@ _Note: The `path` parameter for most tools defaults to the session's working dir
221
180
 
222
181
  ## Resources
223
182
 
224
- **MCP Resources are not implemented in this version (v2.0.0).**
183
+ **MCP Resources are not implemented in this version (v2.0.2).**
225
184
 
226
- This version focuses on the refactored Git tools implementation based on the latest `mcp-ts-template` and MCP SDK v1.10.2. Resource capabilities, previously available, have been temporarily removed during this major update.
185
+ This version focuses on the refactored Git tools implementation based on the latest `mcp-ts-template` and MCP SDK v1.11.0. Resource capabilities, previously available, have been temporarily removed during this major update.
227
186
 
228
187
  If you require MCP Resource access (e.g., for reading file content directly via the server), please use the stable **[v1.2.4 release](https://github.com/cyanheads/git-mcp-server/releases/tag/v1.2.4)**.
229
188
 
230
189
  Future development may reintroduce resource capabilities in a subsequent release.
231
190
 
191
+ > **Note:** This version (v2.0.2) focuses on refactoring and updating the core Git tools based on the latest MCP SDK. MCP Resource capabilities are not implemented in this version. For resource access, please use [v1.2.4](https://github.com/cyanheads/git-mcp-server/releases/tag/v1.2.4).
192
+
232
193
  ## Development
233
194
 
234
195
  ### Build and Test
235
196
 
236
197
  ```bash
237
- # Build the project (compile TS to JS in build/ and make executable)
198
+ # Build the project (compile TS to JS in dist/ and make executable)
238
199
  npm run build
239
200
 
240
- # Watch for changes and recompile automatically
241
- npm run watch
242
-
243
201
  # Test the server locally using the MCP inspector tool (stdio transport)
244
202
  npm run inspector
245
203
 
@@ -251,6 +209,14 @@ npm run tree
251
209
 
252
210
  # Clean build artifacts and then rebuild the project
253
211
  npm run rebuild
212
+
213
+ # Start the server using stdio (default)
214
+ npm start
215
+ # Or explicitly:
216
+ npm run start:stdio
217
+
218
+ # Start the server using HTTP transport
219
+ npm run start:http
254
220
  ```
255
221
 
256
222
  ## License
@@ -2,14 +2,14 @@ import dotenv from "dotenv";
2
2
  import { readFileSync } from "fs";
3
3
  import { dirname, join } from "path";
4
4
  import { fileURLToPath } from "url";
5
- import { logger } from "../utils/logger.js"; // Import McpLogLevel and logger
5
+ // Removed logger import to break circular dependency
6
6
  dotenv.config(); // Load environment variables from .env file
7
7
  // Determine the directory name of the current module
8
8
  const __dirname = dirname(fileURLToPath(import.meta.url));
9
9
  // Construct the path to package.json relative to the current file
10
10
  const pkgPath = join(__dirname, '../../package.json');
11
11
  // Default package information in case package.json is unreadable
12
- let pkg = { name: 'mcp-ts-template', version: '0.0.0' };
12
+ let pkg = { name: 'obsidian-mcp-server', version: '0.0.0' };
13
13
  try {
14
14
  // Read and parse package.json to get server name and version
15
15
  pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
@@ -30,12 +30,24 @@ export const config = {
30
30
  logLevel: process.env.MCP_LOG_LEVEL || "info", // Use MCP_LOG_LEVEL consistently
31
31
  /** The runtime environment (e.g., "development", "production"). Defaults to "development". */
32
32
  environment: process.env.NODE_ENV || "development",
33
+ /** The communication transport type ('stdio' or 'http'). Defaults to 'stdio'. */
34
+ mcpTransportType: process.env.MCP_TRANSPORT_TYPE || "stdio",
35
+ /** Port for the HTTP transport. Defaults to 3000. */
36
+ mcpHttpPort: parseInt(process.env.MCP_HTTP_PORT || "3010", 10),
37
+ /** Host for the HTTP transport. Defaults to '127.0.0.1'. */
38
+ mcpHttpHost: process.env.MCP_HTTP_HOST || "127.0.0.1",
39
+ /** Allowed origins for HTTP transport (comma-separated). */
40
+ mcpAllowedOrigins: process.env.MCP_ALLOWED_ORIGINS?.split(',') || [],
41
+ /** Flag to enable GPG signing for commits made by the git_commit tool. Requires server-side GPG setup. */
42
+ gitSignCommits: process.env.GIT_SIGN_COMMITS === 'true',
33
43
  /** Security-related configurations. */
34
44
  security: {
35
45
  // Placeholder for security settings
36
46
  // Example: authRequired: process.env.AUTH_REQUIRED === 'true'
37
47
  /** Indicates if authentication is required for server operations. */
38
48
  authRequired: false,
49
+ /** Secret key for signing/verifying authentication tokens (required if authRequired is true). */
50
+ mcpAuthSecretKey: process.env.MCP_AUTH_SECRET_KEY || '', // Default to empty string, validation should happen elsewhere
39
51
  }
40
52
  // Note: mcpClient configuration is now loaded separately from mcp-config.json
41
53
  };
@@ -51,19 +63,5 @@ export const logLevel = config.logLevel;
51
63
  * @type {string}
52
64
  */
53
65
  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
66
+ // Logger initialization is now handled in the main application entry point (e.g., src/index.ts)
67
+ // after the config module has been fully loaded.
@@ -1,15 +1,20 @@
1
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";
2
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import http from 'http'; // Import http module
4
+ import { config, environment, logLevel } from "./config/index.js"; // Import logLevel from config
5
+ import { initializeAndStartServer } from "./mcp-server/server.js";
6
+ // Import utils from barrel
7
+ import { logger, requestContextService } from './utils/index.js'; // Import McpLogLevel type
8
8
  /**
9
- * The main MCP server instance.
9
+ * The main MCP server instance (used for stdio transport).
10
10
  * @type {McpServer | undefined}
11
11
  */
12
- let server;
12
+ let mcpServerInstance;
13
+ /**
14
+ * The main HTTP server instance (used for http transport).
15
+ * @type {http.Server | undefined}
16
+ */
17
+ let httpServerInstance;
13
18
  /**
14
19
  * Gracefully shuts down the main MCP server.
15
20
  * Handles process termination signals (SIGTERM, SIGINT) and critical errors.
@@ -17,22 +22,50 @@ let server;
17
22
  * @param signal - The signal or event name that triggered the shutdown (e.g., "SIGTERM", "uncaughtException").
18
23
  */
19
24
  const shutdown = async (signal) => {
20
- // Define context for the shutdown operation
25
+ const transportType = (process.env.MCP_TRANSPORT_TYPE || 'stdio').toLowerCase();
21
26
  const shutdownContext = {
22
27
  operation: 'Shutdown',
23
28
  signal,
29
+ transport: transportType,
24
30
  };
25
31
  logger.info(`Received ${signal}. Starting graceful shutdown...`, shutdownContext);
26
32
  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);
33
+ let closePromise = Promise.resolve();
34
+ if (transportType === 'stdio') {
35
+ // Close the main MCP server instance for stdio
36
+ if (mcpServerInstance) {
37
+ logger.info("Closing main MCP server (stdio)...", shutdownContext);
38
+ closePromise = mcpServerInstance.close();
39
+ }
40
+ else {
41
+ logger.warning("Stdio MCP server instance not found during shutdown.", shutdownContext);
42
+ }
32
43
  }
33
- else {
34
- logger.warning("Server instance not found during shutdown.", shutdownContext);
44
+ else if (transportType === 'http') {
45
+ // Close the main HTTP server listener for http
46
+ if (httpServerInstance) {
47
+ logger.info("Closing main HTTP server listener...", shutdownContext);
48
+ closePromise = new Promise((resolve, reject) => {
49
+ httpServerInstance.close((err) => {
50
+ if (err) {
51
+ logger.error("Error closing HTTP server listener", { ...shutdownContext, error: err.message });
52
+ reject(err);
53
+ }
54
+ else {
55
+ logger.info("Main HTTP server listener closed successfully", shutdownContext);
56
+ resolve();
57
+ }
58
+ });
59
+ });
60
+ }
61
+ else {
62
+ logger.warning("HTTP server instance not found during shutdown.", shutdownContext);
63
+ }
64
+ // Note: Individual session transports (StreamableHTTPServerTransport) are closed
65
+ // when the client disconnects or sends a DELETE request, managed in httpTransport.ts.
35
66
  }
67
+ // Wait for the appropriate server/listener to close
68
+ await closePromise;
36
69
  logger.info("Graceful shutdown completed successfully", shutdownContext);
37
70
  process.exit(0);
38
71
  }
@@ -52,7 +85,21 @@ const shutdown = async (signal) => {
52
85
  * and registers signal handlers for graceful shutdown and error handling.
53
86
  */
54
87
  const start = async () => {
55
- // Create application-level request context using the service instance
88
+ // --- Initialize Logger FIRST ---
89
+ // Define valid MCP log levels based on the logger's type definition
90
+ const validMcpLogLevels = ['debug', 'info', 'notice', 'warning', 'error', 'crit', 'alert', 'emerg'];
91
+ let validatedMcpLogLevel = 'info'; // Default to 'info'
92
+ if (validMcpLogLevels.includes(logLevel)) {
93
+ validatedMcpLogLevel = logLevel;
94
+ }
95
+ else {
96
+ // Use console.warn as logger isn't ready yet
97
+ console.warn(`Invalid MCP_LOG_LEVEL "${logLevel}" found in config. Defaulting to "info".`);
98
+ }
99
+ // Initialize the logger singleton instance with the validated level.
100
+ logger.initialize(validatedMcpLogLevel);
101
+ // Now it's safe to use the logger.
102
+ // --- Start Application ---
56
103
  const transportType = (process.env.MCP_TRANSPORT_TYPE || 'stdio').toLowerCase();
57
104
  const startupContext = requestContextService.createRequestContext({
58
105
  operation: `ServerStartup_${transportType}`, // Include transport in operation name
@@ -64,22 +111,25 @@ const start = async () => {
64
111
  try {
65
112
  // Initialize the server instance and start the selected transport
66
113
  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
114
+ // Start the server transport. This returns the McpServer instance for stdio
115
+ // or the http.Server instance for http.
116
+ const serverOrHttpInstance = await initializeAndStartServer();
117
+ if (transportType === 'stdio' && serverOrHttpInstance instanceof McpServer) {
118
+ mcpServerInstance = serverOrHttpInstance; // Store McpServer for stdio shutdown
119
+ logger.debug("Stored McpServer instance for stdio transport.", startupContext);
120
+ }
121
+ else if (transportType === 'http' && serverOrHttpInstance instanceof http.Server) {
122
+ httpServerInstance = serverOrHttpInstance; // Store http.Server for http shutdown
123
+ logger.debug("Stored http.Server instance for http transport.", startupContext);
73
124
  }
74
125
  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.
126
+ // This case should ideally not happen if initializeAndStartServer works correctly
127
+ logger.warning("initializeAndStartServer did not return the expected instance type.", {
128
+ ...startupContext,
129
+ instanceType: typeof serverOrHttpInstance
130
+ });
81
131
  }
82
- // If initializeAndStartServer failed, it would have thrown an error,
132
+ // If initializeAndStartServer failed internally, it would have thrown an error,
83
133
  // and execution would jump to the outer catch block.
84
134
  logger.info(`${config.mcpServerName} is running with ${transportType} transport`, {
85
135
  ...startupContext,