---
title: "How to Set a Proxy for an MCP Server: env, Node and Python"
description: "An MCP server gets its proxy from the env field in the client config. Node's fetch needs an extra flag, while Python's httpx and Requests read the variable."
url: https://proxynet.io/blog/mcp-server-proxy-config
date: 2026-12-03
author: "Enver Kaya"
category: "AI"
lang: en
---

# How to Set a Proxy for an MCP Server: env, Node and Python

An MCP server's proxy is set through the `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables passed to the server process. The client launches a stdio server and also builds that process's environment. The variable first has to reach the server, then the server's HTTP library has to read it. The trap is in the second step: Node's built-in `fetch` does not read the variable on its own, and the request leaves from your IP without an error. This post covers the `env` field, Node and Python servers, connecting to a remote server through a proxy, and keeping the password safe.

> **Note: Short answer**
>
> Put the proxy URL in the server's `env` field in the client config as `HTTPS_PROXY`. Add `NODE_USE_ENV_PROXY=1` for a Node server; in a Python server, httpx and Requests read the variable on their own.

## Two layers

The proxy variable passes through two layers: the process the client launches, and the HTTP library inside that process. If either one is missing, the request goes direct. Neither the client nor the server reports it as an error.

The first layer is the environment itself. With MCP's stdio transport, the client opens the server as a child process. We covered the difference between transports in [What Is MCP?](/blog/what-is-mcp). The official SDKs do not hand your whole shell environment to that child process. The source code of TypeScript SDK 1.32.2 and Python `mcp` 2.3.0 contains a short allowlist. On Windows, system variables such as `PATH`, `APPDATA` and `TEMP` pass through; on Linux and macOS, only `HOME`, `LOGNAME`, `PATH`, `SHELL`, `TERM` and `USER`. Proxy variables are not on the list.

In our test, `HTTPS_PROXY` was set in the client's own environment. With the `env` field left empty, the server never saw the variable. Not every client app may open the child process the same way. Still, the safe assumption is this: write whatever the server needs into the `env` field explicitly.

| Layer | Job | If missing |
|---|---|---|
| Client `env` field | Carries the variable into the server process | The server does not see the variable |
| Server's HTTP library | Reads the variable and opens the connection to the proxy | The variable sits there, the request goes direct |
| `NO_PROXY` | Keeps local addresses off the proxy | The local service goes to the proxy and fails |

## Setting the proxy in env

The `env` field holds the variables the client adds to the process when it starts the server. Claude Desktop's `claude_desktop_config.json` and Claude Code's `.mcp.json` use the same `mcpServers` format. Here is the full block for a server written in Node:

```json
{
  "mcpServers": {
    "web-fetch": {
      "command": "node",
      "args": ["/path/to/server.mjs"],
      "env": {
        "HTTP_PROXY": "http://user:pass@pr.proxynet.io:8000",
        "HTTPS_PROXY": "http://user:pass@pr.proxynet.io:8000",
        "NO_PROXY": "localhost,127.0.0.1",
        "NODE_USE_ENV_PROXY": "1"
      }
    }
  }
}
```

The URL starts with `http://` even when the target is HTTPS. The server asks the proxy for a `CONNECT` tunnel and sets up TLS inside it. The `NO_PROXY` line stops the server from reaching a database or local API on the same machine through the proxy. In a Python server the `NODE_USE_ENV_PROXY` line is unnecessary, but it does no harm.

