UNPKG

@coretext-ai/public-gsuite-mcp-d495ced0-92b0-4d0e-ad89-f27c9e31f4f9

Version:
226 lines 11.2 kB
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from '@modelcontextprotocol/sdk/types.js'; import { config } from 'dotenv'; import { loadConfig, validateConfig } from './config.js'; import { LogShipper } from './services/log-shipper.js'; import { Logger } from './services/logger.js'; // Load environment variables config(); // Import tools from each template import { GoogleCalendarTools } from './tools/google-calendar-tools.js'; import { GoogleCalendarClient } from './clients/google-calendar-client.js'; import { GoogleContactsTools } from './tools/google-contacts-tools.js'; import { GoogleContactsClient } from './clients/google-contacts-client.js'; import { GoogleDriveTools } from './tools/google-drive-tools.js'; import { GoogleDriveClient } from './clients/google-drive-client.js'; import { GoogleGmailTools } from './tools/google-gmail-tools.js'; import { GoogleGmailClient } from './clients/google-gmail-client.js'; // Import unified OAuth clients for special cases import { GoogleOAuthClient } from './oauth/google-oauth-client.js'; class PublicGsuiteMcpServer { constructor() { // Initialize logging first this.initializeLogging(); this.server = new Server({ name: 'public-gsuite-mcp', version: '1.0.0', }, { capabilities: { tools: {}, }, }); // Initialize OAuth clients first this.googleOAuthClient = new GoogleOAuthClient(); // Initialize template clients and tools // Google service - use unified GoogleOAuthClient this.googleCalendarClient = new GoogleCalendarClient({ authToken: process.env.GOOGLE_OAUTH_CREDENTIALS, gOOGLEOAUTHCREDENTIALS: process.env.GOOGLE_OAUTH_CREDENTIALS, oauth: { "provider": "google", "scopes": [ "https://www.googleapis.com/auth/calendar", "https://www.googleapis.com/auth/calendar.readonly", "https://www.googleapis.com/auth/calendar.events", "https://www.googleapis.com/auth/calendar.events.readonly", "https://www.googleapis.com/auth/calendar.acls", "https://www.googleapis.com/auth/calendar.acls.readonly", "https://www.googleapis.com/auth/calendar.app.created", "https://www.googleapis.com/auth/calendar.calendarlist", "https://www.googleapis.com/auth/calendar.calendarlist.readonly", "https://www.googleapis.com/auth/calendar.calendars", "https://www.googleapis.com/auth/calendar.calendars.readonly", "https://www.googleapis.com/auth/calendar.events.freebusy", "https://www.googleapis.com/auth/calendar.events.owned", "https://www.googleapis.com/auth/calendar.events.owned.readonly", "https://www.googleapis.com/auth/calendar.events.public.readonly", "https://www.googleapis.com/auth/calendar.settings.readonly" ], "credentialFileEnv": "GOOGLE_OAUTH_CREDENTIALS", "redirectUri": "http://localhost:3000/oauth/callback/google", "tokenStoragePath": "~/.mcp/google-tokens/" }, logger: this.logger, oauthClient: this.googleOAuthClient }); this.googleCalendarTools = new GoogleCalendarTools(this.googleCalendarClient); // Google service - use unified GoogleOAuthClient this.googleContactsClient = new GoogleContactsClient({ authToken: process.env.GOOGLE_OAUTH_CREDENTIALS, gOOGLEOAUTHCREDENTIALS: process.env.GOOGLE_OAUTH_CREDENTIALS, oauth: { "provider": "google", "scopes": [ "https://www.googleapis.com/auth/contacts", "https://www.googleapis.com/auth/contacts.readonly", "https://www.googleapis.com/auth/contacts.other.readonly", "https://www.googleapis.com/auth/directory.readonly", "https://www.googleapis.com/auth/user.addresses.read", "https://www.googleapis.com/auth/user.emails.read", "https://www.googleapis.com/auth/user.phonenumbers.read", "https://www.googleapis.com/auth/userinfo.profile" ], "credentialFileEnv": "GOOGLE_OAUTH_CREDENTIALS", "redirectUri": "http://localhost:3000/oauth/callback/google", "tokenStoragePath": "~/.mcp/google-tokens/" }, logger: this.logger, oauthClient: this.googleOAuthClient }); this.googleContactsTools = new GoogleContactsTools(this.googleContactsClient); // Google service - use unified GoogleOAuthClient this.googleDriveClient = new GoogleDriveClient({ authToken: process.env.GOOGLE_OAUTH_CREDENTIALS, gOOGLEOAUTHCREDENTIALS: process.env.GOOGLE_OAUTH_CREDENTIALS, oauth: { "provider": "google", "scopes": [ "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive.readonly", "https://www.googleapis.com/auth/drive.metadata.readonly" ], "credentialFileEnv": "GOOGLE_OAUTH_CREDENTIALS", "redirectUri": "http://localhost:3000/oauth/callback/google", "tokenStoragePath": "~/.mcp/google-tokens/" }, logger: this.logger, oauthClient: this.googleOAuthClient }); this.googleDriveTools = new GoogleDriveTools(this.googleDriveClient); // Google service - use unified GoogleOAuthClient this.googleGmailClient = new GoogleGmailClient({ authToken: process.env.GOOGLE_OAUTH_CREDENTIALS, gOOGLEOAUTHCREDENTIALS: process.env.GOOGLE_OAUTH_CREDENTIALS, oauth: { "provider": "google", "scopes": [ "https://mail.google.com/", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/gmail.modify", "https://www.googleapis.com/auth/gmail.labels", "https://www.googleapis.com/auth/gmail.compose", "https://www.googleapis.com/auth/gmail.send", "https://www.googleapis.com/auth/gmail.settings.basic" ], "credentialFileEnv": "GOOGLE_OAUTH_CREDENTIALS", "redirectUri": "http://localhost:3000/oauth/callback/google", "tokenStoragePath": "~/.mcp/google-tokens/" }, logger: this.logger, oauthClient: this.googleOAuthClient }); this.googleGmailTools = new GoogleGmailTools(this.googleGmailClient); this.setupHandlers(); } initializeLogging() { const config = loadConfig(); const validation = validateConfig(config); if (!validation.isValid) { console.error('Configuration validation failed:', validation.errors); process.exit(1); } this.logShipper = new LogShipper(config.logShipping); this.logger = new Logger({ logLevel: config.logShipping.logLevel, component: 'server', enableConsole: true, enableShipping: config.logShipping.enabled, serverName: 'public-gsuite-mcp', logShipper: this.logShipper }); this.logger.info('SERVER_INIT', 'MCP server initializing', { serverName: 'public-gsuite-mcp', logShippingEnabled: config.logShipping.enabled, logLevel: config.logShipping.logLevel }); } setupHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => { const tools = []; // Add google-calendar tools tools.push(...this.googleCalendarTools.getToolDefinitions()); // Add google-contacts tools tools.push(...this.googleContactsTools.getToolDefinitions()); // Add google-drive tools tools.push(...this.googleDriveTools.getToolDefinitions()); // Add google-gmail tools tools.push(...this.googleGmailTools.getToolDefinitions()); return { tools }; }); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { // Handle google-calendar tools if (this.googleCalendarTools.canHandle(name)) { return await this.googleCalendarTools.executeTool(name, args); } // Handle google-contacts tools if (this.googleContactsTools.canHandle(name)) { return await this.googleContactsTools.executeTool(name, args); } // Handle google-drive tools if (this.googleDriveTools.canHandle(name)) { return await this.googleDriveTools.executeTool(name, args); } // Handle google-gmail tools if (this.googleGmailTools.canHandle(name)) { return await this.googleGmailTools.executeTool(name, args); } throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`); } catch (error) { if (error instanceof McpError) { throw error; } throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${error instanceof Error ? error.message : String(error)}`); } }); } async run() { const transport = new StdioServerTransport(); await this.server.connect(transport); this.logger.info('SERVER_START', 'MCP server started successfully', { serverName: 'public-gsuite-mcp', transport: 'stdio' }); console.error('public-gsuite-mcp MCP server running on stdio'); // Handle graceful shutdown for log shipping const shutdown = async () => { this.logger.info('SERVER_SHUTDOWN', 'MCP server shutting down', { serverName: 'public-gsuite-mcp' }); if (this.logShipper) { await this.logShipper.shutdown(); } process.exit(0); }; process.on('SIGINT', shutdown); process.on('SIGTERM', shutdown); } } const server = new PublicGsuiteMcpServer(); server.run().catch(console.error); //# sourceMappingURL=index.js.map