Skip to content

Add proxy support for HTTPS_PROXY/https_proxy environment variables #4

@mkw

Description

@mkw

Problem

mcpc fails to connect to remote MCP servers when running inside Claude Code's sandbox environment (or any environment that routes network traffic through an HTTP proxy).

Error

[McpClient:mcpc] Failed to connect: TypeError: fetch failed
    at node:internal/deps/undici/undici:16416:13
    ...
  [cause]: Error: getaddrinfo ENOTFOUND example.com
      at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:121:26) {
    errno: -3008,
    code: 'ENOTFOUND',
    syscall: 'getaddrinfo',
    hostname: 'example.com'
  }
}

Root Cause

Claude Code's sandbox routes all network traffic through a local HTTP proxy (set via https_proxy / HTTPS_PROXY environment variables). However, Node.js native fetch() (powered by undici) does not respect these proxy environment variables.

The MCP SDK's StreamableHTTPClientTransport uses native fetch() by default, which attempts direct DNS resolution. In sandboxed environments, direct DNS lookups fail because network access is only available through the proxy.

Environment

  • Claude Code sandbox with https_proxy=http://localhost:<port>
  • mcpc v0.1.7
  • Node.js 20.x
  • macOS (also affects Linux)

Proposed Solution

The MCP SDK's StreamableHTTPClientTransport accepts a custom fetch option. mcpc can provide a proxy-aware fetch implementation using undici's ProxyAgent when proxy environment variables are detected:

async function createProxyAwareFetch(): Promise<FetchLike | undefined> {
  const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY;
  if (!proxyUrl) {
    return undefined;
  }

  const { ProxyAgent, fetch: undiciFetch } = await import('undici');
  const proxyAgent = new ProxyAgent(proxyUrl);

  return (input, init) => {
    return undiciFetch(input, { ...init, dispatcher: proxyAgent });
  };
}

This approach:

  • Only activates when proxy env vars are set (backwards compatible)
  • Uses undici's ProxyAgent to route requests through the proxy
  • Handles DNS resolution correctly through the proxy tunnel
  • Requires adding undici as a direct dependency

Related Issues

  • MCP SDK Issue #484: "SDK makes HTTPS fetches through an HTTP proxy"
  • Claude Code Issue #16222: "Gradle wrapper fails to download distribution - Java doesn't honor https_proxy"

Testing

I have a working implementation that I've tested successfully:

  • Connects to remote MCP servers through Claude Code's sandbox proxy
  • All existing unit tests pass
  • Added 5 new tests for proxy-aware fetch functionality

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions