Skip to content

How to get environment variables on the server? #1277

Closed

Description

@ilvalerione

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

Activity

  1. added
    documentationImprovements or additions to documentation
    ready for workEnough information for someone to start working on
    P2Moderate issues affecting some users, edge cases, potentially valuable feature
    on Oct 7, 2025
  2. felixweinberger commented on Oct 7, 2025

    @felixweinberger
    Contributor

    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.

  3. ameenalkhaldi commented on Mar 15, 2026

    @ameenalkhaldi

    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 install CLI / programmatic clients, and the built-in MCP_-prefixed settings.

  4. maxisbey commented on Jul 24, 2026

    @maxisbey
    Contributor

    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").

    import os
    
    from mcp.server.mcpserver import MCPServer
    
    mcp = MCPServer("php-calculator")
    
    
    @mcp.tool()
    def check_key() -> str:
        api_key = os.environ["API_KEY"]  # from the client config's "env" block
        return f"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.

    AI Disclaimer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Moderate issues affecting some users, edge cases, potentially valuable featuredocumentationImprovements or additions to documentationquestionFurther information is requestedready for workEnough information for someone to start working on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions