mcp-gsheets 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +31 -2
  2. package/dist/index.js +34 -4
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -143,7 +143,9 @@ For other MCP clients, use the standard configuration format shown above. Ensure
143
143
  - Click Share and add the service account email (from JSON file)
144
144
  - Grant "Editor" permissions
145
145
 
146
- ### Alternative: JSON String Authentication
146
+ ### Alternative Authentication Methods
147
+
148
+ #### Option 1: JSON String Authentication
147
149
 
148
150
  Instead of using a file path for credentials, you can provide the service account credentials directly as a JSON string. This is useful for containerized environments, CI/CD pipelines, or when you want to avoid managing credential files.
149
151
 
@@ -168,6 +170,31 @@ Instead of using a file path for credentials, you can provide the service accoun
168
170
  - Newlines in the private key must be represented as `\\n`
169
171
  - If the JSON includes a `project_id`, you can omit `GOOGLE_PROJECT_ID`
170
172
 
173
+ #### Option 2: Private Key Authentication (Simplified)
174
+
175
+ For the most user-friendly approach, you can provide just the private key and email directly. This is the simplest method and requires only two fields from your service account JSON:
176
+
177
+ ```json
178
+ {
179
+ "mcpServers": {
180
+ "mcp-gsheets": {
181
+ "command": "npx",
182
+ "args": ["-y", "mcp-gsheets@latest"],
183
+ "env": {
184
+ "GOOGLE_PRIVATE_KEY": "-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCgR6bvMNOUHZ29\\n+YgbVHAXsT/s+L/jnXTCB193zikCzspSBSfxLu8VRDjkNq9WUoDxizTATzMFNvNf\\n...\\n-----END PRIVATE KEY-----\\n",
185
+ "GOOGLE_CLIENT_EMAIL": "spreadsheet@your-project.iam.gserviceaccount.com"
186
+ }
187
+ }
188
+ }
189
+ }
190
+ ```
191
+
192
+ **Note**: When using `GOOGLE_PRIVATE_KEY`:
193
+ - Newlines in the private key should be represented as `\\n`
194
+ - The private key must include the `-----BEGIN PRIVATE KEY-----` and `-----END PRIVATE KEY-----` markers
195
+ - The client email should be the service account email from your JSON file
196
+ - `GOOGLE_PROJECT_ID` is optional when using this method
197
+
171
198
  ## Local Development Setup
172
199
 
173
200
  If you want to develop or contribute to this project, you can clone and build it locally:
@@ -356,7 +383,9 @@ npm run typecheck
356
383
  **"Authentication failed"**
357
384
  - If using file-based auth: Verify JSON key path is absolute and correct
358
385
  - If using JSON string auth: Ensure JSON is properly escaped and valid
359
- - Check GOOGLE_PROJECT_ID matches your project (or is included in JSON)
386
+ - If using private key auth: Check that the private key includes BEGIN/END markers and newlines are escaped as `\\n`
387
+ - Verify GOOGLE_CLIENT_EMAIL is a valid service account email
388
+ - Check GOOGLE_PROJECT_ID matches your project (or is included in JSON for full JSON auth)
360
389
  - Ensure Sheets API is enabled
361
390
 
362
391
  **"Permission denied"**
package/dist/index.js CHANGED
@@ -12336,6 +12336,13 @@ async function getAuthClient() {
12336
12336
  "Failed to parse GOOGLE_SERVICE_ACCOUNT_KEY: Invalid JSON format. Please ensure the environment variable contains valid JSON."
12337
12337
  );
12338
12338
  }
12339
+ } else if (process.env.GOOGLE_PRIVATE_KEY && process.env.GOOGLE_CLIENT_EMAIL) {
12340
+ const credentials = {
12341
+ type: "service_account",
12342
+ private_key: process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n"),
12343
+ client_email: process.env.GOOGLE_CLIENT_EMAIL
12344
+ };
12345
+ options.credentials = credentials;
12339
12346
  }
12340
12347
  authClient = new GoogleAuth(options);
12341
12348
  }
