Unified email management for Gmail and IMAP accounts via MCP
MCP Email Server
Gmail と IMAP アカウントの両方をサポートする、統合メール管理用の包括的な Model Context Protocol (MCP) サーバーです。
🚀 機能
- MCP プロトコル対応: ストリーミングHTTP MCPサーバー
- Gmail 統合: OAuth2ベースのGmail API アクセスと自動トークン管理
- IMAP サポート: 各種メールプロバイダーへの安全なIMAP接続
- 統一インターフェース: 自動検出機能付きのすべてのメールアカウント用単一MCPインターフェース
- アカウント管理: 一元化された設定と接続テスト
- 堅牢なエラーハンドリング: 詳細なユーザーガイダンス付きの包括的なタイムアウトとエラー管理
- セキュリティ: IMAPアカウント用の暗号化パスワードストレージ
- クロスアカウント検索: 設定済みのすべてのアカウントを同時に検索
📋 利用可能なMCPツール
🔄 統合ツール(アカウント種別自動判定)
| ツール | 説明 | パラメータ |
|---|---|---|
list_emails |
任意のアカウントからメール一覧を取得(種別自動判定) | account_name, limit?, folder?, unread_only? |
search_emails |
任意のアカウントでメール検索(種別自動判定) | account_name, query, limit? |
search_all_emails |
全てのGmailとIMAPアカウントを横断検索 | query, accounts?, limit?, sortBy? |
get_email_detail |
任意のアカウントから詳細メール情報を取得 | account_name, email_id |
archive_email |
任意のアカウントでメールをアーカイブ(種別自動判定) | account_name, email_id, remove_unread? |
send_email |
任意のアカウントからメール送信(種別自動判定) | account_name, to, subject, text/html, cc?, bcc?, attachments? |
🔧 管理ツール
| ツール | 説明 | パラメータ |
|---|---|---|
list_accounts |
設定済みメールアカウントをステータス付きで一覧表示 | なし |
test_connection |
特定のアカウントへの接続テスト | account_name |
get_account_stats |
全アカウントの包括的統計情報を取得 | なし |
🛠️ 利用可能なスクリプト
scripts/ ディレクトリに配置:
📧 認証とセットアップ
| スクリプト | 目的 | 使用方法 |
|---|---|---|
gmail-desktop-auth.mjs |
Gmail OAuth2認証セットアップ | npx tsx scripts/gmail-desktop-auth.mjs [ACCOUNT_NAME] |
cleanup-env-tokens.mjs |
環境変数のクリーンアップと標準化 | npx tsx scripts/cleanup-env-tokens.mjs |
setup-xserver.mjs |
XServer用対話式IMAPアカウントセットアップ | npx tsx scripts/setup-xserver.mjs |
encrypt-password.ts |
IMAPアカウント用パスワード暗号化 | npx tsx scripts/encrypt-password.ts [PASSWORD] |
🖥️ サーバー管理
| スクリプト | 目的 | 使用方法 |
|---|---|---|
server.sh |
MCP Email Server管理(起動/停止/再起動/状態確認) | ./scripts/server.sh {start|stop|restart|status|logs|health} |
monitor-health.ts |
包括的ヘルスチェック(認証状態診断付き) | npm run health:check |
test-search-all.sh |
横断検索機能テスト | ./scripts/test-search-all.sh |
⚙️ インストールとセットアップ
1. インストール
git clone <repository-url>
cd mcp-email-server
npm install
2. 環境設定
# 環境設定ファイルのサンプルをコピー
cp .env.example .env
# 設定を編集
nano .env
3. 必要な環境変数
# 必須 - パスワード保存用暗号化キー
EMAIL_ENCRYPTION_KEY=your-unique-32-character-encryption-key
# Gmail OAuth2設定(Gmailを使用する場合)
GMAIL_CLIENT_ID=your-gmail-client-id
GMAIL_CLIENT_SECRET=your-gmail-client-secret
GMAIL_REDIRECT_URI=urn:ietf:wg:oauth:2.0:oob
# Gmailアカウントトークン(OAuthスクリプトで取得)
GMAIL_REFRESH_TOKEN_accountname=your-refresh-token
# IMAPアカウント設定(IMAPを使用する場合)
IMAP_HOST_accountname=mail.example.com
IMAP_USER_accountname=user@example.com
IMAP_PASSWORD_accountname=encrypted-password-here
IMAP_PORT_accountname=993
IMAP_TLS_accountname=true
4. アカウントセットアップ
Gmailアカウント
# 最初に.envファイルにGmail OAuth2認証情報を設定し、その後:
node scripts/gmail-desktop-auth.mjs ACCOUNT_NAME
# 認証完了後、サーバーを再起動して設定を反映:
./scripts/server.sh restart
IMAPアカウント
# 対話式IMAPセットアップ(推奨):
node scripts/setup-xserver.mjs
# または手動でパスワードを暗号化:
npx tsx scripts/encrypt-password.ts "あなたのパスワード"
5. サーバー起動と確認
# サーバー起動(LaunchAgent使用)
./scripts/server.sh start
# サーバー状態確認
./scripts/server.sh status
# 包括的ヘルスチェック(推奨)
./scripts/server.sh health
# または
npm run health:check
# 完全テストスイート
npm test
🔧 MCP設定
Cursor MCPセットアップ(推奨)
Cursor MCP設定ファイル (~/.cursor/mcp.json) に追加:
{
"mcpServers": {
"mcp-email-server": {
"url": "http://localhost:3456/mcp",
"transport": "http"
}
}
}
【注意】認証なしで接続されるので、サーバーは絶対公開しないでください。
🖥️ サーバーとテスト
bin/ ディレクトリに配置:
| スクリプト | 目的 | 使用方法 |
|---|---|---|
run-streaming-email-server.ts |
本番用MCPサーバー起動 | npx tsx bin/run-streaming-email-server.ts |
| ※ run-stdio-email-server.ts はメンテ終了 |
MacOSの常時起動方法
ストリーミングHTTPサーバーMCPですので、サーバーは利用時常時起動で運用してください。 macOSの場合、以下のようなLaunchAgentのplistファイルを作成しておくと便利です。 /PATH/TOは適切なパスに置換してください。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
<key>Label</key>
<string>localhost.mcp-email-server</string>
<key>Program</key>
<string>/PATH/TO/tsx</string>
<key>ProgramArguments</key>
<array>
<string>/PATH/TO/.nvm/versions/node/v23.7.0/bin/tsx</string>
<string>/PATH/TO/mcp-email-server/bin/run-streaming-email-server.ts</string>
</array>
<key>WorkingDirectory</key>
<string>/PATH/TO/src/git/mcp-email-server</string>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/PATH/TO/Library/Logs/mcp-email-server.log</string>
<key>StandardErrorPath</key>
<string>/PATH/TO/Library/Logs/mcp-email-server-error.log</string>
Tools (9)
list_emailsRetrieve a list of emails from any account with automatic type detection.search_emailsSearch for emails in a specific account.search_all_emailsPerform a cross-account search across all configured Gmail and IMAP accounts.get_email_detailRetrieve detailed information for a specific email.archive_emailArchive an email in any account.send_emailSend an email from any configured account.list_accountsList all configured email accounts with their current status.test_connectionTest the connection to a specific email account.get_account_statsGet comprehensive statistics for all configured accounts.Environment Variables
EMAIL_ENCRYPTION_KEYrequiredUnique 32-character key for encrypting stored passwordsGMAIL_CLIENT_IDClient ID for Gmail OAuth2GMAIL_CLIENT_SECRETClient Secret for Gmail OAuth2IMAP_HOST_accountnameIMAP server host addressConfiguration
{"mcpServers": {"mcp-email-server": {"url": "http://localhost:3456/mcp", "transport": "http"}}}