gcp-utils-get-project-id
Retrieve your current Google Cloud project ID and view recent project history for managing cloud resources and configurations.
Instructions
Get the current Google Cloud project ID and recent project history
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/project-tools.ts:66-108 (handler)The core handler function that executes the tool logic: retrieves current project ID from state or auth, fetches recent projects, formats as markdown, handles errors.async () => { try { // Get the current project ID from the state manager first let projectId = stateManager.getCurrentProjectId(); // If not available in state manager, try to get it from auth if (!projectId) { projectId = await getProjectId(); } const recentProjectIds = await getRecentProjectIds(); let markdown = `# Current Google Cloud Project\n\nCurrent project ID: \`${projectId}\`\n\n`; if (recentProjectIds.length > 0) { markdown += "## Recently Used Projects\n\n"; for (const id of recentProjectIds) { markdown += `- \`${id}\`${id === projectId ? " (current)" : ""}\n`; } } return { content: [ { type: "text", text: markdown, }, ], }; } catch (error: any) { logger.error( `Error in get-project-id tool: ${error instanceof Error ? error.message : String(error)}`, ); return { content: [ { type: "text", text: `# Error Getting Project ID\n\nFailed to get project ID: ${error.message}`, }, ], }; } },
- src/utils/project-tools.ts:58-109 (registration)The server.registerTool call that registers the 'gcp-utils-get-project-id' tool with schema and inline handler.server.registerTool( "gcp-utils-get-project-id", { title: "Get Project ID", description: "Get the current Google Cloud project ID and recent project history", inputSchema: {}, }, async () => { try { // Get the current project ID from the state manager first let projectId = stateManager.getCurrentProjectId(); // If not available in state manager, try to get it from auth if (!projectId) { projectId = await getProjectId(); } const recentProjectIds = await getRecentProjectIds(); let markdown = `# Current Google Cloud Project\n\nCurrent project ID: \`${projectId}\`\n\n`; if (recentProjectIds.length > 0) { markdown += "## Recently Used Projects\n\n"; for (const id of recentProjectIds) { markdown += `- \`${id}\`${id === projectId ? " (current)" : ""}\n`; } } return { content: [ { type: "text", text: markdown, }, ], }; } catch (error: any) { logger.error( `Error in get-project-id tool: ${error instanceof Error ? error.message : String(error)}`, ); return { content: [ { type: "text", text: `# Error Getting Project ID\n\nFailed to get project ID: ${error.message}`, }, ], }; } }, );
- src/utils/project-tools.ts:60-65 (schema)The input schema (empty), title, and description for the tool.{ title: "Get Project ID", description: "Get the current Google Cloud project ID and recent project history", inputSchema: {}, },
- src/utils/auth.ts:155-271 (helper)Key helper function called by the handler to fallback-retrieve the project ID from state, env, credentials, config, or GoogleAuth client.export async function getProjectId(requireAuth = true): Promise<string> { try { // First check the state manager (fastest and most reliable method) const stateProjectId = stateManager.getCurrentProjectId(); if (stateProjectId) { logger.debug(`Using project ID from state manager: ${stateProjectId}`); return stateProjectId; } // Next check environment variable if (process.env.GOOGLE_CLOUD_PROJECT) { logger.debug( `Using project ID from environment: ${process.env.GOOGLE_CLOUD_PROJECT}`, ); // Store in state manager for future use await stateManager.setCurrentProjectId(process.env.GOOGLE_CLOUD_PROJECT); return process.env.GOOGLE_CLOUD_PROJECT; } // Check if we have credentials file and try to extract project ID from it if (process.env.GOOGLE_APPLICATION_CREDENTIALS) { try { const credentialsPath = process.env.GOOGLE_APPLICATION_CREDENTIALS; logger.debug( `Attempting to read project ID from credentials file: ${credentialsPath}`, ); if (fs.existsSync(credentialsPath)) { const credentialsContent = fs.readFileSync(credentialsPath, "utf8"); const credentials = JSON.parse(credentialsContent); if (credentials.project_id) { logger.debug( `Found project ID in credentials file: ${credentials.project_id}`, ); // Store in state manager for future use await stateManager.setCurrentProjectId(credentials.project_id); return credentials.project_id; } } } catch (fileError) { logger.warn( `Error reading credentials file: ${fileError instanceof Error ? fileError.message : String(fileError)}`, ); // Continue to next method } } // Next check if we have a configured default project ID try { await configManager.initialize(); const configuredProjectId = configManager.getDefaultProjectId(); if (configuredProjectId) { logger.debug(`Using project ID from config: ${configuredProjectId}`); // Store in state manager for future use await stateManager.setCurrentProjectId(configuredProjectId); return configuredProjectId; } } catch (configError) { logger.warn( `Config error: ${configError instanceof Error ? configError.message : String(configError)}`, ); // Continue to next method } // Fall back to getting it from auth client try { logger.debug("Attempting to get project ID from auth client..."); const auth = await initGoogleAuth(requireAuth); if (!auth) { logger.warn("Authentication client not available"); if (requireAuth) { throw new Error( "Google Cloud authentication not available. Please configure authentication to access project ID.", ); } return "unknown-project"; } logger.debug("Auth client available, requesting project ID..."); const projectId = await auth.getProjectId(); if (!projectId) { logger.warn("Auth client returned empty project ID"); if (requireAuth) { throw new Error( "Could not determine Google Cloud project ID. Please set GOOGLE_CLOUD_PROJECT environment variable or use the set-project-id tool.", ); } return "unknown-project"; } logger.debug(`Got project ID from auth client: ${projectId}`); // Store in state manager for future use await stateManager.setCurrentProjectId(projectId); return projectId; } catch (authError) { logger.warn( `Auth error while getting project ID: ${authError instanceof Error ? authError.message : String(authError)}`, ); if (requireAuth) { throw authError; } return "unknown-project"; } } catch (error) { logger.error( `Project ID error: ${error instanceof Error ? error.message : String(error)}`, ); if (requireAuth) { throw error; } return "unknown-project"; } }
- src/utils/state-manager.ts:137-143 (helper)State manager method to get the current project ID from persisted state.* Get the current project ID * * @returns The current project ID or null if not set */ getCurrentProjectId(): string | null { return this.state.currentProjectId; }