@mcp-z/mcp-gmail 1.0.3 → 1.0.5
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.
- package/dist/cjs/mcp/tools/messages-export-csv.js +4 -4
- package/dist/cjs/mcp/tools/messages-export-csv.js.map +1 -1
- package/dist/cjs/setup/config.d.cts +3 -2
- package/dist/cjs/setup/config.d.ts +3 -2
- package/dist/cjs/setup/config.js +19 -8
- package/dist/cjs/setup/config.js.map +1 -1
- package/dist/cjs/setup/http.js +1 -1
- package/dist/cjs/setup/http.js.map +1 -1
- package/dist/cjs/setup/runtime.js +6 -6
- package/dist/cjs/setup/runtime.js.map +1 -1
- package/dist/cjs/types.d.cts +2 -2
- package/dist/cjs/types.d.ts +2 -2
- package/dist/esm/mcp/tools/messages-export-csv.js +3 -3
- package/dist/esm/mcp/tools/messages-export-csv.js.map +1 -1
- package/dist/esm/setup/config.d.ts +3 -2
- package/dist/esm/setup/config.js +25 -12
- package/dist/esm/setup/config.js.map +1 -1
- package/dist/esm/setup/http.js +1 -1
- package/dist/esm/setup/http.js.map +1 -1
- package/dist/esm/setup/runtime.js +5 -5
- package/dist/esm/setup/runtime.js.map +1 -1
- package/dist/esm/types.d.ts +2 -2
- package/dist/esm/types.js.map +1 -1
- package/package.json +17 -18
|
@@ -247,14 +247,14 @@ var config = {
|
|
|
247
247
|
* - NO RETRIES (fail fast on error)
|
|
248
248
|
*/ function handler(_0, _1) {
|
|
249
249
|
return _async_to_generator(function(param, extra) {
|
|
250
|
-
var query, maxItems, filename, contentType, excludeThreadHistory, logger, storageContext, transport,
|
|
250
|
+
var query, maxItems, filename, contentType, excludeThreadHistory, logger, storageContext, transport, resourceStoreUri, baseUrl, reservation, storedName, fullPath, gmail, csvHeaders, writeStream, headerLine, totalRows, nextPageToken, started, _exec_metadata, remainingItems, pageSize, exec, csvRows, rowsContent, durationMs, truncated, uri, result, error, _cleanupError, message;
|
|
251
251
|
return _ts_generator(this, function(_state) {
|
|
252
252
|
switch(_state.label){
|
|
253
253
|
case 0:
|
|
254
254
|
query = param.query, maxItems = param.maxItems, filename = param.filename, contentType = param.contentType, excludeThreadHistory = param.excludeThreadHistory;
|
|
255
255
|
logger = extra.logger;
|
|
256
256
|
storageContext = extra.storageContext;
|
|
257
|
-
transport = storageContext.transport,
|
|
257
|
+
transport = storageContext.transport, resourceStoreUri = storageContext.resourceStoreUri, baseUrl = storageContext.baseUrl;
|
|
258
258
|
logger.info('gmail.messages.export-csv called', {
|
|
259
259
|
query: query,
|
|
260
260
|
maxItems: maxItems,
|
|
@@ -264,7 +264,7 @@ var config = {
|
|
|
264
264
|
return [
|
|
265
265
|
4,
|
|
266
266
|
(0, _server.reserveFile)(filename, {
|
|
267
|
-
|
|
267
|
+
resourceStoreUri: resourceStoreUri
|
|
268
268
|
})
|
|
269
269
|
];
|
|
270
270
|
case 1:
|
|
@@ -445,7 +445,7 @@ var config = {
|
|
|
445
445
|
});
|
|
446
446
|
// Generate URI based on transport type (stdio: file://, HTTP: http://)
|
|
447
447
|
uri = (0, _server.getFileUri)(storedName, transport, _object_spread_props(_object_spread({
|
|
448
|
-
|
|
448
|
+
resourceStoreUri: resourceStoreUri
|
|
449
449
|
}, baseUrl && {
|
|
450
450
|
baseUrl: baseUrl
|
|
451
451
|
}), {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/mcp/tools/messages-export-csv.ts"],"sourcesContent":["import { EmailContentTypeSchema, ExcludeThreadHistorySchema } from '@mcp-z/email';\nimport type { EnrichedExtra } from '@mcp-z/oauth-google';\nimport { schemas } from '@mcp-z/oauth-google';\n\nconst { AuthRequiredBranchSchema } = schemas;\n\nimport { getFileUri, reserveFile, type ToolModule } from '@mcp-z/server';\nimport { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';\n\nimport { stringify } from 'csv-stringify/sync';\nimport { createWriteStream } from 'fs';\nimport { unlink } from 'fs/promises';\nimport { type gmail_v1, google } from 'googleapis';\nimport { z } from 'zod';\nimport { DEFAULT_PAGE_SIZE } from '../../constants.ts';\nimport { extractBodyFromPayload } from '../../email/parsing/html-processing.ts';\nimport { executeQuery as executeGmailQuery } from '../../email/querying/execute-query.ts';\nimport { GmailQuerySchema } from '../../schemas/gmail-query-schema.ts';\nimport type { StorageExtra } from '../../types.ts';\n\nconst DEFAULT_MAX_ITEMS = 10000;\nconst MAX_EXPORT_ITEMS = 50000;\n\n/**\n * CSV row format based on EmailDetail\n * All fields are strings (empty string instead of undefined)\n * Includes additional CSV-specific fields: provider and labels\n */\ninterface CsvRow {\n id: string;\n threadId: string;\n from: string;\n to: string;\n cc: string;\n bcc: string;\n subject: string;\n date: string;\n snippet: string;\n body: string;\n provider: string;\n labels: string;\n}\n\nconst inputSchema = z.object({\n query: GmailQuerySchema.optional().describe('Structured query object for filtering messages. Use query-syntax prompt for reference.'),\n maxItems: z.number().int().positive().max(MAX_EXPORT_ITEMS).default(DEFAULT_MAX_ITEMS).describe(`Maximum messages to export (default: ${DEFAULT_MAX_ITEMS}, max: ${MAX_EXPORT_ITEMS})`),\n filename: z.string().trim().min(1).default('gmail-messages.csv').describe('Output filename (default: gmail-messages.csv)'),\n contentType: EmailContentTypeSchema,\n excludeThreadHistory: ExcludeThreadHistorySchema,\n});\n\nconst successBranchSchema = z.object({\n type: z.literal('success'),\n uri: z.string().describe('File URI (file:// or http://)'),\n filename: z.string().describe('Stored filename'),\n rowCount: z.number().describe('Number of messages exported'),\n truncated: z.boolean().describe('Whether export was truncated at maxItems'),\n});\n\nconst outputSchema = z.discriminatedUnion('type', [successBranchSchema, AuthRequiredBranchSchema]);\n\nconst config = {\n description: 'Export Gmail messages to CSV with streaming pagination. Returns file URI. Use query-syntax prompt for query reference.',\n inputSchema: inputSchema,\n outputSchema: z.object({\n result: outputSchema,\n }),\n} as const;\n\nexport type Input = z.infer<typeof inputSchema>;\nexport type Output = z.infer<typeof outputSchema>;\n\n/**\n * Handler for gmail-messages-export-csv tool\n *\n * CRITICAL: Streaming implementation per user requirements\n * - Generate UUID upfront\n * - Write CSV header immediately\n * - Append rows as batches arrive\n * - Delete partial file on error\n * - NO RETRIES (fail fast on error)\n */\nasync function handler({ query, maxItems, filename, contentType, excludeThreadHistory }: Input, extra: EnrichedExtra & StorageExtra) {\n const logger = extra.logger;\n const { storageContext } = extra;\n const { transport, storageDir, baseUrl } = storageContext;\n\n logger.info('gmail.messages.export-csv called', {\n query,\n maxItems,\n filename,\n accountId: extra.authContext.accountId,\n });\n\n // Reserve file location for streaming write (creates directory, generates ID, formats filename)\n const reservation = await reserveFile(filename, {\n storageDir,\n });\n const { storedName, fullPath } = reservation;\n\n logger.info('gmail.messages.export-csv starting streaming export', { path: fullPath, maxItems });\n\n try {\n const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth });\n\n // Create CSV headers (all email fields)\n const csvHeaders = ['id', 'threadId', 'from', 'to', 'cc', 'bcc', 'subject', 'date', 'snippet', 'body', 'provider', 'labels'];\n\n // Create write stream and write headers immediately\n const writeStream = createWriteStream(fullPath, { encoding: 'utf-8' });\n const headerLine = stringify([csvHeaders], { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(headerLine);\n\n // Internal pagination loop - append to CSV with each batch\n // NO RETRIES: If any error occurs, fail the whole operation and clean up\n let totalRows = 0;\n let nextPageToken: string | undefined;\n const started = Date.now();\n\n while (totalRows < maxItems) {\n const remainingItems = maxItems - totalRows;\n const pageSize = Math.min(remainingItems, DEFAULT_PAGE_SIZE);\n\n const exec: {\n items: CsvRow[];\n metadata?: { nextPageToken?: string };\n } = await executeGmailQuery(\n query,\n {\n client: gmail,\n logger,\n pageSize,\n ...(nextPageToken !== undefined && { pageToken: nextPageToken }),\n includeBody: true, // Always include body for CSV export\n },\n (full: unknown): CsvRow => {\n // Type-safe property access with guards\n const fullData = full as {\n id?: unknown;\n threadId?: unknown;\n snippet?: unknown;\n labelIds?: unknown[];\n payload?: { headers?: unknown[] };\n };\n\n const headersArray = Array.isArray(fullData?.payload?.headers) ? fullData.payload.headers : [];\n const headers = Object.fromEntries(\n headersArray.map((h: unknown) => {\n const header = h as { name?: unknown; value?: unknown };\n return [String(header.name ?? ''), String(header.value ?? '')];\n })\n );\n\n const payload = fullData?.payload;\n // Cast to Schema$MessagePart for extractBodyFromPayload\n const body = payload ? extractBodyFromPayload(payload as gmail_v1.Schema$MessagePart, { contentType, excludeThreadHistory }) : '';\n\n const labelIds = Array.isArray(fullData?.labelIds) ? fullData.labelIds.map((id) => String(id ?? '')) : [];\n\n return {\n id: String(fullData?.id ?? ''),\n threadId: fullData?.threadId ? String(fullData.threadId) : '',\n from: headers?.From || '',\n to: headers?.To || '',\n cc: headers?.Cc || '',\n bcc: headers?.Bcc || '',\n subject: headers?.Subject || '',\n date: headers?.Date || '',\n snippet: fullData?.snippet ? String(fullData.snippet) : '',\n body,\n provider: 'gmail',\n labels: labelIds.join(';'),\n };\n }\n );\n\n // Type-safe CSV row mapping\n const csvRows = exec.items.map((row) => {\n return [row.id, row.threadId, row.from, row.to, row.cc, row.bcc, row.subject, row.date, row.snippet, row.body, row.provider, row.labels];\n });\n\n // Append rows to CSV file immediately\n if (csvRows.length > 0) {\n const rowsContent = stringify(csvRows, { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(rowsContent);\n }\n\n totalRows += exec.items.length;\n nextPageToken = exec.metadata?.nextPageToken;\n\n logger.info('gmail.messages.export-csv batch written', {\n batchSize: exec.items.length,\n totalRows,\n hasMore: Boolean(nextPageToken),\n });\n\n // Exit if no more results or reached maxItems\n if (!nextPageToken || exec.items.length === 0) {\n break;\n }\n }\n\n // Close write stream\n await new Promise<void>((resolve, reject) => {\n writeStream.end(() => resolve());\n writeStream.on('error', reject);\n });\n\n const durationMs = Date.now() - started;\n const truncated = totalRows >= maxItems && Boolean(nextPageToken);\n\n logger.info('gmail.messages.export-csv completed', {\n rowCount: totalRows,\n truncated,\n durationMs,\n filename: storedName,\n });\n\n // Generate URI based on transport type (stdio: file://, HTTP: http://)\n const uri = getFileUri(storedName, transport, {\n storageDir,\n ...(baseUrl && { baseUrl }),\n endpoint: '/files',\n });\n\n const result: Output = {\n type: 'success' as const,\n uri,\n filename: storedName,\n rowCount: totalRows,\n truncated,\n };\n\n return {\n content: [\n {\n type: 'text' as const,\n text: JSON.stringify(result),\n },\n ],\n structuredContent: { result },\n };\n } catch (error) {\n // CRITICAL: Clean up partial CSV file on error\n try {\n await unlink(fullPath);\n logger.debug('Cleaned up partial CSV file after error', { path: fullPath });\n } catch (_cleanupError) {\n logger.debug('Could not clean up CSV file (may not exist)', { path: fullPath });\n }\n\n const message = error instanceof Error ? error.message : String(error);\n logger.error('gmail.messages.export-csv error', { error: message });\n\n throw new McpError(ErrorCode.InternalError, `Error exporting messages to CSV: ${message}`, {\n stack: error instanceof Error ? error.stack : undefined,\n });\n }\n}\n\nexport default function createTool() {\n return {\n name: 'messages-export-csv',\n config,\n handler,\n } satisfies ToolModule;\n}\n"],"names":["createTool","AuthRequiredBranchSchema","schemas","DEFAULT_MAX_ITEMS","MAX_EXPORT_ITEMS","inputSchema","z","object","query","GmailQuerySchema","optional","describe","maxItems","number","int","positive","max","default","filename","string","trim","min","contentType","EmailContentTypeSchema","excludeThreadHistory","ExcludeThreadHistorySchema","successBranchSchema","type","literal","uri","rowCount","truncated","boolean","outputSchema","discriminatedUnion","config","description","result","handler","extra","logger","storageContext","transport","storageDir","baseUrl","reservation","storedName","fullPath","gmail","csvHeaders","writeStream","headerLine","totalRows","nextPageToken","started","exec","remainingItems","pageSize","csvRows","rowsContent","durationMs","error","_cleanupError","message","info","accountId","authContext","reserveFile","path","google","version","auth","createWriteStream","encoding","stringify","header","quoted","quote","escape","write","Date","now","Math","DEFAULT_PAGE_SIZE","executeGmailQuery","client","undefined","pageToken","includeBody","full","fullData","headersArray","Array","isArray","payload","headers","Object","fromEntries","map","h","String","name","value","body","extractBodyFromPayload","labelIds","id","threadId","from","From","to","To","cc","Cc","bcc","Bcc","subject","Subject","date","snippet","provider","labels","join","items","row","length","metadata","batchSize","hasMore","Boolean","Promise","resolve","reject","end","on","getFileUri","endpoint","content","text","JSON","structuredContent","unlink","debug","Error","McpError","ErrorCode","InternalError","stack"],"mappings":";;;;+BAoQA;;;eAAwBA;;;qBApQ2C;2BAE3C;sBAIiC;qBACrB;oBAEV;kBACQ;wBACX;0BACe;mBACpB;2BACgB;gCACK;8BACW;kCACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAbjC,IAAM,AAAEC,2BAA6BC,oBAAO,CAApCD;AAgBR,IAAME,oBAAoB;AAC1B,IAAMC,mBAAmB;AAsBzB,IAAMC,cAAcC,MAAC,CAACC,MAAM,CAAC;IAC3BC,OAAOC,oCAAgB,CAACC,QAAQ,GAAGC,QAAQ,CAAC;IAC5CC,UAAUN,MAAC,CAACO,MAAM,GAAGC,GAAG,GAAGC,QAAQ,GAAGC,GAAG,CAACZ,kBAAkBa,OAAO,CAACd,mBAAmBQ,QAAQ,CAAC,AAAC,wCAAkEP,OAA3BD,mBAAkB,WAA0B,OAAjBC,kBAAiB;IACpLc,UAAUZ,MAAC,CAACa,MAAM,GAAGC,IAAI,GAAGC,GAAG,CAAC,GAAGJ,OAAO,CAAC,sBAAsBN,QAAQ,CAAC;IAC1EW,aAAaC,6BAAsB;IACnCC,sBAAsBC,iCAA0B;AAClD;AAEA,IAAMC,sBAAsBpB,MAAC,CAACC,MAAM,CAAC;IACnCoB,MAAMrB,MAAC,CAACsB,OAAO,CAAC;IAChBC,KAAKvB,MAAC,CAACa,MAAM,GAAGR,QAAQ,CAAC;IACzBO,UAAUZ,MAAC,CAACa,MAAM,GAA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jEjB,MAAM0G,IAAAA,kBAAU,EAACzF,YAAYJ,WAAW;wBAC5CC,YAAAA;uBACIC,WAAW;wBAAEA,SAAAA;oBAAQ;wBACzB4F,UAAU;;oBAGNnG,SAAiB;wBACrBV,MAAM;wBACNE,KAAAA;wBACAX,UAAU4B;wBACVhB,UAAUsB;wBACVrB,WAAAA;oBACF;oBAEA;;wBAAO;4BACL0G,OAAO;gCACL;oCACE9G,MAAM;oCACN+G,MAAMC,KAAKjE,SAAS,CAACrC;gCACvB;;4BAEFuG,mBAAmB;gCAAEvG,QAAAA;4BAAO;wBAC9B;;;oBACOwB;;;;;;;;;oBAGL;;wBAAMgF,IAAAA,gBAAM,EAAC9F;;;oBAAb;oBACAP,OAAOsG,KAAK,CAAC,2CAA2C;wBAAE1E,MAAMrB;oBAAS;;;;;;oBAClEe;oBACPtB,OAAOsG,KAAK,CAAC,+CAA+C;wBAAE1E,MAAMrB;oBAAS;;;;;;oBAGzEgB,UAAUF,AAAK,YAALA,OAAiBkF,SAAQlF,MAAME,OAAO,GAAGqC,OAAOvC;oBAChErB,OAAOqB,KAAK,CAAC,mCAAmC;wBAAEA,OAAOE;oBAAQ;oBAEjE,MAAM,IAAIiF,eAAQ,CAACC,gBAAS,CAACC,aAAa,EAAE,AAAC,oCAA2C,OAARnF,UAAW;wBACzFoF,OAAOtF,AAAK,YAALA,OAAiBkF,SAAQlF,MAAMsF,KAAK,GAAG7D;oBAChD;;;;;;;IAEJ;;AAEe,SAAStF;IACtB,OAAO;QACLqG,MAAM;QACNlE,QAAAA;QACAG,SAAAA;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/mcp/tools/messages-export-csv.ts"],"sourcesContent":["import { EmailContentTypeSchema, ExcludeThreadHistorySchema } from '@mcp-z/email';\nimport type { EnrichedExtra } from '@mcp-z/oauth-google';\nimport { schemas } from '@mcp-z/oauth-google';\n\nconst { AuthRequiredBranchSchema } = schemas;\n\nimport { getFileUri, reserveFile, type ToolModule } from '@mcp-z/server';\nimport { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';\n\nimport { stringify } from 'csv-stringify/sync';\nimport { createWriteStream } from 'fs';\nimport { unlink } from 'fs/promises';\nimport { type gmail_v1, google } from 'googleapis';\nimport { z } from 'zod';\nimport { DEFAULT_PAGE_SIZE } from '../../constants.ts';\nimport { extractBodyFromPayload } from '../../email/parsing/html-processing.ts';\nimport { executeQuery as executeGmailQuery } from '../../email/querying/execute-query.ts';\nimport { GmailQuerySchema } from '../../schemas/gmail-query-schema.ts';\nimport type { StorageExtra } from '../../types.ts';\n\nconst DEFAULT_MAX_ITEMS = 10000;\nconst MAX_EXPORT_ITEMS = 50000;\n\n/**\n * CSV row format based on EmailDetail\n * All fields are strings (empty string instead of undefined)\n * Includes additional CSV-specific fields: provider and labels\n */\ninterface CsvRow {\n id: string;\n threadId: string;\n from: string;\n to: string;\n cc: string;\n bcc: string;\n subject: string;\n date: string;\n snippet: string;\n body: string;\n provider: string;\n labels: string;\n}\n\nconst inputSchema = z.object({\n query: GmailQuerySchema.optional().describe('Structured query object for filtering messages. Use query-syntax prompt for reference.'),\n maxItems: z.number().int().positive().max(MAX_EXPORT_ITEMS).default(DEFAULT_MAX_ITEMS).describe(`Maximum messages to export (default: ${DEFAULT_MAX_ITEMS}, max: ${MAX_EXPORT_ITEMS})`),\n filename: z.string().trim().min(1).default('gmail-messages.csv').describe('Output filename (default: gmail-messages.csv)'),\n contentType: EmailContentTypeSchema,\n excludeThreadHistory: ExcludeThreadHistorySchema,\n});\n\nconst successBranchSchema = z.object({\n type: z.literal('success'),\n uri: z.string().describe('File URI (file:// or http://)'),\n filename: z.string().describe('Stored filename'),\n rowCount: z.number().describe('Number of messages exported'),\n truncated: z.boolean().describe('Whether export was truncated at maxItems'),\n});\n\nconst outputSchema = z.discriminatedUnion('type', [successBranchSchema, AuthRequiredBranchSchema]);\n\nconst config = {\n description: 'Export Gmail messages to CSV with streaming pagination. Returns file URI. Use query-syntax prompt for query reference.',\n inputSchema: inputSchema,\n outputSchema: z.object({\n result: outputSchema,\n }),\n} as const;\n\nexport type Input = z.infer<typeof inputSchema>;\nexport type Output = z.infer<typeof outputSchema>;\n\n/**\n * Handler for gmail-messages-export-csv tool\n *\n * CRITICAL: Streaming implementation per user requirements\n * - Generate UUID upfront\n * - Write CSV header immediately\n * - Append rows as batches arrive\n * - Delete partial file on error\n * - NO RETRIES (fail fast on error)\n */\nasync function handler({ query, maxItems, filename, contentType, excludeThreadHistory }: Input, extra: EnrichedExtra & StorageExtra) {\n const logger = extra.logger;\n const { storageContext } = extra;\n const { transport, resourceStoreUri, baseUrl } = storageContext;\n\n logger.info('gmail.messages.export-csv called', {\n query,\n maxItems,\n filename,\n accountId: extra.authContext.accountId,\n });\n\n // Reserve file location for streaming write (creates directory, generates ID, formats filename)\n const reservation = await reserveFile(filename, {\n resourceStoreUri,\n });\n const { storedName, fullPath } = reservation;\n\n logger.info('gmail.messages.export-csv starting streaming export', { path: fullPath, maxItems });\n\n try {\n const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth });\n\n // Create CSV headers (all email fields)\n const csvHeaders = ['id', 'threadId', 'from', 'to', 'cc', 'bcc', 'subject', 'date', 'snippet', 'body', 'provider', 'labels'];\n\n // Create write stream and write headers immediately\n const writeStream = createWriteStream(fullPath, { encoding: 'utf-8' });\n const headerLine = stringify([csvHeaders], { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(headerLine);\n\n // Internal pagination loop - append to CSV with each batch\n // NO RETRIES: If any error occurs, fail the whole operation and clean up\n let totalRows = 0;\n let nextPageToken: string | undefined;\n const started = Date.now();\n\n while (totalRows < maxItems) {\n const remainingItems = maxItems - totalRows;\n const pageSize = Math.min(remainingItems, DEFAULT_PAGE_SIZE);\n\n const exec: {\n items: CsvRow[];\n metadata?: { nextPageToken?: string };\n } = await executeGmailQuery(\n query,\n {\n client: gmail,\n logger,\n pageSize,\n ...(nextPageToken !== undefined && { pageToken: nextPageToken }),\n includeBody: true, // Always include body for CSV export\n },\n (full: unknown): CsvRow => {\n // Type-safe property access with guards\n const fullData = full as {\n id?: unknown;\n threadId?: unknown;\n snippet?: unknown;\n labelIds?: unknown[];\n payload?: { headers?: unknown[] };\n };\n\n const headersArray = Array.isArray(fullData?.payload?.headers) ? fullData.payload.headers : [];\n const headers = Object.fromEntries(\n headersArray.map((h: unknown) => {\n const header = h as { name?: unknown; value?: unknown };\n return [String(header.name ?? ''), String(header.value ?? '')];\n })\n );\n\n const payload = fullData?.payload;\n // Cast to Schema$MessagePart for extractBodyFromPayload\n const body = payload ? extractBodyFromPayload(payload as gmail_v1.Schema$MessagePart, { contentType, excludeThreadHistory }) : '';\n\n const labelIds = Array.isArray(fullData?.labelIds) ? fullData.labelIds.map((id) => String(id ?? '')) : [];\n\n return {\n id: String(fullData?.id ?? ''),\n threadId: fullData?.threadId ? String(fullData.threadId) : '',\n from: headers?.From || '',\n to: headers?.To || '',\n cc: headers?.Cc || '',\n bcc: headers?.Bcc || '',\n subject: headers?.Subject || '',\n date: headers?.Date || '',\n snippet: fullData?.snippet ? String(fullData.snippet) : '',\n body,\n provider: 'gmail',\n labels: labelIds.join(';'),\n };\n }\n );\n\n // Type-safe CSV row mapping\n const csvRows = exec.items.map((row) => {\n return [row.id, row.threadId, row.from, row.to, row.cc, row.bcc, row.subject, row.date, row.snippet, row.body, row.provider, row.labels];\n });\n\n // Append rows to CSV file immediately\n if (csvRows.length > 0) {\n const rowsContent = stringify(csvRows, { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(rowsContent);\n }\n\n totalRows += exec.items.length;\n nextPageToken = exec.metadata?.nextPageToken;\n\n logger.info('gmail.messages.export-csv batch written', {\n batchSize: exec.items.length,\n totalRows,\n hasMore: Boolean(nextPageToken),\n });\n\n // Exit if no more results or reached maxItems\n if (!nextPageToken || exec.items.length === 0) {\n break;\n }\n }\n\n // Close write stream\n await new Promise<void>((resolve, reject) => {\n writeStream.end(() => resolve());\n writeStream.on('error', reject);\n });\n\n const durationMs = Date.now() - started;\n const truncated = totalRows >= maxItems && Boolean(nextPageToken);\n\n logger.info('gmail.messages.export-csv completed', {\n rowCount: totalRows,\n truncated,\n durationMs,\n filename: storedName,\n });\n\n // Generate URI based on transport type (stdio: file://, HTTP: http://)\n const uri = getFileUri(storedName, transport, {\n resourceStoreUri,\n ...(baseUrl && { baseUrl }),\n endpoint: '/files',\n });\n\n const result: Output = {\n type: 'success' as const,\n uri,\n filename: storedName,\n rowCount: totalRows,\n truncated,\n };\n\n return {\n content: [\n {\n type: 'text' as const,\n text: JSON.stringify(result),\n },\n ],\n structuredContent: { result },\n };\n } catch (error) {\n // CRITICAL: Clean up partial CSV file on error\n try {\n await unlink(fullPath);\n logger.debug('Cleaned up partial CSV file after error', { path: fullPath });\n } catch (_cleanupError) {\n logger.debug('Could not clean up CSV file (may not exist)', { path: fullPath });\n }\n\n const message = error instanceof Error ? error.message : String(error);\n logger.error('gmail.messages.export-csv error', { error: message });\n\n throw new McpError(ErrorCode.InternalError, `Error exporting messages to CSV: ${message}`, {\n stack: error instanceof Error ? error.stack : undefined,\n });\n }\n}\n\nexport default function createTool() {\n return {\n name: 'messages-export-csv',\n config,\n handler,\n } satisfies ToolModule;\n}\n"],"names":["createTool","AuthRequiredBranchSchema","schemas","DEFAULT_MAX_ITEMS","MAX_EXPORT_ITEMS","inputSchema","z","object","query","GmailQuerySchema","optional","describe","maxItems","number","int","positive","max","default","filename","string","trim","min","contentType","EmailContentTypeSchema","excludeThreadHistory","ExcludeThreadHistorySchema","successBranchSchema","type","literal","uri","rowCount","truncated","boolean","outputSchema","discriminatedUnion","config","description","result","handler","extra","logger","storageContext","transport","resourceStoreUri","baseUrl","reservation","storedName","fullPath","gmail","csvHeaders","writeStream","headerLine","totalRows","nextPageToken","started","exec","remainingItems","pageSize","csvRows","rowsContent","durationMs","error","_cleanupError","message","info","accountId","authContext","reserveFile","path","google","version","auth","createWriteStream","encoding","stringify","header","quoted","quote","escape","write","Date","now","Math","DEFAULT_PAGE_SIZE","executeGmailQuery","client","undefined","pageToken","includeBody","full","fullData","headersArray","Array","isArray","payload","headers","Object","fromEntries","map","h","String","name","value","body","extractBodyFromPayload","labelIds","id","threadId","from","From","to","To","cc","Cc","bcc","Bcc","subject","Subject","date","snippet","provider","labels","join","items","row","length","metadata","batchSize","hasMore","Boolean","Promise","resolve","reject","end","on","getFileUri","endpoint","content","text","JSON","structuredContent","unlink","debug","Error","McpError","ErrorCode","InternalError","stack"],"mappings":";;;;+BAoQA;;;eAAwBA;;;qBApQ2C;2BAE3C;sBAIiC;qBACrB;oBAEV;kBACQ;wBACX;0BACe;mBACpB;2BACgB;gCACK;8BACW;kCACjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAbjC,IAAM,AAAEC,2BAA6BC,oBAAO,CAApCD;AAgBR,IAAME,oBAAoB;AAC1B,IAAMC,mBAAmB;AAsBzB,IAAMC,cAAcC,MAAC,CAACC,MAAM,CAAC;IAC3BC,OAAOC,oCAAgB,CAACC,QAAQ,GAAGC,QAAQ,CAAC;IAC5CC,UAAUN,MAAC,CAACO,MAAM,GAAGC,GAAG,GAAGC,QAAQ,GAAGC,GAAG,CAACZ,kBAAkBa,OAAO,CAACd,mBAAmBQ,QAAQ,CAAC,AAAC,wCAAkEP,OAA3BD,mBAAkB,WAA0B,OAAjBC,kBAAiB;IACpLc,UAAUZ,MAAC,CAACa,MAAM,GAAGC,IAAI,GAAGC,GAAG,CAAC,GAAGJ,OAAO,CAAC,sBAAsBN,QAAQ,CAAC;IAC1EW,aAAaC,6BAAsB;IACnCC,sBAAsBC,iCAA0B;AAClD;AAEA,IAAMC,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Enable HTTP transport on specified port
|
|
22
22
|
* - --stdio Enable stdio transport (default if no port)
|
|
23
23
|
* - --log-level=<level> Logging level (default: info)
|
|
24
|
-
* - --
|
|
24
|
+
* - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)
|
|
25
25
|
* - --base-url=<url> Base URL for HTTP file serving (optional)
|
|
26
26
|
*
|
|
27
27
|
* Environment Variables:
|
|
@@ -32,9 +32,10 @@ export declare function handleVersionHelp(args: string[]): {
|
|
|
32
32
|
* - DCR_MODE DCR mode (optional, same format as --dcr-mode)
|
|
33
33
|
* - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)
|
|
34
34
|
* - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)
|
|
35
|
+
* - TOKEN_STORE_URI Token storage URI (optional)
|
|
35
36
|
* - PORT Default HTTP port (optional)
|
|
36
37
|
* - LOG_LEVEL Default logging level (optional)
|
|
37
|
-
* -
|
|
38
|
+
* - RESOURCE_STORE_URI Resource store URI (optional, file://)
|
|
38
39
|
* - BASE_URL Default base URL for file serving (optional)
|
|
39
40
|
*
|
|
40
41
|
* OAuth Scopes (from constants.ts):
|
|
@@ -21,7 +21,7 @@ export declare function handleVersionHelp(args: string[]): {
|
|
|
21
21
|
* - --port=<port> Enable HTTP transport on specified port
|
|
22
22
|
* - --stdio Enable stdio transport (default if no port)
|
|
23
23
|
* - --log-level=<level> Logging level (default: info)
|
|
24
|
-
* - --
|
|
24
|
+
* - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)
|
|
25
25
|
* - --base-url=<url> Base URL for HTTP file serving (optional)
|
|
26
26
|
*
|
|
27
27
|
* Environment Variables:
|
|
@@ -32,9 +32,10 @@ export declare function handleVersionHelp(args: string[]): {
|
|
|
32
32
|
* - DCR_MODE DCR mode (optional, same format as --dcr-mode)
|
|
33
33
|
* - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)
|
|
34
34
|
* - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)
|
|
35
|
+
* - TOKEN_STORE_URI Token storage URI (optional)
|
|
35
36
|
* - PORT Default HTTP port (optional)
|
|
36
37
|
* - LOG_LEVEL Default logging level (optional)
|
|
37
|
-
* -
|
|
38
|
+
* - RESOURCE_STORE_URI Resource store URI (optional, file://)
|
|
38
39
|
* - BASE_URL Default base URL for file serving (optional)
|
|
39
40
|
*
|
|
40
41
|
* OAuth Scopes (from constants.ts):
|
package/dist/cjs/setup/config.js
CHANGED
|
@@ -131,7 +131,7 @@ function _type_of(obj) {
|
|
|
131
131
|
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
132
132
|
}
|
|
133
133
|
var pkg = JSON.parse(_fs.readFileSync(_path.join((0, _modulerootsync.default)(_url.fileURLToPath(require("url").pathToFileURL(__filename).toString())), 'package.json'), 'utf-8'));
|
|
134
|
-
var HELP_TEXT = "\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --
|
|
134
|
+
var HELP_TEXT = "\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)\n --base-url=<url> Base URL for HTTP file serving (optional)\n\nEnvironment Variables:\n GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n AUTH_MODE Default authentication mode (optional)\n HEADLESS Disable browser auto-open (optional)\n DCR_MODE DCR mode (optional, same format as --dcr-mode)\n DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n TOKEN_STORE_URI Token storage URI (optional)\n PORT Default HTTP port (optional)\n LOG_LEVEL Default logging level (optional)\n RESOURCE_STORE_URI Resource store URI (optional, file://)\n BASE_URL Default base URL for file serving (optional)\n\nOAuth Scopes:\n openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n\nExamples:\n mcp-gmail # Use default settings\n mcp-gmail --auth=service-account # Use service account auth\n mcp-gmail --port=3000 # HTTP transport on port 3000\n mcp-gmail --resource-store-uri=file:///tmp/emails # Custom resource store URI\n GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var\n".trim();
|
|
135
135
|
function handleVersionHelp(args) {
|
|
136
136
|
var values = (0, _util.parseArgs)({
|
|
137
137
|
args: args,
|
|
@@ -163,7 +163,7 @@ function parseConfig(args, env) {
|
|
|
163
163
|
var oauthConfig = (0, _oauthgoogle.parseConfig)(args, env);
|
|
164
164
|
// Parse DCR configuration if DCR mode is enabled
|
|
165
165
|
var dcrConfig = oauthConfig.auth === 'dcr' ? (0, _oauthgoogle.parseDcrConfig)(args, env, _constantsts.GOOGLE_SCOPE) : undefined;
|
|
166
|
-
// Parse application-level config (LOG_LEVEL,
|
|
166
|
+
// Parse application-level config (LOG_LEVEL, RESOURCE_STORE_URI, BASE_URL)
|
|
167
167
|
var values = (0, _util.parseArgs)({
|
|
168
168
|
args: args,
|
|
169
169
|
options: {
|
|
@@ -173,7 +173,7 @@ function parseConfig(args, env) {
|
|
|
173
173
|
'base-url': {
|
|
174
174
|
type: 'string'
|
|
175
175
|
},
|
|
176
|
-
'
|
|
176
|
+
'resource-store-uri': {
|
|
177
177
|
type: 'string'
|
|
178
178
|
}
|
|
179
179
|
},
|
|
@@ -200,10 +200,10 @@ function parseConfig(args, env) {
|
|
|
200
200
|
var envLogLevel = env.LOG_LEVEL;
|
|
201
201
|
var logLevel = (_ref1 = cliLogLevel !== null && cliLogLevel !== void 0 ? cliLogLevel : envLogLevel) !== null && _ref1 !== void 0 ? _ref1 : 'info';
|
|
202
202
|
// Parse storage configuration
|
|
203
|
-
var
|
|
204
|
-
var
|
|
205
|
-
var
|
|
206
|
-
|
|
203
|
+
var cliResourceStoreUri = typeof values['resource-store-uri'] === 'string' ? values['resource-store-uri'] : undefined;
|
|
204
|
+
var envResourceStoreUri = env.RESOURCE_STORE_URI;
|
|
205
|
+
var defaultResourceStorePath = _path.join(baseDir, name, 'files');
|
|
206
|
+
var resourceStoreUri = normalizeResourceStoreUri((_ref2 = cliResourceStoreUri !== null && cliResourceStoreUri !== void 0 ? cliResourceStoreUri : envResourceStoreUri) !== null && _ref2 !== void 0 ? _ref2 : defaultResourceStorePath);
|
|
207
207
|
var cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;
|
|
208
208
|
var envBaseUrl = env.BASE_URL;
|
|
209
209
|
var baseUrl = cliBaseUrl !== null && cliBaseUrl !== void 0 ? cliBaseUrl : envBaseUrl;
|
|
@@ -215,7 +215,7 @@ function parseConfig(args, env) {
|
|
|
215
215
|
name: name,
|
|
216
216
|
version: pkg.version,
|
|
217
217
|
repositoryUrl: repositoryUrl,
|
|
218
|
-
|
|
218
|
+
resourceStoreUri: resourceStoreUri
|
|
219
219
|
});
|
|
220
220
|
if (baseUrl) result.baseUrl = baseUrl;
|
|
221
221
|
if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;
|
|
@@ -224,4 +224,15 @@ function parseConfig(args, env) {
|
|
|
224
224
|
function createConfig() {
|
|
225
225
|
return parseConfig(process.argv, process.env);
|
|
226
226
|
}
|
|
227
|
+
function normalizeResourceStoreUri(resourceStoreUri) {
|
|
228
|
+
var filePrefix = 'file://';
|
|
229
|
+
if (resourceStoreUri.startsWith(filePrefix)) {
|
|
230
|
+
var rawPath = resourceStoreUri.slice(filePrefix.length);
|
|
231
|
+
var expandedPath = rawPath.startsWith('~') ? rawPath.replace(/^~/, (0, _os.homedir)()) : rawPath;
|
|
232
|
+
return "".concat(filePrefix).concat(_path.resolve(expandedPath));
|
|
233
|
+
}
|
|
234
|
+
if (resourceStoreUri.includes('://')) return resourceStoreUri;
|
|
235
|
+
var expandedPath1 = resourceStoreUri.startsWith('~') ? resourceStoreUri.replace(/^~/, (0, _os.homedir)()) : resourceStoreUri;
|
|
236
|
+
return "".concat(filePrefix).concat(_path.resolve(expandedPath1));
|
|
237
|
+
}
|
|
227
238
|
/* CJS INTEROP */ if (exports.__esModule && exports.default) { try { Object.defineProperty(exports.default, '__esModule', { value: true }); for (var key in exports) { exports.default[key] = exports[key]; } } catch (_) {}; module.exports = exports.default; }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/config.ts"],"sourcesContent":["import { parseDcrConfig, parseConfig as parseOAuthConfig } from '@mcp-z/oauth-google';\nimport { findConfigPath, parseConfig as parseTransportConfig } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport moduleRoot from 'module-root-sync';\nimport { homedir } from 'os';\nimport * as path from 'path';\nimport * as url from 'url';\nimport { parseArgs } from 'util';\nimport { GOOGLE_SCOPE } from '../constants.ts';\nimport type { ServerConfig } from '../types.ts';\n\nconst pkg = JSON.parse(fs.readFileSync(path.join(moduleRoot(url.fileURLToPath(import.meta.url)), 'package.json'), 'utf-8'));\n\nconst HELP_TEXT = `\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --storage-dir=<path> Directory for CSV file storage (default: .mcp-z/files)\n --base-url=<url> Base URL for HTTP file serving (optional)\n\nEnvironment Variables:\n GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n AUTH_MODE Default authentication mode (optional)\n HEADLESS Disable browser auto-open (optional)\n DCR_MODE DCR mode (optional, same format as --dcr-mode)\n DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n PORT Default HTTP port (optional)\n LOG_LEVEL Default logging level (optional)\n STORAGE_DIR Default storage directory (optional)\n BASE_URL Default base URL for file serving (optional)\n\nOAuth Scopes:\n openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n\nExamples:\n mcp-gmail # Use default settings\n mcp-gmail --auth=service-account # Use service account auth\n mcp-gmail --port=3000 # HTTP transport on port 3000\n mcp-gmail --storage-dir=./emails # Custom storage directory\n GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var\n`.trim();\n\n/**\n * Handle --version and --help flags before config parsing.\n * These should work without requiring any configuration.\n */\nexport function handleVersionHelp(args: string[]): { handled: boolean; output?: string } {\n const { values } = parseArgs({\n args,\n options: {\n version: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n strict: false,\n });\n\n if (values.version) return { handled: true, output: pkg.version };\n if (values.help) return { handled: true, output: HELP_TEXT };\n return { handled: false };\n}\n\n/**\n * Parse Gmail server configuration from CLI arguments and environment.\n *\n * CLI Arguments (all optional):\n * - --auth=<mode> Authentication mode (default: loopback-oauth)\n * Modes: loopback-oauth, service-account, dcr\n * - --headless Disable browser auto-open, return auth URL instead\n * - --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n * - --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n * - --dcr-verify-url=<url> External verification endpoint (required for external mode)\n * - --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n * - --port=<port> Enable HTTP transport on specified port\n * - --stdio Enable stdio transport (default if no port)\n * - --log-level=<level> Logging level (default: info)\n * - --storage-dir=<path> Directory for CSV file storage (default: .mcp-z/files)\n * - --base-url=<url> Base URL for HTTP file serving (optional)\n *\n * Environment Variables:\n * - GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n * - GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n * - AUTH_MODE Default authentication mode (optional)\n * - HEADLESS Disable browser auto-open (optional)\n * - DCR_MODE DCR mode (optional, same format as --dcr-mode)\n * - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n * - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n * - PORT Default HTTP port (optional)\n * - LOG_LEVEL Default logging level (optional)\n * - STORAGE_DIR Default storage directory (optional)\n * - BASE_URL Default base URL for file serving (optional)\n *\n * OAuth Scopes (from constants.ts):\n * openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n */\nexport function parseConfig(args: string[], env: Record<string, string | undefined>): ServerConfig {\n const transportConfig = parseTransportConfig(args, env);\n const oauthConfig = parseOAuthConfig(args, env);\n\n // Parse DCR configuration if DCR mode is enabled\n const dcrConfig = oauthConfig.auth === 'dcr' ? parseDcrConfig(args, env, GOOGLE_SCOPE) : undefined;\n\n // Parse application-level config (LOG_LEVEL, STORAGE_DIR, BASE_URL)\n const { values } = parseArgs({\n args,\n options: {\n 'log-level': { type: 'string' },\n 'base-url': { type: 'string' },\n 'storage-dir': { type: 'string' },\n },\n strict: false, // Allow other arguments\n allowPositionals: true,\n });\n\n const name = pkg.name.replace(/^@[^/]+\\//, '');\n // Parse repository URL from package.json, stripping git+ prefix and .git suffix\n const rawRepoUrl = typeof pkg.repository === 'object' ? pkg.repository.url : pkg.repository;\n const repositoryUrl = rawRepoUrl?.replace(/^git\\+/, '').replace(/\\.git$/, '') ?? `https://github.com/mcp-z/${name}`;\n let rootDir = homedir();\n try {\n const configPath = findConfigPath({ config: '.mcp.json', cwd: process.cwd(), stopDir: homedir() });\n rootDir = path.dirname(configPath);\n } catch {\n rootDir = homedir();\n }\n const baseDir = path.join(rootDir, '.mcp-z');\n const cliLogLevel = typeof values['log-level'] === 'string' ? values['log-level'] : undefined;\n const envLogLevel = env.LOG_LEVEL;\n const logLevel = cliLogLevel ?? envLogLevel ?? 'info';\n\n // Parse storage configuration\n const cliStorageDir = typeof values['storage-dir'] === 'string' ? values['storage-dir'] : undefined;\n const envStorageDir = env.STORAGE_DIR;\n let storageDir = cliStorageDir ?? envStorageDir ?? path.join(baseDir, name, 'files');\n if (storageDir.startsWith('~')) storageDir = storageDir.replace(/^~/, homedir());\n\n const cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;\n const envBaseUrl = env.BASE_URL;\n const baseUrl = cliBaseUrl ?? envBaseUrl;\n\n // Combine configs\n const result: ServerConfig = {\n ...oauthConfig, // Includes clientId, auth, headless, redirectUri, serviceAccountKeyFile\n transport: transportConfig.transport,\n logLevel,\n baseDir,\n name,\n version: pkg.version,\n repositoryUrl,\n storageDir: path.resolve(storageDir),\n };\n if (baseUrl) result.baseUrl = baseUrl;\n if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;\n return result;\n}\n\n/**\n * Build production configuration from process globals.\n * Entry point for production server.\n */\nexport function createConfig(): ServerConfig {\n return parseConfig(process.argv, process.env);\n}\n"],"names":["createConfig","handleVersionHelp","parseConfig","pkg","JSON","parse","fs","readFileSync","path","join","moduleRoot","url","fileURLToPath","HELP_TEXT","trim","args","values","parseArgs","options","version","type","help","strict","handled","output","env","cliLogLevel","cliStorageDir","transportConfig","parseTransportConfig","oauthConfig","parseOAuthConfig","dcrConfig","auth","parseDcrConfig","GOOGLE_SCOPE","undefined","allowPositionals","name","replace","rawRepoUrl","repository","repositoryUrl","rootDir","homedir","configPath","findConfigPath","config","cwd","process","stopDir","dirname","baseDir","envLogLevel","LOG_LEVEL","logLevel","envStorageDir","STORAGE_DIR","storageDir","startsWith","cliBaseUrl","envBaseUrl","BASE_URL","baseUrl","result","transport","resolve","argv"],"mappings":";;;;;;;;;;;QA+KgBA;eAAAA;;QAjHAC;eAAAA;;QAgDAC;eAAAA;;;2BA9GgD;sBACI;0DAChD;qEACG;kBACC;4DACF;2DACD;oBACK;2BACG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAG7B,IAAMC,MAAMC,KAAKC,KAAK,CAACC,IAAGC,YAAY,CAACC,MAAKC,IAAI,CAACC,IAAAA,uBAAU,EAACC,KAAIC,aAAa,CAAC,uDAAmB,iBAAiB;AAElH,IAAMC,YAAY,gyEA2ChBC,IAAI;AAMC,SAASb,kBAAkBc,IAAc;IAC9C,IAAM,AAAEC,SAAWC,IAAAA,eAAS,EAAC;QAC3BF,MAAAA;QACAG,SAAS;YACPC,SAAS;gBAAEC,MAAM;YAAU;YAC3BC,MAAM;gBAAED,MAAM;YAAU;QAC1B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
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/config.ts"],"sourcesContent":["import { parseDcrConfig, parseConfig as parseOAuthConfig } from '@mcp-z/oauth-google';\nimport { findConfigPath, parseConfig as parseTransportConfig } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport moduleRoot from 'module-root-sync';\nimport { homedir } from 'os';\nimport * as path from 'path';\nimport * as url from 'url';\nimport { parseArgs } from 'util';\nimport { GOOGLE_SCOPE } from '../constants.ts';\nimport type { ServerConfig } from '../types.ts';\n\nconst pkg = JSON.parse(fs.readFileSync(path.join(moduleRoot(url.fileURLToPath(import.meta.url)), 'package.json'), 'utf-8'));\n\nconst HELP_TEXT = `\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)\n --base-url=<url> Base URL for HTTP file serving (optional)\n\nEnvironment Variables:\n GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n AUTH_MODE Default authentication mode (optional)\n HEADLESS Disable browser auto-open (optional)\n DCR_MODE DCR mode (optional, same format as --dcr-mode)\n DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n TOKEN_STORE_URI Token storage URI (optional)\n PORT Default HTTP port (optional)\n LOG_LEVEL Default logging level (optional)\n RESOURCE_STORE_URI Resource store URI (optional, file://)\n BASE_URL Default base URL for file serving (optional)\n\nOAuth Scopes:\n openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n\nExamples:\n mcp-gmail # Use default settings\n mcp-gmail --auth=service-account # Use service account auth\n mcp-gmail --port=3000 # HTTP transport on port 3000\n mcp-gmail --resource-store-uri=file:///tmp/emails # Custom resource store URI\n GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var\n`.trim();\n\n/**\n * Handle --version and --help flags before config parsing.\n * These should work without requiring any configuration.\n */\nexport function handleVersionHelp(args: string[]): { handled: boolean; output?: string } {\n const { values } = parseArgs({\n args,\n options: {\n version: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n strict: false,\n });\n\n if (values.version) return { handled: true, output: pkg.version };\n if (values.help) return { handled: true, output: HELP_TEXT };\n return { handled: false };\n}\n\n/**\n * Parse Gmail server configuration from CLI arguments and environment.\n *\n * CLI Arguments (all optional):\n * - --auth=<mode> Authentication mode (default: loopback-oauth)\n * Modes: loopback-oauth, service-account, dcr\n * - --headless Disable browser auto-open, return auth URL instead\n * - --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n * - --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n * - --dcr-verify-url=<url> External verification endpoint (required for external mode)\n * - --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n * - --port=<port> Enable HTTP transport on specified port\n * - --stdio Enable stdio transport (default if no port)\n * - --log-level=<level> Logging level (default: info)\n * - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)\n * - --base-url=<url> Base URL for HTTP file serving (optional)\n *\n * Environment Variables:\n * - GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n * - GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n * - AUTH_MODE Default authentication mode (optional)\n * - HEADLESS Disable browser auto-open (optional)\n * - DCR_MODE DCR mode (optional, same format as --dcr-mode)\n * - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n * - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n * - TOKEN_STORE_URI Token storage URI (optional)\n * - PORT Default HTTP port (optional)\n * - LOG_LEVEL Default logging level (optional)\n * - RESOURCE_STORE_URI Resource store URI (optional, file://)\n * - BASE_URL Default base URL for file serving (optional)\n *\n * OAuth Scopes (from constants.ts):\n * openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n */\nexport function parseConfig(args: string[], env: Record<string, string | undefined>): ServerConfig {\n const transportConfig = parseTransportConfig(args, env);\n const oauthConfig = parseOAuthConfig(args, env);\n\n // Parse DCR configuration if DCR mode is enabled\n const dcrConfig = oauthConfig.auth === 'dcr' ? parseDcrConfig(args, env, GOOGLE_SCOPE) : undefined;\n\n // Parse application-level config (LOG_LEVEL, RESOURCE_STORE_URI, BASE_URL)\n const { values } = parseArgs({\n args,\n options: {\n 'log-level': { type: 'string' },\n 'base-url': { type: 'string' },\n 'resource-store-uri': { type: 'string' },\n },\n strict: false, // Allow other arguments\n allowPositionals: true,\n });\n\n const name = pkg.name.replace(/^@[^/]+\\//, '');\n // Parse repository URL from package.json, stripping git+ prefix and .git suffix\n const rawRepoUrl = typeof pkg.repository === 'object' ? pkg.repository.url : pkg.repository;\n const repositoryUrl = rawRepoUrl?.replace(/^git\\+/, '').replace(/\\.git$/, '') ?? `https://github.com/mcp-z/${name}`;\n let rootDir = homedir();\n try {\n const configPath = findConfigPath({ config: '.mcp.json', cwd: process.cwd(), stopDir: homedir() });\n rootDir = path.dirname(configPath);\n } catch {\n rootDir = homedir();\n }\n const baseDir = path.join(rootDir, '.mcp-z');\n const cliLogLevel = typeof values['log-level'] === 'string' ? values['log-level'] : undefined;\n const envLogLevel = env.LOG_LEVEL;\n const logLevel = cliLogLevel ?? envLogLevel ?? 'info';\n\n // Parse storage configuration\n const cliResourceStoreUri = typeof values['resource-store-uri'] === 'string' ? values['resource-store-uri'] : undefined;\n const envResourceStoreUri = env.RESOURCE_STORE_URI;\n const defaultResourceStorePath = path.join(baseDir, name, 'files');\n const resourceStoreUri = normalizeResourceStoreUri(cliResourceStoreUri ?? envResourceStoreUri ?? defaultResourceStorePath);\n\n const cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;\n const envBaseUrl = env.BASE_URL;\n const baseUrl = cliBaseUrl ?? envBaseUrl;\n\n // Combine configs\n const result: ServerConfig = {\n ...oauthConfig, // Includes clientId, auth, headless, redirectUri, serviceAccountKeyFile\n transport: transportConfig.transport,\n logLevel,\n baseDir,\n name,\n version: pkg.version,\n repositoryUrl,\n resourceStoreUri,\n };\n if (baseUrl) result.baseUrl = baseUrl;\n if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;\n return result;\n}\n\n/**\n * Build production configuration from process globals.\n * Entry point for production server.\n */\nexport function createConfig(): ServerConfig {\n return parseConfig(process.argv, process.env);\n}\n\nfunction normalizeResourceStoreUri(resourceStoreUri: string): string {\n const filePrefix = 'file://';\n if (resourceStoreUri.startsWith(filePrefix)) {\n const rawPath = resourceStoreUri.slice(filePrefix.length);\n const expandedPath = rawPath.startsWith('~') ? rawPath.replace(/^~/, homedir()) : rawPath;\n return `${filePrefix}${path.resolve(expandedPath)}`;\n }\n\n if (resourceStoreUri.includes('://')) return resourceStoreUri;\n\n const expandedPath = resourceStoreUri.startsWith('~') ? resourceStoreUri.replace(/^~/, homedir()) : resourceStoreUri;\n return `${filePrefix}${path.resolve(expandedPath)}`;\n}\n"],"names":["createConfig","handleVersionHelp","parseConfig","pkg","JSON","parse","fs","readFileSync","path","join","moduleRoot","url","fileURLToPath","HELP_TEXT","trim","args","values","parseArgs","options","version","type","help","strict","handled","output","env","cliLogLevel","cliResourceStoreUri","transportConfig","parseTransportConfig","oauthConfig","parseOAuthConfig","dcrConfig","auth","parseDcrConfig","GOOGLE_SCOPE","undefined","allowPositionals","name","replace","rawRepoUrl","repository","repositoryUrl","rootDir","homedir","configPath","findConfigPath","config","cwd","process","stopDir","dirname","baseDir","envLogLevel","LOG_LEVEL","logLevel","envResourceStoreUri","RESOURCE_STORE_URI","defaultResourceStorePath","resourceStoreUri","normalizeResourceStoreUri","cliBaseUrl","envBaseUrl","BASE_URL","baseUrl","result","transport","argv","filePrefix","startsWith","rawPath","slice","length","expandedPath","resolve","includes"],"mappings":";;;;;;;;;;;QAiLgBA;eAAAA;;QAlHAC;eAAAA;;QAiDAC;eAAAA;;;2BAhHgD;sBACI;0DAChD;qEACG;kBACC;4DACF;2DACD;oBACK;2BACG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAG7B,IAAMC,MAAMC,KAAKC,KAAK,CAACC,IAAGC,YAAY,CAACC,MAAKC,IAAI,CAACC,IAAAA,uBAAU,EAACC,KAAIC,aAAa,CAAC,uDAAmB,iBAAiB;AAElH,IAAMC,YAAY,q5EA4ChBC,IAAI;AAMC,SAASb,kBAAkBc,IAAc;IAC9C,IAAM,AAAEC,SAAWC,IAAAA,eAAS,EAAC;QAC3BF,MAAAA;QACAG,SAAS;YACPC,SAAS;gBAAEC,MAAM;YAAU;YAC3BC,MAAM;gBAAED,MAAM;YAAU;QAC1B;QACAE,QAAQ;IACV,GAPQN;IASR,IAAIA,OAAOG,OAAO,EAAE,OAAO;QAAEI,SAAS;QAAMC,QAAQrB,IAAIgB,OAAO;IAAC;IAChE,IAAIH,OAAOK,IAAI,EAAE,OAAO;QAAEE,SAAS;QAAMC,QAAQX;IAAU;IAC3D,OAAO;QAAEU,SAAS;IAAM;AAC1B;AAoCO,SAASrB,YAAYa,IAAc,EAAEU,GAAuC;cAiChEC,OAMkCC;IAtCnD,IAAMC,kBAAkBC,IAAAA,mBAAoB,EAACd,MAAMU;IACnD,I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AOV,iBAAiBpB,OAAO,CAAC,MAAMK,IAAAA,WAAO,OAAMe;IACpG,OAAO,AAAC,GAAenD,OAAb4D,YAAwC,OAA3B5D,MAAKkE,OAAO,CAACD;AACtC"}
|
package/dist/cjs/setup/http.js
CHANGED
|
@@ -210,7 +210,7 @@ function createHTTPServer(config, overrides) {
|
|
|
210
210
|
logger.info('Mounted loopback OAuth callback router');
|
|
211
211
|
}
|
|
212
212
|
fileRouter = (0, _server.createFileServingRouter)({
|
|
213
|
-
|
|
213
|
+
resourceStoreUri: config.resourceStoreUri
|
|
214
214
|
}, {
|
|
215
215
|
contentType: 'text/csv',
|
|
216
216
|
contentDisposition: 'attachment'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/http.ts"],"sourcesContent":["import { composeMiddleware, connectHttp, createFileServingRouter, registerPrompts, registerResources, registerTools } from '@mcp-z/server';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport cors from 'cors';\nimport express from 'express';\nimport type { RuntimeOverrides, ServerConfig } from '../types.ts';\nimport { createDefaultRuntime } from './runtime.ts';\n\nexport async function createHTTPServer(config: ServerConfig, overrides?: RuntimeOverrides) {\n const runtime = await createDefaultRuntime(config, overrides);\n const modules = runtime.createDomainModules();\n const layers = runtime.middlewareFactories.map((factory) => factory(runtime.deps));\n const composed = composeMiddleware(modules, layers);\n const logger = runtime.deps.logger;\n const port = config.transport.port;\n if (!port) throw new Error('Port is required for HTTP transport');\n\n const tools = [...composed.tools, ...runtime.deps.oauthAdapters.accountTools];\n const prompts = [...composed.prompts, ...runtime.deps.oauthAdapters.accountPrompts];\n\n const mcpServer = new McpServer({ name: config.name, version: config.version });\n registerTools(mcpServer, tools);\n registerResources(mcpServer, composed.resources);\n registerPrompts(mcpServer, prompts);\n\n const app = express();\n app.use(cors());\n app.use(express.json({ limit: '10mb' }));\n\n if (runtime.deps.oauthAdapters.loopbackRouter) {\n app.use('/', runtime.deps.oauthAdapters.loopbackRouter);\n logger.info('Mounted loopback OAuth callback router');\n }\n\n const fileRouter = createFileServingRouter({
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/http.ts"],"sourcesContent":["import { composeMiddleware, connectHttp, createFileServingRouter, registerPrompts, registerResources, registerTools } from '@mcp-z/server';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport cors from 'cors';\nimport express from 'express';\nimport type { RuntimeOverrides, ServerConfig } from '../types.ts';\nimport { createDefaultRuntime } from './runtime.ts';\n\nexport async function createHTTPServer(config: ServerConfig, overrides?: RuntimeOverrides) {\n const runtime = await createDefaultRuntime(config, overrides);\n const modules = runtime.createDomainModules();\n const layers = runtime.middlewareFactories.map((factory) => factory(runtime.deps));\n const composed = composeMiddleware(modules, layers);\n const logger = runtime.deps.logger;\n const port = config.transport.port;\n if (!port) throw new Error('Port is required for HTTP transport');\n\n const tools = [...composed.tools, ...runtime.deps.oauthAdapters.accountTools];\n const prompts = [...composed.prompts, ...runtime.deps.oauthAdapters.accountPrompts];\n\n const mcpServer = new McpServer({ name: config.name, version: config.version });\n registerTools(mcpServer, tools);\n registerResources(mcpServer, composed.resources);\n registerPrompts(mcpServer, prompts);\n\n const app = express();\n app.use(cors());\n app.use(express.json({ limit: '10mb' }));\n\n if (runtime.deps.oauthAdapters.loopbackRouter) {\n app.use('/', runtime.deps.oauthAdapters.loopbackRouter);\n logger.info('Mounted loopback OAuth callback router');\n }\n\n const fileRouter = createFileServingRouter({ resourceStoreUri: config.resourceStoreUri }, { contentType: 'text/csv', contentDisposition: 'attachment' });\n app.use('/files', fileRouter);\n\n if (runtime.deps.oauthAdapters.dcrRouter) {\n app.use('/', runtime.deps.oauthAdapters.dcrRouter);\n logger.info('Mounted DCR router with OAuth endpoints');\n }\n\n logger.info(`Starting ${config.name} MCP server (http)`);\n const { close, httpServer } = await connectHttp(mcpServer, { logger, app, port });\n logger.info('http transport ready');\n\n return {\n httpServer,\n mcpServer,\n logger,\n close: async () => {\n await close();\n await runtime.close();\n },\n };\n}\n"],"names":["createHTTPServer","config","overrides","runtime","modules","layers","composed","logger","port","tools","prompts","mcpServer","app","fileRouter","close","httpServer","createDefaultRuntime","createDomainModules","middlewareFactories","map","factory","deps","composeMiddleware","transport","Error","oauthAdapters","accountTools","accountPrompts","McpServer","name","version","registerTools","registerResources","resources","registerPrompts","express","use","cors","json","limit","loopbackRouter","info","createFileServingRouter","resourceStoreUri","contentType","contentDisposition","dcrRouter","connectHttp"],"mappings":";;;;+BAOsBA;;;eAAAA;;;sBAPqG;mBACjG;2DACT;8DACG;yBAEiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE9B,SAAeA,iBAAiBC,MAAoB,EAAEC,SAA4B;;YACjFC,SACAC,SACAC,QACAC,UACAC,QACAC,MAGAC,OACAC,SAEAC,WAKAC,KASAC,YASwB,MAAtBC,OAAOC;;;;oBAlCC;;wBAAMC,IAAAA,+BAAoB,EAACf,QAAQC;;;oBAA7CC,UAAU;oBACVC,UAAUD,QAAQc,mBAAmB;oBACrCZ,SAASF,QAAQe,mBAAmB,CAACC,GAAG,CAAC,SAACC;+BAAYA,QAAQjB,QAAQkB,IAAI;;oBAC1Ef,WAAWgB,IAAAA,yBAAiB,EAAClB,SAASC;oBACtCE,SAASJ,QAAQkB,IAAI,CAACd,MAAM;oBAC5BC,OAAOP,OAAOsB,SAAS,CAACf,IAAI;oBAClC,IAAI,CAACA,MAAM,MAAM,IAAIgB,MAAM;oBAErBf,QAAQ,AAAC,qBAAGH,SAASG,KAAK,SAAE,qBAAGN,QAAQkB,IAAI,CAACI,aAAa,CAACC,YAAY;oBACtEhB,UAAU,AAA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AI,CAAC;oBAEZ;;wBAAO;4BACL1B,YAAAA;4BACAJ,WAAAA;4BACAJ,QAAAA;4BACAO,OAAO;;;;;gDACL;;oDAAMA;;;gDAAN;gDACA;;oDAAMX,QAAQW,KAAK;;;gDAAnB;;;;;;gCACF;;wBACF;;;;IACF"}
|
|
@@ -310,12 +310,12 @@ function createLogger(config) {
|
|
|
310
310
|
}
|
|
311
311
|
function createTokenStore(baseDir) {
|
|
312
312
|
return _async_to_generator(function() {
|
|
313
|
-
var
|
|
313
|
+
var tokenStoreUri;
|
|
314
314
|
return _ts_generator(this, function(_state) {
|
|
315
|
-
|
|
315
|
+
tokenStoreUri = process.env.TOKEN_STORE_URI || "file://".concat(_path.join(baseDir, 'tokens.json'));
|
|
316
316
|
return [
|
|
317
317
|
2,
|
|
318
|
-
(0, _createstorets.default)(
|
|
318
|
+
(0, _createstorets.default)(tokenStoreUri)
|
|
319
319
|
];
|
|
320
320
|
});
|
|
321
321
|
})();
|
|
@@ -391,8 +391,8 @@ function createStorageLayer(storageContext) {
|
|
|
391
391
|
};
|
|
392
392
|
}
|
|
393
393
|
function assertStorageConfig(config) {
|
|
394
|
-
if (!config.
|
|
395
|
-
throw new Error('gmail-messages-export-csv: Server configuration missing
|
|
394
|
+
if (!config.resourceStoreUri) {
|
|
395
|
+
throw new Error('gmail-messages-export-csv: Server configuration missing resourceStoreUri.');
|
|
396
396
|
}
|
|
397
397
|
if (config.transport.type === 'http' && !config.baseUrl && !config.transport.port) {
|
|
398
398
|
throw new Error('gmail-messages-export-csv: HTTP transport requires either baseUrl in server config or port in transport config. This is a server configuration error - please provide --base-url or --port.');
|
|
@@ -459,7 +459,7 @@ function createDefaultRuntime(config, overrides) {
|
|
|
459
459
|
},
|
|
460
460
|
function() {
|
|
461
461
|
return createStorageLayer({
|
|
462
|
-
|
|
462
|
+
resourceStoreUri: config.resourceStoreUri,
|
|
463
463
|
baseUrl: config.baseUrl,
|
|
464
464
|
transport: config.transport
|
|
465
465
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/runtime.ts"],"sourcesContent":["import { sanitizeForLoggingFormatter } from '@mcp-z/oauth';\nimport type { CachedToken } from '@mcp-z/oauth-google';\nimport type { Logger, MiddlewareLayer } from '@mcp-z/server';\nimport { createLoggingMiddleware } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport pino from 'pino';\nimport createStore from '../lib/create-store.ts';\nimport * as mcp from '../mcp/index.ts';\nimport type { CommonRuntime, RuntimeDeps, RuntimeOverrides, ServerConfig, StorageContext } from '../types.ts';\nimport { createOAuthAdapters, type OAuthAdapters } from './oauth-google.ts';\n\nexport function createLogger(config: ServerConfig): Logger {\n const hasStdio = config.transport.type === 'stdio';\n const logsPath = path.join(config.baseDir, 'logs', `${config.name}.log`);\n if (hasStdio) fs.mkdirSync(path.dirname(logsPath), { recursive: true });\n return pino({ level: config.logLevel ?? 'info', formatters: sanitizeForLoggingFormatter() }, hasStdio ? pino.destination({ dest: logsPath, sync: false }) : pino.destination(1));\n}\n\nexport async function createTokenStore(baseDir: string) {\n const
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/runtime.ts"],"sourcesContent":["import { sanitizeForLoggingFormatter } from '@mcp-z/oauth';\nimport type { CachedToken } from '@mcp-z/oauth-google';\nimport type { Logger, MiddlewareLayer } from '@mcp-z/server';\nimport { createLoggingMiddleware } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport pino from 'pino';\nimport createStore from '../lib/create-store.ts';\nimport * as mcp from '../mcp/index.ts';\nimport type { CommonRuntime, RuntimeDeps, RuntimeOverrides, ServerConfig, StorageContext } from '../types.ts';\nimport { createOAuthAdapters, type OAuthAdapters } from './oauth-google.ts';\n\nexport function createLogger(config: ServerConfig): Logger {\n const hasStdio = config.transport.type === 'stdio';\n const logsPath = path.join(config.baseDir, 'logs', `${config.name}.log`);\n if (hasStdio) fs.mkdirSync(path.dirname(logsPath), { recursive: true });\n return pino({ level: config.logLevel ?? 'info', formatters: sanitizeForLoggingFormatter() }, hasStdio ? pino.destination({ dest: logsPath, sync: false }) : pino.destination(1));\n}\n\nexport async function createTokenStore(baseDir: string) {\n const tokenStoreUri = process.env.TOKEN_STORE_URI || `file://${path.join(baseDir, 'tokens.json')}`;\n return createStore<CachedToken>(tokenStoreUri);\n}\n\nexport async function createDcrStore(baseDir: string, required: boolean) {\n if (!required) return undefined;\n const dcrStoreUri = process.env.DCR_STORE_URI || `file://${path.join(baseDir, 'dcr.json')}`;\n return createStore<unknown>(dcrStoreUri);\n}\n\nexport function createAuthLayer(authMiddleware: OAuthAdapters['middleware']): MiddlewareLayer {\n return {\n withTool: authMiddleware.withToolAuth,\n withResource: authMiddleware.withResourceAuth,\n withPrompt: authMiddleware.withPromptAuth,\n };\n}\n\nexport function createLoggingLayer(logger: Logger): MiddlewareLayer {\n const logging = createLoggingMiddleware({ logger });\n return {\n withTool: logging.withToolLogging,\n withResource: logging.withResourceLogging,\n withPrompt: logging.withPromptLogging,\n };\n}\n\nexport function createStorageLayer(storageContext: StorageContext): MiddlewareLayer {\n const wrapAtPosition = <T extends { name: string; handler: unknown; [key: string]: unknown }>(module: T, extraPosition: number): T => {\n const originalHandler = module.handler as (...args: unknown[]) => Promise<unknown>;\n\n const wrappedHandler = async (...allArgs: unknown[]) => {\n const extra = allArgs[extraPosition];\n (extra as { storageContext?: StorageContext }).storageContext = storageContext;\n return await originalHandler(...allArgs);\n };\n\n return {\n ...module,\n handler: wrappedHandler,\n } as T;\n };\n\n return {\n withTool: <T extends { name: string; config: unknown; handler: unknown }>(module: T): T => wrapAtPosition(module, 1) as T,\n };\n}\n\nexport function assertStorageConfig(config: ServerConfig) {\n if (!config.resourceStoreUri) {\n throw new Error('gmail-messages-export-csv: Server configuration missing resourceStoreUri.');\n }\n if (config.transport.type === 'http' && !config.baseUrl && !config.transport.port) {\n throw new Error('gmail-messages-export-csv: HTTP transport requires either baseUrl in server config or port in transport config. This is a server configuration error - please provide --base-url or --port.');\n }\n}\n\nexport async function createDefaultRuntime(config: ServerConfig, overrides?: RuntimeOverrides): Promise<CommonRuntime> {\n if (config.auth === 'dcr' && config.transport.type !== 'http') throw new Error('DCR mode requires an HTTP transport');\n\n assertStorageConfig(config);\n const logger = createLogger(config);\n const tokenStore = await createTokenStore(config.baseDir);\n const baseUrl = config.baseUrl ?? (config.transport.type === 'http' && config.transport.port ? `http://localhost:${config.transport.port}` : undefined);\n const dcrStore = await createDcrStore(config.baseDir, config.auth === 'dcr');\n const oauthAdapters = await createOAuthAdapters(config, { logger, tokenStore, dcrStore }, baseUrl);\n const deps: RuntimeDeps = { config, logger, tokenStore, oauthAdapters, baseUrl };\n const createDomainModules =\n overrides?.createDomainModules ??\n (() => ({\n tools: Object.values(mcp.toolFactories).map((factory) => factory()),\n resources: Object.values(mcp.resourceFactories).map((factory) => factory()),\n prompts: Object.values(mcp.promptFactories).map((factory) => factory()),\n }));\n const middlewareFactories = overrides?.middlewareFactories ?? [() => createAuthLayer(oauthAdapters.middleware), () => createLoggingLayer(logger), () => createStorageLayer({ resourceStoreUri: config.resourceStoreUri, baseUrl: config.baseUrl, transport: config.transport })];\n\n return {\n deps,\n middlewareFactories,\n createDomainModules,\n close: async () => {},\n };\n}\n"],"names":["assertStorageConfig","createAuthLayer","createDcrStore","createDefaultRuntime","createLogger","createLoggingLayer","createStorageLayer","createTokenStore","config","hasStdio","transport","type","logsPath","path","join","baseDir","name","fs","mkdirSync","dirname","recursive","pino","level","logLevel","formatters","sanitizeForLoggingFormatter","destination","dest","sync","tokenStoreUri","process","env","TOKEN_STORE_URI","createStore","required","dcrStoreUri","undefined","DCR_STORE_URI","authMiddleware","withTool","withToolAuth","withResource","withResourceAuth","withPrompt","withPromptAuth","logger","logging","createLoggingMiddleware","withToolLogging","withResourceLogging","withPromptLogging","storageContext","wrapAtPosition","module","extraPosition","originalHandler","handler","wrappedHandler","allArgs","extra","resourceStoreUri","Error","baseUrl","port","overrides","tokenStore","dcrStore","oauthAdapters","deps","createDomainModules","middlewareFactories","auth","createOAuthAdapters","tools","Object","values","mcp","toolFactories","map","factory","resources","resourceFactories","prompts","promptFactories","middleware","close"],"mappings":";;;;;;;;;;;QAoEgBA;eAAAA;;QAtCAC;eAAAA;;QANMC;eAAAA;;QAqDAC;eAAAA;;QAjENC;eAAAA;;QA0BAC;eAAAA;;QASAC;eAAAA;;QA5BMC;eAAAA;;;qBAnBsB;sBAGJ;0DACpB;4DACE;2DACL;oEACO;+DACH;6BAEmC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEjD,SAASH,aAAaI,MAAoB;QAI1BA;IAHrB,IAAMC,WAAWD,OAAOE,SAAS,CAACC,IAAI,KAAK;IAC3C,IAAMC,WAAWC,MAAKC,IAAI,CAACN,OAAOO,OAAO,EAAE,QAAQ,AAAC,GAAc,OAAZP,OAAOQ,IAAI,EAAC;IAClE,IAAIP,UAAUQ,IAAGC,SAAS,CAACL,MAAKM,OAAO,CAACP,WAAW;QAAEQ,WAAW;IAAK;IACrE,OAAOC,IAAAA,aAAI,EAAC;QAAEC,KAAK,GAAEd,mBAAAA,OAAOe,QAAQ,cAAff,8BAAAA,mBAAmB;QAAQgB,YAAYC,IAAAA,kCAA2B;IAAG,GAAGhB,WAAWY,aAAI,CAACK,WAAW,CAAC;QAAEC,MAAMf;QAAUgB,MAAM;IAAM,KAAKP,aAAI,CAACK,WAAW,CAAC;AAC/K;AAEO,SAAenB,i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G,CAAC,SAACC;uCAAYA;;4BACjEG,SAASR,OAAOC,MAAM,CAACC,SAAIO,eAAe,EAAEL,GAAG,CAAC,SAACC;uCAAYA;;wBAC/D;;oBACIT,+BAAsBN,sBAAAA,gCAAAA,UAAWM,mBAAmB;wBAAK;mCAAMrE,gBAAgBkE,cAAciB,UAAU;;wBAAG;mCAAM/E,mBAAmBwC;;wBAAS;mCAAMvC,mBAAmB;gCAAEsD,kBAAkBpD,OAAOoD,gBAAgB;gCAAEE,SAAStD,OAAOsD,OAAO;gCAAEpD,WAAWF,OAAOE,SAAS;4BAAC;;;oBAE7Q;;wBAAO;4BACL0D,MAAAA;4BACAE,qBAAAA;4BACAD,qBAAAA;4BACAgB,OAAO;;;;;;;gCAAa;;wBACtB;;;;IACF"}
|
package/dist/cjs/types.d.cts
CHANGED
|
@@ -12,12 +12,12 @@ export interface ServerConfig extends BaseServerConfig, OAuthConfig {
|
|
|
12
12
|
name: string;
|
|
13
13
|
version: string;
|
|
14
14
|
repositoryUrl: string;
|
|
15
|
-
|
|
15
|
+
resourceStoreUri: string;
|
|
16
16
|
baseUrl?: string;
|
|
17
17
|
dcrConfig?: DcrConfig;
|
|
18
18
|
}
|
|
19
19
|
export interface StorageContext {
|
|
20
|
-
|
|
20
|
+
resourceStoreUri: string;
|
|
21
21
|
baseUrl?: string;
|
|
22
22
|
transport: BaseServerConfig['transport'];
|
|
23
23
|
}
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -12,12 +12,12 @@ export interface ServerConfig extends BaseServerConfig, OAuthConfig {
|
|
|
12
12
|
name: string;
|
|
13
13
|
version: string;
|
|
14
14
|
repositoryUrl: string;
|
|
15
|
-
|
|
15
|
+
resourceStoreUri: string;
|
|
16
16
|
baseUrl?: string;
|
|
17
17
|
dcrConfig?: DcrConfig;
|
|
18
18
|
}
|
|
19
19
|
export interface StorageContext {
|
|
20
|
-
|
|
20
|
+
resourceStoreUri: string;
|
|
21
21
|
baseUrl?: string;
|
|
22
22
|
transport: BaseServerConfig['transport'];
|
|
23
23
|
}
|
|
@@ -51,7 +51,7 @@ const config = {
|
|
|
51
51
|
*/ async function handler({ query, maxItems, filename, contentType, excludeThreadHistory }, extra) {
|
|
52
52
|
const logger = extra.logger;
|
|
53
53
|
const { storageContext } = extra;
|
|
54
|
-
const { transport,
|
|
54
|
+
const { transport, resourceStoreUri, baseUrl } = storageContext;
|
|
55
55
|
logger.info('gmail.messages.export-csv called', {
|
|
56
56
|
query,
|
|
57
57
|
maxItems,
|
|
@@ -60,7 +60,7 @@ const config = {
|
|
|
60
60
|
});
|
|
61
61
|
// Reserve file location for streaming write (creates directory, generates ID, formats filename)
|
|
62
62
|
const reservation = await reserveFile(filename, {
|
|
63
|
-
|
|
63
|
+
resourceStoreUri
|
|
64
64
|
});
|
|
65
65
|
const { storedName, fullPath } = reservation;
|
|
66
66
|
logger.info('gmail.messages.export-csv starting streaming export', {
|
|
@@ -207,7 +207,7 @@ const config = {
|
|
|
207
207
|
});
|
|
208
208
|
// Generate URI based on transport type (stdio: file://, HTTP: http://)
|
|
209
209
|
const uri = getFileUri(storedName, transport, {
|
|
210
|
-
|
|
210
|
+
resourceStoreUri,
|
|
211
211
|
...baseUrl && {
|
|
212
212
|
baseUrl
|
|
213
213
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/mcp/tools/messages-export-csv.ts"],"sourcesContent":["import { EmailContentTypeSchema, ExcludeThreadHistorySchema } from '@mcp-z/email';\nimport type { EnrichedExtra } from '@mcp-z/oauth-google';\nimport { schemas } from '@mcp-z/oauth-google';\n\nconst { AuthRequiredBranchSchema } = schemas;\n\nimport { getFileUri, reserveFile, type ToolModule } from '@mcp-z/server';\nimport { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';\n\nimport { stringify } from 'csv-stringify/sync';\nimport { createWriteStream } from 'fs';\nimport { unlink } from 'fs/promises';\nimport { type gmail_v1, google } from 'googleapis';\nimport { z } from 'zod';\nimport { DEFAULT_PAGE_SIZE } from '../../constants.ts';\nimport { extractBodyFromPayload } from '../../email/parsing/html-processing.ts';\nimport { executeQuery as executeGmailQuery } from '../../email/querying/execute-query.ts';\nimport { GmailQuerySchema } from '../../schemas/gmail-query-schema.ts';\nimport type { StorageExtra } from '../../types.ts';\n\nconst DEFAULT_MAX_ITEMS = 10000;\nconst MAX_EXPORT_ITEMS = 50000;\n\n/**\n * CSV row format based on EmailDetail\n * All fields are strings (empty string instead of undefined)\n * Includes additional CSV-specific fields: provider and labels\n */\ninterface CsvRow {\n id: string;\n threadId: string;\n from: string;\n to: string;\n cc: string;\n bcc: string;\n subject: string;\n date: string;\n snippet: string;\n body: string;\n provider: string;\n labels: string;\n}\n\nconst inputSchema = z.object({\n query: GmailQuerySchema.optional().describe('Structured query object for filtering messages. Use query-syntax prompt for reference.'),\n maxItems: z.number().int().positive().max(MAX_EXPORT_ITEMS).default(DEFAULT_MAX_ITEMS).describe(`Maximum messages to export (default: ${DEFAULT_MAX_ITEMS}, max: ${MAX_EXPORT_ITEMS})`),\n filename: z.string().trim().min(1).default('gmail-messages.csv').describe('Output filename (default: gmail-messages.csv)'),\n contentType: EmailContentTypeSchema,\n excludeThreadHistory: ExcludeThreadHistorySchema,\n});\n\nconst successBranchSchema = z.object({\n type: z.literal('success'),\n uri: z.string().describe('File URI (file:// or http://)'),\n filename: z.string().describe('Stored filename'),\n rowCount: z.number().describe('Number of messages exported'),\n truncated: z.boolean().describe('Whether export was truncated at maxItems'),\n});\n\nconst outputSchema = z.discriminatedUnion('type', [successBranchSchema, AuthRequiredBranchSchema]);\n\nconst config = {\n description: 'Export Gmail messages to CSV with streaming pagination. Returns file URI. Use query-syntax prompt for query reference.',\n inputSchema: inputSchema,\n outputSchema: z.object({\n result: outputSchema,\n }),\n} as const;\n\nexport type Input = z.infer<typeof inputSchema>;\nexport type Output = z.infer<typeof outputSchema>;\n\n/**\n * Handler for gmail-messages-export-csv tool\n *\n * CRITICAL: Streaming implementation per user requirements\n * - Generate UUID upfront\n * - Write CSV header immediately\n * - Append rows as batches arrive\n * - Delete partial file on error\n * - NO RETRIES (fail fast on error)\n */\nasync function handler({ query, maxItems, filename, contentType, excludeThreadHistory }: Input, extra: EnrichedExtra & StorageExtra) {\n const logger = extra.logger;\n const { storageContext } = extra;\n const { transport, storageDir, baseUrl } = storageContext;\n\n logger.info('gmail.messages.export-csv called', {\n query,\n maxItems,\n filename,\n accountId: extra.authContext.accountId,\n });\n\n // Reserve file location for streaming write (creates directory, generates ID, formats filename)\n const reservation = await reserveFile(filename, {\n storageDir,\n });\n const { storedName, fullPath } = reservation;\n\n logger.info('gmail.messages.export-csv starting streaming export', { path: fullPath, maxItems });\n\n try {\n const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth });\n\n // Create CSV headers (all email fields)\n const csvHeaders = ['id', 'threadId', 'from', 'to', 'cc', 'bcc', 'subject', 'date', 'snippet', 'body', 'provider', 'labels'];\n\n // Create write stream and write headers immediately\n const writeStream = createWriteStream(fullPath, { encoding: 'utf-8' });\n const headerLine = stringify([csvHeaders], { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(headerLine);\n\n // Internal pagination loop - append to CSV with each batch\n // NO RETRIES: If any error occurs, fail the whole operation and clean up\n let totalRows = 0;\n let nextPageToken: string | undefined;\n const started = Date.now();\n\n while (totalRows < maxItems) {\n const remainingItems = maxItems - totalRows;\n const pageSize = Math.min(remainingItems, DEFAULT_PAGE_SIZE);\n\n const exec: {\n items: CsvRow[];\n metadata?: { nextPageToken?: string };\n } = await executeGmailQuery(\n query,\n {\n client: gmail,\n logger,\n pageSize,\n ...(nextPageToken !== undefined && { pageToken: nextPageToken }),\n includeBody: true, // Always include body for CSV export\n },\n (full: unknown): CsvRow => {\n // Type-safe property access with guards\n const fullData = full as {\n id?: unknown;\n threadId?: unknown;\n snippet?: unknown;\n labelIds?: unknown[];\n payload?: { headers?: unknown[] };\n };\n\n const headersArray = Array.isArray(fullData?.payload?.headers) ? fullData.payload.headers : [];\n const headers = Object.fromEntries(\n headersArray.map((h: unknown) => {\n const header = h as { name?: unknown; value?: unknown };\n return [String(header.name ?? ''), String(header.value ?? '')];\n })\n );\n\n const payload = fullData?.payload;\n // Cast to Schema$MessagePart for extractBodyFromPayload\n const body = payload ? extractBodyFromPayload(payload as gmail_v1.Schema$MessagePart, { contentType, excludeThreadHistory }) : '';\n\n const labelIds = Array.isArray(fullData?.labelIds) ? fullData.labelIds.map((id) => String(id ?? '')) : [];\n\n return {\n id: String(fullData?.id ?? ''),\n threadId: fullData?.threadId ? String(fullData.threadId) : '',\n from: headers?.From || '',\n to: headers?.To || '',\n cc: headers?.Cc || '',\n bcc: headers?.Bcc || '',\n subject: headers?.Subject || '',\n date: headers?.Date || '',\n snippet: fullData?.snippet ? String(fullData.snippet) : '',\n body,\n provider: 'gmail',\n labels: labelIds.join(';'),\n };\n }\n );\n\n // Type-safe CSV row mapping\n const csvRows = exec.items.map((row) => {\n return [row.id, row.threadId, row.from, row.to, row.cc, row.bcc, row.subject, row.date, row.snippet, row.body, row.provider, row.labels];\n });\n\n // Append rows to CSV file immediately\n if (csvRows.length > 0) {\n const rowsContent = stringify(csvRows, { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(rowsContent);\n }\n\n totalRows += exec.items.length;\n nextPageToken = exec.metadata?.nextPageToken;\n\n logger.info('gmail.messages.export-csv batch written', {\n batchSize: exec.items.length,\n totalRows,\n hasMore: Boolean(nextPageToken),\n });\n\n // Exit if no more results or reached maxItems\n if (!nextPageToken || exec.items.length === 0) {\n break;\n }\n }\n\n // Close write stream\n await new Promise<void>((resolve, reject) => {\n writeStream.end(() => resolve());\n writeStream.on('error', reject);\n });\n\n const durationMs = Date.now() - started;\n const truncated = totalRows >= maxItems && Boolean(nextPageToken);\n\n logger.info('gmail.messages.export-csv completed', {\n rowCount: totalRows,\n truncated,\n durationMs,\n filename: storedName,\n });\n\n // Generate URI based on transport type (stdio: file://, HTTP: http://)\n const uri = getFileUri(storedName, transport, {\n storageDir,\n ...(baseUrl && { baseUrl }),\n endpoint: '/files',\n });\n\n const result: Output = {\n type: 'success' as const,\n uri,\n filename: storedName,\n rowCount: totalRows,\n truncated,\n };\n\n return {\n content: [\n {\n type: 'text' as const,\n text: JSON.stringify(result),\n },\n ],\n structuredContent: { result },\n };\n } catch (error) {\n // CRITICAL: Clean up partial CSV file on error\n try {\n await unlink(fullPath);\n logger.debug('Cleaned up partial CSV file after error', { path: fullPath });\n } catch (_cleanupError) {\n logger.debug('Could not clean up CSV file (may not exist)', { path: fullPath });\n }\n\n const message = error instanceof Error ? error.message : String(error);\n logger.error('gmail.messages.export-csv error', { error: message });\n\n throw new McpError(ErrorCode.InternalError, `Error exporting messages to CSV: ${message}`, {\n stack: error instanceof Error ? error.stack : undefined,\n });\n }\n}\n\nexport default function createTool() {\n return {\n name: 'messages-export-csv',\n config,\n handler,\n } satisfies ToolModule;\n}\n"],"names":["EmailContentTypeSchema","ExcludeThreadHistorySchema","schemas","AuthRequiredBranchSchema","getFileUri","reserveFile","ErrorCode","McpError","stringify","createWriteStream","unlink","google","z","DEFAULT_PAGE_SIZE","extractBodyFromPayload","executeQuery","executeGmailQuery","GmailQuerySchema","DEFAULT_MAX_ITEMS","MAX_EXPORT_ITEMS","inputSchema","object","query","optional","describe","maxItems","number","int","positive","max","default","filename","string","trim","min","contentType","excludeThreadHistory","successBranchSchema","type","literal","uri","rowCount","truncated","boolean","outputSchema","discriminatedUnion","config","description","result","handler","extra","logger","storageContext","transport","storageDir","baseUrl","info","accountId","authContext","reservation","storedName","fullPath","path","gmail","version","auth","csvHeaders","writeStream","encoding","headerLine","header","quoted","quote","escape","write","totalRows","nextPageToken","started","Date","now","exec","remainingItems","pageSize","Math","client","undefined","pageToken","includeBody","full","fullData","headersArray","Array","isArray","payload","headers","Object","fromEntries","map","h","String","name","value","body","labelIds","id","threadId","from","From","to","To","cc","Cc","bcc","Bcc","subject","Subject","date","snippet","provider","labels","join","csvRows","items","row","length","rowsContent","metadata","batchSize","hasMore","Boolean","Promise","resolve","reject","end","on","durationMs","endpoint","content","text","JSON","structuredContent","error","debug","_cleanupError","message","Error","InternalError","stack","createTool"],"mappings":"AAAA,SAASA,sBAAsB,EAAEC,0BAA0B,QAAQ,eAAe;AAElF,SAASC,OAAO,QAAQ,sBAAsB;AAE9C,MAAM,EAAEC,wBAAwB,EAAE,GAAGD;AAErC,SAASE,UAAU,EAAEC,WAAW,QAAyB,gBAAgB;AACzE,SAASC,SAAS,EAAEC,QAAQ,QAAQ,qCAAqC;AAEzE,SAASC,SAAS,QAAQ,qBAAqB;AAC/C,SAASC,iBAAiB,QAAQ,KAAK;AACvC,SAASC,MAAM,QAAQ,cAAc;AACrC,SAAwBC,MAAM,QAAQ,aAAa;AACnD,SAASC,CAAC,QAAQ,MAAM;AACxB,SAASC,iBAAiB,QAAQ,qBAAqB;AACvD,SAASC,sBAAsB,QAAQ,yCAAyC;AAChF,SAASC,gBAAgBC,iBAAiB,QAAQ,wCAAwC;AAC1F,SAASC,gBAAgB,QAAQ,sCAAsC;AAGvE,MAAMC,oBAAoB;AAC1B,MAAMC,mBAAmB;AAsBzB,MAAMC,cAAcR,EAAES,MAAM,CAAC;IAC3BC,OAAOL,iBAAiBM,QAAQ,GAAGC,QAAQ,CAAC;IAC5CC,UAAUb,EAAEc,MAAM,GAAGC,GAAG,GAAGC,QAAQ,GAAGC,GAAG,CAACV,kBAAkBW,OAAO,CAACZ,mBAAmBM,QAAQ,CAAC,CAAC,qCAAqC,EAAEN,kBAAkB,OAAO,EAAEC,iBAAiB,CAAC,CAAC;IACtLY,UAAUnB,EAAEoB,MAAM,GAAGC,IAAI,GAAGC,GAAG,CAAC,GAAGJ,OAAO,CAAC,sBAAsBN,QAAQ,CAAC;IAC1EW,aAAanC;IACboC,sBAAsBnC;AACxB;AAEA,MAAMoC,sBAAsBzB,EAAES,MAAM,CAAC;IACnCiB,MAAM1B,EAAE2B,OAAO,CAAC;IA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C;gBAAEhF,MAAMD;YAAS;QAC3E,EAAE,OAAOkF,eAAe;YACtB5F,OAAO2F,KAAK,CAAC,+CAA+C;gBAAEhF,MAAMD;YAAS;QAC/E;QAEA,MAAMmF,UAAUH,iBAAiBI,QAAQJ,MAAMG,OAAO,GAAG7C,OAAO0C;QAChE1F,OAAO0F,KAAK,CAAC,mCAAmC;YAAEA,OAAOG;QAAQ;QAEjE,MAAM,IAAIzI,SAASD,UAAU4I,aAAa,EAAE,CAAC,iCAAiC,EAAEF,SAAS,EAAE;YACzFG,OAAON,iBAAiBI,QAAQJ,MAAMM,KAAK,GAAG9D;QAChD;IACF;AACF;AAEA,eAAe,SAAS+D;IACtB,OAAO;QACLhD,MAAM;QACNtD;QACAG;IACF;AACF"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/mcp/tools/messages-export-csv.ts"],"sourcesContent":["import { EmailContentTypeSchema, ExcludeThreadHistorySchema } from '@mcp-z/email';\nimport type { EnrichedExtra } from '@mcp-z/oauth-google';\nimport { schemas } from '@mcp-z/oauth-google';\n\nconst { AuthRequiredBranchSchema } = schemas;\n\nimport { getFileUri, reserveFile, type ToolModule } from '@mcp-z/server';\nimport { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';\n\nimport { stringify } from 'csv-stringify/sync';\nimport { createWriteStream } from 'fs';\nimport { unlink } from 'fs/promises';\nimport { type gmail_v1, google } from 'googleapis';\nimport { z } from 'zod';\nimport { DEFAULT_PAGE_SIZE } from '../../constants.ts';\nimport { extractBodyFromPayload } from '../../email/parsing/html-processing.ts';\nimport { executeQuery as executeGmailQuery } from '../../email/querying/execute-query.ts';\nimport { GmailQuerySchema } from '../../schemas/gmail-query-schema.ts';\nimport type { StorageExtra } from '../../types.ts';\n\nconst DEFAULT_MAX_ITEMS = 10000;\nconst MAX_EXPORT_ITEMS = 50000;\n\n/**\n * CSV row format based on EmailDetail\n * All fields are strings (empty string instead of undefined)\n * Includes additional CSV-specific fields: provider and labels\n */\ninterface CsvRow {\n id: string;\n threadId: string;\n from: string;\n to: string;\n cc: string;\n bcc: string;\n subject: string;\n date: string;\n snippet: string;\n body: string;\n provider: string;\n labels: string;\n}\n\nconst inputSchema = z.object({\n query: GmailQuerySchema.optional().describe('Structured query object for filtering messages. Use query-syntax prompt for reference.'),\n maxItems: z.number().int().positive().max(MAX_EXPORT_ITEMS).default(DEFAULT_MAX_ITEMS).describe(`Maximum messages to export (default: ${DEFAULT_MAX_ITEMS}, max: ${MAX_EXPORT_ITEMS})`),\n filename: z.string().trim().min(1).default('gmail-messages.csv').describe('Output filename (default: gmail-messages.csv)'),\n contentType: EmailContentTypeSchema,\n excludeThreadHistory: ExcludeThreadHistorySchema,\n});\n\nconst successBranchSchema = z.object({\n type: z.literal('success'),\n uri: z.string().describe('File URI (file:// or http://)'),\n filename: z.string().describe('Stored filename'),\n rowCount: z.number().describe('Number of messages exported'),\n truncated: z.boolean().describe('Whether export was truncated at maxItems'),\n});\n\nconst outputSchema = z.discriminatedUnion('type', [successBranchSchema, AuthRequiredBranchSchema]);\n\nconst config = {\n description: 'Export Gmail messages to CSV with streaming pagination. Returns file URI. Use query-syntax prompt for query reference.',\n inputSchema: inputSchema,\n outputSchema: z.object({\n result: outputSchema,\n }),\n} as const;\n\nexport type Input = z.infer<typeof inputSchema>;\nexport type Output = z.infer<typeof outputSchema>;\n\n/**\n * Handler for gmail-messages-export-csv tool\n *\n * CRITICAL: Streaming implementation per user requirements\n * - Generate UUID upfront\n * - Write CSV header immediately\n * - Append rows as batches arrive\n * - Delete partial file on error\n * - NO RETRIES (fail fast on error)\n */\nasync function handler({ query, maxItems, filename, contentType, excludeThreadHistory }: Input, extra: EnrichedExtra & StorageExtra) {\n const logger = extra.logger;\n const { storageContext } = extra;\n const { transport, resourceStoreUri, baseUrl } = storageContext;\n\n logger.info('gmail.messages.export-csv called', {\n query,\n maxItems,\n filename,\n accountId: extra.authContext.accountId,\n });\n\n // Reserve file location for streaming write (creates directory, generates ID, formats filename)\n const reservation = await reserveFile(filename, {\n resourceStoreUri,\n });\n const { storedName, fullPath } = reservation;\n\n logger.info('gmail.messages.export-csv starting streaming export', { path: fullPath, maxItems });\n\n try {\n const gmail = google.gmail({ version: 'v1', auth: extra.authContext.auth });\n\n // Create CSV headers (all email fields)\n const csvHeaders = ['id', 'threadId', 'from', 'to', 'cc', 'bcc', 'subject', 'date', 'snippet', 'body', 'provider', 'labels'];\n\n // Create write stream and write headers immediately\n const writeStream = createWriteStream(fullPath, { encoding: 'utf-8' });\n const headerLine = stringify([csvHeaders], { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(headerLine);\n\n // Internal pagination loop - append to CSV with each batch\n // NO RETRIES: If any error occurs, fail the whole operation and clean up\n let totalRows = 0;\n let nextPageToken: string | undefined;\n const started = Date.now();\n\n while (totalRows < maxItems) {\n const remainingItems = maxItems - totalRows;\n const pageSize = Math.min(remainingItems, DEFAULT_PAGE_SIZE);\n\n const exec: {\n items: CsvRow[];\n metadata?: { nextPageToken?: string };\n } = await executeGmailQuery(\n query,\n {\n client: gmail,\n logger,\n pageSize,\n ...(nextPageToken !== undefined && { pageToken: nextPageToken }),\n includeBody: true, // Always include body for CSV export\n },\n (full: unknown): CsvRow => {\n // Type-safe property access with guards\n const fullData = full as {\n id?: unknown;\n threadId?: unknown;\n snippet?: unknown;\n labelIds?: unknown[];\n payload?: { headers?: unknown[] };\n };\n\n const headersArray = Array.isArray(fullData?.payload?.headers) ? fullData.payload.headers : [];\n const headers = Object.fromEntries(\n headersArray.map((h: unknown) => {\n const header = h as { name?: unknown; value?: unknown };\n return [String(header.name ?? ''), String(header.value ?? '')];\n })\n );\n\n const payload = fullData?.payload;\n // Cast to Schema$MessagePart for extractBodyFromPayload\n const body = payload ? extractBodyFromPayload(payload as gmail_v1.Schema$MessagePart, { contentType, excludeThreadHistory }) : '';\n\n const labelIds = Array.isArray(fullData?.labelIds) ? fullData.labelIds.map((id) => String(id ?? '')) : [];\n\n return {\n id: String(fullData?.id ?? ''),\n threadId: fullData?.threadId ? String(fullData.threadId) : '',\n from: headers?.From || '',\n to: headers?.To || '',\n cc: headers?.Cc || '',\n bcc: headers?.Bcc || '',\n subject: headers?.Subject || '',\n date: headers?.Date || '',\n snippet: fullData?.snippet ? String(fullData.snippet) : '',\n body,\n provider: 'gmail',\n labels: labelIds.join(';'),\n };\n }\n );\n\n // Type-safe CSV row mapping\n const csvRows = exec.items.map((row) => {\n return [row.id, row.threadId, row.from, row.to, row.cc, row.bcc, row.subject, row.date, row.snippet, row.body, row.provider, row.labels];\n });\n\n // Append rows to CSV file immediately\n if (csvRows.length > 0) {\n const rowsContent = stringify(csvRows, { header: false, quoted: true, quote: '\"', escape: '\"' });\n writeStream.write(rowsContent);\n }\n\n totalRows += exec.items.length;\n nextPageToken = exec.metadata?.nextPageToken;\n\n logger.info('gmail.messages.export-csv batch written', {\n batchSize: exec.items.length,\n totalRows,\n hasMore: Boolean(nextPageToken),\n });\n\n // Exit if no more results or reached maxItems\n if (!nextPageToken || exec.items.length === 0) {\n break;\n }\n }\n\n // Close write stream\n await new Promise<void>((resolve, reject) => {\n writeStream.end(() => resolve());\n writeStream.on('error', reject);\n });\n\n const durationMs = Date.now() - started;\n const truncated = totalRows >= maxItems && Boolean(nextPageToken);\n\n logger.info('gmail.messages.export-csv completed', {\n rowCount: totalRows,\n truncated,\n durationMs,\n filename: storedName,\n });\n\n // Generate URI based on transport type (stdio: file://, HTTP: http://)\n const uri = getFileUri(storedName, transport, {\n resourceStoreUri,\n ...(baseUrl && { baseUrl }),\n endpoint: '/files',\n });\n\n const result: Output = {\n type: 'success' as const,\n uri,\n filename: storedName,\n rowCount: totalRows,\n truncated,\n };\n\n return {\n content: [\n {\n type: 'text' as const,\n text: JSON.stringify(result),\n },\n ],\n structuredContent: { result },\n };\n } catch (error) {\n // CRITICAL: Clean up partial CSV file on error\n try {\n await unlink(fullPath);\n logger.debug('Cleaned up partial CSV file after error', { path: fullPath });\n } catch (_cleanupError) {\n logger.debug('Could not clean up CSV file (may not exist)', { path: fullPath });\n }\n\n const message = error instanceof Error ? error.message : String(error);\n logger.error('gmail.messages.export-csv error', { error: message });\n\n throw new McpError(ErrorCode.InternalError, `Error exporting messages to CSV: ${message}`, {\n stack: error instanceof Error ? error.stack : undefined,\n });\n }\n}\n\nexport default function createTool() {\n return {\n name: 'messages-export-csv',\n config,\n handler,\n } satisfies ToolModule;\n}\n"],"names":["EmailContentTypeSchema","ExcludeThreadHistorySchema","schemas","AuthRequiredBranchSchema","getFileUri","reserveFile","ErrorCode","McpError","stringify","createWriteStream","unlink","google","z","DEFAULT_PAGE_SIZE","extractBodyFromPayload","executeQuery","executeGmailQuery","GmailQuerySchema","DEFAULT_MAX_ITEMS","MAX_EXPORT_ITEMS","inputSchema","object","query","optional","describe","maxItems","number","int","positive","max","default","filename","string","trim","min","contentType","excludeThreadHistory","successBranchSchema","type","literal","uri","rowCount","truncated","boolean","outputSchema","discriminatedUnion","config","description","result","handler","extra","logger","storageContext","transport","resourceStoreUri","baseUrl","info","accountId","authContext","reservation","storedName","fullPath","path","gmail","version","auth","csvHeaders","writeStream","encoding","headerLine","header","quoted","quote","escape","write","totalRows","nextPageToken","started","Date","now","exec","remainingItems","pageSize","Math","client","undefined","pageToken","includeBody","full","fullData","headersArray","Array","isArray","payload","headers","Object","fromEntries","map","h","String","name","value","body","labelIds","id","threadId","from","From","to","To","cc","Cc","bcc","Bcc","subject","Subject","date","snippet","provider","labels","join","csvRows","items","row","length","rowsContent","metadata","batchSize","hasMore","Boolean","Promise","resolve","reject","end","on","durationMs","endpoint","content","text","JSON","structuredContent","error","debug","_cleanupError","message","Error","InternalError","stack","createTool"],"mappings":"AAAA,SAASA,sBAAsB,EAAEC,0BAA0B,QAAQ,eAAe;AAElF,SAASC,OAAO,QAAQ,sBAAsB;AAE9C,MAAM,EAAEC,wBAAwB,EAAE,GAAGD;AAErC,SAASE,UAAU,EAAEC,WAAW,QAAyB,gBAAgB;A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C,EAAEF,SAAS,EAAE;YACzFG,OAAON,iBAAiBI,QAAQJ,MAAMM,KAAK,GAAG9D;QAChD;IACF;AACF;AAEA,eAAe,SAAS+D;IACtB,OAAO;QACLhD,MAAM;QACNtD;QACAG;IACF;AACF"}
|
|
@@ -21,7 +21,7 @@ export declare function handleVersionHelp(args: string[]): {
|
|
|
21
21
|
* - --port=<port> Enable HTTP transport on specified port
|
|
22
22
|
* - --stdio Enable stdio transport (default if no port)
|
|
23
23
|
* - --log-level=<level> Logging level (default: info)
|
|
24
|
-
* - --
|
|
24
|
+
* - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)
|
|
25
25
|
* - --base-url=<url> Base URL for HTTP file serving (optional)
|
|
26
26
|
*
|
|
27
27
|
* Environment Variables:
|
|
@@ -32,9 +32,10 @@ export declare function handleVersionHelp(args: string[]): {
|
|
|
32
32
|
* - DCR_MODE DCR mode (optional, same format as --dcr-mode)
|
|
33
33
|
* - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)
|
|
34
34
|
* - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)
|
|
35
|
+
* - TOKEN_STORE_URI Token storage URI (optional)
|
|
35
36
|
* - PORT Default HTTP port (optional)
|
|
36
37
|
* - LOG_LEVEL Default logging level (optional)
|
|
37
|
-
* -
|
|
38
|
+
* - RESOURCE_STORE_URI Resource store URI (optional, file://)
|
|
38
39
|
* - BASE_URL Default base URL for file serving (optional)
|
|
39
40
|
*
|
|
40
41
|
* OAuth Scopes (from constants.ts):
|
package/dist/esm/setup/config.js
CHANGED
|
@@ -26,7 +26,7 @@ Options:
|
|
|
26
26
|
--port=<port> Enable HTTP transport on specified port
|
|
27
27
|
--stdio Enable stdio transport (default if no port)
|
|
28
28
|
--log-level=<level> Logging level (default: info)
|
|
29
|
-
--
|
|
29
|
+
--resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)
|
|
30
30
|
--base-url=<url> Base URL for HTTP file serving (optional)
|
|
31
31
|
|
|
32
32
|
Environment Variables:
|
|
@@ -37,9 +37,10 @@ Environment Variables:
|
|
|
37
37
|
DCR_MODE DCR mode (optional, same format as --dcr-mode)
|
|
38
38
|
DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)
|
|
39
39
|
DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)
|
|
40
|
+
TOKEN_STORE_URI Token storage URI (optional)
|
|
40
41
|
PORT Default HTTP port (optional)
|
|
41
42
|
LOG_LEVEL Default logging level (optional)
|
|
42
|
-
|
|
43
|
+
RESOURCE_STORE_URI Resource store URI (optional, file://)
|
|
43
44
|
BASE_URL Default base URL for file serving (optional)
|
|
44
45
|
|
|
45
46
|
OAuth Scopes:
|
|
@@ -49,7 +50,7 @@ Examples:
|
|
|
49
50
|
mcp-gmail # Use default settings
|
|
50
51
|
mcp-gmail --auth=service-account # Use service account auth
|
|
51
52
|
mcp-gmail --port=3000 # HTTP transport on port 3000
|
|
52
|
-
mcp-gmail --
|
|
53
|
+
mcp-gmail --resource-store-uri=file:///tmp/emails # Custom resource store URI
|
|
53
54
|
GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var
|
|
54
55
|
`.trim();
|
|
55
56
|
/**
|
|
@@ -94,7 +95,7 @@ Examples:
|
|
|
94
95
|
* - --port=<port> Enable HTTP transport on specified port
|
|
95
96
|
* - --stdio Enable stdio transport (default if no port)
|
|
96
97
|
* - --log-level=<level> Logging level (default: info)
|
|
97
|
-
* - --
|
|
98
|
+
* - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)
|
|
98
99
|
* - --base-url=<url> Base URL for HTTP file serving (optional)
|
|
99
100
|
*
|
|
100
101
|
* Environment Variables:
|
|
@@ -105,9 +106,10 @@ Examples:
|
|
|
105
106
|
* - DCR_MODE DCR mode (optional, same format as --dcr-mode)
|
|
106
107
|
* - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)
|
|
107
108
|
* - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)
|
|
109
|
+
* - TOKEN_STORE_URI Token storage URI (optional)
|
|
108
110
|
* - PORT Default HTTP port (optional)
|
|
109
111
|
* - LOG_LEVEL Default logging level (optional)
|
|
110
|
-
* -
|
|
112
|
+
* - RESOURCE_STORE_URI Resource store URI (optional, file://)
|
|
111
113
|
* - BASE_URL Default base URL for file serving (optional)
|
|
112
114
|
*
|
|
113
115
|
* OAuth Scopes (from constants.ts):
|
|
@@ -118,7 +120,7 @@ Examples:
|
|
|
118
120
|
const oauthConfig = parseOAuthConfig(args, env);
|
|
119
121
|
// Parse DCR configuration if DCR mode is enabled
|
|
120
122
|
const dcrConfig = oauthConfig.auth === 'dcr' ? parseDcrConfig(args, env, GOOGLE_SCOPE) : undefined;
|
|
121
|
-
// Parse application-level config (LOG_LEVEL,
|
|
123
|
+
// Parse application-level config (LOG_LEVEL, RESOURCE_STORE_URI, BASE_URL)
|
|
122
124
|
const { values } = parseArgs({
|
|
123
125
|
args,
|
|
124
126
|
options: {
|
|
@@ -128,7 +130,7 @@ Examples:
|
|
|
128
130
|
'base-url': {
|
|
129
131
|
type: 'string'
|
|
130
132
|
},
|
|
131
|
-
'
|
|
133
|
+
'resource-store-uri': {
|
|
132
134
|
type: 'string'
|
|
133
135
|
}
|
|
134
136
|
},
|
|
@@ -155,10 +157,10 @@ Examples:
|
|
|
155
157
|
const envLogLevel = env.LOG_LEVEL;
|
|
156
158
|
const logLevel = (_ref1 = cliLogLevel !== null && cliLogLevel !== void 0 ? cliLogLevel : envLogLevel) !== null && _ref1 !== void 0 ? _ref1 : 'info';
|
|
157
159
|
// Parse storage configuration
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
160
|
+
const cliResourceStoreUri = typeof values['resource-store-uri'] === 'string' ? values['resource-store-uri'] : undefined;
|
|
161
|
+
const envResourceStoreUri = env.RESOURCE_STORE_URI;
|
|
162
|
+
const defaultResourceStorePath = path.join(baseDir, name, 'files');
|
|
163
|
+
const resourceStoreUri = normalizeResourceStoreUri((_ref2 = cliResourceStoreUri !== null && cliResourceStoreUri !== void 0 ? cliResourceStoreUri : envResourceStoreUri) !== null && _ref2 !== void 0 ? _ref2 : defaultResourceStorePath);
|
|
162
164
|
const cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;
|
|
163
165
|
const envBaseUrl = env.BASE_URL;
|
|
164
166
|
const baseUrl = cliBaseUrl !== null && cliBaseUrl !== void 0 ? cliBaseUrl : envBaseUrl;
|
|
@@ -171,7 +173,7 @@ Examples:
|
|
|
171
173
|
name,
|
|
172
174
|
version: pkg.version,
|
|
173
175
|
repositoryUrl,
|
|
174
|
-
|
|
176
|
+
resourceStoreUri
|
|
175
177
|
};
|
|
176
178
|
if (baseUrl) result.baseUrl = baseUrl;
|
|
177
179
|
if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;
|
|
@@ -183,3 +185,14 @@ Examples:
|
|
|
183
185
|
*/ export function createConfig() {
|
|
184
186
|
return parseConfig(process.argv, process.env);
|
|
185
187
|
}
|
|
188
|
+
function normalizeResourceStoreUri(resourceStoreUri) {
|
|
189
|
+
const filePrefix = 'file://';
|
|
190
|
+
if (resourceStoreUri.startsWith(filePrefix)) {
|
|
191
|
+
const rawPath = resourceStoreUri.slice(filePrefix.length);
|
|
192
|
+
const expandedPath = rawPath.startsWith('~') ? rawPath.replace(/^~/, homedir()) : rawPath;
|
|
193
|
+
return `${filePrefix}${path.resolve(expandedPath)}`;
|
|
194
|
+
}
|
|
195
|
+
if (resourceStoreUri.includes('://')) return resourceStoreUri;
|
|
196
|
+
const expandedPath = resourceStoreUri.startsWith('~') ? resourceStoreUri.replace(/^~/, homedir()) : resourceStoreUri;
|
|
197
|
+
return `${filePrefix}${path.resolve(expandedPath)}`;
|
|
198
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/config.ts"],"sourcesContent":["import { parseDcrConfig, parseConfig as parseOAuthConfig } from '@mcp-z/oauth-google';\nimport { findConfigPath, parseConfig as parseTransportConfig } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport moduleRoot from 'module-root-sync';\nimport { homedir } from 'os';\nimport * as path from 'path';\nimport * as url from 'url';\nimport { parseArgs } from 'util';\nimport { GOOGLE_SCOPE } from '../constants.ts';\nimport type { ServerConfig } from '../types.ts';\n\nconst pkg = JSON.parse(fs.readFileSync(path.join(moduleRoot(url.fileURLToPath(import.meta.url)), 'package.json'), 'utf-8'));\n\nconst HELP_TEXT = `\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --storage-dir=<path> Directory for CSV file storage (default: .mcp-z/files)\n --base-url=<url> Base URL for HTTP file serving (optional)\n\nEnvironment Variables:\n GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n AUTH_MODE Default authentication mode (optional)\n HEADLESS Disable browser auto-open (optional)\n DCR_MODE DCR mode (optional, same format as --dcr-mode)\n DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n PORT Default HTTP port (optional)\n LOG_LEVEL Default logging level (optional)\n STORAGE_DIR Default storage directory (optional)\n BASE_URL Default base URL for file serving (optional)\n\nOAuth Scopes:\n openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n\nExamples:\n mcp-gmail # Use default settings\n mcp-gmail --auth=service-account # Use service account auth\n mcp-gmail --port=3000 # HTTP transport on port 3000\n mcp-gmail --storage-dir=./emails # Custom storage directory\n GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var\n`.trim();\n\n/**\n * Handle --version and --help flags before config parsing.\n * These should work without requiring any configuration.\n */\nexport function handleVersionHelp(args: string[]): { handled: boolean; output?: string } {\n const { values } = parseArgs({\n args,\n options: {\n version: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n strict: false,\n });\n\n if (values.version) return { handled: true, output: pkg.version };\n if (values.help) return { handled: true, output: HELP_TEXT };\n return { handled: false };\n}\n\n/**\n * Parse Gmail server configuration from CLI arguments and environment.\n *\n * CLI Arguments (all optional):\n * - --auth=<mode> Authentication mode (default: loopback-oauth)\n * Modes: loopback-oauth, service-account, dcr\n * - --headless Disable browser auto-open, return auth URL instead\n * - --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n * - --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n * - --dcr-verify-url=<url> External verification endpoint (required for external mode)\n * - --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n * - --port=<port> Enable HTTP transport on specified port\n * - --stdio Enable stdio transport (default if no port)\n * - --log-level=<level> Logging level (default: info)\n * - --storage-dir=<path> Directory for CSV file storage (default: .mcp-z/files)\n * - --base-url=<url> Base URL for HTTP file serving (optional)\n *\n * Environment Variables:\n * - GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n * - GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n * - AUTH_MODE Default authentication mode (optional)\n * - HEADLESS Disable browser auto-open (optional)\n * - DCR_MODE DCR mode (optional, same format as --dcr-mode)\n * - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n * - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n * - PORT Default HTTP port (optional)\n * - LOG_LEVEL Default logging level (optional)\n * - STORAGE_DIR Default storage directory (optional)\n * - BASE_URL Default base URL for file serving (optional)\n *\n * OAuth Scopes (from constants.ts):\n * openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n */\nexport function parseConfig(args: string[], env: Record<string, string | undefined>): ServerConfig {\n const transportConfig = parseTransportConfig(args, env);\n const oauthConfig = parseOAuthConfig(args, env);\n\n // Parse DCR configuration if DCR mode is enabled\n const dcrConfig = oauthConfig.auth === 'dcr' ? parseDcrConfig(args, env, GOOGLE_SCOPE) : undefined;\n\n // Parse application-level config (LOG_LEVEL, STORAGE_DIR, BASE_URL)\n const { values } = parseArgs({\n args,\n options: {\n 'log-level': { type: 'string' },\n 'base-url': { type: 'string' },\n 'storage-dir': { type: 'string' },\n },\n strict: false, // Allow other arguments\n allowPositionals: true,\n });\n\n const name = pkg.name.replace(/^@[^/]+\\//, '');\n // Parse repository URL from package.json, stripping git+ prefix and .git suffix\n const rawRepoUrl = typeof pkg.repository === 'object' ? pkg.repository.url : pkg.repository;\n const repositoryUrl = rawRepoUrl?.replace(/^git\\+/, '').replace(/\\.git$/, '') ?? `https://github.com/mcp-z/${name}`;\n let rootDir = homedir();\n try {\n const configPath = findConfigPath({ config: '.mcp.json', cwd: process.cwd(), stopDir: homedir() });\n rootDir = path.dirname(configPath);\n } catch {\n rootDir = homedir();\n }\n const baseDir = path.join(rootDir, '.mcp-z');\n const cliLogLevel = typeof values['log-level'] === 'string' ? values['log-level'] : undefined;\n const envLogLevel = env.LOG_LEVEL;\n const logLevel = cliLogLevel ?? envLogLevel ?? 'info';\n\n // Parse storage configuration\n const cliStorageDir = typeof values['storage-dir'] === 'string' ? values['storage-dir'] : undefined;\n const envStorageDir = env.STORAGE_DIR;\n let storageDir = cliStorageDir ?? envStorageDir ?? path.join(baseDir, name, 'files');\n if (storageDir.startsWith('~')) storageDir = storageDir.replace(/^~/, homedir());\n\n const cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;\n const envBaseUrl = env.BASE_URL;\n const baseUrl = cliBaseUrl ?? envBaseUrl;\n\n // Combine configs\n const result: ServerConfig = {\n ...oauthConfig, // Includes clientId, auth, headless, redirectUri, serviceAccountKeyFile\n transport: transportConfig.transport,\n logLevel,\n baseDir,\n name,\n version: pkg.version,\n repositoryUrl,\n storageDir: path.resolve(storageDir),\n };\n if (baseUrl) result.baseUrl = baseUrl;\n if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;\n return result;\n}\n\n/**\n * Build production configuration from process globals.\n * Entry point for production server.\n */\nexport function createConfig(): ServerConfig {\n return parseConfig(process.argv, process.env);\n}\n"],"names":["parseDcrConfig","parseConfig","parseOAuthConfig","findConfigPath","parseTransportConfig","fs","moduleRoot","homedir","path","url","parseArgs","GOOGLE_SCOPE","pkg","JSON","parse","readFileSync","join","fileURLToPath","HELP_TEXT","trim","handleVersionHelp","args","values","options","version","type","help","strict","handled","output","env","cliLogLevel","cliStorageDir","transportConfig","oauthConfig","dcrConfig","auth","undefined","allowPositionals","name","replace","rawRepoUrl","repository","repositoryUrl","rootDir","configPath","config","cwd","process","stopDir","dirname","baseDir","envLogLevel","LOG_LEVEL","logLevel","envStorageDir","STORAGE_DIR","storageDir","startsWith","cliBaseUrl","envBaseUrl","BASE_URL","baseUrl","result","transport","resolve","createConfig","argv"],"mappings":"AAAA,SAASA,cAAc,EAAEC,eAAeC,gBAAgB,QAAQ,sBAAsB;AACtF,SAASC,cAAc,EAAEF,eAAeG,oBAAoB,QAAQ,gBAAgB;AACpF,YAAYC,QAAQ,KAAK;AACzB,OAAOC,gBAAgB,mBAAmB;AAC1C,SAASC,OAAO,QAAQ,KAAK;AAC7B,YAAYC,UAAU,OAAO;AAC7B,YAAYC,SAAS,MAAM;AAC3B,SAASC,SAAS,QAAQ,OAAO;AACjC,SAASC,YAAY,QAAQ,kBAAkB;AAG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G7B,WAAW;QACd8B,WAAW/B,gBAAgB+B,SAAS;QACpCV;QACAH;QACAZ;QACAf,SAASZ,IAAIY,OAAO;QACpBmB;QACAc,YAAYjD,KAAKyD,OAAO,CAACR;IAC3B;IACA,IAAIK,SAASC,OAAOD,OAAO,GAAGA;IAC9B,IAAI3B,cAAcE,WAAW0B,OAAO5B,SAAS,GAAGA;IAChD,OAAO4B;AACT;AAEA;;;CAGC,GACD,OAAO,SAASG;IACd,OAAOjE,YAAY+C,QAAQmB,IAAI,EAAEnB,QAAQlB,GAAG;AAC9C"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/config.ts"],"sourcesContent":["import { parseDcrConfig, parseConfig as parseOAuthConfig } from '@mcp-z/oauth-google';\nimport { findConfigPath, parseConfig as parseTransportConfig } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport moduleRoot from 'module-root-sync';\nimport { homedir } from 'os';\nimport * as path from 'path';\nimport * as url from 'url';\nimport { parseArgs } from 'util';\nimport { GOOGLE_SCOPE } from '../constants.ts';\nimport type { ServerConfig } from '../types.ts';\n\nconst pkg = JSON.parse(fs.readFileSync(path.join(moduleRoot(url.fileURLToPath(import.meta.url)), 'package.json'), 'utf-8'));\n\nconst HELP_TEXT = `\nUsage: mcp-gmail [options]\n\nMCP server for Gmail email management with OAuth authentication.\n\nOptions:\n --version Show version number\n --help Show this help message\n --auth=<mode> Authentication mode (default: loopback-oauth)\n Modes: loopback-oauth, service-account, dcr\n --headless Disable browser auto-open, return auth URL instead\n --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n --dcr-verify-url=<url> External verification endpoint (required for external mode)\n --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n --port=<port> Enable HTTP transport on specified port\n --stdio Enable stdio transport (default if no port)\n --log-level=<level> Logging level (default: info)\n --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)\n --base-url=<url> Base URL for HTTP file serving (optional)\n\nEnvironment Variables:\n GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n AUTH_MODE Default authentication mode (optional)\n HEADLESS Disable browser auto-open (optional)\n DCR_MODE DCR mode (optional, same format as --dcr-mode)\n DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n TOKEN_STORE_URI Token storage URI (optional)\n PORT Default HTTP port (optional)\n LOG_LEVEL Default logging level (optional)\n RESOURCE_STORE_URI Resource store URI (optional, file://)\n BASE_URL Default base URL for file serving (optional)\n\nOAuth Scopes:\n openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n\nExamples:\n mcp-gmail # Use default settings\n mcp-gmail --auth=service-account # Use service account auth\n mcp-gmail --port=3000 # HTTP transport on port 3000\n mcp-gmail --resource-store-uri=file:///tmp/emails # Custom resource store URI\n GOOGLE_CLIENT_ID=xxx mcp-gmail # Set client ID via env var\n`.trim();\n\n/**\n * Handle --version and --help flags before config parsing.\n * These should work without requiring any configuration.\n */\nexport function handleVersionHelp(args: string[]): { handled: boolean; output?: string } {\n const { values } = parseArgs({\n args,\n options: {\n version: { type: 'boolean' },\n help: { type: 'boolean' },\n },\n strict: false,\n });\n\n if (values.version) return { handled: true, output: pkg.version };\n if (values.help) return { handled: true, output: HELP_TEXT };\n return { handled: false };\n}\n\n/**\n * Parse Gmail server configuration from CLI arguments and environment.\n *\n * CLI Arguments (all optional):\n * - --auth=<mode> Authentication mode (default: loopback-oauth)\n * Modes: loopback-oauth, service-account, dcr\n * - --headless Disable browser auto-open, return auth URL instead\n * - --redirect-uri=<uri> OAuth redirect URI (default: ephemeral loopback)\n * - --dcr-mode=<mode> DCR mode (self-hosted or external, default: self-hosted)\n * - --dcr-verify-url=<url> External verification endpoint (required for external mode)\n * - --dcr-store-uri=<uri> DCR client storage URI (required for self-hosted mode)\n * - --port=<port> Enable HTTP transport on specified port\n * - --stdio Enable stdio transport (default if no port)\n * - --log-level=<level> Logging level (default: info)\n * - --resource-store-uri=<uri> Resource store URI for CSV file storage (default: file://~/.mcp-z/mcp-gmail/files)\n * - --base-url=<url> Base URL for HTTP file serving (optional)\n *\n * Environment Variables:\n * - GOOGLE_CLIENT_ID OAuth client ID (REQUIRED)\n * - GOOGLE_CLIENT_SECRET OAuth client secret (optional)\n * - AUTH_MODE Default authentication mode (optional)\n * - HEADLESS Disable browser auto-open (optional)\n * - DCR_MODE DCR mode (optional, same format as --dcr-mode)\n * - DCR_VERIFY_URL External verification URL (optional, same as --dcr-verify-url)\n * - DCR_STORE_URI DCR storage URI (optional, same as --dcr-store-uri)\n * - TOKEN_STORE_URI Token storage URI (optional)\n * - PORT Default HTTP port (optional)\n * - LOG_LEVEL Default logging level (optional)\n * - RESOURCE_STORE_URI Resource store URI (optional, file://)\n * - BASE_URL Default base URL for file serving (optional)\n *\n * OAuth Scopes (from constants.ts):\n * openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://mail.google.com/\n */\nexport function parseConfig(args: string[], env: Record<string, string | undefined>): ServerConfig {\n const transportConfig = parseTransportConfig(args, env);\n const oauthConfig = parseOAuthConfig(args, env);\n\n // Parse DCR configuration if DCR mode is enabled\n const dcrConfig = oauthConfig.auth === 'dcr' ? parseDcrConfig(args, env, GOOGLE_SCOPE) : undefined;\n\n // Parse application-level config (LOG_LEVEL, RESOURCE_STORE_URI, BASE_URL)\n const { values } = parseArgs({\n args,\n options: {\n 'log-level': { type: 'string' },\n 'base-url': { type: 'string' },\n 'resource-store-uri': { type: 'string' },\n },\n strict: false, // Allow other arguments\n allowPositionals: true,\n });\n\n const name = pkg.name.replace(/^@[^/]+\\//, '');\n // Parse repository URL from package.json, stripping git+ prefix and .git suffix\n const rawRepoUrl = typeof pkg.repository === 'object' ? pkg.repository.url : pkg.repository;\n const repositoryUrl = rawRepoUrl?.replace(/^git\\+/, '').replace(/\\.git$/, '') ?? `https://github.com/mcp-z/${name}`;\n let rootDir = homedir();\n try {\n const configPath = findConfigPath({ config: '.mcp.json', cwd: process.cwd(), stopDir: homedir() });\n rootDir = path.dirname(configPath);\n } catch {\n rootDir = homedir();\n }\n const baseDir = path.join(rootDir, '.mcp-z');\n const cliLogLevel = typeof values['log-level'] === 'string' ? values['log-level'] : undefined;\n const envLogLevel = env.LOG_LEVEL;\n const logLevel = cliLogLevel ?? envLogLevel ?? 'info';\n\n // Parse storage configuration\n const cliResourceStoreUri = typeof values['resource-store-uri'] === 'string' ? values['resource-store-uri'] : undefined;\n const envResourceStoreUri = env.RESOURCE_STORE_URI;\n const defaultResourceStorePath = path.join(baseDir, name, 'files');\n const resourceStoreUri = normalizeResourceStoreUri(cliResourceStoreUri ?? envResourceStoreUri ?? defaultResourceStorePath);\n\n const cliBaseUrl = typeof values['base-url'] === 'string' ? values['base-url'] : undefined;\n const envBaseUrl = env.BASE_URL;\n const baseUrl = cliBaseUrl ?? envBaseUrl;\n\n // Combine configs\n const result: ServerConfig = {\n ...oauthConfig, // Includes clientId, auth, headless, redirectUri, serviceAccountKeyFile\n transport: transportConfig.transport,\n logLevel,\n baseDir,\n name,\n version: pkg.version,\n repositoryUrl,\n resourceStoreUri,\n };\n if (baseUrl) result.baseUrl = baseUrl;\n if (dcrConfig !== undefined) result.dcrConfig = dcrConfig;\n return result;\n}\n\n/**\n * Build production configuration from process globals.\n * Entry point for production server.\n */\nexport function createConfig(): ServerConfig {\n return parseConfig(process.argv, process.env);\n}\n\nfunction normalizeResourceStoreUri(resourceStoreUri: string): string {\n const filePrefix = 'file://';\n if (resourceStoreUri.startsWith(filePrefix)) {\n const rawPath = resourceStoreUri.slice(filePrefix.length);\n const expandedPath = rawPath.startsWith('~') ? rawPath.replace(/^~/, homedir()) : rawPath;\n return `${filePrefix}${path.resolve(expandedPath)}`;\n }\n\n if (resourceStoreUri.includes('://')) return resourceStoreUri;\n\n const expandedPath = resourceStoreUri.startsWith('~') ? resourceStoreUri.replace(/^~/, homedir()) : resourceStoreUri;\n return `${filePrefix}${path.resolve(expandedPath)}`;\n}\n"],"names":["parseDcrConfig","parseConfig","parseOAuthConfig","findConfigPath","parseTransportConfig","fs","moduleRoot","homedir","path","url","parseArgs","GOOGLE_SCOPE","pkg","JSON","parse","readFileSync","join","fileURLToPath","HELP_TEXT","trim","handleVersionHelp","args","values","options","version","type","help","strict","handled","output","env","cliLogLevel","cliResourceStoreUri","transportConfig","oauthConfig","dcrConfig","auth","undefined","allowPositionals","name","replace","rawRepoUrl","repository","repositoryUrl","rootDir","configPath","config","cwd","process","stopDir","dirname","baseDir","envLogLevel","LOG_LEVEL","logLevel","envResourceStoreUri","RESOURCE_STORE_URI","defaultResourceStorePath","resourceStoreUri","normalizeResourceStoreUri","cliBaseUrl","envBaseUrl","BASE_URL","baseUrl","result","transport","createConfig","argv","filePrefix","startsWith","rawPath","slice","length","expandedPath","resolve","includes"],"mappings":"AAAA,SAASA,cAAc,EAAEC,eAAeC,gBAAgB,QAAQ,sBAAsB;AACtF,SAASC,cAAc,EAAEF,eAAeG,oBAAoB,QAAQ,gBAAgB;AACpF,YAAYC,QAAQ,KAAK;AACzB,OAAOC,gBAAgB,mBAAmB;AAC1C,SAASC,OAAO,QAAQ,KAAK;AAC7B,YAAYC,UAAU,OAAO;AAC7B,YAAYC,SAAS,MAAM;AAC3B,SAASC,SAAS,QAAQ,OAAO;AACjC,SAASC,YAAY,QAAQ,kBAAkB;AAG/C,MAAMC,MAAMC,KAAKC,KAAK,CAACT,GAAGU,YAAY,CAACP,KAAKQ,IAAI,CAACV,WAAWG,IAAIQ,aAAa,CAAC,YAAYR,GAAG,IAAI,iBAAiB;AAElH,MAAMS,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CnB,CAAC,CAACC,IAAI;AAEN;;;CAGC,GAC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QmB,IAAI,EAAEnB,QAAQlB,GAAG;AAC9C;AAEA,SAAS6B,0BAA0BD,gBAAwB;IACzD,MAAMU,aAAa;IACnB,IAAIV,iBAAiBW,UAAU,CAACD,aAAa;QAC3C,MAAME,UAAUZ,iBAAiBa,KAAK,CAACH,WAAWI,MAAM;QACxD,MAAMC,eAAeH,QAAQD,UAAU,CAAC,OAAOC,QAAQ9B,OAAO,CAAC,MAAMjC,aAAa+D;QAClF,OAAO,GAAGF,aAAa5D,KAAKkE,OAAO,CAACD,eAAe;IACrD;IAEA,IAAIf,iBAAiBiB,QAAQ,CAAC,QAAQ,OAAOjB;IAE7C,MAAMe,eAAef,iBAAiBW,UAAU,CAAC,OAAOX,iBAAiBlB,OAAO,CAAC,MAAMjC,aAAamD;IACpG,OAAO,GAAGU,aAAa5D,KAAKkE,OAAO,CAACD,eAAe;AACrD"}
|
package/dist/esm/setup/http.js
CHANGED
|
@@ -36,7 +36,7 @@ export async function createHTTPServer(config, overrides) {
|
|
|
36
36
|
logger.info('Mounted loopback OAuth callback router');
|
|
37
37
|
}
|
|
38
38
|
const fileRouter = createFileServingRouter({
|
|
39
|
-
|
|
39
|
+
resourceStoreUri: config.resourceStoreUri
|
|
40
40
|
}, {
|
|
41
41
|
contentType: 'text/csv',
|
|
42
42
|
contentDisposition: 'attachment'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/http.ts"],"sourcesContent":["import { composeMiddleware, connectHttp, createFileServingRouter, registerPrompts, registerResources, registerTools } from '@mcp-z/server';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport cors from 'cors';\nimport express from 'express';\nimport type { RuntimeOverrides, ServerConfig } from '../types.ts';\nimport { createDefaultRuntime } from './runtime.ts';\n\nexport async function createHTTPServer(config: ServerConfig, overrides?: RuntimeOverrides) {\n const runtime = await createDefaultRuntime(config, overrides);\n const modules = runtime.createDomainModules();\n const layers = runtime.middlewareFactories.map((factory) => factory(runtime.deps));\n const composed = composeMiddleware(modules, layers);\n const logger = runtime.deps.logger;\n const port = config.transport.port;\n if (!port) throw new Error('Port is required for HTTP transport');\n\n const tools = [...composed.tools, ...runtime.deps.oauthAdapters.accountTools];\n const prompts = [...composed.prompts, ...runtime.deps.oauthAdapters.accountPrompts];\n\n const mcpServer = new McpServer({ name: config.name, version: config.version });\n registerTools(mcpServer, tools);\n registerResources(mcpServer, composed.resources);\n registerPrompts(mcpServer, prompts);\n\n const app = express();\n app.use(cors());\n app.use(express.json({ limit: '10mb' }));\n\n if (runtime.deps.oauthAdapters.loopbackRouter) {\n app.use('/', runtime.deps.oauthAdapters.loopbackRouter);\n logger.info('Mounted loopback OAuth callback router');\n }\n\n const fileRouter = createFileServingRouter({
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/http.ts"],"sourcesContent":["import { composeMiddleware, connectHttp, createFileServingRouter, registerPrompts, registerResources, registerTools } from '@mcp-z/server';\nimport { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';\nimport cors from 'cors';\nimport express from 'express';\nimport type { RuntimeOverrides, ServerConfig } from '../types.ts';\nimport { createDefaultRuntime } from './runtime.ts';\n\nexport async function createHTTPServer(config: ServerConfig, overrides?: RuntimeOverrides) {\n const runtime = await createDefaultRuntime(config, overrides);\n const modules = runtime.createDomainModules();\n const layers = runtime.middlewareFactories.map((factory) => factory(runtime.deps));\n const composed = composeMiddleware(modules, layers);\n const logger = runtime.deps.logger;\n const port = config.transport.port;\n if (!port) throw new Error('Port is required for HTTP transport');\n\n const tools = [...composed.tools, ...runtime.deps.oauthAdapters.accountTools];\n const prompts = [...composed.prompts, ...runtime.deps.oauthAdapters.accountPrompts];\n\n const mcpServer = new McpServer({ name: config.name, version: config.version });\n registerTools(mcpServer, tools);\n registerResources(mcpServer, composed.resources);\n registerPrompts(mcpServer, prompts);\n\n const app = express();\n app.use(cors());\n app.use(express.json({ limit: '10mb' }));\n\n if (runtime.deps.oauthAdapters.loopbackRouter) {\n app.use('/', runtime.deps.oauthAdapters.loopbackRouter);\n logger.info('Mounted loopback OAuth callback router');\n }\n\n const fileRouter = createFileServingRouter({ resourceStoreUri: config.resourceStoreUri }, { contentType: 'text/csv', contentDisposition: 'attachment' });\n app.use('/files', fileRouter);\n\n if (runtime.deps.oauthAdapters.dcrRouter) {\n app.use('/', runtime.deps.oauthAdapters.dcrRouter);\n logger.info('Mounted DCR router with OAuth endpoints');\n }\n\n logger.info(`Starting ${config.name} MCP server (http)`);\n const { close, httpServer } = await connectHttp(mcpServer, { logger, app, port });\n logger.info('http transport ready');\n\n return {\n httpServer,\n mcpServer,\n logger,\n close: async () => {\n await close();\n await runtime.close();\n },\n };\n}\n"],"names":["composeMiddleware","connectHttp","createFileServingRouter","registerPrompts","registerResources","registerTools","McpServer","cors","express","createDefaultRuntime","createHTTPServer","config","overrides","runtime","modules","createDomainModules","layers","middlewareFactories","map","factory","deps","composed","logger","port","transport","Error","tools","oauthAdapters","accountTools","prompts","accountPrompts","mcpServer","name","version","resources","app","use","json","limit","loopbackRouter","info","fileRouter","resourceStoreUri","contentType","contentDisposition","dcrRouter","close","httpServer"],"mappings":"AAAA,SAASA,iBAAiB,EAAEC,WAAW,EAAEC,uBAAuB,EAAEC,eAAe,EAAEC,iBAAiB,EAAEC,aAAa,QAAQ,gBAAgB;AAC3I,SAASC,SAAS,QAAQ,0CAA0C;AACpE,OAAOC,UAAU,OAAO;AACxB,OAAOC,aAAa,UAAU;AAE9B,SAASC,oBAAoB,QAAQ,eAAe;AAEpD,OAAO,eAAeC,iBAAiBC,MAAoB,EAAEC,SAA4B;IACvF,MAAMC,UAAU,MAAMJ,qBAAqBE,QAAQC;IACnD,MAAME,UAAUD,QAAQE,mBAAmB;IAC3C,MAAMC,SAASH,QAAQI,mBAAmB,CAACC,GAAG,CAAC,CAACC,UAAYA,QAAQN,QAAQO,IAAI;IAChF,MAAMC,WAAWrB,kBAA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,EAAEC,UAAU,EAAE,GAAG,MAAM9C,YAAY8B,WAAW;QAAET;QAAQa;QAAKZ;IAAK;IAC/ED,OAAOkB,IAAI,CAAC;IAEZ,OAAO;QACLO;QACAhB;QACAT;QACAwB,OAAO;YACL,MAAMA;YACN,MAAMjC,QAAQiC,KAAK;QACrB;IACF;AACF"}
|
|
@@ -22,8 +22,8 @@ export function createLogger(config) {
|
|
|
22
22
|
}) : pino.destination(1));
|
|
23
23
|
}
|
|
24
24
|
export async function createTokenStore(baseDir) {
|
|
25
|
-
const
|
|
26
|
-
return createStore(
|
|
25
|
+
const tokenStoreUri = process.env.TOKEN_STORE_URI || `file://${path.join(baseDir, 'tokens.json')}`;
|
|
26
|
+
return createStore(tokenStoreUri);
|
|
27
27
|
}
|
|
28
28
|
export async function createDcrStore(baseDir, required) {
|
|
29
29
|
if (!required) return undefined;
|
|
@@ -65,8 +65,8 @@ export function createStorageLayer(storageContext) {
|
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
67
|
export function assertStorageConfig(config) {
|
|
68
|
-
if (!config.
|
|
69
|
-
throw new Error('gmail-messages-export-csv: Server configuration missing
|
|
68
|
+
if (!config.resourceStoreUri) {
|
|
69
|
+
throw new Error('gmail-messages-export-csv: Server configuration missing resourceStoreUri.');
|
|
70
70
|
}
|
|
71
71
|
if (config.transport.type === 'http' && !config.baseUrl && !config.transport.port) {
|
|
72
72
|
throw new Error('gmail-messages-export-csv: HTTP transport requires either baseUrl in server config or port in transport config. This is a server configuration error - please provide --base-url or --port.');
|
|
@@ -101,7 +101,7 @@ export async function createDefaultRuntime(config, overrides) {
|
|
|
101
101
|
()=>createAuthLayer(oauthAdapters.middleware),
|
|
102
102
|
()=>createLoggingLayer(logger),
|
|
103
103
|
()=>createStorageLayer({
|
|
104
|
-
|
|
104
|
+
resourceStoreUri: config.resourceStoreUri,
|
|
105
105
|
baseUrl: config.baseUrl,
|
|
106
106
|
transport: config.transport
|
|
107
107
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/runtime.ts"],"sourcesContent":["import { sanitizeForLoggingFormatter } from '@mcp-z/oauth';\nimport type { CachedToken } from '@mcp-z/oauth-google';\nimport type { Logger, MiddlewareLayer } from '@mcp-z/server';\nimport { createLoggingMiddleware } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport pino from 'pino';\nimport createStore from '../lib/create-store.ts';\nimport * as mcp from '../mcp/index.ts';\nimport type { CommonRuntime, RuntimeDeps, RuntimeOverrides, ServerConfig, StorageContext } from '../types.ts';\nimport { createOAuthAdapters, type OAuthAdapters } from './oauth-google.ts';\n\nexport function createLogger(config: ServerConfig): Logger {\n const hasStdio = config.transport.type === 'stdio';\n const logsPath = path.join(config.baseDir, 'logs', `${config.name}.log`);\n if (hasStdio) fs.mkdirSync(path.dirname(logsPath), { recursive: true });\n return pino({ level: config.logLevel ?? 'info', formatters: sanitizeForLoggingFormatter() }, hasStdio ? pino.destination({ dest: logsPath, sync: false }) : pino.destination(1));\n}\n\nexport async function createTokenStore(baseDir: string) {\n const
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/setup/runtime.ts"],"sourcesContent":["import { sanitizeForLoggingFormatter } from '@mcp-z/oauth';\nimport type { CachedToken } from '@mcp-z/oauth-google';\nimport type { Logger, MiddlewareLayer } from '@mcp-z/server';\nimport { createLoggingMiddleware } from '@mcp-z/server';\nimport * as fs from 'fs';\nimport * as path from 'path';\nimport pino from 'pino';\nimport createStore from '../lib/create-store.ts';\nimport * as mcp from '../mcp/index.ts';\nimport type { CommonRuntime, RuntimeDeps, RuntimeOverrides, ServerConfig, StorageContext } from '../types.ts';\nimport { createOAuthAdapters, type OAuthAdapters } from './oauth-google.ts';\n\nexport function createLogger(config: ServerConfig): Logger {\n const hasStdio = config.transport.type === 'stdio';\n const logsPath = path.join(config.baseDir, 'logs', `${config.name}.log`);\n if (hasStdio) fs.mkdirSync(path.dirname(logsPath), { recursive: true });\n return pino({ level: config.logLevel ?? 'info', formatters: sanitizeForLoggingFormatter() }, hasStdio ? pino.destination({ dest: logsPath, sync: false }) : pino.destination(1));\n}\n\nexport async function createTokenStore(baseDir: string) {\n const tokenStoreUri = process.env.TOKEN_STORE_URI || `file://${path.join(baseDir, 'tokens.json')}`;\n return createStore<CachedToken>(tokenStoreUri);\n}\n\nexport async function createDcrStore(baseDir: string, required: boolean) {\n if (!required) return undefined;\n const dcrStoreUri = process.env.DCR_STORE_URI || `file://${path.join(baseDir, 'dcr.json')}`;\n return createStore<unknown>(dcrStoreUri);\n}\n\nexport function createAuthLayer(authMiddleware: OAuthAdapters['middleware']): MiddlewareLayer {\n return {\n withTool: authMiddleware.withToolAuth,\n withResource: authMiddleware.withResourceAuth,\n withPrompt: authMiddleware.withPromptAuth,\n };\n}\n\nexport function createLoggingLayer(logger: Logger): MiddlewareLayer {\n const logging = createLoggingMiddleware({ logger });\n return {\n withTool: logging.withToolLogging,\n withResource: logging.withResourceLogging,\n withPrompt: logging.withPromptLogging,\n };\n}\n\nexport function createStorageLayer(storageContext: StorageContext): MiddlewareLayer {\n const wrapAtPosition = <T extends { name: string; handler: unknown; [key: string]: unknown }>(module: T, extraPosition: number): T => {\n const originalHandler = module.handler as (...args: unknown[]) => Promise<unknown>;\n\n const wrappedHandler = async (...allArgs: unknown[]) => {\n const extra = allArgs[extraPosition];\n (extra as { storageContext?: StorageContext }).storageContext = storageContext;\n return await originalHandler(...allArgs);\n };\n\n return {\n ...module,\n handler: wrappedHandler,\n } as T;\n };\n\n return {\n withTool: <T extends { name: string; config: unknown; handler: unknown }>(module: T): T => wrapAtPosition(module, 1) as T,\n };\n}\n\nexport function assertStorageConfig(config: ServerConfig) {\n if (!config.resourceStoreUri) {\n throw new Error('gmail-messages-export-csv: Server configuration missing resourceStoreUri.');\n }\n if (config.transport.type === 'http' && !config.baseUrl && !config.transport.port) {\n throw new Error('gmail-messages-export-csv: HTTP transport requires either baseUrl in server config or port in transport config. This is a server configuration error - please provide --base-url or --port.');\n }\n}\n\nexport async function createDefaultRuntime(config: ServerConfig, overrides?: RuntimeOverrides): Promise<CommonRuntime> {\n if (config.auth === 'dcr' && config.transport.type !== 'http') throw new Error('DCR mode requires an HTTP transport');\n\n assertStorageConfig(config);\n const logger = createLogger(config);\n const tokenStore = await createTokenStore(config.baseDir);\n const baseUrl = config.baseUrl ?? (config.transport.type === 'http' && config.transport.port ? `http://localhost:${config.transport.port}` : undefined);\n const dcrStore = await createDcrStore(config.baseDir, config.auth === 'dcr');\n const oauthAdapters = await createOAuthAdapters(config, { logger, tokenStore, dcrStore }, baseUrl);\n const deps: RuntimeDeps = { config, logger, tokenStore, oauthAdapters, baseUrl };\n const createDomainModules =\n overrides?.createDomainModules ??\n (() => ({\n tools: Object.values(mcp.toolFactories).map((factory) => factory()),\n resources: Object.values(mcp.resourceFactories).map((factory) => factory()),\n prompts: Object.values(mcp.promptFactories).map((factory) => factory()),\n }));\n const middlewareFactories = overrides?.middlewareFactories ?? [() => createAuthLayer(oauthAdapters.middleware), () => createLoggingLayer(logger), () => createStorageLayer({ resourceStoreUri: config.resourceStoreUri, baseUrl: config.baseUrl, transport: config.transport })];\n\n return {\n deps,\n middlewareFactories,\n createDomainModules,\n close: async () => {},\n };\n}\n"],"names":["sanitizeForLoggingFormatter","createLoggingMiddleware","fs","path","pino","createStore","mcp","createOAuthAdapters","createLogger","config","hasStdio","transport","type","logsPath","join","baseDir","name","mkdirSync","dirname","recursive","level","logLevel","formatters","destination","dest","sync","createTokenStore","tokenStoreUri","process","env","TOKEN_STORE_URI","createDcrStore","required","undefined","dcrStoreUri","DCR_STORE_URI","createAuthLayer","authMiddleware","withTool","withToolAuth","withResource","withResourceAuth","withPrompt","withPromptAuth","createLoggingLayer","logger","logging","withToolLogging","withResourceLogging","withPromptLogging","createStorageLayer","storageContext","wrapAtPosition","module","extraPosition","originalHandler","handler","wrappedHandler","allArgs","extra","assertStorageConfig","resourceStoreUri","Error","baseUrl","port","createDefaultRuntime","overrides","auth","tokenStore","dcrStore","oauthAdapters","deps","createDomainModules","tools","Object","values","toolFactories","map","factory","resources","resourceFactories","prompts","promptFactories","middlewareFactories","middleware","close"],"mappings":"AAAA,SAASA,2BAA2B,QAAQ,eAAe;AAG3D,SAASC,uBAAuB,QAAQ,gBAAgB;AACxD,YAAYC,QAAQ,KAAK;AACzB,YAAYC,UAAU,OAAO;AAC7B,OAAOC,UAAU,OAAO;AACxB,OAAOC,iBAAiB,yBAAyB;AACjD,YAAYC,SAAS,kBAAkB;AAEvC,SAASC,mBAAmB,QAA4B,oBAAoB;AAE5E,OAAO,SAASC,aAAaC,MAAoB;QAI1BA;IAHrB,MAAMC,WAAWD,OAAOE,SAAS,CAACC,IAAI,KAAK;IAC3C,MAAMC,WAAWV,KAAKW,IAAI,CAACL,OAAOM,OAAO,EAAE,QAAQ,GAAGN,OAAOO,IAAI,CAAC,IAAI,CAAC;IACvE,IAAIN,UAAUR,GAAGe,SAAS,CAACd,KAAKe,OAAO,CAACL,WAAW;QAAEM,WAAW;IAAK;IACrE,OAAOf,KAAK;QAAEgB,KAAK,GAAEX,mBAAAA,OAAOY,QAAQ,cAAfZ,8BAAAA,mBAAmB;QAAQa,YAAYtB;IAA8B,GAAGU,WAAWN,KAAKmB,WAAW,CAAC;QAAEC,MAAMX;QAAUY,MAAM;IAAM,KAAKrB,KAAKmB,WAAW,CAAC;AAC/K;AAEA,OAAO,eAAeG,iBAAiBX,OAAe;IACpD,MAAMY,gBAAgBC,QAAQC,GAAG,CAACC,eAAe,IAAI,CAAC,OAAO,EAAE3B,KAAKW,IAAI,CAACC,SAAS,gBAAgB;IAClG,OAAOV,YAAyBsB;AAClC;AAEA,OAAO,eAAeI,eAAehB,OAAe,EAAEiB,QAAiB;IACrE,IAAI,CAACA,UAAU,OAAOC;IACtB,MAAMC,cAAcN,QAAQC,GAAG,CAACM,aAAa,IAAI,CAAC,OAAO,EAA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, OAuthConfig {
|
|
|
12
12
|
name: string;
|
|
13
13
|
version: string;
|
|
14
14
|
repositoryUrl: string;
|
|
15
|
-
|
|
15
|
+
resourceStoreUri: string;
|
|
16
16
|
baseUrl?: string;
|
|
17
17
|
dcrConfig?: DcrConfig;
|
|
18
18
|
}
|
|
19
19
|
export interface StorageContext {
|
|
20
|
-
|
|
20
|
+
resourceStoreUri: string;
|
|
21
21
|
baseUrl?: string;
|
|
22
22
|
transport: BaseServerConfig['transport'];
|
|
23
23
|
}
|
package/dist/esm/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/types.ts"],"sourcesContent":["import type { CachedToken, DcrConfig, OAuthConfig } from '@mcp-z/oauth-google';\nimport type { BaseServerConfig, MiddlewareLayer, PromptModule, ResourceModule, Logger as ServerLogger, ToolModule } from '@mcp-z/server';\nimport type { Keyv } from 'keyv';\nimport type { OAuthAdapters } from './setup/oauth-google.ts';\n\nexport type Logger = Pick<Console, 'info' | 'error' | 'warn' | 'debug'>;\n\n/**\n * Composes transport config, OAuth config, and application-level config\n */\nexport interface ServerConfig extends BaseServerConfig, OAuthConfig {\n logLevel: string;\n baseDir: string;\n name: string;\n version: string;\n repositoryUrl: string;\n\n // File serving configuration for CSV exports\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/Projects/mcp-z/mcp-gmail/src/types.ts"],"sourcesContent":["import type { CachedToken, DcrConfig, OAuthConfig } from '@mcp-z/oauth-google';\nimport type { BaseServerConfig, MiddlewareLayer, PromptModule, ResourceModule, Logger as ServerLogger, ToolModule } from '@mcp-z/server';\nimport type { Keyv } from 'keyv';\nimport type { OAuthAdapters } from './setup/oauth-google.ts';\n\nexport type Logger = Pick<Console, 'info' | 'error' | 'warn' | 'debug'>;\n\n/**\n * Composes transport config, OAuth config, and application-level config\n */\nexport interface ServerConfig extends BaseServerConfig, OAuthConfig {\n logLevel: string;\n baseDir: string;\n name: string;\n version: string;\n repositoryUrl: string;\n\n // File serving configuration for CSV exports\n resourceStoreUri: string;\n baseUrl?: string;\n\n // DCR configuration (when auth === 'dcr')\n dcrConfig?: DcrConfig;\n}\n\nexport interface StorageContext {\n resourceStoreUri: string;\n baseUrl?: string;\n transport: BaseServerConfig['transport'];\n}\n\nexport interface StorageExtra {\n storageContext: StorageContext;\n}\n\n/** Runtime dependencies exposed to middleware/factories. */\nexport interface RuntimeDeps {\n config: ServerConfig;\n logger: ServerLogger;\n tokenStore: Keyv<CachedToken>;\n oauthAdapters: OAuthAdapters;\n baseUrl?: string;\n}\n\n/** Collections of MCP modules produced by domain factories. */\nexport type DomainModules = {\n tools: ToolModule[];\n resources: ResourceModule[];\n prompts: PromptModule[];\n};\n\n/** Factory that produces a middleware layer given runtime dependencies. */\nexport type MiddlewareFactory = (deps: RuntimeDeps) => MiddlewareLayer;\n\n/** Shared runtime configuration returned by `createDefaultRuntime`. */\nexport interface CommonRuntime {\n deps: RuntimeDeps;\n middlewareFactories: MiddlewareFactory[];\n createDomainModules: () => DomainModules;\n close: () => Promise<void>;\n}\n\nexport interface RuntimeOverrides {\n middlewareFactories?: MiddlewareFactory[];\n createDomainModules?: () => DomainModules;\n}\n\nexport type { GmailQuery } from './schemas/index.ts';\n"],"names":[],"mappings":"AAmEA,WAAqD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mcp-z/mcp-gmail",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "MCP server for Gmail integration with OAuth authentication, message search, batch operations, and Google Sheets export",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"gmail",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"bin"
|
|
60
60
|
],
|
|
61
61
|
"scripts": {
|
|
62
|
-
"build": "
|
|
62
|
+
"build": "tsds validate",
|
|
63
63
|
"format": "tsds format",
|
|
64
64
|
"prepublish:check": "ncp",
|
|
65
65
|
"prepublishOnly": "tsds validate",
|
|
@@ -72,33 +72,32 @@
|
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@mcp-z/email": "^1.0.0",
|
|
75
|
-
"@mcp-z/oauth": "^1.0.
|
|
76
|
-
"@mcp-z/oauth-google": "^1.0.
|
|
77
|
-
"@mcp-z/server": "^1.0.
|
|
78
|
-
"@modelcontextprotocol/sdk": "^1.
|
|
79
|
-
"cors": "^2.
|
|
80
|
-
"csv-stringify": "^6.
|
|
81
|
-
"express": "^5.
|
|
75
|
+
"@mcp-z/oauth": "^1.0.0",
|
|
76
|
+
"@mcp-z/oauth-google": "^1.0.0",
|
|
77
|
+
"@mcp-z/server": "^1.0.0",
|
|
78
|
+
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
79
|
+
"cors": "^2.0.0",
|
|
80
|
+
"csv-stringify": "^6.0.0",
|
|
81
|
+
"express": "^5.0.0",
|
|
82
82
|
"googleapis": "^169.0.0",
|
|
83
|
-
"keyv-registry": "^0.
|
|
84
|
-
"module-root-sync": "^2.0.
|
|
85
|
-
"node-html-parser": "^7.0.
|
|
86
|
-
"pino": "^10.
|
|
87
|
-
"zod": "^4.
|
|
83
|
+
"keyv-registry": "^1.0.0",
|
|
84
|
+
"module-root-sync": "^2.0.0",
|
|
85
|
+
"node-html-parser": "^7.0.0",
|
|
86
|
+
"pino": "^10.0.0",
|
|
87
|
+
"zod": "^4.0.0"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
|
-
"@mcp-z/client": "^1.0.
|
|
90
|
+
"@mcp-z/client": "^1.0.5",
|
|
91
91
|
"@types/cors": "^2.8.19",
|
|
92
92
|
"@types/express": "^5.0.6",
|
|
93
93
|
"@types/mocha": "^10.0.10",
|
|
94
94
|
"@types/node": "^25.0.3",
|
|
95
95
|
"dotenv": "^17.2.3",
|
|
96
96
|
"get-port": "^7.1.0",
|
|
97
|
-
"keyv": "^5.
|
|
97
|
+
"keyv": "^5.0.0",
|
|
98
98
|
"node-version-use": "^2.4.7",
|
|
99
99
|
"ts-dev-stack": "^1.22.1",
|
|
100
|
-
"tsds-config": "^1.0.4"
|
|
101
|
-
"typescript": "^5.9.3"
|
|
100
|
+
"tsds-config": "^1.0.4"
|
|
102
101
|
},
|
|
103
102
|
"engines": {
|
|
104
103
|
"node": ">=20"
|