{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreifsfa4t6pvk5rb6hgyy3guha4e6wlhdnhjyfpbvgtsf2cpvxclcfe",
    "uri": "at://did:plc:pgryn3ephfd2xgft23qokfzt/app.bsky.feed.post/3mhsbtenvbs72"
  },
  "path": "/t/where-to-set-environment-variable-for-mcp-client-tutorial/174569#post_2",
  "publishedAt": "2026-03-24T07:30:05.000Z",
  "site": "https://discuss.huggingface.co",
  "tags": [
    "Hugging Face",
    "GitHub"
  ],
  "textContent": "Since `HF_TOKEN` is used more often these days, I recommend setting it as a Spaces secret (an environment variable that isn’t visible to others; regular environment variables are visible to others) :\n\n* * *\n\nSet it in your **Hugging Face Space** , not in your local shell and not only in your account token page.\n\nFor this tutorial, the right place is:\n\n**Your Space → Settings → Variables and secrets → New secret**\n\nThen your Gradio app can read it with `os.getenv(...)` at runtime. In a Gradio Space, secrets and variables are exposed to the app process as environment variables. Hugging Face also says tokens belong in **Secrets** , while non-sensitive config belongs in **Variables**. (Hugging Face)\n\nAt a high level, your Space is the hosted runtime for the MCP client app, so it reads runtime config from its own environment rather than from your laptop.\n\n## The exact answer\n\nIf your code is:\n\n\n    model = InferenceClientModel(token=os.getenv(\"HUGGINGFACE_API_TOKEN\"))\n\n\nthen create a **secret** in the Space with this exact key:\n\n\n    HUGGINGFACE_API_TOKEN\n\n\nand set its value to your Hugging Face access token.\n\nIf the key in the Space is different, `os.getenv(\"HUGGINGFACE_API_TOKEN\")` will return `None`, and your model client will not authenticate. The name must match exactly. This follows from how Spaces expose secrets as environment variables and how `os.getenv(...)` reads by key name. (Hugging Face)\n\n## Why this is the right place\n\nThere are two separate things here:\n\n### 1. Your Hugging Face account token\n\nYou create the token in your account settings. Hugging Face recommends User Access Tokens for authentication. (Hugging Face)\n\n### 2. Your Space runtime configuration\n\nYour deployed Gradio app does not automatically inherit your laptop’s environment or your account page state. It runs inside the Space runtime. For Gradio and other non-Static, non-Docker Spaces, Hugging Face exposes secrets and variables to the app as environment variables. (Hugging Face)\n\nSo the flow is:\n\n  1. Create the token in your account.\n  2. Open the Space where the app runs.\n  3. Put that token into the Space as a **secret**.\n  4. Read it in Python with `os.getenv(...)`. (Hugging Face)\n\n\n\n## Step by step in the UI\n\nOpen your Space and do this:\n\n  1. Go to the Space page.\n\n  2. Click **Settings**.\n\n  3. Find **Variables and secrets**.\n\n  4. Click **New secret**.\n\n  5. Enter:\n\n     * **Key** : `HUGGINGFACE_API_TOKEN`\n     * **Value** : your Hugging Face access token\n  6. Save.\n\n\n\n\nAfter that, the Space restarts. Hugging Face documents that changing Space configuration such as secrets triggers an app restart. (Hugging Face)\n\n## Secret or variable?\n\nUse a **secret**.\n\nHugging Face’s Spaces docs are explicit:\n\n  * **Variables** are for non-sensitive values and are publicly accessible/viewable.\n  * **Secrets** are for access tokens, API keys, and other sensitive credentials. Their values are private and are not readable back from the Space settings page once set. (Hugging Face)\n\n\n\nSince your Hugging Face token is a credential, it should be a **secret** , not a variable. (Hugging Face)\n\n## Why this tutorial is confusing\n\nThe tutorial page itself mixes two different environment variable names.\n\nIn one part, the course source says to set the access token with the environment variable `HF_TOKEN` and shows:\n\n\n    model = InferenceClientModel(token=os.getenv(\"HF_TOKEN\"))\n\n\nBut in the complete example later on the same page, it uses:\n\n\n    model = InferenceClientModel(token=os.getenv(\"HUGGINGFACE_API_TOKEN\"))\n\n\nBoth can work. The key rule is simple:\n\n> The secret name in the Space must exactly match the name inside `os.getenv(...)`.\n\nThat inconsistency is in the tutorial source itself. (GitHub)\n\n## Which name should you use?\n\nUse **`HF_TOKEN`** if you want the most standard Hugging Face setup.\n\nWhy:\n\n  * Hugging Face’s `huggingface_hub` quick start explicitly says the environment variable `HF_TOKEN` can be used for authentication.\n  * It also says this is especially useful in a Space, where you can set `HF_TOKEN` as a Space secret. (Hugging Face)\n\n\n\nSo the most conventional version is:\n\n\n    import os\n    from smolagents import InferenceClientModel\n\n    model = InferenceClientModel(token=os.getenv(\"HF_TOKEN\"))\n\n\nand then in the Space settings, create a secret named:\n\n\n    HF_TOKEN\n\n\nThat is the cleanest option because it matches Hugging Face’s own auth documentation. (Hugging Face)\n\n## What to do in your specific case\n\nYou have two valid options.\n\n### Option A. Keep your current code unchanged\n\nYour current code:\n\n\n    model = InferenceClientModel(token=os.getenv(\"HUGGINGFACE_API_TOKEN\"))\n\n\nThen create this Space secret:\n\n\n    HUGGINGFACE_API_TOKEN\n\n\nThis works because the names match. The tutorial’s complete example uses that exact pattern. (GitHub)\n\n### Option B. Switch to the standard name\n\nChange your code to:\n\n\n    model = InferenceClientModel(token=os.getenv(\"HF_TOKEN\"))\n\n\nThen create this Space secret:\n\n\n    HF_TOKEN\n\n\nThis is the option I recommend because it aligns with Hugging Face’s official quick start. (Hugging Face)\n\n## The background that matters\n\nA lot of confusion comes from treating these as the same thing:\n\n  * a token stored on your machine\n  * a token created in your account\n  * a token available to a deployed Space\n\n\n\nThey are not the same.\n\nLocally, you might authenticate with `hf auth login` or a local environment variable. But when the app is running inside a Space, the Space needs its own runtime secret. Hugging Face says authentication via environment variable or secret has priority, and specifically calls out `HF_TOKEN` as useful in Spaces. (Hugging Face)\n\nSo the deployed Space does not care whether your laptop shell has:\n\n\n    export HF_TOKEN=...\n\n\nunless you are running the app locally on that same machine. Once deployed, the Space uses its own configured secrets. (Hugging Face)\n\n## A safe version of the code\n\nThis is the version I would actually use:\n\n\n    import os\n    from smolagents import InferenceClientModel\n\n    hf_token = os.getenv(\"HF_TOKEN\")\n    if not hf_token:\n        raise ValueError(\"HF_TOKEN is missing. Add it in your Space: Settings → Variables and secrets → New secret\")\n\n    model = InferenceClientModel(token=hf_token)\n\n\nThis helps because the failure becomes immediate and obvious instead of appearing later as a vague authentication or inference error.\n\nIf you want to keep the tutorial’s alternate name, use the same pattern with `HUGGINGFACE_API_TOKEN` instead. The logic is the same. (GitHub)\n\n## One more detail about Space types\n\nHugging Face distinguishes Space SDKs:\n\n  * **Gradio / most non-Static Spaces** : secrets and variables are exposed as environment variables to the app.\n  * **Static Spaces** : values are exposed differently, through client-side JavaScript.\n  * **Docker Spaces** : environment handling is documented separately. (Hugging Face)\n\n\n\nSince you are following a **Gradio MCP client** tutorial, the normal `os.getenv(...)` server-side pattern is the correct one. (Hugging Face)\n\n## The clean final recommendation\n\nFor the least confusing setup:\n\n  1. Create a Hugging Face User Access Token. (Hugging Face)\n  2. Open your Space.\n  3. Go to **Settings → Variables and secrets**.\n  4. Add a **New secret** named `HF_TOKEN`.\n  5. In code, use:\n\n\n\n\n    model = InferenceClientModel(token=os.getenv(\"HF_TOKEN\"))\n\n\nThat is the best match to the official Hugging Face docs and the clearest way to avoid the tutorial’s naming inconsistency. (Hugging Face)",
  "title": "Where to set environment variable for mcp-client tutorial?"
}