Skip to content

Commit f5c2f1e

Browse files
committed
Added Google Drive 'service account' auth.
1 parent ce75414 commit f5c2f1e

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ GOOGLE_DRIVE_FOLDER_ID=
2929
GOOGLE_DRIVE_REFRESH_TOKEN=
3030
GOOGLE_DRIVE_CLIENT_ID=
3131
GOOGLE_DRIVE_CLIENT_SECRET=
32+
GOOGLE_DRIVE_SERVICE_ACCOUNT_JSON=
3233

3334
ONEDRIVE_FOLDER_ID=
3435
ONEDRIVE_REFRESH_TOKEN=

src/tools.ts

+38-25
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,8 @@ export function registerTools(server: McpServer) {
16681668
Accepts optional Google Drive folder identifier, and an optional read limit for the number of files to ingest.
16691669
For example, with Google Drive URI (https://drive.google.com/drive/u/0/folders/32tzhRD12KDh2hXABY8OZRFv7Smy8WBkQ), the folder identifier is 32tzhRD12KDh2hXABY8OZRFv7Smy8WBkQ.
16701670
If no folder identifier provided, ingests files from root Google Drive folder.
1671-
Requires environment variables to be configured: GOOGLE_DRIVE_CLIENT_ID, GOOGLE_DRIVE_CLIENT_SECRET, GOOGLE_DRIVE_REFRESH_TOKEN.
1671+
Requires environment variables to be configured: GOOGLE_DRIVE_SERVICE_ACCOUNT_JSON -or- GOOGLE_DRIVE_CLIENT_ID, GOOGLE_DRIVE_CLIENT_SECRET, GOOGLE_DRIVE_REFRESH_TOKEN.
1672+
If service account JSON is provided, uses service account authentication. Else, uses user authentication.
16721673
Executes asynchronously, creates Google Drive feed, and returns the feed identifier.`,
16731674
{
16741675
folderId: z.string().optional().describe("Google Drive folder identifier, optional."),
@@ -1678,38 +1679,50 @@ export function registerTools(server: McpServer) {
16781679
const client = new Graphlit();
16791680

16801681
try {
1681-
const clientId = process.env.GOOGLE_DRIVE_CLIENT_ID;
1682-
if (!clientId) {
1683-
console.error("Please set GOOGLE_DRIVE_CLIENT_ID environment variable.");
1684-
process.exit(1);
1685-
}
1682+
var clientId;
1683+
var clientSecret;
1684+
var refreshToken;
1685+
var authenticationType = GoogleDriveAuthenticationTypes.ServiceAccount;
16861686

1687-
const clientSecret = process.env.GOOGLE_DRIVE_CLIENT_SECRET;
1688-
if (!clientSecret) {
1689-
console.error("Please set GOOGLE_DRIVE_CLIENT_SECRET environment variable.");
1690-
process.exit(1);
1691-
}
1687+
const serviceAccountJson = process.env.GOOGLE_DRIVE_SERVICE_ACCOUNT_JSON;
16921688

1693-
const refreshToken = process.env.GOOGLE_DRIVE_REFRESH_TOKEN;
1694-
if (!refreshToken) {
1695-
console.error("Please set GOOGLE_DRIVE_REFRESH_TOKEN environment variable.");
1696-
process.exit(1);
1689+
if (!serviceAccountJson) {
1690+
authenticationType = GoogleDriveAuthenticationTypes.User;
1691+
1692+
clientId = process.env.GOOGLE_DRIVE_CLIENT_ID;
1693+
if (!clientId) {
1694+
console.error("Please set GOOGLE_DRIVE_CLIENT_ID environment variable.");
1695+
process.exit(1);
1696+
}
1697+
1698+
clientSecret = process.env.GOOGLE_DRIVE_CLIENT_SECRET;
1699+
if (!clientSecret) {
1700+
console.error("Please set GOOGLE_DRIVE_CLIENT_SECRET environment variable.");
1701+
process.exit(1);
1702+
}
1703+
1704+
refreshToken = process.env.GOOGLE_DRIVE_REFRESH_TOKEN;
1705+
if (!refreshToken) {
1706+
console.error("Please set GOOGLE_DRIVE_REFRESH_TOKEN environment variable.");
1707+
process.exit(1);
1708+
}
16971709
}
16981710

16991711
const response = await client.createFeed({
17001712
name: `Google Drive`,
17011713
type: FeedTypes.Site,
17021714
site: {
1703-
type: FeedServiceTypes.GoogleDrive,
1704-
googleDrive: {
1705-
authenticationType: GoogleDriveAuthenticationTypes.User,
1706-
folderId: folderId,
1707-
clientId: clientId,
1708-
clientSecret: clientSecret,
1709-
refreshToken: refreshToken,
1710-
},
1711-
isRecursive: true,
1712-
readLimit: readLimit || 100
1715+
type: FeedServiceTypes.GoogleDrive,
1716+
googleDrive: {
1717+
authenticationType: authenticationType,
1718+
folderId: folderId,
1719+
clientId: clientId,
1720+
clientSecret: clientSecret,
1721+
refreshToken: refreshToken,
1722+
serviceAccountJson: serviceAccountJson,
1723+
},
1724+
isRecursive: true,
1725+
readLimit: readLimit || 100
17131726
}
17141727
});
17151728

0 commit comments

Comments
 (0)