Skip to main content
Glama
krzko

Google Cloud MCP Server

by krzko

gcp-utils-get-project-id

Retrieve and review the current Google Cloud project ID along with recent project history for quick reference and management.

Instructions

Get the current Google Cloud project ID and recent project history

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Handler function for the 'gcp-utils-get-project-id' tool. Retrieves the current project ID (preferring state manager, falling back to getProjectId()), recent project IDs, and returns formatted markdown response or error.
    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}`, }, ], }; } },
  • Schema definition for the 'gcp-utils-get-project-id' tool, specifying title, description, and empty input schema (no parameters required).
    { title: "Get Project ID", description: "Get the current Google Cloud project ID and recent project history", inputSchema: {}, },
  • MCP server tool registration call for 'gcp-utils-get-project-id', including name, schema, and 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}`, }, ], }; } }, );
  • Key helper function 'getProjectId()' invoked by the tool handler. Determines project ID from multiple fallback sources: state manager, environment variable, service account file, 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"; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/krzko/google-cloud-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server