File size: 4,559 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getDocsClient } from '../../../clients.js';
import { DocumentIdParameter, validateHexColor, hexToRgbColor } from '../../../types.js';
import { getTableById } from '../structureHelpers.js';
import * as GDocsHelpers from '../../../googleDocsApiHelpers.js';

const BorderSideSchema = z.strictObject({
  color: z
    .string()
    .refine(validateHexColor, { message: 'Invalid hex color format (e.g., #000000).' })
    .optional(),
  widthPt: z.number().min(0).optional(),
  dashStyle: z.enum(['SOLID', 'DASHED', 'DOTTED']).optional(),
});

export function register(server: FastMCP) {
  server.addTool({
    name: 'updateTableBorders',
    description:
      'Applies table border styles to a Google Docs table range. Supports top/bottom/left/right borders across a cell range.',
    parameters: DocumentIdParameter.extend({
      tableId: z.string().min(1).describe('The MCP table ID returned by listDocumentTables.'),
      rowStart: z.number().int().min(0).describe('Zero-based starting row index.'),
      rowEnd: z.number().int().min(0).describe('Zero-based ending row index (inclusive).'),
      columnStart: z.number().int().min(0).describe('Zero-based starting column index.'),
      columnEnd: z.number().int().min(0).describe('Zero-based ending column index (inclusive).'),
      top: BorderSideSchema.optional(),
      bottom: BorderSideSchema.optional(),
      left: BorderSideSchema.optional(),
      right: BorderSideSchema.optional(),
      tabId: z.string().optional().describe('Optional target tab ID.'),
    })
      .refine((data) => data.rowEnd >= data.rowStart, {
        message: 'rowEnd must be greater than or equal to rowStart',
        path: ['rowEnd'],
      })
      .refine((data) => data.columnEnd >= data.columnStart, {
        message: 'columnEnd must be greater than or equal to columnStart',
        path: ['columnEnd'],
      }),
    execute: async (args, { log }) => {
      const docs = await getDocsClient();
      log.info(
        `Updating table borders in ${args.tableId} for doc ${args.documentId}${args.tabId ? ` (tab: ${args.tabId})` : ''}`
      );

      try {
        const res = await docs.documents.get({
          documentId: args.documentId,
          includeTabsContent: true,
          fields:
            'body(content(startIndex,endIndex,table(tableRows(tableCells(startIndex,endIndex))))),tabs(tabProperties(tabId,title),documentTab(body(content(startIndex,endIndex,table(tableRows(tableCells(startIndex,endIndex)))))))',
        });

        const table = getTableById(res.data, args.tableId, args.tabId);
        if (!table) throw new UserError(`Table "${args.tableId}" not found in document.`);
        if (table.startIndex == null) {
          throw new UserError(`Table "${args.tableId}" does not expose a valid table start index.`);
        }

        const defaultColor = hexToRgbColor('#000000')!;
        const makeBorder = (side?: {
          color?: string;
          widthPt?: number;
          dashStyle?: 'SOLID' | 'DASHED' | 'DOTTED';
        }) =>
          side
            ? GDocsHelpers.buildTableBorder(
                hexToRgbColor(side.color ?? '#000000') ?? defaultColor,
                side.widthPt ?? 1,
                side.dashStyle ?? 'SOLID'
              )
            : undefined;

        const requestInfo = GDocsHelpers.buildTableCellStyleRequest(
          table.startIndex,
          args.rowStart,
          args.columnStart,
          {
            rowSpan: args.rowEnd - args.rowStart + 1,
            columnSpan: args.columnEnd - args.columnStart + 1,
            borderTop: makeBorder(args.top),
            borderBottom: makeBorder(args.bottom),
            borderLeft: makeBorder(args.left),
            borderRight: makeBorder(args.right),
          },
          args.tabId
        );

        if (!requestInfo) {
          throw new UserError('No border style options were provided.');
        }

        await GDocsHelpers.executeBatchUpdate(docs, args.documentId, [requestInfo.request]);
        return `Successfully updated table borders (${requestInfo.fields.join(', ')}) for ${args.tableId}.`;
      } catch (error: any) {
        log.error(
          `Error updating table borders for ${args.tableId} in doc ${args.documentId}: ${error.message || error}`
        );
        if (error instanceof UserError) throw error;
        throw new UserError(`Failed to update table borders: ${error.message || 'Unknown error'}`);
      }
    },
  });
}