File size: 2,077 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
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { google } from 'googleapis';
import { getDocsClient, getDriveClient, getAuthClient } from '../../../clients.js';
import { DocumentIdParameter } from '../../../types.js';

export function register(server: FastMCP) {
  server.addTool({
    name: 'listComments',
    description:
      'Lists all comments in a document with their IDs, authors, status, and quoted text. Returns data needed to call getComment, replyToComment, resolveComment, or deleteComment.',
    parameters: DocumentIdParameter,
    execute: async (args, { log }) => {
      log.info(`Listing comments for document ${args.documentId}`);
      const docsClient = await getDocsClient();
      const driveClient = await getDriveClient();

      try {
        // First get the document to have context
        const doc = await docsClient.documents.get({ documentId: args.documentId });

        // Use Drive API v3 with proper fields to get quoted content
        const authClient = await getAuthClient();
        const drive = google.drive({ version: 'v3', auth: authClient });
        const response = await drive.comments.list({
          fileId: args.documentId,
          fields: 'comments(id,content,quotedFileContent,author,createdTime,resolved)',
          pageSize: 100,
        });

        const comments = (response.data.comments || []).map((comment: any) => ({
          id: comment.id,
          author: comment.author?.displayName || null,
          content: comment.content,
          quotedText: comment.quotedFileContent?.value || null,
          resolved: comment.resolved || false,
          createdTime: comment.createdTime,
          modifiedTime: comment.modifiedTime,
          replyCount: comment.replies?.length || 0,
        }));
        return JSON.stringify({ comments }, null, 2);
      } catch (error: any) {
        log.error(`Error listing comments: ${error.message || error}`);
        throw new UserError(`Failed to list comments: ${error.message || 'Unknown error'}`);
      }
    },
  });
}