Closed
Labels
P2Moderate issues affecting some users, edge cases, potentially valuable featureModerate issues affecting some users, edge cases, potentially valuable featuredocumentationImprovements or additions to documentationImprovements or additions to documentationquestionFurther information is requestedFurther information is requestedready for workEnough information for someone to start working onEnough information for someone to start working on
Description
Question
I haven't been able to figure out how to get environment variables in tool functions on an MCP server, or if they're actually supported.
This should be a valid client configuration:
{
"mcpServers": {
"php-calculator": {
"command": "python",
"args": ["/absolute/path/to/your/mcp-server.py"],
"env": {"API_KEY": "xxxx"}
}
}
}How can I get the API_KEY environment variable on the server?
Additional Context
No response
Metadata
Metadata
Assignees
Labels
P2Moderate issues affecting some users, edge cases, potentially valuable featureModerate issues affecting some users, edge cases, potentially valuable featuredocumentationImprovements or additions to documentationImprovements or additions to documentationquestionFurther information is requestedFurther information is requestedready for workEnough information for someone to start working onEnough information for someone to start working on
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