---
title: The MCP token in an environment variable isn't read
description: The token is exported and the server still answers 401. Each app writes a variable its own way, and most read it only when they start.
url: https://hamloy.com/mcp-fix/token-variable-not-read
---

[MCP fix](https://hamloy.com/mcp-fix)

# The MCP token in an environment variable isn't read

The token is exported and the server still answers 401. Each app writes a variable its own way, and most read it only when they start.

You keep the token out of the file, as you should: the file says `${HAMLOY_TOKEN}` and your shell profile exports the token. The server still answers that the token is missing or unknown. The variable never made it into the header.

## Each app writes a variable its own way

| App | In the file | Example |
| --- | --- | --- |
| Claude Code | `${NAME}` | `"Authorization": "Bearer ${HAMLOY_TOKEN}"` |
| Cursor | `${env:NAME}` | `"Authorization": "Bearer ${env:HAMLOY_TOKEN}"` |
| VS Code | `${input:id}`, asked once and kept by VS Code | `"Authorization": "Bearer ${input:hamloy-token}"` |
| Codex | the variable's name, with no `${}` | `bearer_token_env_var = "HAMLOY_TOKEN"` |

Written another app's way, the text isn't filled in. The app sends the header as it stands, `Bearer ${HAMLOY_TOKEN}` and all, and the server has never heard of that token.

## Claude Code, in a repo several people share

```
{
  "mcpServers": {
    "hamloy": {
      "type": "http",
      "url": "https://hamloy.com/mcp/YOUR_PROJECT_ID",
      "headers": { "Authorization": "Bearer ${HAMLOY_TOKEN}" }
    }
  }
}
```

The file goes in the repo without a secret in it, and each person keeps a token of their own in `HAMLOY_TOKEN`.

## VS Code asks instead of reading a variable

```
{
  "servers": {
    "hamloy": {
      "type": "http",
      "url": "https://hamloy.com/mcp/YOUR_PROJECT_ID",
      "headers": {
        "Authorization": "Bearer ${input:hamloy-token}"
      }
    }
  },
  "inputs": [
    {
      "type": "promptString",
      "id": "hamloy-token",
      "description": "Hamloy API token",
      "password": true
    }
  ]
}
```

The `id` under `inputs` has to match the name after `input:`. With no matching entry, VS Code has nothing to ask you for.

## Codex names the variable

```
codex mcp add hamloy --url https://hamloy.com/mcp --bearer-token-env-var HAMLOY_TOKEN
export HAMLOY_TOKEN=YOUR_TOKEN
```

`bearer_token_env_var` holds the variable's name, never the token itself.

## The app started before the variable was there

An app reads its environment when it starts. Cursor reads `HAMLOY_TOKEN` when it starts; so does Codex, each time. If you added the `export` line a minute ago:

1. Put it in your shell profile, not in the repo, so every new terminal has it.
2. Open a new terminal, or run the `export` line in the one you have.
3. Quit the app completely and start it again. An app started from the dock may not see what your terminal's profile sets; start it from the terminal if it still doesn't.

## Check that it worked

Ask "What's open in Hamloy?" Your lists come back instead of a 401, and on Hamloy's Connect page the token under **API tokens** shows as used just now.

If the server says the header is written wrong, `Bearer` is missing before the variable: see [Missing, unknown or expired token](https://hamloy.com/mcp-fix/missing-unknown-or-expired-token). The [MCP Config Validator](https://hamloy.com/mcp-config-validator) checks that the variable is written the way your app reads it. If the machine has a browser, you may not need a token at all: [Remote link or API token](https://hamloy.com/docs/remote-link-or-token).
