systemprompt-mcp-notion
Version:
A specialized Model Context Protocol (MCP) server that integrates Notion into your AI workflows. This server enables seamless access to Notion through MCP, allowing AI agents to interact with pages, databases, and comments.
64 lines (63 loc) • 2.6 kB
JavaScript
import { google } from "googleapis";
import * as fs from "fs";
import * as path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TOKEN_PATH = path.join(__dirname, "../../credentials/google-token.json");
const CREDENTIALS_PATH = path.join(__dirname, "../../credentials/google-credentials.json");
export class GoogleAuthService {
static instance;
oAuth2Client = null;
authUrl = null;
constructor() { }
static getInstance() {
if (!GoogleAuthService.instance) {
GoogleAuthService.instance = new GoogleAuthService();
}
return GoogleAuthService.instance;
}
async initialize() {
try {
const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_PATH, "utf-8"));
const { client_secret, client_id, redirect_uris } = credentials.installed;
this.oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
// Try to load existing token
if (fs.existsSync(TOKEN_PATH)) {
const token = JSON.parse(fs.readFileSync(TOKEN_PATH, "utf-8"));
this.oAuth2Client.setCredentials(token);
}
}
catch (error) {
console.error("Error loading client secrets file:", error);
throw error;
}
}
async authenticate() {
if (!this.oAuth2Client) {
throw new Error("OAuth2Client not initialized");
}
// If we already have a token, no need to authenticate
if (fs.existsSync(TOKEN_PATH)) {
return;
}
// Since we can't authenticate automatically in MCP context,
// throw an error with instructions
throw new Error("Google authentication required. Please follow these steps:\n" +
"1. Set up Google Cloud Project and enable required APIs\n" +
"2. Create OAuth 2.0 credentials (Desktop Application type)\n" +
"3. Download credentials and save as 'credentials/google-credentials.json'\n" +
"4. Run the authentication helper script: 'npm run auth-google'\n" +
"5. Restart the MCP server after authentication is complete");
}
getAuth() {
if (!this.oAuth2Client) {
throw new Error("OAuth2Client not initialized");
}
return this.oAuth2Client;
}
async saveToken(token) {
fs.writeFileSync(TOKEN_PATH, JSON.stringify(token));
console.log("Token stored to", TOKEN_PATH);
}
}