get-project-id
Extract the project ID from Google Cloud services using natural language queries with the MCP server. Simplify access to Logging, Spanner, Monitoring, and Cloud Trace data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/utils/project-tools.ts:46-86 (handler)The handler implementation for the 'get-project-id' MCP tool. Registers the tool with no input parameters and executes logic to fetch current project ID from state or auth helpers, compile recent projects list, and return markdown-formatted response. Includes error handling.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/utils/auth.ts:127-225 (helper)Supporting helper function called by the tool handler to resolve the actual project ID from multiple fallback sources: state manager, env var, credentials 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) { 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'; } }
- src/index.ts:165-165 (registration)Top-level registration call in main server setup that invokes registerProjectTools to add the 'get-project-id' tool (and related project tools) to the MCP server.registerProjectTools(server);