Skip to content

Instantly share code, notes, and snippets.

@FutureExcited
Created April 10, 2025 21:54
Show Gist options
  • Save FutureExcited/aa6591736c9290ee9b25d94da5da9c0f to your computer and use it in GitHub Desktop.
Save FutureExcited/aa6591736c9290ee9b25d94da5da9c0f to your computer and use it in GitHub Desktop.

Revisions

  1. FutureExcited created this gist Apr 10, 2025.
    97 changes: 97 additions & 0 deletions vibe-tools notion
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    import type { Command, CommandGenerator, CommandOptions } from '../../types.js';
    import { loadEnv, loadConfig } from '../../config.js';
    import { RunCommand } from '../mcp/run.js';
    import { MarketplaceManager } from '../mcp/marketplace.js';
    import {
    MCPAuthError,
    MCPConnectionError,
    MCPServerError,
    MCPToolError,
    MCPConfigError,
    } from '../mcp/client/errors.js';

    // Load environment variables and config
    loadEnv();

    export class NotionCommand implements Command {
    private config = loadConfig();
    private marketplaceManager = new MarketplaceManager(this.config);
    private runCommand = new RunCommand(this.marketplaceManager);

    async *execute(query: string, options: CommandOptions): CommandGenerator {
    try {
    // Check if NOTION_ACCESS_KEY is set
    if (!process.env.NOTION_ACCESS_KEY) {
    console.error('Error: NOTION_ACCESS_KEY environment variable is not set');
    console.error(
    'Please add your Notion API key to ~/.vibe-tools/.env as NOTION_ACCESS_KEY=ntn_...'
    );
    console.error(
    'You can create a Notion integration at https://www.notion.so/profile/integrations'
    );
    console.error('Make sure to share your pages/databases with the integration in Notion');
    return;
    }

    // Set the OPENAPI_MCP_HEADERS environment variable required by the Notion MCP server
    const headers = {
    Authorization: `Bearer ${process.env.NOTION_ACCESS_KEY}`,
    'Notion-Version': '2022-06-28',
    };
    process.env.OPENAPI_MCP_HEADERS = JSON.stringify(headers);

    // Adjust the query to specify the Notion MCP server
    const notionQuery = `Using the notion-mcp-server: ${query}`;

    // Forward the query to the MCP run command
    yield* this.runCommand.execute(notionQuery, options);
    } catch (error) {
    this.handleError(error, options?.debug);
    throw error;
    }
    }

    private handleError(error: unknown, debug?: boolean) {
    if (error instanceof MCPAuthError) {
    console.error(
    'Authentication error: ' +
    error.message +
    '\nPlease check your NOTION_ACCESS_KEY in ~/.vibe-tools/.env'
    );
    } else if (error instanceof MCPConnectionError) {
    console.error(
    'Connection error: ' +
    error.message +
    '\nPlease check if the Notion MCP server is accessible.'
    );
    } else if (error instanceof MCPServerError) {
    console.error(
    'Server error: ' +
    error.message +
    (error.code ? ` (Code: ${error.code})` : '') +
    '\nPlease try again later or contact support if the issue persists.'
    );
    } else if (error instanceof MCPToolError) {
    console.error(
    'Tool error: ' +
    error.message +
    (error.toolName ? ` (Tool: ${error.toolName})` : '') +
    '\nPlease check if your Notion integration has access to the requested pages/databases.'
    );
    } else if (error instanceof MCPConfigError) {
    console.error(
    'Configuration error: ' + error.message + '\nPlease check your MCP configuration.'
    );
    } else if (error instanceof Error) {
    console.error('Error: ' + error.message);
    if (debug) {
    console.error(error.stack);
    }
    } else {
    console.error('An unknown error occurred');
    }
    }
    }

    // Export the command instance
    export default new NotionCommand();