gcp-utils-set-project-id
Set the default Google Cloud project ID for all operations, ensuring consistent project context across tasks and interactions within the Google Cloud MCP Server 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 (registration)Registration of the MCP tool 'gcp-utils-set-project-id' including input schema, title, description, and inline handler function that delegates to setProjectId and returns formatted response.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/auth.ts:273-281 (helper)Helper function setProjectId that is called by the tool handler to delegate the project ID setting to the state manager./** * Sets the default project ID to use for all Google Cloud operations * * @param projectId The project ID to set as default */ 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:145-177 (helper)Core state management function that persists the project ID to a JSON state file, updates environment variable, config manager, emits event, and logs the change./** * Set the current project ID * * @param projectId The project ID to set */ 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}`); }