gmail-mcp-service
v1.0.33
Published
MCP server for read-only Gmail access
Maintainers
Readme
Gmail MCP Server
A Model Context Protocol (MCP) server that provides read-only access to Gmail via Google Gmail API.
Features
- List messages from Gmail folders/labels
- Read complete message details including body and attachments
- Search messages using Gmail search syntax
- View all Gmail labels and their statistics
- Read-only access with OAuth2 authentication
Prerequisites
- Node.js 18+ and npm
- Google Cloud project with Gmail API enabled
- OAuth2 credentials (client ID and client secret)
Setup
1. Get Google OAuth2 Credentials
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable Gmail API: APIs & Services > Library > Gmail API > Enable
- Create OAuth2 credentials:
- APIs & Services > Credentials > Create Credentials > OAuth client ID
- Application type: Desktop app
- Name: Gmail MCP Server
- Copy the Client ID and Client Secret
2. Install Dependencies
npm install3. Configure Environment Variables
Create a .env file:
GMAIL_CLIENT_ID=your_client_id.apps.googleusercontent.com
GMAIL_CLIENT_SECRET=your_client_secretNote: The redirect URI is automatically generated using the first available port (default: 8080). You can optionally set GMAIL_REDIRECT_URI if you need a specific port.
4. Build the Project
npm run build5. Test with MCP Inspector
npm run inspectThis will:
- Check for existing tokens in
~/.gmail-mcp/tokens.json - If not found, open browser for OAuth2 authorization
- Authorize the application to access Gmail
- Start the MCP Inspector for testing
Usage
The server provides the following tools:
gmail_list_messages
List messages from Gmail folders/labels.
Parameters:
labelIds(optional): Array of label IDs (default: ["INBOX"])maxResults(optional): Number of messages to return (default: 20, max: 100)pageToken(optional): Pagination tokenincludeSpamTrash(optional): Include spam/trash (default: false)
gmail_get_message
Get complete message details.
Parameters:
messageId(required): Message IDformat(optional): "full", "metadata", or "minimal" (default: "metadata")
gmail_search_messages
Search messages using Gmail search syntax.
Parameters:
query(required): Search query (e.g., "from:[email protected]", "subject:meeting")maxResults(optional): Number of results (default: 20)pageToken(optional): Pagination token
gmail_list_labels
List all Gmail labels.
No parameters required.
gmail_get_label
Get details of a specific label.
Parameters:
labelId(required): Label ID
Configuration with MCP Clients
Claude Desktop / Cline / OpenAI
Create or edit the config file:
- macOS/Linux:
~/.config/claude/claude_desktop_config.json - Windows:
%APPDATA%\Claude\claude_desktop_config.json
- macOS/Linux:
Add the Gmail MCP server configuration:
{
"mcpServers": {
"gmail": {
"command": "node",
"args": [
"/absolute/path/to/gmail-mcp/build/index.js"
],
"env": {
"GMAIL_CLIENT_ID": "your_client_id.apps.googleusercontent.com",
"GMAIL_CLIENT_SECRET": "your_client_secret"
}
}
}
}Note: The redirect URI is automatically generated. You can optionally set GMAIL_REDIRECT_URI if you need a specific port.
Gemini CLI / gemini-cli
Locate or create the Gemini configuration file:
- macOS/Linux:
~/.config/gemini/config.json - Windows:
%APPDATA%\gemini\config.json
- macOS/Linux:
Add the MCP server configuration:
{
"mcp": {
"servers": {
"gmail": {
"command": "node",
"args": [
"/absolute/path/to/gmail-mcp/build/index.js"
],
"env": {
"GMAIL_CLIENT_ID": "your_client_id.apps.googleusercontent.com",
"GMAIL_CLIENT_SECRET": "your_client_secret"
}
}
}
}
}- Restart Gemini CLI
openencode
Locate or create the opencode configuration file:
- macOS/Linux:
~/.config/opencode/mcp.json - Windows:
%APPDATA%\opencode\mcp.json
- macOS/Linux:
Add the MCP server configuration:
{
"mcpServers": {
"gmail": {
"command": "node",
"args": [
"/absolute/path/to/gmail-mcp/build/index.js"
],
"env": {
"GMAIL_CLIENT_ID": "your_client_id.apps.googleusercontent.com",
"GMAIL_CLIENT_SECRET": "your_client_secret"
}
}
}
}- Restart the opencode application
Using npx (Recommended)
You can use npx directly without installing:
npx -y gmail-mcp-serviceIn your MCP client configuration:
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"-y",
"gmail-mcp-service"
],
"env": {
"GMAIL_CLIENT_ID": "your_client_id.apps.googleusercontent.com",
"GMAIL_CLIENT_SECRET": "your_client_secret"
}
}
}
}Using npm global installation
If you prefer to install the package globally:
npm install -g @your-scope/gmail-mcpThen in your MCP client configuration, use:
{
"mcpServers": {
"gmail": {
"command": "npx",
"args": [
"@your-scope/gmail-mcp"
],
"env": {
"GMAIL_CLIENT_ID": "your_client_id.apps.googleusercontent.com",
"GMAIL_CLIENT_SECRET": "your_client_secret"
}
}
}
}First-time authorization
When you first use the Gmail MCP server with any client:
- The server will check for existing tokens in
~/.gmail-mcp/tokens.json - If no tokens are found, it will open your browser for OAuth2 authorization
- Authorize the application to access your Gmail
- Tokens are stored locally for future use
- The server will then be available to your MCP client
Development
# Watch mode for development
npm run dev
# Run tests
npm test
# Build for production
npm run buildCI/CD
This project uses GitHub Actions for continuous integration and automatic npm publishing.
How it works
CI Workflow (
.github/workflows/ci.yml):- Runs on every push and pull request to
main - Tests on Node.js 18 and 20
- Runs all tests and TypeScript compilation
- Runs on every push and pull request to
Publish Workflow (
.github/workflows/publish.yml):- Runs on push to
mainbranch - Checks if version in
package.jsonis newer than npm registry - If version changed:
- Runs tests and build
- Publishes to npm
- Creates a GitHub release
- Runs on push to
Setting up npm publishing
To enable automatic npm publishing:
- Go to your GitHub repository Settings → Secrets and variables → Actions
- Click New repository secret
- Add a secret named
NPM_TOKEN - Use your npm authentication token as the value
To create an npm token:
- Go to npmjs.com → Access Tokens
- Click "Generate New Token" → "Automation"
- Copy the generated token
- Add it as
NPM_TOKENin GitHub Actions secrets
Releasing a new version
Update the version in
package.json:npm version major # or minor or patchCommit and push to
main:git commit -am "chore: bump version to X.Y.Z" git push origin mainGitHub Actions will automatically:
- Run tests
- Build the project
- Publish to npm
- Create a GitHub release
Security
- Tokens are stored locally in
~/.gmail-mcp/tokens.json - Only read-only Gmail access is requested
- No credentials are shared or transmitted
License
MIT
