UNPKG

@bdmarvin/mcp-server-gbp

Version:

MCP server for Google Business Profile Performance API.

58 lines (57 loc) 2.67 kB
const API_BASE_URL = 'https://mybusiness.googleapis.com/v4'; export class GbpLocalPostsServiceV4 { async fetchWithAuth(url, method, accessToken, body = null) { const headers = { 'Authorization': `Bearer ${accessToken}`, 'Content-Type': 'application/json', }; const options = { method, headers, }; if (body) { options.body = JSON.stringify(body); } const response = await fetch(url, options); if (!response.ok) { const errorBody = await response.text(); throw new Error(`API request failed with status ${response.status}: ${errorBody}`); } // DELETE responses might be empty, handle that gracefully if (response.status === 204 || response.headers.get('content-length') === '0') { return {}; } return response.json(); } async createLocalPost(accessToken, params) { const { accountId, locationId, post } = params; const url = `${API_BASE_URL}/accounts/${accountId}/locations/${locationId}/localPosts`; return this.fetchWithAuth(url, 'POST', accessToken, post); } async listLocalPosts(accessToken, params) { const { accountId, locationId, pageSize, pageToken } = params; const query = new URLSearchParams(); if (pageSize) query.append('pageSize', pageSize.toString()); if (pageToken) query.append('pageToken', pageToken); const url = `${API_BASE_URL}/accounts/${accountId}/locations/${locationId}/localPosts?${query.toString()}`; return this.fetchWithAuth(url, 'GET', accessToken); } async getLocalPost(accessToken, params) { const { accountId, locationId, localPostId } = params; const url = `${API_BASE_URL}/accounts/${accountId}/locations/${locationId}/localPosts/${localPostId}`; return this.fetchWithAuth(url, 'GET', accessToken); } async updateLocalPost(accessToken, params) { const { accountId, locationId, localPostId, updateMask, post } = params; const query = new URLSearchParams({ updateMask }); const url = `${API_BASE_URL}/accounts/${accountId}/locations/${locationId}/localPosts/${localPostId}?${query.toString()}`; return this.fetchWithAuth(url, 'PATCH', accessToken, post); } async deleteLocalPost(accessToken, params) { const { accountId, locationId, localPostId } = params; const url = `${API_BASE_URL}/accounts/${accountId}/locations/${locationId}/localPosts/${localPostId}`; return this.fetchWithAuth(url, 'DELETE', accessToken); } }