Gmail + SAP MCP Server
Provides MCP tools for searching, reading, and drafting Gmail emails, as well as querying SAP OData endpoints with safe read operations and mock mode.
README
Gmail + SAP MCP Integration
A complete starter project that exposes Gmail and SAP OData operations as Model Context Protocol (MCP) tools.
What this project can do
Gmail tools
- Authenticate with Google OAuth 2.0
- Search emails using normal Gmail search syntax
- Read an email
- List recent inbox emails
- Create an email draft
- Send an email, disabled by default for safety
SAP tools
- Test the SAP connection
- Read an OData entity set
- Read a single OData entity
- Create an OData record, disabled by default
- Update an OData record, disabled by default
- Execute a safe raw GET request
- Run without a real SAP system using built-in mock data
MCP
- Runs as a standard
stdioMCP server - Can be connected to MCP-compatible clients
- Uses explicit environment flags for write operations
- Returns JSON-friendly tool results
Project structure
gmail_sap_mcp_project/
├── app/
│ ├── config.py
│ ├── server.py
│ ├── gmail_client.py
│ ├── sap_client.py
│ ├── models.py
│ └── utils.py
├── scripts/
│ ├── gmail_auth.py
│ └── test_connections.py
├── tests/
│ ├── test_sap_client.py
│ └── test_utils.py
├── credentials/
│ └── .gitkeep
├── .env.example
├── .gitignore
├── mcp-config.example.json
├── pyproject.toml
├── requirements.txt
├── run_server.py
└── README.md
1. Prerequisites
Install:
- Python 3.11 or 3.12
- A Google Cloud project
- Gmail API enabled in Google Cloud
- OAuth Desktop App credentials
- Optional: SAP S/4HANA, SAP Gateway, SAP Business One service layer, or another SAP OData endpoint
This project does not require a Google service-account key. Gmail user data is accessed using OAuth user consent.
2. Setup on Windows PowerShell
Open PowerShell in the project folder.
py -3.11 -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
pip install -r requirements.txt
Copy-Item .env.example .env
If PowerShell blocks activation:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
.venv\Scripts\Activate.ps1
3. Configure Gmail
Google Cloud steps
- Create or select a Google Cloud project.
- Open APIs & Services → Library.
- Enable Gmail API.
- Open Google Auth Platform.
- Configure the consent screen.
- Add your Gmail address as a test user while the app is in testing.
- Open Clients → Create Client.
- Select Desktop app.
- Download the JSON file.
- Rename it to
credentials.json. - Put it here:
credentials/credentials.json
Generate the Gmail token
python scripts/gmail_auth.py
A browser window will ask you to approve Gmail access. The generated token is stored at:
credentials/token.json
The default scopes allow reading Gmail, creating drafts, and sending mail. To use read-only access, change GMAIL_SCOPES in .env, delete credentials/token.json, and authenticate again.
4. Configure SAP
Copy .env.example to .env.
Option A: Start in mock mode
Keep:
SAP_MOCK_MODE=true
No real SAP credentials are needed. This is the best way to learn and test the MCP integration.
Option B: Use a real SAP OData service
Example configuration:
SAP_MOCK_MODE=false
SAP_BASE_URL=https://your-sap-host.example.com
SAP_ODATA_PATH=/sap/opu/odata/sap/API_BUSINESS_PARTNER
SAP_AUTH_TYPE=basic
SAP_USERNAME=your_username
SAP_PASSWORD=your_password
SAP_VERIFY_SSL=true
For bearer-token authentication:
SAP_AUTH_TYPE=bearer
SAP_BEARER_TOKEN=your_access_token
SAP_ODATA_PATH should point to the service root, not an individual entity set.
Examples:
/sap/opu/odata/sap/API_BUSINESS_PARTNER
/sap/opu/odata/sap/API_SALES_ORDER_SRV
Entity-set names vary by SAP service, such as:
A_BusinessPartner
A_SalesOrder
A_Product
5. Test connections
python scripts/test_connections.py
This checks configuration, Gmail authentication, and SAP connectivity.
6. Run the MCP server
python run_server.py
Because this is a stdio MCP server, it waits for an MCP client and normally does not show a web page.
Important: do not add print() statements to the MCP server's standard output. Logging goes to standard error.
7. Connect it to an MCP client
Use the absolute path to your Python executable and project folder.
Example MCP configuration:
{
"mcpServers": {
"gmail-sap": {
"command": "C:\\absolute\\path\\gmail_sap_mcp_project\\.venv\\Scripts\\python.exe",
"args": [
"C:\\absolute\\path\\gmail_sap_mcp_project\\run_server.py"
],
"cwd": "C:\\absolute\\path\\gmail_sap_mcp_project",
"env": {
"ENV_FILE": "C:\\absolute\\path\\gmail_sap_mcp_project\\.env"
}
}
}
}
A ready template is included as mcp-config.example.json.
Restart your MCP client after changing its configuration.
8. Available MCP tools
Gmail
| Tool | Purpose |
|---|---|
gmail_status |
Check whether Gmail credentials and token are available |
gmail_list_recent |
List recent inbox messages |
gmail_search |
Search Gmail using Gmail query syntax |
gmail_read_message |
Read one message by ID |
gmail_create_draft |
Create a Gmail draft |
gmail_send_email |
Send an email when enabled |
Examples of Gmail search queries:
from:manager@example.com newer_than:7d
subject:invoice has:attachment
is:unread in:inbox
SAP
| Tool | Purpose |
|---|---|
sap_status |
Show safe SAP configuration status |
sap_test_connection |
Test the service connection |
sap_list_entities |
Read an OData entity set |
sap_get_entity |
Read one entity by OData key |
sap_safe_get |
Perform a restricted GET request |
sap_create_entity |
Create a record when enabled |
sap_update_entity |
Update a record when enabled |
9. Safety settings
Write operations are disabled by default:
ALLOW_GMAIL_SEND=false
ALLOW_SAP_WRITE=false
To enable them:
ALLOW_GMAIL_SEND=true
ALLOW_SAP_WRITE=true
Restart the MCP server after changing .env.
For production:
- Use a secrets manager rather than committing secrets.
- Use least-privilege Google scopes.
- Use a dedicated SAP communication user.
- Restrict SAP authorizations to required business objects.
- Keep TLS verification enabled.
- Add approval steps before sending email or changing SAP data.
- Log tool invocation metadata, but never log tokens or passwords.
10. Example real-life workflow
A manager asks:
Find unread supplier emails received this week, extract the supplier numbers, and check those suppliers in SAP.
The AI client can:
- Call
gmail_searchwithis:unread newer_than:7d supplier. - Read selected messages using
gmail_read_message. - Extract supplier IDs.
- Query the relevant SAP entity set using
sap_list_entities. - Present a combined summary.
A write workflow could draft a reply with gmail_create_draft. Sending remains blocked unless explicitly enabled.
11. Run tests
pytest -q
12. Common errors
credentials.json was not found
Put the downloaded Desktop OAuth JSON file at:
credentials/credentials.json
Google says the app is not verified
For development, keep the app in testing and add your Gmail account as a test user.
invalid_grant
Delete credentials/token.json and run:
python scripts/gmail_auth.py
SAP returns 401 or 403
Check the authentication method, username, password/token, communication arrangement, and SAP authorizations.
SAP returns 404
Confirm SAP_BASE_URL, SAP_ODATA_PATH, and entity-set spelling. Open the service $metadata endpoint in a browser or API client.
SSL certificate error
Prefer installing the proper corporate CA certificate. Only for local testing, set:
SAP_VERIFY_SSL=false
Do not disable TLS verification in production.
Notes
MCP connects the AI client to tools. It does not automatically decide your SAP business rules, approve transactions, or bypass Google/SAP permissions.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。