Skip to content

fix: use agent function to support HTTP/HTTPS protocol switching in self-hosted GitLab#372

Merged
zereight merged 2 commits intomainfrom
copilot/fix-http-protocol-error
Mar 22, 2026
Merged

fix: use agent function to support HTTP/HTTPS protocol switching in self-hosted GitLab#372
zereight merged 2 commits intomainfrom
copilot/fix-http-protocol-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 17, 2026

Self-hosted GitLab instances behind HTTPS proxies often return http:// URLs in API responses or issue HTTP redirects internally. node-fetch was configured with a static agent (e.g., HTTPS-only), so any protocol switch during redirects would fail with Protocol "http:" not supported. Expected "https:".

Changes

  • gitlab-client-pool.ts: Add getAgentFunctionForUrl() returning a (parsedURL: URL) => Agent function that dynamically selects HTTP or HTTPS agent per-request. Extract shared agent lookup into private getOrCreateAgentsForUrl().
  • index.ts: Switch getFetchConfig() from static agent to agent function.

node-fetch supports an agent function that is called per-request, including on redirects:

// Before: static agent breaks on protocol switch
agent: clientPool.getOrCreateAgentForUrl(effectiveApiUrl)

// After: function selects correct agent per URL
agent: clientPool.getAgentFunctionForUrl(effectiveApiUrl)
// returns (parsedURL: URL) => parsedURL.protocol === "https:" ? httpsAgent : httpAgent
Original prompt

This section details on the original issue you should resolve

<issue_title>Self-hosted GitLab with HTTP: "Protocol 'http:' not supported" error</issue_title>
<issue_description>## Description

When using a self-hosted GitLab instance that returns http:// URLs in API responses (e.g. self links, resource URLs), some operations fail with:

MCP error -32603: Protocol "http:" not supported. Expected "https:"

This happens even when GITLAB_API_URL is set to https://. The issue is that GitLab API responses may contain internal links using http:// (common in self-hosted setups where external_url differs from the actual protocol), and the MCP server's HTTP client rejects them.

Steps to reproduce

  1. Configure a self-hosted GitLab instance that returns http:// in API response bodies
  2. Set GITLAB_API_URL=https://gitlab.example.com/api/v4
  3. Call any tool that follows URLs from API responses, e.g. get_project

Expected behavior

The server should handle http:// URLs from API responses, or at minimum use the protocol from GITLAB_API_URL when constructing follow-up requests.

Actual behavior

Error: MCP error -32603: Protocol "http:" not supported. Expected "https:"

Workaround ...


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI changed the title [WIP] Fix 'http:' not supported error in self-hosted GitLab fix: use agent function to support HTTP/HTTPS protocol switching in self-hosted GitLab Mar 17, 2026
Copilot AI requested a review from zereight March 17, 2026 18:42
@zereight zereight marked this pull request as ready for review March 22, 2026 13:17
Copilot AI review requested due to automatic review settings March 22, 2026 13:17
@chatgpt-codex-connector
Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Copilot AI and others added 2 commits March 22, 2026 22:19
…hing

When self-hosted GitLab instances redirect between HTTP and HTTPS (e.g.,
when external_url differs from the actual internal protocol), node-fetch
needs an agent function instead of a static agent to select the correct
HTTP or HTTPS agent based on each request's URL protocol.

- Add getAgentFunctionForUrl() to GitLabClientPool that returns a
  (parsedURL: URL) => Agent function for use with node-fetch
- Extract shared logic into private getOrCreateAgentsForUrl() method
- Update getFetchConfig() to use the agent function

Fixes: Protocol "http:" not supported. Expected "https:" error

Co-authored-by: zereight <42544600+zereight@users.noreply.github.com>
@zereight zereight force-pushed the copilot/fix-http-protocol-error branch from 55e75c2 to af41324 Compare March 22, 2026 13:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the GitLab HTTP client configuration to support protocol switching (HTTP ↔ HTTPS) during redirects and when following http:// links returned by some self-hosted GitLab instances, avoiding node-fetch agent/protocol mismatch errors.

Changes:

  • Introduces getAgentFunctionForUrl() in the GitLab client pool to dynamically select an HTTP vs HTTPS agent per request URL.
  • Refactors shared agent-pair lookup/creation into a private getOrCreateAgentsForUrl() helper.
  • Switches index.ts fetch configuration to use the new agent function instead of a static agent instance.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
gitlab-client-pool.ts Adds an agent-selection function (and shared agent-pair retrieval) to handle protocol changes across redirects/URLs.
index.ts Uses the agent function in getFetchConfig() so node-fetch can pick the correct agent per request/redirect.
Comments suppressed due to low confidence (1)

gitlab-client-pool.ts:112

  • The new protocol-switching behavior (agent function selecting HTTP vs HTTPS agents) is a regression-prone fix, but there’s no automated test exercising it. Consider adding a small unit test for getAgentFunctionForUrl() that asserts the returned function selects httpAgent for http: URLs and httpsAgent for https: URLs (and ideally covers the original failure mode via a simulated redirect/protocol switch).
  private createAgentsForUrl(apiUrl: string): ClientAgents {
    const { httpProxy, httpsProxy, noProxy, rejectUnauthorized, caCertPath } = this.options;
    const url = new URL(apiUrl);

    let sslOptions: { rejectUnauthorized?: boolean; ca?: Buffer } = {};

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zereight zereight merged commit e883701 into main Mar 22, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self-hosted GitLab with HTTP: "Protocol 'http:' not supported" error

3 participants