Skip to main content
Glama
domdomegg

Google Maps Places MCP

places-api.ts2.47 kB
// Google Places API (New) configuration and utilities export const PLACES_API_BASE_URL = 'https://places.googleapis.com/v1'; // Common helper to create base headers for OAuth function createBaseHeaders(accessToken: string): Record<string, string> { return { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }; } // Common helper to handle API errors async function handleApiError(response: Response): Promise<never> { const errorText = await response.text(); throw new Error(`Places API error: ${response.status} ${response.statusText} - ${errorText}`); } // Common helper to parse JSON response async function parseJsonResponse(response: Response): Promise<unknown> { if (!response.ok) { await handleApiError(response); } const responseText = await response.text(); if (!responseText.trim()) { return {success: true, message: 'Operation completed successfully'}; } try { return JSON.parse(responseText); } catch (error) { throw new Error(`Failed to parse JSON response: ${error instanceof Error ? error.message : String(error)}`); } } // Utility function to make Places API POST calls (for text search) export async function makePlacesApiCall( endpoint: string, accessToken: string, body: unknown, fieldMask: string, ) { const url = `${PLACES_API_BASE_URL}${endpoint}`; const headers = createBaseHeaders(accessToken); headers['X-Goog-FieldMask'] = fieldMask; const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(body), }); return parseJsonResponse(response); } // Utility function to get photo media URL (returns the photoUri) export async function getPlacesPhotoUrl( photoName: string, accessToken: string, maxHeightPx?: number, maxWidthPx?: number, ): Promise<string> { const params = new URLSearchParams(); if (maxHeightPx) { params.set('maxHeightPx', maxHeightPx.toString()); } if (maxWidthPx) { params.set('maxWidthPx', maxWidthPx.toString()); } // Need at least one dimension if (!maxHeightPx && !maxWidthPx) { params.set('maxHeightPx', '400'); } params.set('skipHttpRedirect', 'true'); const url = `${PLACES_API_BASE_URL}/${photoName}/media?${params.toString()}`; const response = await fetch(url, { method: 'GET', headers: { Authorization: `Bearer ${accessToken}`, }, }); if (!response.ok) { await handleApiError(response); } const data = await response.json() as {photoUri: string}; return data.photoUri; }

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/domdomegg/google-maps-places-mcp'

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