|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Environment Variable Demo MCP Server using FastMCP |
| 4 | +
|
| 5 | +This server demonstrates the environment variable passing capability. |
| 6 | +It reads environment variables that are configured in mcp.json and |
| 7 | +exposes them through MCP tools. |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import time |
| 12 | +from typing import Any, Dict |
| 13 | + |
| 14 | +from fastmcp import FastMCP |
| 15 | + |
| 16 | +# Initialize the MCP server |
| 17 | +mcp = FastMCP("Environment Variable Demo") |
| 18 | + |
| 19 | + |
| 20 | +@mcp.tool |
| 21 | +def get_env_var(var_name: str) -> Dict[str, Any]: |
| 22 | + """Get the value of a specific environment variable. |
| 23 | + |
| 24 | + This tool demonstrates how environment variables configured in mcp.json |
| 25 | + are passed to the MCP server process. |
| 26 | + |
| 27 | + Args: |
| 28 | + var_name: Name of the environment variable to retrieve |
| 29 | + |
| 30 | + Returns: |
| 31 | + MCP contract shape with the environment variable value: |
| 32 | + { |
| 33 | + "results": { |
| 34 | + "var_name": str, |
| 35 | + "var_value": str or None, |
| 36 | + "is_set": bool |
| 37 | + }, |
| 38 | + "meta_data": { |
| 39 | + "elapsed_ms": float |
| 40 | + } |
| 41 | + } |
| 42 | + """ |
| 43 | + start = time.perf_counter() |
| 44 | + |
| 45 | + var_value = os.environ.get(var_name) |
| 46 | + is_set = var_name in os.environ |
| 47 | + |
| 48 | + elapsed_ms = round((time.perf_counter() - start) * 1000, 3) |
| 49 | + |
| 50 | + return { |
| 51 | + "results": { |
| 52 | + "var_name": var_name, |
| 53 | + "var_value": var_value, |
| 54 | + "is_set": is_set |
| 55 | + }, |
| 56 | + "meta_data": { |
| 57 | + "elapsed_ms": elapsed_ms |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +@mcp.tool |
| 63 | +def list_configured_env_vars() -> Dict[str, Any]: |
| 64 | + """List all environment variables that were configured in mcp.json. |
| 65 | + |
| 66 | + This tool shows which environment variables from the mcp.json configuration |
| 67 | + are available to this server. It returns commonly expected configuration |
| 68 | + variables that might be set. |
| 69 | + |
| 70 | + Returns: |
| 71 | + MCP contract shape with environment variables: |
| 72 | + { |
| 73 | + "results": { |
| 74 | + "configured_vars": dict of var_name -> var_value, |
| 75 | + "total_count": int |
| 76 | + }, |
| 77 | + "meta_data": { |
| 78 | + "elapsed_ms": float |
| 79 | + } |
| 80 | + } |
| 81 | + """ |
| 82 | + start = time.perf_counter() |
| 83 | + |
| 84 | + # List of common configuration environment variables |
| 85 | + # This demonstrates what might be passed from mcp.json |
| 86 | + common_config_vars = [ |
| 87 | + "CLOUD_PROFILE", |
| 88 | + "CLOUD_REGION", |
| 89 | + "API_KEY", |
| 90 | + "DEBUG_MODE", |
| 91 | + "MAX_RETRIES", |
| 92 | + "TIMEOUT_SECONDS", |
| 93 | + "ENVIRONMENT", |
| 94 | + "SERVICE_URL" |
| 95 | + ] |
| 96 | + |
| 97 | + configured_vars = {} |
| 98 | + for var_name in common_config_vars: |
| 99 | + if var_name in os.environ: |
| 100 | + configured_vars[var_name] = os.environ[var_name] |
| 101 | + |
| 102 | + elapsed_ms = round((time.perf_counter() - start) * 1000, 3) |
| 103 | + |
| 104 | + return { |
| 105 | + "results": { |
| 106 | + "configured_vars": configured_vars, |
| 107 | + "total_count": len(configured_vars) |
| 108 | + }, |
| 109 | + "meta_data": { |
| 110 | + "elapsed_ms": elapsed_ms |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | +@mcp.tool |
| 116 | +def demonstrate_env_usage(operation: str = "info") -> Dict[str, Any]: |
| 117 | + """Demonstrate how environment variables can be used in MCP server operations. |
| 118 | + |
| 119 | + This tool shows practical examples of using environment variables for: |
| 120 | + - Configuration (e.g., region, profile) |
| 121 | + - Feature flags (e.g., debug mode) |
| 122 | + - API credentials (e.g., API keys) |
| 123 | + |
| 124 | + Args: |
| 125 | + operation: Type of demonstration ("info", "config", "credentials") |
| 126 | + |
| 127 | + Returns: |
| 128 | + MCP contract shape with demonstration results: |
| 129 | + { |
| 130 | + "results": { |
| 131 | + "operation": str, |
| 132 | + "example": str, |
| 133 | + "details": dict |
| 134 | + }, |
| 135 | + "meta_data": { |
| 136 | + "elapsed_ms": float |
| 137 | + } |
| 138 | + } |
| 139 | + """ |
| 140 | + start = time.perf_counter() |
| 141 | + |
| 142 | + if operation == "config": |
| 143 | + # Demonstrate configuration from environment |
| 144 | + cloud_profile = os.environ.get("CLOUD_PROFILE", "default") |
| 145 | + cloud_region = os.environ.get("CLOUD_REGION", "us-east-1") |
| 146 | + |
| 147 | + example = f"Using cloud profile '{cloud_profile}' in region '{cloud_region}'" |
| 148 | + details = { |
| 149 | + "profile": cloud_profile, |
| 150 | + "region": cloud_region, |
| 151 | + "source": "environment variables from mcp.json" |
| 152 | + } |
| 153 | + |
| 154 | + elif operation == "credentials": |
| 155 | + # Demonstrate secure credential handling |
| 156 | + api_key = os.environ.get("API_KEY") |
| 157 | + has_key = api_key is not None |
| 158 | + |
| 159 | + example = f"API key is {'configured' if has_key else 'not configured'}" |
| 160 | + details = { |
| 161 | + "has_api_key": has_key, |
| 162 | + "key_length": len(api_key) if api_key else 0, |
| 163 | + "masked_key": f"{api_key[:4]}...{api_key[-4:]}" if api_key and len(api_key) > 8 else None, |
| 164 | + "source": "environment variable ${API_KEY} from mcp.json" |
| 165 | + } |
| 166 | + |
| 167 | + else: # info |
| 168 | + example = "Environment variables can be configured in mcp.json" |
| 169 | + details = { |
| 170 | + "usage": "Set env dict in mcp.json server configuration", |
| 171 | + "syntax": { |
| 172 | + "literal": "KEY: 'literal-value'", |
| 173 | + "substitution": "KEY: '${SYSTEM_ENV_VAR}'" |
| 174 | + }, |
| 175 | + "example_config": { |
| 176 | + "env": { |
| 177 | + "CLOUD_PROFILE": "my-profile-9", |
| 178 | + "CLOUD_REGION": "us-east-7", |
| 179 | + "API_KEY": "${MY_API_KEY}" |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + elapsed_ms = round((time.perf_counter() - start) * 1000, 3) |
| 185 | + |
| 186 | + return { |
| 187 | + "results": { |
| 188 | + "operation": operation, |
| 189 | + "example": example, |
| 190 | + "details": details |
| 191 | + }, |
| 192 | + "meta_data": { |
| 193 | + "elapsed_ms": elapsed_ms |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + mcp.run() |
0 commit comments