by baryhuang
The MCP Headless Gmail Server is a Model Context Protocol (MCP) server designed to interact with Gmail without requiring local credentials or token setup. It enables programs to access Gmail functionalities, such as sending and receiving emails, in headless and remote environments.
MCP Headless Gmail Server is a Model Context Protocol (MCP) server designed to interact with Gmail without requiring local credential or token setup. It enables programmatic access to Gmail functionalities like sending and receiving emails in a headless and remote environment, making it suitable for server-side applications and integrations.
To use MCP Headless Gmail Server, you can either install it via pip or use its Docker image. After installation, you start the server, and then interact with it using an MCP client (like Claude Desktop) by sending JSON payloads to perform actions like refreshing tokens, getting emails, or sending emails. Authentication is handled by providing Google API credentials (client ID, client secret, access token, and refresh token) in the tool calls. The server supports a token refresh workflow to manage expired access tokens.
git clone https://github.com/baryhuang/mcp-headless-gmail.git
cd mcp-headless-gmail
pip install -e .
docker build -t mcp-headless-gmail .
docker run -i --rm buryhuang/mcp-headless-gmail:latest
mcp-server-headless-gmail
Refreshing Tokens:
{
"google_access_token": "your_access_token",
"google_refresh_token": "your_refresh_token",
"google_client_id": "your_client_id",
"google_client_secret": "your_client_secret"
}
Getting Recent Emails:
{
"google_access_token": "your_access_token",
"max_results": 5,
"unread_only": false
}
Sending an Email:
{
"google_access_token": "your_access_token",
"to": "recipient@example.com",
"subject": "Hello from MCP Gmail",
"body": "This is a test email sent via MCP Gmail server"
}
google-api-python-client
library.Q: What are the prerequisites for running MCP Headless Gmail Server? A: You need Python 3.10 or higher and Google API credentials (client ID, client secret, access token, and refresh token).
Q: How do I obtain Google API credentials?
A: You can obtain them from the Google Cloud Console by creating a project, enabling the Gmail API, configuring the OAuth consent screen, and creating OAuth client ID credentials (Desktop app type). You'll also need to obtain access and refresh tokens with the necessary scopes (https://www.googleapis.com/auth/gmail.readonly
and https://www.googleapis.com/auth/gmail.send
).
Q: How does token refreshing work? A: The server implements automatic token refreshing. When your access token expires, the Google API client will use the refresh token, client ID, and client secret to obtain a new access token without requiring user intervention.
Q: Is it secure to use this server with my Gmail credentials? A: The server requires direct access to your Google API credentials. It is crucial to keep your tokens and credentials secure and never share them with untrusted parties. The decoupled architecture helps by separating credential storage from the server implementation itself.
A MCP (Model Context Protocol) server that provides get, send Gmails without local credential or token setup.
# Clone the repository
git clone https://github.com/baryhuang/mcp-headless-gmail.git
cd mcp-headless-gmail
# Install dependencies
pip install -e .
# Build the Docker image
docker build -t mcp-headless-gmail .
You can configure Claude Desktop to use the Docker image by adding the following to your Claude configuration:
docker
{
"mcpServers": {
"gmail": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"buryhuang/mcp-headless-gmail:latest"
]
}
}
}
npm version
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"@peakmojo/mcp-server-headless-gmail"
]
}
}
}
Note: With this configuration, you'll need to provide your Google API credentials in the tool calls as shown in the Using the Tools section. Gmail credentials are not passed as environment variables to maintain separation between credential storage and server implementation.
To publish the Docker image for multiple platforms, you can use the docker buildx
command. Follow these steps:
Create a new builder instance (if you haven't already):
docker buildx create --use
Build and push the image for multiple platforms:
docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 -t buryhuang/mcp-headless-gmail:latest --push .
Verify the image is available for the specified platforms:
docker buildx imagetools inspect buryhuang/mcp-headless-gmail:latest
The server provides Gmail functionality through MCP tools. Authentication handling is simplified with a dedicated token refresh tool.
mcp-server-headless-gmail
When using an MCP client like Claude, you have two main ways to handle authentication:
If you have both access and refresh tokens:
{
"google_access_token": "your_access_token",
"google_refresh_token": "your_refresh_token",
"google_client_id": "your_client_id",
"google_client_secret": "your_client_secret"
}
If your access token has expired, you can refresh with just the refresh token:
{
"google_refresh_token": "your_refresh_token",
"google_client_id": "your_client_id",
"google_client_secret": "your_client_secret"
}
This will return a new access token and its expiration time, which you can use for subsequent calls.
Retrieves recent emails with the first 1k characters of each email body:
{
"google_access_token": "your_access_token",
"max_results": 5,
"unread_only": false
}
Response includes:
body_size_bytes
: Total size of the email body in bytescontains_full_body
: Boolean indicating if the entire body is included (true) or truncated (false)For emails with bodies larger than 1k characters, you can retrieve the full content in chunks:
{
"google_access_token": "your_access_token",
"message_id": "message_id_from_get_recent_emails",
"offset": 0
}
You can also get email content by thread ID:
{
"google_access_token": "your_access_token",
"thread_id": "thread_id_from_get_recent_emails",
"offset": 1000
}
The response includes:
body_size_bytes
: Total size of the email bodychunk_size
: Size of the returned chunkcontains_full_body
: Boolean indicating if the chunk contains the remainder of the bodyTo retrieve the entire email body of a long message, make sequential calls increasing the offset by 1000 each time until contains_full_body
is true.
{
"google_access_token": "your_access_token",
"to": "recipient@example.com",
"subject": "Hello from MCP Gmail",
"body": "This is a test email sent via MCP Gmail server",
"html_body": "<p>This is a <strong>test email</strong> sent via MCP Gmail server</p>"
}
gmail_refresh_token
tool with either:
gmail_refresh_token
tool again to get a new token.This approach simplifies most API calls by not requiring client credentials for every operation, while still enabling token refresh when needed.
To obtain the required Google API credentials, follow these steps:
https://www.googleapis.com/auth/gmail.readonly
(for reading emails)https://www.googleapis.com/auth/gmail.send
(for sending emails)This server implements automatic token refreshing. When your access token expires, the Google API client will use the refresh token, client ID, and client secret to obtain a new access token without requiring user intervention.
This server requires direct access to your Google API credentials. Always keep your tokens and credentials secure and never share them with untrusted parties.
See the LICENSE file for details.
Reviews feature coming soon
Stay tuned for community discussions and feedback