config_set_project
Set the default Google Cloud project ID for streamlined resource management in GCP MCP operations. Input the project ID to configure and validate settings.
Instructions
Set the default Google Cloud project.
Args:
project_id: The ID of the project to set as default
Returns:
Status message indicating whether the project was set
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes |
Implementation Reference
- The core handler function for the 'config_set_project' tool. Decorated with @mcp.tool() for automatic registration. Updates the global project ID, writes to gcloud config file using helper, and verifies project accessibility using GCP Resource Manager API.@mcp.tool() def config_set_project(project_id: str) -> str: """ Set the default Google Cloud project. Args: project_id: The ID of the project to set as default Returns: Status message indicating whether the project was set """ nonlocal _current_project_id try: # Update global project ID _current_project_id = project_id # Create or update the config file _set_project_id_in_config(project_id) # Verify the project exists try: from google.cloud import resourcemanager_v3 import google.auth credentials, _ = google.auth.default() client = resourcemanager_v3.ProjectsClient(credentials=credentials) name = f"projects/{project_id}" try: project = client.get_project(name=name) return f"Default project set to {project_id} ({project.display_name})." except Exception: # Project might not exist or user might not have access return f"Default project set to {project_id}. Note: Could not verify if this project exists or if you have access to it." except Exception as e: # Don't fail if we can't verify the project return f"Default project set to {project_id}." except Exception as e: return f"Error setting project: {str(e)}"
- Helper function called by the handler to persist the project ID in the standard gcloud configuration file (~/.config/gcloud/configurations/config_default).def _set_project_id_in_config(project_id: str) -> None: """Set the project ID in the configuration file.""" config_dir = _get_config_path() os.makedirs(config_dir, exist_ok=True) config_file = os.path.join(config_dir, 'configurations', 'config_default') os.makedirs(os.path.dirname(config_file), exist_ok=True) # Read existing config if it exists config_data = {} if os.path.exists(config_file): try: with open(config_file, 'r') as f: for line in f: if '=' in line: key, value = line.strip().split('=', 1) config_data[key.strip()] = value.strip() except: pass # Update project config_data['project'] = project_id # Write back config with open(config_file, 'w') as f: for key, value in config_data.items(): f.write(f"{key} = {value}\n")
- src/gcp_mcp/gcp_modules/auth/tools.py:249-249 (registration)The @mcp.tool() decorator registers the config_set_project function as an MCP tool within the register_tools(mcp) function.@mcp.tool()