@aot-tech/gmail-mcp-server
Version:
Gmail MCP Server with Bearer Token Authentication - A Model Context Protocol server for Gmail access
81 lines (80 loc) • 2.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeGmailRequest = makeGmailRequest;
exports.sendEmail = sendEmail;
exports.searchEmails = searchEmails;
exports.getEmailMessage = getEmailMessage;
exports.getEmailMetadata = getEmailMetadata;
exports.deleteEmail = deleteEmail;
exports.listLabels = listLabels;
exports.getMultipleEmailDetails = getMultipleEmailDetails;
const index_1 = require("../config/index");
const auth_1 = require("../utils/auth");
async function makeGmailRequest(endpoint, options = {}) {
(0, auth_1.validateAuthentication)();
const bearerToken = (0, auth_1.getBearerToken)();
if (!bearerToken) {
throw new Error('No bearer token available');
}
const url = `${index_1.config.api.base}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${bearerToken}`,
'Content-Type': 'application/json',
...options.headers,
},
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Gmail API request failed: ${response.status} ${response.statusText}\n${errorText}`);
}
return response.json();
}
async function sendEmail(encodedMessage) {
return makeGmailRequest(index_1.config.api.endpoints.send, {
method: 'POST',
body: JSON.stringify({
raw: encodedMessage,
}),
});
}
async function searchEmails(query, maxResults = 10) {
const endpoint = `${index_1.config.api.endpoints.messages}?q=${encodeURIComponent(query)}&maxResults=${maxResults}`;
return makeGmailRequest(endpoint);
}
async function getEmailMessage(messageId) {
const endpoint = `${index_1.config.api.endpoints.messages}/${messageId}`;
return makeGmailRequest(endpoint);
}
async function getEmailMetadata(messageId) {
const endpoint = `${index_1.config.api.endpoints.messages}/${messageId}?format=metadata&metadataHeaders=From&metadataHeaders=Subject&metadataHeaders=Date`;
return makeGmailRequest(endpoint);
}
async function deleteEmail(messageId) {
const endpoint = `${index_1.config.api.endpoints.messages}/${messageId}`;
await makeGmailRequest(endpoint, {
method: 'DELETE',
});
}
async function listLabels() {
return makeGmailRequest(index_1.config.api.endpoints.labels);
}
async function getMultipleEmailDetails(messageIds) {
const promises = messageIds.map(async (messageId) => {
try {
const details = await getEmailMetadata(messageId);
return {
id: messageId,
details,
};
}
catch (error) {
return {
id: messageId,
error: 'Error loading details',
};
}
});
return Promise.all(promises);
}