File size: 2,387 Bytes
7dc28be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getSheetsClient } from '../../clients.js';
import * as SheetsHelpers from '../../googleSheetsApiHelpers.js';

export function register(server: FastMCP) {
  server.addTool({
    name: 'getTable',
    description:
      'Gets detailed information about a specific table including its columns, range, and properties. Use the table name or ID returned by listTables.',
    parameters: z.strictObject({
      spreadsheetId: z
        .string()
        .describe(
          'The spreadsheet ID — the long string between /d/ and /edit in a Google Sheets URL.'
        ),
      tableIdentifier: z
        .string()
        .describe(
          'The table name or table ID. Names are resolved first, then IDs. Use listTables to see available tables.'
        ),
    }),
    execute: async (args, { log }) => {
      const sheets = await getSheetsClient();
      log.info(`Getting table details for: ${args.tableIdentifier}`);

      try {
        const { table, sheetName, sheetId } = await SheetsHelpers.resolveTableIdentifier(
          sheets,
          args.spreadsheetId,
          args.tableIdentifier
        );

        // Build detailed table information
        const columns =
          table.columnProperties?.map((col) => ({
            index: col.columnIndex,
            name: col.columnName,
          })) || [];

        const range = table.range
          ? `${sheetName}!${SheetsHelpers.rowColToA1(
              table.range.startRowIndex || 0,
              table.range.startColumnIndex || 0
            )}:${SheetsHelpers.rowColToA1(
              (table.range.endRowIndex || 1) - 1,
              (table.range.endColumnIndex || 1) - 1
            )}`
          : 'Unknown';

        return JSON.stringify(
          {
            tableId: table.tableId,
            name: table.name,
            sheetName,
            sheetId,
            range,
            columns,
            columnCount: table.columnProperties?.length || 0,
          },
          null,
          2
        );
      } catch (error: any) {
        log.error(`Error getting table details: ${error.message || error}`);
        if (error instanceof UserError) throw error;
        throw new UserError(`Failed to get table details: ${error.message || 'Unknown error'}`);
      }
    },
  });
}