gcp-utils-set-project-id
Set the default Google Cloud project ID for all subsequent billing, monitoring, and resource management operations within the GCP environment.
Instructions
Set the default Google Cloud project ID to use for all operations
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_id | Yes | The Google Cloud project ID to set as default |
Implementation Reference
- src/utils/project-tools.ts:19-55 (handler)The registration and inline handler function for the 'gcp-utils-set-project-id' tool. The handler takes a project_id input, calls setProjectId(project_id), and returns a markdown-formatted success or error message.server.registerTool( "gcp-utils-set-project-id", { title: "Set Project ID", description: "Set the default Google Cloud project ID to use for all operations", inputSchema: { project_id: z .string() .describe("The Google Cloud project ID to set as default"), }, }, async ({ project_id }) => { try { await setProjectId(project_id); return { content: [ { type: "text", text: `# Project ID Updated\n\nDefault Google Cloud project ID has been set to: \`${project_id}\`\n\nThis project ID will be used for all Google Cloud operations until changed.`, }, ], }; } catch (error: any) { // Error handling for set-project-id tool return { content: [ { type: "text", text: `# Error Setting Project ID\n\nFailed to set project ID: ${error.message}`, }, ], }; } }, );
- src/utils/project-tools.ts:21-29 (schema)Input schema definition for the tool using Zod, requiring a string 'project_id'.{ title: "Set Project ID", description: "Set the default Google Cloud project ID to use for all operations", inputSchema: { project_id: z .string() .describe("The Google Cloud project ID to set as default"), },
- src/index.ts:244-244 (registration)Top-level registration call that invokes registerProjectTools(server), which registers the 'gcp-utils-set-project-id' tool among others.registerProjectTools(server);
- src/utils/auth.ts:278-281 (helper)Helper function setProjectId that is called by the tool handler to delegate project ID setting to the state manager.export async function setProjectId(projectId: string): Promise<void> { // Use the state manager to set the project ID await stateManager.setCurrentProjectId(projectId); }
- src/utils/state-manager.ts:150-177 (helper)Core state management logic in StateManager.setCurrentProjectId: updates in-memory state, sets env var, persists to config and state file (~/.google-cloud-mcp/state.json), emits event.async setCurrentProjectId(projectId: string): Promise<void> { if (!projectId) { throw new Error("Project ID cannot be empty"); } // Update in-memory state this.state.currentProjectId = projectId; // Set in environment variable for immediate use process.env.GOOGLE_CLOUD_PROJECT = projectId; // Update config for persistence try { await configManager.setDefaultProjectId(projectId); } catch (error) { logger.warn( `Could not save project ID to config: ${error instanceof Error ? error.message : String(error)}`, ); } // Save state to file await this.saveState(); // Emit change event this.emit("projectIdChanged", projectId); logger.info(`Current project ID set to: ${projectId}`); }