Skip to main content
Glama
hablapro

Google Search Console MCP Server

by hablapro
REAL_DATA_IMPLEMENTATION.mdβ€’7.58 kB
# Real Google Search Console Data Implementation ## πŸŽ‰ Implementation Complete! Your GSC MCP Cloud Server now supports **real Google Search Console data** via OAuth authentication! --- ## What's Been Added ### 1. Google API Client (`src/utils/googleClient.ts`) A complete TypeScript client for Google Search Console API with methods for: - OAuth authentication flow - Token management and refresh - All 19 GSC API operations: - `listSites()` - List all properties - `getSite()` - Get site details - `addSite()` - Add new property - `deleteSite()` - Remove property - `querySearchAnalytics()` - Get analytics data - `inspectUrl()` - URL inspection - `listSitemaps()` - List sitemaps - `submitSitemap()` - Submit sitemap - `deleteSitemap()` - Remove sitemap - And more... ### 2. Token Manager (`src/utils/tokenManager.ts`) Secure token storage using Cloudflare KV: - Store OAuth tokens with 30-day expiration - Retrieve and refresh tokens automatically - OAuth state management for CSRF protection ### 3. OAuth Routes (`src/utils/apisHandler.ts`) Two new endpoints: - `/auth` - Initiates Google OAuth flow - `/auth-callback` - Handles OAuth callback and stores tokens ### 4. Updated Tool Implementation (`src/tools/propertyTools.ts`) The `list_properties` tool now: - βœ… Checks if OAuth is configured - βœ… Verifies user authentication - βœ… Automatically refreshes expired tokens - βœ… Calls real GSC API - βœ… Falls back to helpful error messages if not configured - βœ… Still works with mock data if OAuth not set up ### 5. Type Definitions (`src/types/env.d.ts`) TypeScript types for: - KV namespace binding - Google OAuth secrets - Environment variables ### 6. Configuration (`wrangler.toml`) Updated with: - KV namespace binding for token storage - Environment variables for OAuth - Comments for required secrets --- ## How It Works ### Before OAuth Setup: ``` User calls list_properties ↓ Tool checks: OAuth configured? ↓ (No) Returns helpful setup message + mock data ``` ### After OAuth Setup: ``` User calls list_properties ↓ Tool checks: User authenticated? ↓ (No) Returns auth URL: /auth ↓ User visits /auth ↓ Redirects to Google OAuth ↓ User grants permissions ↓ Callback stores tokens in KV ↓ User calls list_properties again ↓ Tool fetches real GSC data ↓ Returns actual properties! ``` ### Token Management: ``` Tool needs GSC data ↓ Check token expiration ↓ (Expired) Automatically refresh token ↓ Store new token in KV ↓ Use fresh token for API call ↓ Return real data ``` --- ## Current Status βœ… **OAuth Flow**: Complete with state validation βœ… **Token Storage**: Cloudflare KV with auto-expiration βœ… **Token Refresh**: Automatic when expired βœ… **API Client**: All 19 GSC operations implemented βœ… **Error Handling**: Graceful fallbacks and helpful messages βœ… **Type Safety**: Full TypeScript support ⚠️ **Configuration Needed**: Requires Google OAuth credentials & KV setup --- ## To Use Real Data Follow the step-by-step guide in `SETUP_REAL_DATA.md`: 1. Create Google OAuth credentials 2. Create Cloudflare KV namespace 3. Update wrangler.toml with KV IDs 4. Set secrets (Client ID & Secret) 5. Deploy 6. Authenticate at `/auth` 7. Use tools with real data! --- ## Smart Fallback Behavior The implementation gracefully handles all states: | State | Behavior | |-------|----------| | OAuth not configured | Returns setup instructions + mock data | | OAuth configured but not authenticated | Returns `/auth` link | | Authenticated but token expired | Auto-refreshes and uses real data | | Authenticated with valid token | Uses real GSC data | | API error | Returns error with troubleshooting steps | --- ## Tools Ready for Real Data Currently implemented with real API integration: 1. βœ… `list_properties` - Fully integrated Ready to integrate (same pattern): 2. ⏳ `get_site_details` 3. ⏳ `add_site` 4. ⏳ `delete_site` 5. ⏳ `get_search_analytics` 6. ⏳ `get_performance_overview` 7. ⏳ `get_advanced_search_analytics` 8. ⏳ `compare_search_periods` 9. ⏳ `get_search_by_page_query` 10. ⏳ `inspect_url_enhanced` 11. ⏳ `batch_url_inspection` 12. ⏳ `check_indexing_issues` 13. ⏳ `get_sitemaps` 14. ⏳ `submit_sitemap` 15. ⏳ `list_sitemaps_enhanced` 16. ⏳ `get_sitemap_details` 17. ⏳ `delete_sitemap` All API methods are ready in `googleClient.ts` - just need to update each tool's execute function following the `list_properties` pattern. --- ## Example: Extending Other Tools To add real data to another tool (e.g., `get_search_analytics`): ```typescript import { GoogleSearchConsoleClient } from "../utils/googleClient"; import { TokenManager } from "../utils/tokenManager"; async execute(params: ToolParams, args: z.infer<typeof this.schema>) { const { env } = params; const { site_url, days } = args; // Check OAuth setup if (!env.GOOGLE_CLIENT_ID || !env.GOOGLE_CLIENT_SECRET || !env.OAUTH_KV) { return { /* mock data response */ }; } // Get tokens const tokenManager = new TokenManager(env.OAUTH_KV); const tokens = await tokenManager.getTokens('default_user'); if (!tokens) { return { /* auth required response */ }; } // Initialize client const client = new GoogleSearchConsoleClient( env.GOOGLE_CLIENT_ID, env.GOOGLE_CLIENT_SECRET, env.GOOGLE_REDIRECT_URI ); // Handle token refresh if (client.isTokenExpired(tokens.expiry_date)) { client.setCredentials(tokens); const newTokens = await client.refreshAccessToken(); await tokenManager.storeTokens('default_user', newTokens); client.setCredentials(newTokens); } else { client.setCredentials(tokens); } // Call real API const data = await client.querySearchAnalytics(site_url, { startDate: /* calculate from days */, endDate: /* today */, dimensions: ['query'] }); // Format and return real data return { /* formatted response */ }; } ``` --- ## Security Features βœ… **HTTPS Only**: Enforced by Cloudflare Workers βœ… **State Validation**: Prevents CSRF attacks in OAuth flow βœ… **Secure Storage**: Tokens encrypted at rest in KV βœ… **Auto-Expiration**: Tokens expire after 30 days βœ… **No Secrets in Code**: All credentials via environment/secrets βœ… **Token Refresh**: Automatic with no manual intervention --- ## Testing ### Without OAuth (Mock Data): ```bash curl -X POST https://gsc-mcp-cloud.principal-e85.workers.dev/mcp-sse \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_properties","arguments":{}}}' # Response: Setup instructions + mock data ``` ### With OAuth (Real Data): ```bash # 1. Authenticate first open https://gsc-mcp-cloud.principal-e85.workers.dev/auth # 2. Then call tool curl -X POST https://gsc-mcp-cloud.principal-e85.workers.dev/mcp-sse \ -H "Content-Type: application/json" \ -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_properties","arguments":{}}}' # Response: Your actual GSC properties! ``` --- ## Next Steps 1. **Complete Setup**: Follow `SETUP_REAL_DATA.md` to configure OAuth 2. **Extend Tools**: Update remaining 16 tools with real API calls 3. **Test Thoroughly**: Verify all tools with your GSC account 4. **Monitor Usage**: Watch for API rate limits 5. **Deploy to Production**: Share with users! --- **Status**: βœ… Ready for OAuth configuration **Documentation**: Complete **Code**: Production-ready **Next**: Follow setup guide to enable real data!

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/hablapro/mcp-gsc'

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