@@ -12353,12 +12360,35 @@ async function getAuthenticatedClient() {
12353
12360
  return sheetsClient;
12354
12361
  }
12355
12362
  function validateAuth() {
12356
- if (!process.env.GOOGLE_APPLICATION_CREDENTIALS && !process.env.GOOGLE_SERVICE_ACCOUNT_KEY) {
12363
+ const hasFileAuth = !!process.env.GOOGLE_APPLICATION_CREDENTIALS;
12364
+ const hasJsonAuth = !!process.env.GOOGLE_SERVICE_ACCOUNT_KEY;
12365
+ const hasPrivateKeyAuth = !!process.env.GOOGLE_PRIVATE_KEY && !!process.env.GOOGLE_CLIENT_EMAIL;
12366
+ if (!hasFileAuth && !hasJsonAuth && !hasPrivateKeyAuth) {
12357
12367
  throw new Error(
12358
- "No authentication method provided. Please set either:\n- GOOGLE_APPLICATION_CREDENTIALS to the path of your service account key file, or\n- GOOGLE_SERVICE_ACCOUNT_KEY to the JSON string of your service account credentials."
12368
+ "No authentication method provided. Please set one of:\n- GOOGLE_APPLICATION_CREDENTIALS to the path of your service account key file\n- GOOGLE_SERVICE_ACCOUNT_KEY to the JSON string of your service account credentials\n- GOOGLE_PRIVATE_KEY and GOOGLE_CLIENT_EMAIL for direct private key authentication"
12359
12369
  );
12360
12370
  }
12361
- if (!process.env.GOOGLE_APPLICATION_CREDENTIALS && process.env.GOOGLE_SERVICE_ACCOUNT_KEY) {
12371
+ if (!hasFileAuth && !hasJsonAuth && hasPrivateKeyAuth) {
12372
+ if (!process.env.GOOGLE_PRIVATE_KEY) {
12373
+ throw new Error("GOOGLE_PRIVATE_KEY is required when using private key authentication");
12374
+ }
12375
+ if (!process.env.GOOGLE_CLIENT_EMAIL) {
12376
+ throw new Error("GOOGLE_CLIENT_EMAIL is required when using private key authentication");
12377
+ }
12378
+ const privateKey = process.env.GOOGLE_PRIVATE_KEY.replace(/\\n/g, "\n");
12379
+ if (!privateKey.includes("BEGIN PRIVATE KEY") || !privateKey.includes("END PRIVATE KEY")) {
12380
+ throw new Error(
12381
+ "GOOGLE_PRIVATE_KEY appears to be invalid. It should start with -----BEGIN PRIVATE KEY----- and end with -----END PRIVATE KEY-----"
12382
+ );
12383
+ }
12384
+ const emailRegex2 = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
12385
+ if (!emailRegex2.test(process.env.GOOGLE_CLIENT_EMAIL)) {
12386
+ throw new Error(
12387
+ "GOOGLE_CLIENT_EMAIL appears to be invalid. It should be a valid email address (e.g., your-service-account@your-project.iam.gserviceaccount.com)"
12388
+ );
12389
+ }
12390
+ }
12391
+ if (!hasFileAuth && hasJsonAuth) {
12362
12392
  try {
12363
12393
  const credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_KEY);
12364
12394
  if (!credentials.type || credentials.type !== "service_account") {
@@ -12382,7 +12412,7 @@ function validateAuth() {
12382
12412
  throw error;
12383
12413
  }
12384
12414
  }
12385
- if (!process.env.GOOGLE_PROJECT_ID) {
12415
+ if (!process.env.GOOGLE_PROJECT_ID && !hasPrivateKeyAuth) {
12386
12416
  throw new Error(
12387
12417
  "GOOGLE_PROJECT_ID environment variable is not set. Please set it to your Google Cloud project ID, or ensure it is included in your service account credentials."
12388
12418
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-gsheets",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "Model Context Protocol (MCP) server for Google Sheets API integration",
5
5
  "author": "freema",
6
6
  "license": "MIT",