adsense-mcp
MCP server for Google AdSense management. Create ad units, generate framework-specific ad code, manage earnings reports, and automate ads.txt — all from your AI assistant.
README
adsense-mcp
MCP (Model Context Protocol) server for Google AdSense management. Create ad units, generate framework-specific ad code, manage earnings reports, and automate ads.txt — all from your AI assistant.
Built for solo developers who build websites with AI agents and want to monetize them without leaving the terminal.
Features
- Ad Unit CRUD — Create and manage ad units via AdSense Management API v2
- Ad Code Retrieval — Get ready-to-paste HTML/JS embed code for any ad unit
- Framework Snippets — Generate ad integration code for React, Next.js, Vue, Nuxt, Astro, Svelte
- ads.txt Automation — Generate and remotely verify ads.txt for your domains
- Earnings Reports — Query revenue, page views, clicks with flexible date ranges and dimensions
- Alerts & Payments — Monitor policy issues and payment history
- Auto-detection — Automatically resolves your account and ad client, so most parameters are optional
- Lightweight — Uses direct REST calls instead of the heavy
googleapispackage
Prerequisites
- Active Google AdSense account (already approved)
- Node.js 18+
- Google Cloud OAuth credentials (see Setup below)
Setup
1. Create Google Cloud OAuth Credentials
- Go to Google Cloud Console
- Create a new project (or select an existing one)
- Enable the AdSense Management API:
- Navigate to APIs & Services > Library
- Search for "AdSense Management API"
- Click Enable
- Create OAuth credentials:
- Go to APIs & Services > Credentials
- Click Create Credentials > OAuth client ID
- Choose Desktop app as the application type
- Download the JSON file
- Configure the OAuth consent screen:
- Go to APIs & Services > OAuth consent screen
- Keep the publishing status as Testing
- Add your Google account as a Test user
Note: While in "Testing" mode, only test users you add can authenticate. This is fine for personal use — no Google review required.
2. Install
npm install -g adsense-mcp
3. Authenticate
adsense-mcp auth /path/to/downloaded-credentials.json
This will:
- Save your OAuth credentials to
~/.adsense-mcp/credentials.json - Open your browser for Google OAuth consent
- Save access/refresh tokens to
~/.adsense-mcp/tokens.json
You only need to do this once. Tokens auto-refresh on subsequent use.
4. Configure Your AI Tool
Claude Code
Add to your project's .mcp.json:
{
"mcpServers": {
"adsense": {
"command": "npx",
"args": ["-y", "adsense-mcp"]
}
}
}
Or add globally in ~/.claude/settings.json:
{
"mcpServers": {
"adsense": {
"command": "npx",
"args": ["-y", "adsense-mcp"]
}
}
}
Cursor
Add to .cursor/mcp.json in your project root:
{
"mcpServers": {
"adsense": {
"command": "npx",
"args": ["-y", "adsense-mcp"]
}
}
}
Windsurf
Add to ~/.codeium/windsurf/mcp_config.json:
{
"mcpServers": {
"adsense": {
"command": "npx",
"args": ["-y", "adsense-mcp"]
}
}
}
Tools Reference
Account & Configuration
list_accounts
List all AdSense accounts linked to your Google account.
Parameters: (none)
Example prompt: "Show me my AdSense accounts"
list_ad_clients
List ad clients (publisher IDs like ca-pub-XXXX) for an account.
Parameters:
account (optional) Account name. Auto-detected if omitted.
Example prompt: "What's my AdSense publisher ID?"
list_sites
List all sites with their approval status and auto-ads setting.
Parameters:
account (optional) Account name. Auto-detected if omitted.
Example prompt: "Which sites are approved in my AdSense?"
Ad Unit Management
create_ad_unit
Create a new ad unit. Returns the unit name, slot ID, and state.
Parameters:
displayName (required) Human-readable name (e.g., "Homepage Banner")
type (optional) DISPLAY | IN_FEED | IN_ARTICLE | MATCHED_CONTENT (default: DISPLAY)
adClient (optional) Ad client name. Auto-detected if omitted.
Example prompt: "Create a display ad unit called 'Sidebar Ad' for my blog"
list_ad_units
List all ad units with their name, display name, state, type, and size.
Parameters:
adClient (optional) Ad client name. Auto-detected if omitted.
Example prompt: "List all my ad units"
get_ad_code
Get the raw HTML/JS ad code for a specific ad unit, as provided by Google.
Parameters:
adUnit (required) Full ad unit name (e.g., accounts/pub-XXX/adclients/ca-pub-XXX/adunits/123)
Example prompt: "Get the embed code for my sidebar ad unit"
get_ad_snippet
Generate framework-specific ad component code, ready to drop into your project.
Parameters:
framework (required) html | react | nextjs | vue | nuxt | astro | svelte
publisherId (required) Publisher ID (e.g., ca-pub-1234567890123456)
slotId (required) Ad unit slot ID (e.g., 1234567890)
style (optional) responsive | in-article | in-feed | fixed (default: responsive)
width (optional) Width in px (only for "fixed" style)
height (optional) Height in px (only for "fixed" style)
Example prompt: "Generate a Next.js component for my ad unit slot 9876543210"
Supported frameworks and what gets generated:
| Framework | Output |
|---|---|
html |
Standard <ins> tag with inline script |
react |
AdUnit component with useEffect push |
nextjs |
Layout with next/script + AdUnit component |
vue |
SFC with onMounted push |
nuxt |
nuxt.config.ts head config + Vue SFC |
astro |
.astro component with is:inline script |
svelte |
Component with onMount push |
ads.txt
generate_ads_txt
Generate the correct ads.txt content for your AdSense account.
Parameters:
account (optional) Account name. Auto-detected if omitted.
Example prompt: "Generate ads.txt for my site"
Returns the entry line and instructions for where to place the file in various project types.
verify_ads_txt
Fetch a domain's /ads.txt and check if it contains your publisher entry.
Parameters:
domain (required) Domain to check (e.g., example.com)
account (optional) Account name. Auto-detected if omitted.
Example prompt: "Check if ads.txt is set up correctly on myblog.com"
Analytics
generate_report
Generate an earnings report with customizable date range, metrics, and dimensions.
Parameters:
dateRange (optional) TODAY | YESTERDAY | LAST_7_DAYS | LAST_30_DAYS |
MONTH_TO_DATE | YEAR_TO_DATE | LAST_3_MONTHS | LAST_12_MONTHS
(default: LAST_7_DAYS)
metrics (optional) Array of metrics (default: [ESTIMATED_EARNINGS, PAGE_VIEWS, CLICKS, PAGE_VIEWS_CTR])
dimensions (optional) Array of dimensions (default: [DATE])
account (optional) Account name. Auto-detected if omitted.
Available metrics: ESTIMATED_EARNINGS, PAGE_VIEWS, IMPRESSIONS, CLICKS, PAGE_VIEWS_CTR, COST_PER_CLICK, PAGE_VIEWS_RPM, AD_REQUESTS, AD_REQUESTS_CTR
Available dimensions: DATE, WEEK, MONTH, SITE_DOMAIN, AD_UNIT_NAME, COUNTRY_NAME, PLATFORM_TYPE
Example prompt: "Show my earnings for the last 30 days broken down by site"
list_alerts
List policy alerts and issues that need attention.
Parameters:
account (optional) Account name. Auto-detected if omitted.
Example prompt: "Are there any AdSense policy issues I should know about?"
list_payments
List payment history.
Parameters:
account (optional) Account name. Auto-detected if omitted.
Example prompt: "Show my AdSense payment history"
Usage Examples
Full workflow: Add ads to a new Next.js site
You: "I just deployed myblog.com with Next.js. Set up AdSense ads for it."
AI: 1. Calls list_accounts → gets your account
2. Calls list_sites → confirms myblog.com is approved
3. Calls create_ad_unit → creates "myblog-header" (DISPLAY)
4. Calls create_ad_unit → creates "myblog-article" (IN_ARTICLE)
5. Calls get_ad_snippet (nextjs) → generates Next.js components
6. Calls generate_ads_txt → generates ads.txt content
7. Inserts ad components into your layout and article pages
8. Creates public/ads.txt
9. Calls verify_ads_txt → confirms setup after deploy
Quick report
You: "How are my ads performing this month?"
AI: Calls generate_report with dateRange=MONTH_TO_DATE,
dimensions=[DATE, SITE_DOMAIN]
→ Shows earnings trend by site
Ad placement for existing units
You: "Add my existing ad units to this Astro blog"
AI: 1. Calls list_ad_units → gets your units and slot IDs
2. Calls get_ad_snippet (astro) for each unit
3. Inserts components into your Astro layouts
Troubleshooting
"Not authenticated" error
Re-run the auth flow:
adsense-mcp auth
If tokens exist but are invalid, delete them and re-authenticate:
rm ~/.adsense-mcp/tokens.json
adsense-mcp auth
"AdSense Management API has not been used" error
Enable the API in Google Cloud Console:
- Go to APIs & Services > Library
- Search "AdSense Management API"
- Click Enable
"Access Not Configured" or 403 errors
Make sure:
- Your Google account is added as a test user in the OAuth consent screen
- The AdSense Management API is enabled in your GCP project
- Your AdSense account is active and approved
Token refresh failures
Delete the saved tokens and re-authenticate:
rm ~/.adsense-mcp/tokens.json
adsense-mcp auth
Security
- OAuth tokens are stored in
~/.adsense-mcp/tokens.json(file permission: user-only recommended) - Credentials are stored in
~/.adsense-mcp/credentials.json - The server uses
https://www.googleapis.com/auth/adsensescope (full read/write) - No data is sent to any third party — all API calls go directly to Google
- Auth callback runs on
127.0.0.1with a randomly assigned port
To restrict to read-only access, modify the SCOPES array in src/auth.ts to:
const SCOPES = ['https://www.googleapis.com/auth/adsense.readonly'];
License
MIT
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。