get-project-id
Retrieve the active Google Cloud project ID to configure authentication and access cloud services through the Google Cloud MCP Server.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get-project-id"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/project-tools.ts:45-86 (handler)Primary implementation of the 'get-project-id' tool: registers the tool with empty input schema and provides the async handler function that fetches the current project ID (from stateManager or getProjectId helper), retrieves recent project IDs, formats a markdown response with current and recent projects, handles errors gracefully.
// Tool to get the current project ID server.tool( 'get-project-id', {}, async (_, context) => { 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) { console.error('Error in get-project-id tool:', error); return { content: [{ type: 'text', text: `# Error Getting Project ID\n\nFailed to get project ID: ${error.message}` }] }; } } ); - src/index.ts:165-165 (registration)Top-level registration call that invokes registerProjectTools(server), which in turn registers the 'get-project-id' tool.
registerProjectTools(server); - src/utils/auth.ts:127-225 (helper)Key helper function 'getProjectId()' called by the tool handler to retrieve the actual project ID from multiple fallback sources: state manager, environment variables, credentials file, config manager, 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) { console.log(`Using project ID from state manager: ${stateProjectId}`); return stateProjectId; } // Next check environment variable if (process.env.GOOGLE_CLOUD_PROJECT) { console.log(`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; console.log(`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) { console.log(`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) { console.error(`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) { console.log(`Using project ID from config: ${configuredProjectId}`); // Store in state manager for future use await stateManager.setCurrentProjectId(configuredProjectId); return configuredProjectId; } } catch (configError) { console.error(`Config error: ${configError instanceof Error ? configError.message : String(configError)}`); // Continue to next method } // Fall back to getting it from auth client try { console.log('Attempting to get project ID from auth client...'); const auth = await initGoogleAuth(requireAuth); if (!auth) { console.error('Authentication client not available'); if (requireAuth) { throw new Error('Google Cloud authentication not available. Please configure authentication to access project ID.'); } return 'unknown-project'; } console.log('Auth client available, requesting project ID...'); const projectId = await auth.getProjectId(); if (!projectId) { console.error('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'; } console.log(`Got project ID from auth client: ${projectId}`); // Store in state manager for future use await stateManager.setCurrentProjectId(projectId); return projectId; } catch (authError) { console.error(`Auth error while getting project ID: ${authError instanceof Error ? authError.message : String(authError)}`); if (requireAuth) { throw authError; } return 'unknown-project'; } } catch (error) { console.error(`Project ID error: ${error instanceof Error ? error.message : String(error)}`); if (requireAuth) { throw error; } return 'unknown-project'; } }