| 1 | import { |
| 2 | ContextItem, |
| 3 | ContextProviderDescription, |
| 4 | ContextProviderExtras, |
| 5 | ContextSubmenuItem, |
| 6 | LoadSubmenuItemsArgs, |
| 7 | } from "../../index.js"; |
| 8 | import { BaseContextProvider } from "../index.js"; |
| 9 | |
| 10 | class PostgresContextProvider extends BaseContextProvider { |
| 11 | static description: ContextProviderDescription = { |
| 12 | title: "postgres", |
| 13 | displayTitle: "PostgreSQL", |
| 14 | description: "Retrieve PostgreSQL table schema and sample rows", |
| 15 | type: "submenu", |
| 16 | renderInlineAs: "", |
| 17 | }; |
| 18 | |
| 19 | static ALL_TABLES = "__all_tables"; |
| 20 | static DEFAULT_SAMPLE_ROWS = 3; |
| 21 | |
| 22 | private async getPool() { |
| 23 | // @ts-ignore |
| 24 | const pg = await import("pg"); |
| 25 | |
| 26 | return new pg.Pool({ |
| 27 | host: this.options.host, |
| 28 | port: this.options.port, |
| 29 | user: this.options.user, |
| 30 | password: this.options.password, |
| 31 | database: this.options.database, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | private async getTableNames(pool: any): Promise<string[]> { |
| 36 | const schema = this.options.schema ?? "public"; |
| 37 | let tablesInfoQuery = ` |
| 38 | SELECT table_schema, table_name |
| 39 | FROM information_schema.tables`; |
| 40 | if (schema !== null) { |
| 41 | tablesInfoQuery += ` WHERE table_schema = '${schema}'`; |
| 42 | } |
| 43 | const { rows: tablesInfo } = await pool.query(tablesInfoQuery); |
| 44 | return tablesInfo.map( |
| 45 | (tableInfo: any) => `${tableInfo.table_schema}.${tableInfo.table_name}`, |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | async getContextItems( |
| 50 | query = "", |
| 51 | _: ContextProviderExtras = {} as ContextProviderExtras, |
| 52 | ): Promise<ContextItem[]> { |
| 53 | const pool = await this.getPool(); |
| 54 | |
| 55 | try { |
| 56 | const contextItems: ContextItem[] = []; |
| 57 | |
| 58 | const tableNames = []; |
| 59 | if (query === PostgresContextProvider.ALL_TABLES) { |
| 60 | tableNames.push(...(await this.getTableNames(pool))); |
| 61 | } else { |
| 62 | tableNames.push(query); |
| 63 | } |
| 64 | |
| 65 | for (const tableName of tableNames) { |
| 66 | // Get the table schema |
| 67 | if (!tableName.includes(".")) { |
| 68 | throw new Error( |
| 69 | `Table name must be in format schema.table_name, got ${tableName}`, |
| 70 | ); |
| 71 | } |
| 72 | const schemaQuery = ` |
| 73 | SELECT column_name, data_type, character_maximum_length |
| 74 | FROM INFORMATION_SCHEMA.COLUMNS |
| 75 | WHERE table_schema = '${tableName.split(".")[0]}' |
| 76 | AND table_name = '${tableName.split(".")[1]}'`; |
| 77 | console.log("schemaQuery", schemaQuery); |
| 78 | const { rows: tableSchema } = await pool.query(schemaQuery); |
| 79 | |
| 80 | // Get the sample rows |
| 81 | const sampleRows = |
| 82 | this.options.sampleRows ?? |
| 83 | PostgresContextProvider.DEFAULT_SAMPLE_ROWS; |
| 84 | const { rows: sampleRowResults } = await pool.query(` |
| 85 | SELECT * |
| 86 | FROM ${tableName} |
| 87 | LIMIT ${sampleRows}`); |
| 88 | |
| 89 | // Create prompt from the table schema and sample rows |
| 90 | let prompt = `Postgres schema for database ${this.options.database} table ${tableName}:\n`; |
| 91 | prompt += `${JSON.stringify(tableSchema, null, 2)}\n\n`; |
| 92 | prompt += `Sample rows: ${JSON.stringify(sampleRowResults, null, 2)}`; |
| 93 | |
| 94 | contextItems.push({ |
| 95 | name: `${this.options.database}-${tableName}-schema-and-sample-rows`, |
| 96 | description: `Schema and sample rows for table ${tableName}`, |
| 97 | content: prompt, |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | return contextItems; |
| 102 | } catch (error) { |
| 103 | throw new Error(`Failed to query PostgreSQL database: ${error}`); |
| 104 | } finally { |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | async loadSubmenuItems( |
| 109 | _: LoadSubmenuItemsArgs, |
| 110 | ): Promise<ContextSubmenuItem[]> { |
| 111 | const pool = await this.getPool(); |
| 112 | |
| 113 | try { |
| 114 | const contextItems: ContextSubmenuItem[] = []; |
| 115 | const tableNames = await this.getTableNames(pool); |
| 116 | |
| 117 | for (const tableName of tableNames) { |
| 118 | contextItems.push({ |
| 119 | id: tableName, |
| 120 | title: tableName, |
| 121 | description: `Schema from ${tableName} and ${this.options.sampleRows} sample rows.`, |
| 122 | }); |
| 123 | } |
| 124 | contextItems.push({ |
| 125 | id: PostgresContextProvider.ALL_TABLES, |
| 126 | title: "All tables", |
| 127 | description: `Schema from all tables and ${this.options.sampleRows} sample rows each.`, |
| 128 | }); |
| 129 | |
| 130 | return contextItems; |
| 131 | } catch (error) { |
| 132 | throw new Error(`Failed to query PostgreSQL database: ${error}`); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | get deprecationMessage() { |
| 137 | return "The Postgres context provider is deprecated. Please consider using the Postgres MCP (such as github.com/crystaldba/postgres-mcp) instead."; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | export default PostgresContextProvider; |