Connect a cloud coding agent to self-hosted GitLab and private packages without a broad VPN. Use scoped credentials, an outbound MCP path, and negative tests to prove the agent cannot publish or administer GitLab.
GitLab self-hosted access from a cloud coding agent works best through an outbound HTTPS connection to a narrowly scoped GitLab MCP server, not through a general VPN.
Run the agent in a GitLab Workspace, connect it to the self-managed instance at /api/v4/mcp, and give the workspace separate read-only credentials for the repository and package registry. Then test the negative cases: a second project, a package publish, and GitLab administration must all fail.
Use a supported connection method first
The first decision is not which tunnel to install. It is whether the agent runtime supports an outbound HTTPS MCP connection and whether your GitLab instance can receive it.
GitLab Workspaces are isolated development environments scoped to a GitLab project. They run on a Linux Kubernetes cluster connected to GitLab through the GitLab agent for Kubernetes.
A Workspace has its own dependencies, libraries, and tools, so it is a practical boundary for an agent that needs to clone one project and install its private dependencies.
For the agent connection, use the GitLab MCP server’s HTTP transport. GitLab documents this endpoint for self-managed installations:
{
"mcpServers": {
"GitLab": {
"type": "http",
"url": "https://gitlab.example.com/api/v4/mcp"
}
}
}
OpenAI Codex is one documented client. GitLab’s setup uses its MCP add command with the self-managed URL, followed by the Codex MCP login command.
Claude Code, Cursor, Gemini Code Assist, and other MCP clients have their own documented configuration paths. Check the runtime’s documentation before you assume it can run a local proxy, install Node.js, or reach an internal hostname.
If the agent is hosted outside your network, the self-managed GitLab MCP endpoint needs an allowed path from that hosted runtime.
GitLab’s documentation says a Claude organization connector connects from Anthropic’s cloud, so a private instance must be reachable from that cloud or each user must run the MCP connection locally. That is a connection prerequisite, not a reason to expose the entire GitLab installation.
For a policy-controlled path that avoids inbound firewall holes, Zero Trust AI access can make the GitLab service reachable only after the agent identity and policy authorize the connection. The agent should receive the GitLab service, not broad network reachability.
Scope the repository before the agent starts
Give the agent one project identity with read-only repository access. Do not reuse a human personal access token and do not give the agent a group-wide credential just because it is easier to configure.
GitLab deploy tokens are separate from user accounts and can be created at project or group scope. The read_repository scope allows a git clone; read_package_registry allows package downloads. The same page documents write_registry and write_package_registry as separate write scopes, so leave them out of an agent token.
Create a project deploy token with:
read_repositoryfor the permitted project.read_package_registryif the build needs private packages.- An explicit expiration date that matches the task window.
- A secret injection path supplied by the Workspace or its secret manager, never a committed
.envfile.
GitLab says a deploy token has a username and token value, and that the token cannot be retrieved again after leaving the creation page. Treat both values as credentials. Revoke the token when the agent task ends.
The boundary should look like this:
| Resource | Agent operation | Credential or control | Must fail |
|---|---|---|---|
| Permitted GitLab project | Clone and read files | Project deploy token with read_repository | Push or merge request creation when not granted |
| Private package registry | Resolve and download dependencies | read_package_registry or the package type’s read-only equivalent | Publish, delete, or overwrite a package |
| GitLab MCP endpoint | Read named project context and approved tools | OAuth mcp scope or a fine-grained token with MCP Execute permission | A project or tool outside the policy |
| GitLab administration | No operation | No admin credential in the Workspace | Settings, user, group, token, or runner changes |
The table is a policy target, not proof. You prove it with the checks in the last section.
Connect private packages without widening Git access
Private package access is a separate control from repository access. Configure the package manager against the exact GitLab registry endpoint, and give it a credential with package read permission only.
GitLab’s package registry supports GitLab.com, GitLab Self-Managed, and GitLab Dedicated. Its documentation lists package-specific authentication instructions for npm, PyPI, Maven, NuGet, Terraform, and generic packages.
The registry can also show which pipeline published a package and the commit and user that triggered it. That gives you a useful audit trail for the write path even when the agent is read-only.
For a Python dependency, the read path should use the registry URL for the permitted project and a read-only deploy token. Keep the token in the Workspace’s injected environment, and avoid putting it in the command history or a generated lockfile. For npm, use the same pattern with a project registry URL and an authentication token that cannot publish.
Container images need their own decision. GitLab Workspaces can use private container registries through a Kubernetes image pull secret named in the GitLab agent configuration. That secret controls image pulls for the Workspace infrastructure. It is not a reason to give the coding agent write_registry or cluster credentials.
Redirects deserve an explicit check. Follow the package manager’s documented redirect behavior, but do not silently turn a GitLab registry URL into an unrelated host. Allow only the registry hostnames required by the package type, validate TLS, and inspect the final host in the build log. A redirect to an artifact store is a new trust decision.
Limit the GitLab MCP toolset
The MCP server is a control plane for GitLab operations, not a replacement for repository credentials. GitLab supports OAuth access tokens with the mcp scope and fine-grained personal access tokens with the MCP tool resource and Execute permission. Legacy, project, and group access tokens are rejected by the MCP server even when they carry an mcp or api scope.
Start with the smallest toolset. GitLab documents core, repository, merge_requests, work_items, and ci as tool groups, with other groups available only when requested or licensed. Configure only the groups needed for the task. Listing a tool does not grant access: GitLab still checks project and namespace permissions on each call.
For an agent that only needs to inspect code and answer questions, start with repository and core tools. If the task needs pipeline status, add ci and nothing else. Leave write-capable actions unavailable where your client and GitLab governance settings permit it. GitLab’s tool governance can omit tools set to Always Deny and mark tools set to Always Ask.
Do not confuse authentication with authorization. An OAuth login proves who connected the client. Project permissions, MCP tool rules, and deploy-token scopes decide what the agent can do after it connects.
Prove the boundary with negative tests
A secure connection is not demonstrated by a successful clone. Run the agent through a test repository and record both successful reads and denied actions.
- Clone the permitted project and read one known file. Confirm the credential works.
- Read package metadata and download one private dependency. Confirm the package manager reaches the expected registry host.
- Attempt to clone a different private project. Expect an authorization failure.
- Attempt
git pushto the permitted project. Expect failure because the deploy token has no write scope. - Attempt to publish a package. Expect failure because
write_package_registryis absent. - Attempt a package delete or overwrite. Expect failure and preserve the response in the audit record.
- Ask the MCP client for a project or tool outside the approved namespace. Expect denial.
- Attempt a GitLab settings, user, group, runner, or token operation. Expect denial because the agent has no administration credential.
- Inspect the final package download host after any redirect. Stop if it is not on the approved allowlist.
- Revoke the deploy token and OAuth grant, terminate the Workspace, and confirm that a repeat read fails.
The test must include the negative cases. A green clone alone proves reachability, not least privilege. GitLab records AI audit events for MCP tool calls, and its package registry can record publish and delete events when audit events are enabled. Send those records to the same review trail as the Workspace logs.
Common failure modes
The most common mistake is putting the agent behind a VPN and then granting it a broad user token. That solves routing while leaving the authorization problem intact. The agent can reach GitLab, but nobody can show which project, package, or API action it was allowed to use.
Another mistake is assuming a package read token can also authenticate the GitLab API. GitLab says deploy tokens cannot be used with the public API, even though they work with some package registry endpoints.
Use the MCP OAuth or fine-grained token only for the approved MCP operations, and use the deploy token only for the repository and package paths it needs.
Finally, do not assume a Workspace is a hostile sandbox by itself. GitLab notes that Workspace pods can interact with one another unless you isolate them in the cluster. Apply Kubernetes network policy, restrict egress, use trusted projects, and keep agent credentials short-lived. The Workspace is an execution boundary. Your cluster policy still determines how strong that boundary is.
FAQ
Can a cloud coding agent access self-hosted GitLab without a VPN?
Yes, if the runtime supports an outbound HTTPS MCP connection and the self-managed GitLab MCP endpoint is reachable through an approved path. Use GitLab’s /api/v4/mcp endpoint, authenticate with OAuth or a supported fine-grained token, and expose only the GitLab service the agent needs. The endpoint can serve GitLab.com, GitLab Self-Managed, or GitLab Dedicated.
What is the safest token for cloning one private repository?
A project deploy token with the read_repository scope is the narrowest documented choice for cloning one project. Add read_package_registry only when the build needs private packages, and omit every write scope. Set an expiry date and revoke the token after the agent task ends.
Can a read-only agent install private packages?
Yes. Give the package manager a project or registry credential with the package type’s read permission, such as read_package_registry, and configure the exact registry URL. Installation must not require write_package_registry, write_registry, or a GitLab administrator credential. Keep the credential out of source control and command history.
Can a GitLab MCP connection publish code or packages?
It can only perform actions allowed by the authenticated identity, project permissions, tool rules, and token scopes. Prove the result by attempting a push and package publish in a test project, then keep the denied responses in the audit record. A successful login alone does not prove write access is absent.
Does GitLab Workspaces support private container images?
Yes. GitLab documents Kubernetes image pull secrets for private container registries in Workspace configuration. Configure the secret in the GitLab agent settings, and keep that infrastructure pull credential separate from the agent’s repository and package read credentials. The pull secret does not grant the coding agent permission to publish images.
What should happen when the private GitLab instance is unreachable?
Stop and fix the approved connectivity path. Do not publish the GitLab endpoint to the public internet or install an unreviewed VPN client in the agent runtime just to make the first clone work. Review the hosted runtime’s documented network requirements before changing GitLab exposure.
Sources and references
- GitLab Workspaces: isolated development environments, project scope, tokens, and workspace limits.
- Configure Workspaces: Kubernetes prerequisites, private registry image pulls, and runtime options.
- GitLab MCP server: HTTP transport, Codex setup, authentication, toolsets, permissions, and audit events.
- GitLab deploy tokens: repository and registry scopes, cloning, package pulls, and write boundaries.
- GitLab package registry: package types, CI authentication, visibility permissions, and audit events.
- NetFoundry AI Security: identity-first, outbound-only access for AI workloads.