@appsyogi/adsense-mcp-server 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,342 @@
1
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
2
+ import * as google_auth_library from 'google-auth-library';
3
+ import { OAuth2Client } from 'google-auth-library';
4
+
5
+ /**
6
+ * MCP Server - Main setup
7
+ */
8
+
9
+ /**
10
+ * Create and configure the MCP server
11
+ */
12
+ declare function createServer(): Server<{
13
+ method: string;
14
+ params?: {
15
+ [x: string]: unknown;
16
+ _meta?: {
17
+ [x: string]: unknown;
18
+ progressToken?: string | number | undefined;
19
+ "io.modelcontextprotocol/related-task"?: {
20
+ taskId: string;
21
+ } | undefined;
22
+ } | undefined;
23
+ } | undefined;
24
+ }, {
25
+ method: string;
26
+ params?: {
27
+ [x: string]: unknown;
28
+ _meta?: {
29
+ [x: string]: unknown;
30
+ progressToken?: string | number | undefined;
31
+ "io.modelcontextprotocol/related-task"?: {
32
+ taskId: string;
33
+ } | undefined;
34
+ } | undefined;
35
+ } | undefined;
36
+ }, {
37
+ [x: string]: unknown;
38
+ _meta?: {
39
+ [x: string]: unknown;
40
+ progressToken?: string | number | undefined;
41
+ "io.modelcontextprotocol/related-task"?: {
42
+ taskId: string;
43
+ } | undefined;
44
+ } | undefined;
45
+ }>;
46
+ /**
47
+ * Start the MCP server with stdio transport
48
+ */
49
+ declare function startServer(accountId?: string): Promise<void>;
50
+
51
+ /**
52
+ * AdSense MCP Server - Type Definitions
53
+ */
54
+ declare const ADSENSE_SCOPES: Record<string, string[]>;
55
+ type ScopeType = 'readonly';
56
+ interface Config {
57
+ authType: 'oauth' | 'service-account';
58
+ clientId?: string;
59
+ clientSecret?: string;
60
+ serviceAccountPath?: string;
61
+ defaultAccountId?: string;
62
+ scope: ScopeType;
63
+ }
64
+ interface StoredTokens {
65
+ accessToken: string;
66
+ refreshToken?: string;
67
+ expiryDate?: number;
68
+ }
69
+ interface AdSenseAccount {
70
+ name: string;
71
+ displayName?: string;
72
+ timeZone?: {
73
+ id: string;
74
+ };
75
+ createTime?: string;
76
+ premium?: boolean;
77
+ pendingTasks?: string[];
78
+ }
79
+ interface AdSenseSite {
80
+ name: string;
81
+ reportingDimensionId?: string;
82
+ domain: string;
83
+ state: 'REQUIRES_REVIEW' | 'GETTING_READY' | 'READY' | 'NEEDS_ATTENTION' | 'STATE_UNSPECIFIED';
84
+ autoAdsEnabled?: boolean;
85
+ }
86
+ interface AdSenseAlert {
87
+ name: string;
88
+ severity: 'SEVERITY_UNSPECIFIED' | 'INFO' | 'WARNING' | 'SEVERE';
89
+ message: string;
90
+ type: string;
91
+ }
92
+ interface AdSensePolicyIssue {
93
+ name: string;
94
+ site?: string;
95
+ siteSection?: string;
96
+ uri?: string;
97
+ adRequestCount?: string;
98
+ entityType?: 'ENTITY_TYPE_UNSPECIFIED' | 'SITE' | 'SITE_SECTION' | 'PAGE';
99
+ action?: 'ENFORCEMENT_ACTION_UNSPECIFIED' | 'WARNED' | 'AD_SERVING_RESTRICTED' | 'AD_SERVING_DISABLED' | 'AD_SERVED_WITH_CLICK_CONFIRMATION' | 'AD_PERSONALIZATION_RESTRICTED';
100
+ warningEscalationDate?: {
101
+ year: number;
102
+ month: number;
103
+ day: number;
104
+ };
105
+ }
106
+ interface AdSensePayment {
107
+ name: string;
108
+ amount?: string;
109
+ date?: {
110
+ year: number;
111
+ month: number;
112
+ day: number;
113
+ };
114
+ }
115
+ interface AdSenseAdClient {
116
+ name: string;
117
+ reportingDimensionId?: string;
118
+ productCode?: 'AFC' | 'AFG' | 'AFMC' | 'AFS' | 'AFV' | 'AFVH';
119
+ state?: 'READY' | 'GETTING_READY' | 'REQUIRES_REVIEW' | 'STATE_UNSPECIFIED';
120
+ }
121
+ interface AdSenseAdUnit {
122
+ name: string;
123
+ reportingDimensionId?: string;
124
+ displayName: string;
125
+ state: 'ACTIVE' | 'ARCHIVED' | 'STATE_UNSPECIFIED';
126
+ contentAdsSettings?: {
127
+ type?: 'DISPLAY' | 'FEED' | 'ARTICLE' | 'MATCHED_CONTENT' | 'LINK';
128
+ size?: string;
129
+ };
130
+ }
131
+ type ReportDimension = 'DATE' | 'WEEK' | 'MONTH' | 'DOMAIN_CODE' | 'DOMAIN_NAME' | 'PAGE_URL' | 'AD_UNIT_ID' | 'AD_UNIT_NAME' | 'AD_UNIT_SIZE_CODE' | 'AD_UNIT_SIZE_NAME' | 'AD_FORMAT_CODE' | 'AD_FORMAT_NAME' | 'AD_PLACEMENT_CODE' | 'AD_PLACEMENT_NAME' | 'COUNTRY_CODE' | 'COUNTRY_NAME' | 'PLATFORM_TYPE_CODE' | 'PLATFORM_TYPE_NAME' | 'CONTENT_PLATFORM_CODE' | 'CONTENT_PLATFORM_NAME' | 'TRAFFIC_SOURCE_CODE' | 'TRAFFIC_SOURCE_NAME' | 'BUYER_NETWORK_ID' | 'BUYER_NETWORK_NAME' | 'BID_TYPE_CODE' | 'BID_TYPE_NAME' | 'TARGETING_TYPE_CODE' | 'TARGETING_TYPE_NAME' | 'CUSTOM_CHANNEL_ID' | 'CUSTOM_CHANNEL_NAME' | 'URL_CHANNEL_ID' | 'URL_CHANNEL_NAME' | 'OWNED_SITE_ID' | 'OWNED_SITE_DOMAIN_NAME';
132
+ type ReportMetric = 'ESTIMATED_EARNINGS' | 'TOTAL_EARNINGS' | 'COST_PER_CLICK' | 'PAGE_VIEWS' | 'AD_REQUESTS' | 'MATCHED_AD_REQUESTS' | 'IMPRESSIONS' | 'INDIVIDUAL_AD_IMPRESSIONS' | 'CLICKS' | 'AD_REQUESTS_COVERAGE' | 'PAGE_VIEWS_CTR' | 'AD_REQUESTS_CTR' | 'IMPRESSIONS_CTR' | 'PAGE_VIEWS_RPM' | 'AD_REQUESTS_RPM' | 'IMPRESSIONS_RPM' | 'ACTIVE_VIEW_MEASURABILITY' | 'ACTIVE_VIEW_VIEWABILITY' | 'ACTIVE_VIEW_TIME' | 'PAGE_VIEWS_SPAM_RATIO' | 'AD_REQUESTS_SPAM_RATIO' | 'CLICKS_SPAM_RATIO' | 'IMPRESSIONS_SPAM_RATIO';
133
+ interface ReportQuery {
134
+ accountId: string;
135
+ startDate: string;
136
+ endDate: string;
137
+ dimensions?: ReportDimension[];
138
+ metrics?: ReportMetric[];
139
+ orderBy?: string;
140
+ limit?: number;
141
+ filters?: string[];
142
+ currencyCode?: string;
143
+ dateRange?: 'CUSTOM' | 'TODAY' | 'YESTERDAY' | 'MONTH_TO_DATE' | 'YEAR_TO_DATE' | 'LAST_7_DAYS' | 'LAST_30_DAYS';
144
+ }
145
+ interface ReportRow {
146
+ cells: Array<{
147
+ value: string;
148
+ }>;
149
+ }
150
+ interface ReportResponse {
151
+ headers: Array<{
152
+ name: string;
153
+ type: 'DIMENSION' | 'METRIC_TALLY' | 'METRIC_RATIO' | 'METRIC_CURRENCY' | 'METRIC_MILLISECONDS' | 'METRIC_DECIMAL';
154
+ currencyCode?: string;
155
+ }>;
156
+ rows: ReportRow[];
157
+ totals?: ReportRow;
158
+ averages?: ReportRow;
159
+ startDate?: {
160
+ year: number;
161
+ month: number;
162
+ day: number;
163
+ };
164
+ endDate?: {
165
+ year: number;
166
+ month: number;
167
+ day: number;
168
+ };
169
+ totalMatchedRows?: string;
170
+ }
171
+ interface EarningsSummary {
172
+ today: EarningsPeriod;
173
+ yesterday: EarningsPeriod;
174
+ last7Days: EarningsPeriod;
175
+ thisMonth: EarningsPeriod;
176
+ lastMonth: EarningsPeriod;
177
+ }
178
+ interface EarningsPeriod {
179
+ earnings: number;
180
+ impressions: number;
181
+ clicks: number;
182
+ ctr: number;
183
+ rpm: number;
184
+ pageViews: number;
185
+ }
186
+ interface CacheEntry {
187
+ id?: number;
188
+ cacheKey: string;
189
+ accountId: string;
190
+ queryHash: string;
191
+ responseData: string;
192
+ createdAt: number;
193
+ expiresAt: number;
194
+ }
195
+ interface ListAccountsInput {
196
+ }
197
+ interface EarningsSummaryInput {
198
+ accountId?: string;
199
+ }
200
+ interface GenerateReportInput {
201
+ accountId?: string;
202
+ startDate?: string;
203
+ endDate?: string;
204
+ dimensions?: string[];
205
+ metrics?: string[];
206
+ orderBy?: string;
207
+ limit?: number;
208
+ }
209
+ interface ComparePeriodsInput {
210
+ accountId?: string;
211
+ period1Start: string;
212
+ period1End: string;
213
+ period2Start: string;
214
+ period2End: string;
215
+ dimensions?: string[];
216
+ }
217
+ interface ListSitesInput {
218
+ accountId?: string;
219
+ }
220
+ interface ListAlertsInput {
221
+ accountId?: string;
222
+ }
223
+ interface ListPolicyIssuesInput {
224
+ accountId?: string;
225
+ }
226
+ interface ListPaymentsInput {
227
+ accountId?: string;
228
+ }
229
+ interface ListAdUnitsInput {
230
+ accountId?: string;
231
+ }
232
+ interface GetAdCodeInput {
233
+ accountId?: string;
234
+ adClientId: string;
235
+ adUnitId: string;
236
+ }
237
+ interface ExportCsvInput {
238
+ accountId?: string;
239
+ startDate: string;
240
+ endDate: string;
241
+ dimensions?: string[];
242
+ metrics?: string[];
243
+ }
244
+
245
+ /**
246
+ * AdSense API Client
247
+ *
248
+ * Wraps the googleapis client with:
249
+ * - Automatic authentication (OAuth or service account)
250
+ * - Rate limiting with exponential backoff
251
+ * - Response caching for performance
252
+ * - Pagination handling
253
+ */
254
+
255
+ /**
256
+ * AdSense API Client
257
+ */
258
+ declare class AdSenseClient {
259
+ private adsense;
260
+ private defaultAccountId?;
261
+ private constructor();
262
+ /**
263
+ * Create an authenticated AdSense client
264
+ */
265
+ static create(accountIdOverride?: string): Promise<AdSenseClient>;
266
+ /**
267
+ * Get the account ID to use (explicit > default > first available)
268
+ */
269
+ private resolveAccountId;
270
+ /**
271
+ * Extract publisher ID from account name
272
+ */
273
+ private extractPubId;
274
+ /**
275
+ * List all AdSense accounts
276
+ */
277
+ listAccounts(): Promise<AdSenseAccount[]>;
278
+ /**
279
+ * Get a specific account
280
+ */
281
+ getAccount(accountId?: string): Promise<AdSenseAccount | null>;
282
+ /**
283
+ * List all sites for an account
284
+ */
285
+ listSites(accountId?: string): Promise<AdSenseSite[]>;
286
+ /**
287
+ * List alerts for an account
288
+ */
289
+ listAlerts(accountId?: string): Promise<AdSenseAlert[]>;
290
+ /**
291
+ * List policy issues for an account
292
+ */
293
+ listPolicyIssues(accountId?: string): Promise<AdSensePolicyIssue[]>;
294
+ /**
295
+ * List payments for an account
296
+ */
297
+ listPayments(accountId?: string): Promise<AdSensePayment[]>;
298
+ /**
299
+ * List ad clients for an account
300
+ */
301
+ listAdClients(accountId?: string): Promise<AdSenseAdClient[]>;
302
+ /**
303
+ * List ad units for an account (across all ad clients)
304
+ */
305
+ listAdUnits(accountId?: string): Promise<AdSenseAdUnit[]>;
306
+ /**
307
+ * Get ad code for an ad unit
308
+ */
309
+ getAdCode(adClientId: string, adUnitId: string, accountId?: string): Promise<string>;
310
+ /**
311
+ * Generate a report
312
+ */
313
+ generateReport(query: ReportQuery): Promise<ReportResponse>;
314
+ /**
315
+ * Generate a CSV report
316
+ */
317
+ generateCsvReport(query: ReportQuery): Promise<string>;
318
+ /**
319
+ * Get earnings summary (today, yesterday, last 7 days, this month, last month)
320
+ */
321
+ getEarningsSummary(accountId?: string): Promise<EarningsSummary>;
322
+ /**
323
+ * Extract earnings data from a report response
324
+ */
325
+ private extractEarningsPeriod;
326
+ /**
327
+ * Determine appropriate cache TTL based on date range
328
+ */
329
+ private getReportTTL;
330
+ }
331
+
332
+ /**
333
+ * Create an authenticated OAuth2 client
334
+ */
335
+ declare function createOAuthClient(): Promise<OAuth2Client>;
336
+
337
+ /**
338
+ * Create an authenticated client using service account credentials
339
+ */
340
+ declare function createServiceAccountClient(): Promise<google_auth_library.JWT>;
341
+
342
+ export { ADSENSE_SCOPES, type AdSenseAccount, type AdSenseAdClient, type AdSenseAdUnit, type AdSenseAlert, AdSenseClient, type AdSensePayment, type AdSensePolicyIssue, type AdSenseSite, type CacheEntry, type ComparePeriodsInput, type Config, type EarningsPeriod, type EarningsSummary, type EarningsSummaryInput, type ExportCsvInput, type GenerateReportInput, type GetAdCodeInput, type ListAccountsInput, type ListAdUnitsInput, type ListAlertsInput, type ListPaymentsInput, type ListPolicyIssuesInput, type ListSitesInput, type ReportDimension, type ReportMetric, type ReportQuery, type ReportResponse, type ReportRow, type ScopeType, type StoredTokens, createOAuthClient, createServer, createServiceAccountClient, startServer };