Skip to content

Commit a102e94

Browse files
author
agent
committed
feat: add GITLAB_LOCK_PROJECT environment variable
Add ability to lock the MCP server to a single GitLab project by setting GITLAB_LOCK_PROJECT=true. When enabled, the server will only allow access to the project specified in GITLAB_PROJECT_ID and deny access to any other projects with a clear error message. This is useful for: - Enhanced security by restricting access to a single project - Preventing accidental operations on wrong projects - Creating dedicated MCP instances for specific projects
1 parent 1c7a62b commit a102e94

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ When using with the Claude App, you need to set up your API key and URLs directl
2525
"env": {
2626
"GITLAB_PERSONAL_ACCESS_TOKEN": "your_gitlab_token",
2727
"GITLAB_API_URL": "your_gitlab_api_url",
28+
"GITLAB_PROJECT_ID": "your_project_id", // Optional: default project
29+
"GITLAB_LOCK_PROJECT": "false", // Optional: lock to single project
2830
"GITLAB_READ_ONLY_MODE": "false",
2931
"USE_GITLAB_WIKI": "false", // use wiki api?
3032
"USE_MILESTONE": "false", // use milestone api?
@@ -168,6 +170,7 @@ $ sh scripts/image_push.sh docker_user_name
168170
- `GITLAB_PERSONAL_ACCESS_TOKEN`: Your GitLab personal access token.
169171
- `GITLAB_API_URL`: Your GitLab API URL. (Default: `https://gitlab.com/api/v4`)
170172
- `GITLAB_PROJECT_ID`: Default project ID. If set, Overwrite this value when making an API request.
173+
- `GITLAB_LOCK_PROJECT`: When set to 'true', locks the MCP server to only access the project specified in `GITLAB_PROJECT_ID`. Any attempts to access other projects will be denied. Requires `GITLAB_PROJECT_ID` to be set.
171174
- `GITLAB_READ_ONLY_MODE`: When set to 'true', restricts the server to only expose read-only operations. Useful for enhanced security or when write access is not needed. Also useful for using with Cursor and it's 40 tool limit.
172175
- `USE_GITLAB_WIKI`: When set to 'true', enables the wiki-related tools (list_wiki_pages, get_wiki_page, create_wiki_page, update_wiki_page, delete_wiki_page). By default, wiki features are disabled.
173176
- `USE_MILESTONE`: When set to 'true', enables the milestone-related tools (list_milestones, get_milestone, create_milestone, edit_milestone, delete_milestone, get_milestone_issue, get_milestone_merge_requests, promote_milestone, get_milestone_burndown_events). By default, milestone features are disabled.

index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,12 +826,18 @@ function normalizeGitLabApiUrl(url?: string): string {
826826
// Use the normalizeGitLabApiUrl function to handle various URL formats
827827
const GITLAB_API_URL = normalizeGitLabApiUrl(process.env.GITLAB_API_URL || "");
828828
const GITLAB_PROJECT_ID = process.env.GITLAB_PROJECT_ID;
829+
const GITLAB_LOCK_PROJECT = process.env.GITLAB_LOCK_PROJECT === "true";
829830

830831
if (!GITLAB_PERSONAL_ACCESS_TOKEN) {
831832
console.error("GITLAB_PERSONAL_ACCESS_TOKEN environment variable is not set");
832833
process.exit(1);
833834
}
834835

836+
if (GITLAB_LOCK_PROJECT && !GITLAB_PROJECT_ID) {
837+
console.error("GITLAB_PROJECT_ID must be set when GITLAB_LOCK_PROJECT is enabled");
838+
process.exit(1);
839+
}
840+
835841
/**
836842
* Utility function for handling GitLab API errors
837843
* API 에러 처리를 위한 유틸리티 함수 (Utility function for handling API errors)
@@ -857,8 +863,18 @@ async function handleGitLabError(response: import("node-fetch").Response): Promi
857863
/**
858864
* @param {string} projectId - The project ID parameter passed to the function
859865
* @returns {string} The project ID to use for the API call
866+
* @throws {Error} If GITLAB_LOCK_PROJECT is enabled and a different project is requested
860867
*/
861868
function getEffectiveProjectId(projectId: string): string {
869+
if (GITLAB_LOCK_PROJECT) {
870+
if (!GITLAB_PROJECT_ID) {
871+
throw new Error("GITLAB_PROJECT_ID must be set when GITLAB_LOCK_PROJECT is enabled");
872+
}
873+
if (projectId && projectId !== GITLAB_PROJECT_ID) {
874+
throw new Error(`Access denied: This MCP server is locked to project ${GITLAB_PROJECT_ID}. Cannot access project ${projectId}`);
875+
}
876+
return GITLAB_PROJECT_ID;
877+
}
862878
return GITLAB_PROJECT_ID || projectId;
863879
}
864880

0 commit comments

Comments
 (0)