You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to get environment variables on the server? #1277
Added a dedicated documentation page covering environment variable patterns — accessing them in tool functions, passing them via Claude Desktop config / mcp install CLI / programmatic clients, and the built-in MCP_-prefixed settings.
The env block in that config becomes the environment of the process the host launches your server in, so inside a tool it's just standard Python: os.environ["API_KEY"] or os.getenv("API_KEY").
importosfrommcp.server.mcpserverimportMCPServermcp=MCPServer("php-calculator")
@mcp.tool()defcheck_key() ->str:
api_key=os.environ["API_KEY"] # from the client config's "env" blockreturnf"got a {len(api_key)}-character key"
As felixweinberger noted, how the variable gets set is up to the host. For Claude Desktop the config file (including its env field) is covered in Connect to a real host → Claude Desktop, and mcp install -v KEY=VALUE / -f .env will write the variables into that entry for you — see Running your server → The mcp command.
Closing this as part of a wider backlog cleanup around the v2 release. Feel free to reopen if this is still relevant.
Activity
felixweinberger commentedon Oct 7, 2025
This depends on the environment you're running your MCP server in - there's an example for desktop in the README here: https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov-file#claude-desktop-integration
But probably worth adding something to docs on patterns for how to deal with this common use case.
docs: add environment variables guide
ameenalkhaldi commentedon Mar 15, 2026
I've opened a PR addressing this: #2296
Added a dedicated documentation page covering environment variable patterns — accessing them in tool functions, passing them via Claude Desktop config /
mcp installCLI / programmatic clients, and the built-inMCP_-prefixed settings.maxisbey commentedon Jul 24, 2026
The
envblock in that config becomes the environment of the process the host launches your server in, so inside a tool it's just standard Python:os.environ["API_KEY"]oros.getenv("API_KEY").As felixweinberger noted, how the variable gets set is up to the host. For Claude Desktop the config file (including its
envfield) is covered in Connect to a real host → Claude Desktop, andmcp install -v KEY=VALUE/-f .envwill write the variables into that entry for you — see Running your server → Themcpcommand.Closing this as part of a wider backlog cleanup around the v2 release. Feel free to reopen if this is still relevant.
AI Disclaimer