The client reads its config at startup. After editing the file, quit the client completely and open it again. This setting only applies to stdio servers you launch yourself. You cannot set the outbound route of a remote server running on another machine. If the country and type of the exit IP matter, take the address from a pool such as [Residential Proxy](https://proxynet.io/residential-proxy).

## Built-in fetch in a Node server

Node's built-in `fetch` does not read proxy variables by default. In our test, a server on Node 24.22.2 saw the `HTTPS_PROXY` variable from the `env` field. The request still never touched the proxy and came back with 200. Since the proxy log has no entry, you only notice the problem by checking the exit IP.

There are two fixes. From Node 24.0.0 and 22.21.0 on, the `NODE_USE_ENV_PROXY=1` variable routes the built-in `fetch` and the `http`/`https` modules through the proxy in the environment. The `--use-env-proxy` flag, which does the same, arrived in v24.5.0. The Node docs mark the feature as being in active development. If you launch the server with `npx`, a flag is awkward to insert; the variable in the `env` field is easier.

On an older Node version, or when you own the server's code, use the `EnvHttpProxyAgent` class from the undici package. It reads the same variables and applies to every `fetch` call:

```js
// server.mjs: at the top of the file, before the tools are defined
import { EnvHttpProxyAgent, setGlobalDispatcher } from 'undici';

setGlobalDispatcher(new EnvHttpProxyAgent());
```

In our test with undici 8.14.0, these two lines routed the built-in `fetch` through the proxy. Watch two details. If a lowercase `https_proxy` is set, it takes precedence over the uppercase variable. And `EnvHttpProxyAgent` did not read a `socks5://` URL given in `ALL_PROXY`; the request went direct. Use an HTTP proxy URL in a Node server.

## httpx and Requests in a Python server

In Python servers the `env` field is usually enough on its own. httpx and Requests read proxy variables from the environment by default. This behavior depends on the `trust_env` setting, which defaults to `True`. In our test, tools written with httpx 0.28.1 and Requests 2.34.2 went through the proxy with nothing but the `env` field.

If the code says `trust_env=False`, the variable is ignored:

```python
import httpx

httpx.get("https://httpbin.org/ip")                       # uses the proxy from the environment
httpx.Client(trust_env=False).get("https://httpbin.org/ip")  # goes direct
```

In our test, the second line never touched the proxy. If `trust_env` appears in the server's source code, check that line first. In servers written with aiohttp the default is the reverse: `trust_env` is `False`.

If you want SOCKS5, httpx needs an extra package. With `ALL_PROXY=socks5://…`, httpx raised `ImportError` in our test and suggested `pip install httpx[socks]`. At least the error is not silent: the tool call fails and the message lands in the server's log.

## Connecting to a remote server through a proxy

With the Streamable HTTP transport, the client opens the connection, so the proxy is a client setting. The server's `env` field is no use here. The server does not run on your machine.

If you use a ready-made client app, look at its network settings. According to the Claude Code docs, the app reads the `HTTPS_PROXY` and `NO_PROXY` variables. It also applies this setting to requests to remote MCP servers. The same docs say SOCKS proxies are not supported.

If you write the client yourself with the TypeScript SDK, use the transport's `fetch` option. undici's `ProxyAgent` class sends every request through the proxy you give it:

```js
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { ProxyAgent, fetch as undiciFetch } from 'undici';

const agent = new ProxyAgent(process.env.HTTPS_PROXY);
const transport = new StreamableHTTPClientTransport(
  new URL('https://mcp.example.com/mcp'),
  { fetch: (url, init) => undiciFetch(url, { ...init, dispatcher: agent }) }
);

const client = new Client({ name: 'proxy-client', version: '1.0.0' });
await client.connect(transport);
```

In our test, all of this client's MCP traffic showed up in the proxy log. Initialization, the tool list and the tool call appeared as separate `POST` requests, and the event stream as a `GET` request.

## The password in the config file

A password written into the `env` field is plain text. Anyone who can read the file can use your proxy account. Three places are especially risky: a project file committed to Git, a shared screenshot, and a config attached to a support ticket. A project-scoped `.mcp.json` exists to be shared with the team, so it is the file that leaks most easily.

Claude Code supports variable expansion in `.mcp.json`. The value lives in your environment, not in the file:

```json
"env": {
  "HTTPS_PROXY": "${PROXY_URL}",
  "NODE_USE_ENV_PROXY": "1"
}
```

According to the docs, the `${VAR}` form is expanded in the `command`, `args`, `env`, `url` and `headers` fields. If the variable is not set, the config still loads and a warning appears for the server. For other clients, confirm this support in their own docs.

One more thing. The server's code can read every value you pass in the `env` field. Giving a proxy password to a server whose source you do not know means giving it to that code's author. If the password contains `@` or `#`, write it percent-encoded: `pass@123` becomes `pass%40123`. Keeping the password in a `.env` file, and what to do when it leaks into a repo, are covered in [How to Store a Proxy Password in .env](/blog/proxy-env-dotenv-secrets).

## Debugging

The first check is whether the variable reaches the server. At the start of the server, write whether the value exists to `stderr`, not the value itself. A stdio server must not write anything to `stdout` other than MCP messages. Claude Desktop keeps the server's `stderr` output in the `mcp-server-<name>.log` file.

| Symptom | Likely cause | Fix |
|---|---|---|
| No request in the proxy log, the server does not see the variable | `env` field empty, the shell variable was not passed on | Write the variable in the `env` field, reopen the client |
| Variable is set, the request still goes direct | Node built-in `fetch` | `NODE_USE_ENV_PROXY=1` or `EnvHttpProxyAgent` |
| `fetch failed` in Node, cause "Request was cancelled." | The proxy returned 407, wrong password | Check the password and its percent-encoding |
| httpx `ProxyError: 407` | Wrong credentials | Same check |
| Cannot connect to a local service | The local address also goes to the proxy | `NO_PROXY=localhost,127.0.0.1` |
| httpx `ImportError` and `socksio` | Extra package for SOCKS5 missing | `pip install httpx[socks]` |

The Node 407 row is misleading. In our test with a wrong password, Node did not show the 407 code in its error message. In the proxy log, though, the request was recorded as `auth=wrong`. Other cases where the variable is set but the proxy is never used are in [Why Is the HTTP_PROXY Variable Not Working?](/blog/http-proxy-env-not-applied).

## How we tested

We ran the tests on December 3, 2026, on Windows 11. Versions: Node 24.22.2, `@modelcontextprotocol/sdk` 1.32.2, undici 8.14.0, Python 3.13.17, `mcp` 2.3.0, httpx 0.28.1, Requests 2.34.2. The setup had three parts. First, a local HTTP proxy on `127.0.0.1` that required Basic authentication and logged every request. Second, a stdio MCP server with a single tool (`fetch_url`). Third, an SDK client that launched the server as a new process for each scenario. The target was `https://httpbin.org/ip`. We judged whether a request went through the proxy by checking the proxy log.

| Scenario | Proxy log |
|---|---|
| Node, `env` field empty | ✗ |
| Node, proxy in the `env` field | ✗ |
| Node, `env` + `NODE_USE_ENV_PROXY=1` | ✓ `CONNECT` |
| Node, `env` + `EnvHttpProxyAgent` | ✓ `CONNECT` |
| Python httpx and Requests, proxy in the `env` field | ✓ `CONNECT` |
| `NO_PROXY=httpbin.org` | ✗ (expected) |

For the remote server test, we ran an MCP server with the Streamable HTTP transport on the same machine. All four requests from the client connecting with `ProxyAgent` appeared in the proxy log.

## The bottom line

Write the proxy into the client's `env` field, add `NODE_USE_ENV_PROXY=1` for a Node server, and keep local addresses out with `NO_PROXY`. For APIs that want a fixed exit IP, [ISP Proxy](https://proxynet.io/static-isp-residential-proxy) is a good fit. Above all, check the proxy log: a variable sitting in the environment does not prove the request went through the proxy.

## Sources

- [Node.js, HTTP: Built-in Proxy Support: nodejs.org](https://nodejs.org/api/http.html#built-in-proxy-support)
- [Node.js, CLI: NODE_USE_ENV_PROXY=1: nodejs.org](https://nodejs.org/api/cli.html#node_use_env_proxy1)
- [undici, EnvHttpProxyAgent: github.com](https://github.com/nodejs/undici/blob/main/docs/docs/api/EnvHttpProxyAgent.md)
- [HTTPX, Environment Variables: python-httpx.org](https://www.python-httpx.org/environment_variables/)
- [Claude Code, Connect Claude Code to tools via MCP: code.claude.com](https://code.claude.com/docs/en/mcp)

## Frequently asked questions

### Does an MCP server see the HTTPS_PROXY variable from my shell?

Not always. The official TypeScript and Python SDKs start a stdio server with a short list of variables, and proxy variables are not on that list. In our test, `HTTPS_PROXY` was set in the client's environment and the server did not see it. Writing the variable explicitly into the server's `env` field is the route that works whatever the client. Reopen the client after changing the config.

### Which Node version supports NODE_USE_ENV_PROXY?

Node 24.0.0 and 22.21.0 onward. When the variable is `1`, the built-in `fetch` and the `http` and `https` modules read `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`. The `--use-env-proxy` flag, which does the same, arrived in v24.5.0. On an older version, register undici's `EnvHttpProxyAgent` class with `setGlobalDispatcher`. `node --version` shows your version.

### Does a Python MCP server need any extra setting?

Usually no. httpx and Requests read proxy variables from the environment with the default `trust_env`, so the `env` field is enough. If the code says `trust_env=False`, the variable is ignored and the request goes direct. In servers using aiohttp the default is `False`, so the session has to be opened with `trust_env=True`. You can see which library the server uses in its dependency list.

### How do I connect to a remote MCP server through a proxy?

With the client's proxy setting. The client opens the Streamable HTTP connection, so the server's `env` field is no use here. In ready-made apps, look at the app's network settings. Claude Code, for example, reads the `HTTPS_PROXY` and `NO_PROXY` variables. In your own client, give the TypeScript SDK transport a `fetch` function that uses undici's `ProxyAgent`.

### Is it safe to put the proxy password in the MCP config file?

It is risky. The value in the `env` field is plain text, and the project file can end up in Git. Claude Code supports variable expansion such as `${PROXY_URL}` in `.mcp.json`, so the password lives in your environment instead of the file. The server's code can also read this value, so do not give a proxy password to a server whose source you do not know. If the password leaks, change it right away.

### Can an MCP server use a SOCKS5 proxy?

It depends on the library. httpx supports SOCKS5 with the `httpx[socks]` extra and raises `ImportError` without it. In our test, undici's `EnvHttpProxyAgent` did not read a `socks5://` URL given in `ALL_PROXY`, and the request went direct. In Node servers, an HTTP proxy URL is the least troublesome route. Claude Code also does not support SOCKS proxies, according to its docs.
