@cepharum/mcp-gitlab 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +170 -2
- package/package.json +50 -5
- package/src/audit.js +185 -0
- package/src/cli.js +411 -0
- package/src/connect.js +335 -0
- package/src/consent.js +115 -0
- package/src/downloads.js +94 -0
- package/src/errors.js +13 -0
- package/src/git.js +94 -0
- package/src/gitlab/client.js +406 -0
- package/src/gitlab/diff.js +79 -0
- package/src/hosts.js +228 -0
- package/src/paths.js +71 -0
- package/src/permissions/catalog.js +75 -0
- package/src/permissions/policy.js +230 -0
- package/src/permissions/profiles.js +66 -0
- package/src/pins.js +36 -0
- package/src/platform/index.js +76 -0
- package/src/platform/macos.js +129 -0
- package/src/platform/service.js +2 -0
- package/src/process.js +56 -0
- package/src/refs.js +146 -0
- package/src/secrets.js +51 -0
- package/src/server.js +146 -0
- package/src/session.js +140 -0
- package/src/text.js +68 -0
- package/src/tools/artifacts.js +288 -0
- package/src/tools/audit.js +53 -0
- package/src/tools/environments.js +147 -0
- package/src/tools/files.js +101 -0
- package/src/tools/index.js +33 -0
- package/src/tools/issues.js +465 -0
- package/src/tools/merge-requests.js +698 -0
- package/src/tools/packages.js +322 -0
- package/src/tools/pipelines.js +415 -0
- package/src/tools/registry.js +107 -0
- package/src/tools/repositories.js +174 -0
- package/src/tools/schedules.js +521 -0
- package/src/tools/shared.js +93 -0
- package/src/tools/variables.js +294 -0
- package/src/tools/wikis.js +168 -0
- package/src/zip.js +178 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes of `@cepharum/mcp-gitlab`. Versions follow [semantic versioning](https://semver.org); before 1.0.0, a minor version may contain changes that break existing setups, a patch version doesn't.
|
|
4
|
+
|
|
5
|
+
## Unreleased
|
|
6
|
+
|
|
7
|
+
## 0.1.0 — 2026-12-06
|
|
8
|
+
|
|
9
|
+
First public release, for macOS.
|
|
10
|
+
|
|
11
|
+
- MCP server for gitlab.com and self-managed GitLab instances, contacting registered hosts only, over HTTPS, without following redirects that would forward the access token.
|
|
12
|
+
- Access tokens are kept in the macOS keychain and never reach agents, logs or the audit log; every host is approved by the user in a dialog.
|
|
13
|
+
- Permission profiles `readonly` (default), `developer`, `devops` and `maintainer`, custom profiles, repository scopes, confirmation dialogs once per session or every time, and sessions of up to 24 hours (`mcp-gitlab session 8h`).
|
|
14
|
+
- Tools for repositories and files, merge requests, issues and milestones, wikis, pipelines and jobs, pipeline schedules, job artifacts, packages including uploads to generic packages, container registries, environments, runners and CI/CD variables. Values of sensitive variables are entered by the user in dialogs and never pass the agent.
|
|
15
|
+
- Audit log of every tool call, kept for 90 days, with free text and values recorded by length only.
|
|
16
|
+
- `mcp-gitlab connect claude-code|claude-desktop` adds the server to these agents.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 cepharum GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,171 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mcp-gitlab
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An MCP server that lets agents such as Claude Code work with GitLab — gitlab.com and any number of self-managed instances — without ever seeing an access token. Tokens are kept in the credential store of the operating system, every host is approved by the user once per session in a desktop dialog, and a permission profile decides which tools an agent gets.
|
|
4
|
+
|
|
5
|
+
> **Status:** feature complete for macOS: host registry, token storage, confirmation dialogs, permission profiles, sessions, audit log, repositories and files, merge requests, issues, milestones, wikis, pipelines and their schedules, jobs and their logs, job artifacts, packages, container registries, environments, runners and CI/CD variables. See [docs/ROADMAP.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/docs/ROADMAP.md) for what was built when and [docs/ARCHITECTURE.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/docs/ARCHITECTURE.md) for how it works.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js 22 or later
|
|
10
|
+
- macOS (Windows is prepared for, see [docs/ARCHITECTURE.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/docs/ARCHITECTURE.md#windows-planned))
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install -g @cepharum/mcp-gitlab # provides the command mcp-gitlab
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
To work on mcp-gitlab itself, install it from a clone instead:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
git clone https://gitlab.com/cepharum-foss/mcp-gitlab.git
|
|
22
|
+
cd mcp-gitlab
|
|
23
|
+
npm ci
|
|
24
|
+
npm link
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Setup
|
|
28
|
+
|
|
29
|
+
Register every GitLab instance you want agents to use and store a [personal access token](https://docs.gitlab.com/user/profile/personal_access_tokens/) for it. Use the scope `read_api` if agents only read, `api` for profiles that change things.
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
mcp-gitlab host add https://gitlab.com
|
|
33
|
+
mcp-gitlab host add https://git.example.com --default
|
|
34
|
+
mcp-gitlab token set gitlab.com
|
|
35
|
+
mcp-gitlab token set git.example.com
|
|
36
|
+
mcp-gitlab token check
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`token set` lets the keychain prompt for the token, so it never shows up in the shell history or a process list.
|
|
40
|
+
|
|
41
|
+
Options of `host add`:
|
|
42
|
+
|
|
43
|
+
| Option | Use |
|
|
44
|
+
|---|---|
|
|
45
|
+
| `--name <name>` | name used in references, default: the host name of the URL |
|
|
46
|
+
| `--ssh-host <host>` | host name of SSH clone URLs if it differs from the web host, so local clones are recognized |
|
|
47
|
+
| `--storage-host <pattern>` | host that downloads of artifacts and packages may be redirected to, e.g. `*.s3.example.com` for an instance keeping them in object storage. gitlab.com gets `*.gitlab-static.net` by default |
|
|
48
|
+
| `--default` | host used for references without host name |
|
|
49
|
+
|
|
50
|
+
Only registered hosts are ever contacted, and only over HTTPS (plain HTTP is accepted for `localhost`). Instances with a certificate of a private CA need `NODE_EXTRA_CA_CERTS` pointing to that CA in the environment of the server.
|
|
51
|
+
|
|
52
|
+
### Connecting agents
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
mcp-gitlab connect claude-code --profile developer
|
|
56
|
+
mcp-gitlab connect claude-desktop --profile devops
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
`connect` adds this installation of the server to an agent, with absolute paths of Node.js and of mcp-gitlab, since Claude Desktop starts servers without the `PATH` of your shell. It shows the change and asks before making it.
|
|
60
|
+
|
|
61
|
+
- `claude-code` uses `claude mcp add-json … --scope user`, so the server is available in all projects. An existing server of the same name is replaced only after asking.
|
|
62
|
+
- `claude-desktop` changes `claude_desktop_config.json`, keeps all other entries and a copy of the previous version as `.bak`. Restart Claude Desktop afterwards.
|
|
63
|
+
- `--profile <name>` sets the profile of that agent, `--name <name>` the name of the server (default `gitlab`), `--yes` skips the question.
|
|
64
|
+
|
|
65
|
+
After upgrading Node.js, e.g. via nvm, the paths may be gone: `mcp-gitlab connect --check` tells, and running `connect` again fixes it.
|
|
66
|
+
|
|
67
|
+
## Repositories
|
|
68
|
+
|
|
69
|
+
Tools take a repository as `<host>:<group>/<project>`, e.g. `gitlab.com:my-group/my-project`, as `repo_list` returns it. `<group>/<project>` refers to the default host, which is the only host registered or the one marked `--default`. Clone and web URLs of registered hosts work as well. Without a repository, tools use the local clone given as `directory`, or the working directory of the server, and derive the repository from its remote `origin`.
|
|
70
|
+
|
|
71
|
+
## Permissions and profiles
|
|
72
|
+
|
|
73
|
+
Every tool requires a permission. A profile grants permissions and can ask the user to confirm some of them in a dialog, once per session or every time.
|
|
74
|
+
|
|
75
|
+
| Profile | Grants |
|
|
76
|
+
|---|---|
|
|
77
|
+
| `readonly` (default) | reading everything, pinning repositories |
|
|
78
|
+
| `developer` | also creating and changing merge requests, issues and wiki pages, running pipelines and pipeline schedules, downloading artifacts, uploading files to generic packages |
|
|
79
|
+
| `devops` | reading everything, running, retrying and cancelling pipelines (starting confirmed once per session, cancelling every time), downloading artifacts, creating and changing issues and wiki pages, commenting on merge requests, managing CI/CD variables, pipeline schedules and packages; no merge requests of its own |
|
|
80
|
+
| `maintainer` | also approving and merging merge requests, both confirmed, and managing CI/CD variables, pipeline schedules and packages |
|
|
81
|
+
|
|
82
|
+
`mcp-gitlab profile list`, `profile show <name>` and `profile use <name>` inspect and select profiles. Custom profiles go into `settings.json` in the configuration folder (`~/Library/Application Support/mcp-gitlab` on macOS):
|
|
83
|
+
|
|
84
|
+
```json
|
|
85
|
+
{
|
|
86
|
+
"profile": "careful",
|
|
87
|
+
"profiles": {
|
|
88
|
+
"careful": {
|
|
89
|
+
"extends": "developer",
|
|
90
|
+
"deny": [ "pipeline.*" ],
|
|
91
|
+
"confirm": { "mr.create": "always" },
|
|
92
|
+
"repos": [ "git.example.com:my-team/**" ]
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"deny": [ "artifact.*" ],
|
|
96
|
+
"repos": [ "!gitlab.com:customer/**" ]
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Permissions are named `<area>.<action>`, e.g. `mr.merge` or `pipeline.read`; `mcp-gitlab profile show <name>` lists those of a profile. Patterns such as `pipeline.*` or `*.read` select several.
|
|
101
|
+
|
|
102
|
+
`repos` limits a profile to repositories matching the patterns (`*` within a path segment, `**` across segments, case ignored). A pattern starting with `!` excludes the repositories it matches; a list of exclusions only allows every other repository, an empty list none. A profile without `repos` takes those of the profile it extends.
|
|
103
|
+
|
|
104
|
+
`deny` and `repos` at the top level apply to every profile, in addition to those of the profile: a repository has to be allowed by both. This way, repositories another MCP server is responsible for can be excluded once for all profiles. The server reads its profile when it starts.
|
|
105
|
+
|
|
106
|
+
## CI/CD variables
|
|
107
|
+
|
|
108
|
+
Agents see the names, types, environment scopes and flags of all variables of a repository, and the values of variables that aren't sensitive. A variable is sensitive if it is masked or hidden, or if its name contains `token`, `secret`, `password`, `passwort` or `key`, ignoring case.
|
|
109
|
+
|
|
110
|
+
- Reading the value of a sensitive variable needs the user's confirmation every time, whatever the profile says. GitLab never returns values of hidden variables.
|
|
111
|
+
- Values of sensitive variables are entered by the user in a dialog with hidden input when an agent creates or changes one; agents can't pass them. Entering the value replaces the confirmation the profile asks for. File variables need a value that fits on one line this way.
|
|
112
|
+
- Changing and deleting variables needs `variable.update` and `variable.delete` (profiles `devops` and `maintainer`; changing confirmed once per session, deleting every time) and the Maintainer role in GitLab.
|
|
113
|
+
- Only variables of repositories are covered, so repository scopes apply. Variables of groups and the instance aren't.
|
|
114
|
+
|
|
115
|
+
## Pipeline schedules
|
|
116
|
+
|
|
117
|
+
Agents see all pipeline schedules of a repository and their variables, and can run a schedule now (`developer` and up), or create, change and delete schedules (`devops` and `maintainer`).
|
|
118
|
+
|
|
119
|
+
- Variables of schedules have no flags, so only their names make them sensitive, as for CI/CD variables. `schedule_get` leaves out their values; naming one as `variable` returns its value after confirmation every time. When creating or changing a schedule, the user enters their values in dialogs with hidden input.
|
|
120
|
+
- Pipelines of a schedule run with the permissions of its owner, and only the owner can change it. An agent changes a schedule of someone else only after taking it over with `takeOwnership`, which is confirmed every time, naming the previous owner. Running a schedule is confirmed naming its owner.
|
|
121
|
+
- Deleting a schedule is confirmed every time.
|
|
122
|
+
|
|
123
|
+
## Sessions
|
|
124
|
+
|
|
125
|
+
Dialogs offer to allow access to a host or an action "for this session". Normally a session lasts as long as the server runs, which ends when the agent restarts or reloads it, and differs between agents.
|
|
126
|
+
|
|
127
|
+
`mcp-gitlab session 8h` starts a session of its own, of 24 hours at most. While it runs, the button reads "Allow until 18:30", and what is allowed that way applies to every agent using mcp-gitlab, beyond restarts of the server, until the session ends. Allowing once and confirmations a profile requires every time are unaffected.
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
mcp-gitlab session 8h # start a session, replacing a running one and its approvals
|
|
131
|
+
mcp-gitlab session # when it ends and what was approved for it
|
|
132
|
+
mcp-gitlab session end # end it now, revoking its approvals
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Starting a session approves nothing by itself, so it needs no dialog: every approval is still granted in a dialog naming when it ends. The session is kept in `session.json` in the configuration folder.
|
|
136
|
+
|
|
137
|
+
## Packages and container registry
|
|
138
|
+
|
|
139
|
+
Agents read packages, files of generic packages and the images and tags of the container registry of a repository.
|
|
140
|
+
|
|
141
|
+
- `package_upload` (`developer` and up) uploads a local file to a version of a generic package. It takes files of a local clone only, the one given as `directory` or the one the server runs in, never hidden files or folders such as `.git` or `.env`, and no files named like keys. Every upload is confirmed in a dialog naming the local file, its size and the package, whatever the profile says.
|
|
142
|
+
- `package_delete` (`devops` and `maintainer`) deletes a version of a package or a file of it, confirmed every time.
|
|
143
|
+
|
|
144
|
+
## Saved files
|
|
145
|
+
|
|
146
|
+
`artifact_download` saves files of job artifacts in `downloads/<host>/<group>/<project>/<job>/` in the configuration folder and tells the agent the local path, without showing the content to the agent. Saved files are removed after 7 days, when the server starts.
|
|
147
|
+
|
|
148
|
+
## Audit log
|
|
149
|
+
|
|
150
|
+
Every tool call is recorded in `audit/<day>.jsonl` in the configuration folder: time, tool, repository, outcome and arguments, with free text recorded by length only and anything looking like a token removed. Entries are removed after 90 days. `mcp-gitlab audit` shows the latest ones, agents with `audit.read` can use `audit_list`.
|
|
151
|
+
|
|
152
|
+
## Development
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
npm test
|
|
156
|
+
npm run lint
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
CI runs lint, `npm audit` (warning on branches, blocking on tags and on the weekly scheduled pipeline of `main`) and the tests, and keeps a CycloneDX SBOM (`sbom.cdx.json`) of every release tag. See [RELEASING.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/RELEASING.md) for how releases are made, and [docs/ARCHITECTURE.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/docs/ARCHITECTURE.md) for the structure of the code and how to add tools.
|
|
160
|
+
|
|
161
|
+
## Security
|
|
162
|
+
|
|
163
|
+
Agents using mcp-gitlab only through its tools never see an access token, and can't answer its dialogs. An agent allowed to run any command as the user can work around that: it could read tokens from the keychain with `security`, or change the files in the configuration folder. Keep agents from running `security` and from writing to the configuration folder, e.g. by the permissions of Claude Code. See [docs/ARCHITECTURE.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/docs/ARCHITECTURE.md#limits) for details.
|
|
164
|
+
|
|
165
|
+
Please report vulnerabilities to [security@cepharum.de](mailto:security@cepharum.de) instead of opening a public issue.
|
|
166
|
+
|
|
167
|
+
Security fixes are published as patch releases of the latest minor version only, e.g. 0.1.x until 0.2.0 is released; upgrade to receive them. They are provided until the end of 2028 at least, unless the project is discontinued earlier, which would be announced here and in [CHANGELOG.md](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/CHANGELOG.md). Every release comes with a CycloneDX SBOM (`sbom.cdx.json`) as artifact of its tag pipeline.
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
[MIT](https://gitlab.com/cepharum-foss/mcp-gitlab/-/blob/main/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@cepharum/mcp-gitlab",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP server exposing common developer workflows of gitlab.com and self-managed GitLab instances without handing access tokens to the agent",
|
|
5
|
+
"author": "cepharum GmbH (https://cepharum.de)",
|
|
6
|
+
"homepage": "https://gitlab.com/cepharum-foss/mcp-gitlab#readme",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://gitlab.com/cepharum-foss/mcp-gitlab.git"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://gitlab.com/cepharum-foss/mcp-gitlab/-/issues"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"bin": {
|
|
16
|
+
"mcp-gitlab": "src/cli.js"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"README.md",
|
|
21
|
+
"CHANGELOG.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"start": "node src/cli.js serve",
|
|
29
|
+
"test": "node --test \"test/*.test.js\"",
|
|
30
|
+
"lint": "eslint src test",
|
|
31
|
+
"lint:fix": "eslint src test --fix"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"mcp",
|
|
35
|
+
"gitlab",
|
|
36
|
+
"devops"
|
|
37
|
+
],
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
41
|
+
"diff": "^9.0.0",
|
|
42
|
+
"zod": "^4.6.7"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"eslint": "^10.16.0",
|
|
46
|
+
"eslint-config-cepharum": "^3.0.0"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/audit.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { appendFile, mkdir, readdir, readFile, unlink } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDirectory } from "./paths.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* One call of a tool, as recorded in the audit log.
|
|
8
|
+
*
|
|
9
|
+
* @typedef {object} AuditEntry
|
|
10
|
+
* @property {string} time ISO time the call started
|
|
11
|
+
* @property {string} run ID of the server process, to tell sessions apart
|
|
12
|
+
* @property {string} tool
|
|
13
|
+
* @property {string} [repo] formatted reference of the repository
|
|
14
|
+
* @property {"ok"|"denied"|"error"} outcome
|
|
15
|
+
* @property {number} ms duration
|
|
16
|
+
* @property {string} [error] message of a failure
|
|
17
|
+
* @property {Record<string, unknown>} args arguments, with texts shortened and secrets removed
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Days entries are kept. */
|
|
21
|
+
export const RETENTION_DAYS = 90;
|
|
22
|
+
|
|
23
|
+
/** Arguments holding free text, which is recorded by length only. */
|
|
24
|
+
const TEXT_ARGUMENTS = new Set( [ "body", "description", "content", "message", "note", "text", "value" ] );
|
|
25
|
+
|
|
26
|
+
/** Arguments mapping names to values, e.g. variables of pipelines, whose values are recorded by length only. */
|
|
27
|
+
const VALUE_MAPS = new Set( ["variables"] );
|
|
28
|
+
|
|
29
|
+
/** Patterns of tokens that must never end up in the log, even if an agent passes one as argument. */
|
|
30
|
+
const SECRETS = /\b(?:glpat|gloas|gldt|glrt|glcbt|glptt|glft|glimt|glagent|glsoat|glffct)-[\w-]{10,}/g;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {string} value
|
|
34
|
+
* @returns {string}
|
|
35
|
+
*/
|
|
36
|
+
function redactText( value ) {
|
|
37
|
+
const clean = value.replace( SECRETS, "[token]" );
|
|
38
|
+
|
|
39
|
+
return clean.length > 300 ? `${clean.slice( 0, 300 )}… (${clean.length} characters)` : clean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Prepares arguments of a tool for the log.
|
|
44
|
+
*
|
|
45
|
+
* @param {unknown} value
|
|
46
|
+
* @param {string} [key]
|
|
47
|
+
* @param {number} [depth]
|
|
48
|
+
* @returns {unknown}
|
|
49
|
+
*/
|
|
50
|
+
export function redact( value, key = "", depth = 0 ) {
|
|
51
|
+
if ( typeof value === "string" ) {
|
|
52
|
+
return TEXT_ARGUMENTS.has( key ) ? `[${value.length} characters]` : redactText( value );
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if ( Array.isArray( value ) ) {
|
|
56
|
+
return depth > 3 ? "[…]" : value.slice( 0, 50 ).map( item => redact( item, "", depth + 1 ) );
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if ( value && typeof value === "object" ) {
|
|
60
|
+
const valuesOnly = VALUE_MAPS.has( key );
|
|
61
|
+
|
|
62
|
+
return depth > 3 ? "[…]" : Object.fromEntries( Object.entries( value ).map( ( [ name, item ] ) => [ name, redact( item, valuesOnly ? "value" : name, depth + 1 ) ] ) );
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The audit log of tool calls, one file of JSON lines per day.
|
|
70
|
+
*
|
|
71
|
+
* @param {{ directory?: string, now?: () => Date }} [options]
|
|
72
|
+
*/
|
|
73
|
+
export function createAudit( { directory = join( configDirectory(), "audit" ), now = () => new Date() } = {} ) {
|
|
74
|
+
const run = randomUUID();
|
|
75
|
+
const fileOf = day => join( directory, `${day}.jsonl` );
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
run,
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Appends an entry. Failures to write are reported to stderr, but don't fail the tool call.
|
|
82
|
+
*
|
|
83
|
+
* @param {Omit<AuditEntry, "run"|"time"> & { started: Date }} entry
|
|
84
|
+
* @returns {Promise<void>}
|
|
85
|
+
*/
|
|
86
|
+
async record( { started, args, error, ...rest } ) {
|
|
87
|
+
/** @type {AuditEntry} */
|
|
88
|
+
const line = {
|
|
89
|
+
time: started.toISOString(),
|
|
90
|
+
run,
|
|
91
|
+
...rest,
|
|
92
|
+
...error ? { error: redactText( error ) } : {},
|
|
93
|
+
args: /** @type {Record<string, unknown>} */ redact( args ) ,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
await mkdir( directory, { recursive: true, mode: 0o700 } );
|
|
98
|
+
await appendFile( fileOf( line.time.slice( 0, 10 ) ), `${JSON.stringify( line )}\n`, { mode: 0o600 } );
|
|
99
|
+
} catch ( cause ) {
|
|
100
|
+
process.stderr.write( `mcp-gitlab: writing the audit log failed: ${cause.message}\n` );
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Reads entries, newest first.
|
|
106
|
+
*
|
|
107
|
+
* @param {{ since?: Date, until?: Date, tool?: string, outcome?: string, repo?: string, run?: string, offset?: number, limit?: number }} [filter]
|
|
108
|
+
* @returns {Promise<{ entries: AuditEntry[], nextOffset: number|null }>}
|
|
109
|
+
*/
|
|
110
|
+
async list( { since, until, tool, outcome, repo, run: runId, offset = 0, limit = 50 } = {} ) {
|
|
111
|
+
const days = ( await files() )
|
|
112
|
+
.filter( day => ( !since || day >= since.toISOString().slice( 0, 10 ) ) && ( !until || day <= until.toISOString().slice( 0, 10 ) ) )
|
|
113
|
+
.sort()
|
|
114
|
+
.reverse();
|
|
115
|
+
|
|
116
|
+
const matching = [];
|
|
117
|
+
|
|
118
|
+
for ( const day of days ) {
|
|
119
|
+
const lines = ( await readFile( fileOf( day ), "utf8" ) ).split( "\n" ).filter( Boolean ).reverse();
|
|
120
|
+
|
|
121
|
+
for ( const text of lines ) {
|
|
122
|
+
let entry;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
entry = JSON.parse( text );
|
|
126
|
+
} catch {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const time = new Date( entry.time );
|
|
131
|
+
|
|
132
|
+
if ( ( since && time < since ) || ( until && time > until ) || ( tool && entry.tool !== tool ) ||
|
|
133
|
+
( outcome && entry.outcome !== outcome ) || ( runId && entry.run !== runId ) ||
|
|
134
|
+
( repo && !String( entry.repo ?? "" ).toLowerCase().includes( repo.toLowerCase() ) ) ) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
matching.push( entry );
|
|
139
|
+
|
|
140
|
+
if ( matching.length > offset + limit ) {
|
|
141
|
+
return { entries: matching.slice( offset, offset + limit ), nextOffset: offset + limit };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return { entries: matching.slice( offset, offset + limit ), nextOffset: null };
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Removes the files of days older than the retention period.
|
|
151
|
+
*
|
|
152
|
+
* @returns {Promise<number>} number of removed files
|
|
153
|
+
*/
|
|
154
|
+
async prune() {
|
|
155
|
+
const oldest = new Date( now().getTime() - ( RETENTION_DAYS * 86_400_000 ) ).toISOString().slice( 0, 10 );
|
|
156
|
+
let removed = 0;
|
|
157
|
+
|
|
158
|
+
for ( const day of await files() ) {
|
|
159
|
+
if ( day < oldest ) {
|
|
160
|
+
await unlink( fileOf( day ) );
|
|
161
|
+
removed++;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return removed;
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @returns {Promise<string[]>} days with a file, e.g. "2026-12-03"
|
|
171
|
+
*/
|
|
172
|
+
async function files() {
|
|
173
|
+
try {
|
|
174
|
+
return ( await readdir( directory ) )
|
|
175
|
+
.filter( name => /^\d{4}-\d{2}-\d{2}\.jsonl$/.test( name ) )
|
|
176
|
+
.map( name => name.slice( 0, 10 ) );
|
|
177
|
+
} catch ( error ) {
|
|
178
|
+
if ( error.code === "ENOENT" ) {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|