MCP · Lesson 72 of 120
Configuration
Environment variables.
Servers need settings. A database path, an API key, a price threshold. Don't hard code them. Read them from environment variables, so each host can set its own.

- Settings
- Not in code
- Environment variables
Reading settings
The server reads the free delivery threshold from an environment variable, with a default of fifty. Our demo sets it to one hundred before loading the server. Run it. Now a seventy five pound order is no longer free.
config.pypython
import os
os.environ["FREE_FROM"] = "100" # a host would set this
from mcp.server.mcpserver import MCPServer
from tester import call
FREE_FROM = float(os.environ.get("FREE_FROM", "50"))
mcp = MCPServer("northwind")
@mcp.tool()
def delivery_cost(order_total: float) -> str:
"""UK delivery cost for an order total in pounds."""
return "free" if order_total >= FREE_FROM else "£4.99"
print("threshold:", FREE_FROM)
print("£75 order:", call(mcp, "delivery_cost", {"order_total": 75}).content[0].text)Output
threshold: 100.0 £75 order: £4.99
Also uses tester.py
import asyncio
from mcp import Client
def call(server, tool, args=None, **options):
async def go():
async with Client(server, **options) as client:
return await client.call_tool(tool, args or {})
return asyncio.run(go())
def tools(server):
async def go():
async with Client(server) as client:
return (await client.list_tools()).tools
return asyncio.run(go())Good habits
Give every setting a safe default. Keep secrets, like API keys, only in the environment. Check settings at startup, and fail early with a clear message. And list them in your read me.
| key | value |
|---|---|
| Defaults | safe |
| Secrets | environment only |
| Check | at startup |
| Document | in the README |
Summary
Read settings from environment variables, with safe defaults. Next, connecting Claude Desktop.
Key takeaways- os.environ
- Defaults
- Secrets outside code
Next up: Claude Desktop — the config file