Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README for GDrive #7

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions src/gdrive/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,45 @@

This MCP server integrates with Google Drive to allow listing, reading, and searching over files.

## Components

### Tools

- **search**
- Search for files in Google Drive
- Input: `query` (string): Search query
- Returns file names and MIME types of matching files

### Resources

The server provides access to Google Drive files:

- **Files** (`gdrive://<file_id>`)
Copy link
Member

@jspahrsummers jspahrsummers Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should ideally put the file ID into the path, rather than the URL hostname, so it'd be: gdrive:///<file_id> (note the triple slashes)

However, this README is accurate to how it works right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the code & readme to do ///

- Supports all file types
- Google Workspace files are automatically exported:
- Docs → Markdown
- Sheets → CSV
- Presentations → Plain text
- Drawings → PNG
- Other files are provided in their native format

## Getting started

1. Create a new Google Cloud project
2. Enable the Google Drive API
3. Configure an OAuth consent screen ("internal" is fine for testing)
1. [Create a new Google Cloud project](https://console.cloud.google.com/projectcreate)
2. [Enable the Google Drive API](https://console.cloud.google.com/workspace-api/products)
3. [Configure an OAuth consent screen](https://console.cloud.google.com/apis/credentials/consent) ("internal" is fine for testing)
4. Add OAuth scope `https://www.googleapis.com/auth/drive.readonly`
5. Create an OAuth Client ID for application type "Desktop App"
5. [Create an OAuth Client ID](https://console.cloud.google.com/apis/credentials/oauthclient) for application type "Desktop App"
6. Download the JSON file of your client's OAuth keys
7. Rename the key file to `gcp-oauth.keys.json` and place into the root of this repo
7. Rename the key file to `gcp-oauth.keys.json` and place into the root of this repo (i.e. `servers/gcp-oauth.keys.json`)

Make sure to build the server with either `npm run build` or `npm run watch`.

### Authentication

To authenticate and save credentials:

1. Run the server with the `auth` argument: `node build/gdrive auth`
1. Run the server with the `auth` argument: `node ./dist auth`
2. This will open an authentication flow in your system browser
3. Complete the authentication process
4. Credentials will be saved for future use
Expand All @@ -27,7 +49,7 @@ To authenticate and save credentials:

After authenticating:

1. Run the server normally: `node build/gdrive`
1. Run the server normally: `node ./dist`
2. The server will load the saved credentials and start

Note: If you haven't authenticated yet, the server will prompt you to run with the `auth` argument first.
14 changes: 8 additions & 6 deletions src/gdrive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@

server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "search") {
const query = request.params.arguments?.query as string;

const userQuery = request.params.arguments?.query as string;
const escapedQuery = userQuery.replace(/'/g, "\\'");
Fixed Show fixed Hide fixed
const formattedQuery = `fullText contains '${escapedQuery}'`;

const res = await drive.files.list({
q: query,
q: formattedQuery,
pageSize: 10,
fields: "files(id, name, mimeType, modifiedTime, size)",
});

const fileList = res.data.files
?.map((file: any) => `${file.name} (${file.mimeType})`)
.join("\n");
Expand All @@ -175,15 +177,15 @@

const credentialsPath = path.join(
path.dirname(new URL(import.meta.url).pathname),
"../../.gdrive-server-credentials.json",
"../../../.gdrive-server-credentials.json",
);

async function authenticateAndSaveCredentials() {
console.log("Launching auth flow…");
const auth = await authenticate({
keyfilePath: path.join(
path.dirname(new URL(import.meta.url).pathname),
"../../gcp-oauth.keys.json",
"../../../gcp-oauth.keys.json",
),
scopes: ["https://www.googleapis.com/auth/drive.readonly"],
});
Expand Down