DEV Community

Cover image for Dynamic Configuration for MCP Servers Using Environment Variables
Krzysztof Żuraw for Saleor Commerce

Posted on

4 1

Dynamic Configuration for MCP Servers Using Environment Variables

This blog post explains how to configure Model Context Protocol (MCP) server that can be dynamically controlled through environment variables.

Problem

When setting up mcp-graphql, we needed a way for the MCP server to use a dynamic GraphQL endpoint and token. Hardcoding these values was not an option, as different Saleor environments require different configurations.

Solution

Here’s what we did:

  1. Created a shell script to manage the mcp-graphql startup.
  2. Loaded the endpoint and token from a .env file within the script.
  3. Launched the MCP server using these environment variables.
  4. Used this shell script as the entry point for MCP configuration in clients like Cursor or VSCode

Below is the shell script (start-mcp-graphql.sh) that performs steps 1–3:

#!/usr/bin/env bash

set -a
# Load environment variables from .env file next to this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/.env"
set +a

ENDPOINT="$MCP_GRAPHQL_ENDPOINT" HEADERS="{\"Authorization\":\"Bearer $MCP_GRAPHQL_TOKEN\"}" npx mcp-graphql
Enter fullscreen mode Exit fullscreen mode

Using the Script in MCP Clients

To use this setup with Cursor, add the following to your configuration:

{
  "mcpServers": {
    "Saleor GraphQL API": {
      "command": "./mcp/start-mcp-graphql.sh"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

For VSCode, configure the server as follows:

{
  "servers": {
    "Saleor GraphQL API": {
      "type": "stdio",
      "command": "${workspaceFolder}/mcp/start-mcp-graphql.sh"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Summary

With this approach, you can easily configure your MCP server dynamically using environment variables. You’ll find all the source code, scripts, and editor configs ready to explore in the Saleor apps repository.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
nevodavid profile image
Nevo David

mad respect for making the config flexible like this, saves me a ton of pain - you ever find there’s one annoying gotcha that pops up with env setups in the long run

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Discover this thought-provoking article in the thriving DEV Community. Developers of every background are encouraged to jump in, share expertise, and uplift our collective knowledge.

A simple "thank you" can make someone's day—drop your kudos in the comments!

On DEV, spreading insights lights the path forward and bonds us. If you appreciated this write-up, a brief note of appreciation to the author speaks volumes.

Get Started