set-api-key
Configure authentication for carbon emissions calculations by setting your Climatiq API key to enable authorized access to climate impact data.
Instructions
Set the Climatiq API key for authentication. This allows the server to make authorized requests to the Climatiq API.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| api_key | Yes | Your Climatiq API key obtained from app.climatiq.io |
Implementation Reference
- src/climatiq_mcp_server/tools.py:9-54 (handler)The handler function that extracts the api_key from arguments, validates it by making a test API request to Climatiq, stores it in the config if valid, and returns a success message.async def set_api_key_tool(config, arguments, server, climatiq_request): """ Set the Climatiq API key for authentication with the API. This tool configures the Climatiq API key used for all subsequent API calls. The key is stored in memory for the duration of the server session. """ api_key = arguments.get("api_key") if not api_key: raise ValueError("API key is required") # Validate API key by making a test request test_headers = { "Authorization": f"Bearer {api_key}", "Content-Type": "application/json" } try: # Use httpx directly as we can't use climatiq_request yet async with httpx.AsyncClient(timeout=10.0) as client: response = await client.get(f"{config['base_url']}/data/v1/search", params={"query": "electricity", "data_version": config["data_version"]}, headers=test_headers) if response.status_code != 200: error_detail = "Invalid API key or API connection issue" try: error_json = response.json() if "error" in error_json: error_detail = error_json["error"] elif "message" in error_json: error_detail = error_json["message"] except: pass raise ValueError(f"API key validation failed: {error_detail}") # If we get here, the API key is valid config["api_key"] = api_key return "Climatiq API key configured successfully. You can now use other tools to calculate emissions." except httpx.RequestError as e: raise ValueError(f"Failed to connect to Climatiq API: {str(e)}") except Exception as e: raise ValueError(f"Error validating API key: {str(e)}")
- The input schema definition specifying that the tool requires a string 'api_key' parameter.inputSchema={ "type": "object", "properties": { "api_key": {"type": "string", "description": "Your Climatiq API key obtained from app.climatiq.io"}, }, "required": ["api_key"], },
- src/climatiq_mcp_server/tools.py:745-755 (registration)The tool registration in get_tool_definitions() list, including name, description, and input schema.types.Tool( name="set-api-key", description="Set the Climatiq API key for authentication. This allows the server to make authorized requests to the Climatiq API.", inputSchema={ "type": "object", "properties": { "api_key": {"type": "string", "description": "Your Climatiq API key obtained from app.climatiq.io"}, }, "required": ["api_key"], }, ),
- src/climatiq_mcp_server/server.py:246-247 (registration)The dispatch logic in handle_call_tool() that routes 'set-api-key' calls to the set_api_key_tool handler.if name == "set-api-key": result_text = await set_api_key_tool(config, arguments, server, climatiq_request)
- src/climatiq_mcp_server/server.py:229-229 (registration)The list_tools() handler returns the tool definitions from tools.get_tool_definitions(), which includes 'set-api-key'.return get_tool_definitions()