@google-cloud/cloud-run-mcp 1.5.0 → 1.7.0

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 (46) hide show
  1. package/.c8rc.json +9 -0
  2. package/.github/workflows/lint-checker.yml +17 -0
  3. package/.github/workflows/npm-publish.yml +1 -1
  4. package/.kokoro/kokoro_build.sh +22 -0
  5. package/.kokoro/presubmit.cfg +21 -0
  6. package/.kokoro/run_tests.sh +31 -0
  7. package/CONTRIBUTING.md +24 -9
  8. package/README.md +52 -11
  9. package/REVIEWING.md +20 -0
  10. package/gemini-extension/GEMINI.md +1 -1
  11. package/lib/cloud-api/billing.js +183 -0
  12. package/lib/cloud-api/build.js +200 -0
  13. package/lib/{gcp-api-helpers.js → cloud-api/helpers.js} +81 -35
  14. package/lib/{gcp-projects.js → cloud-api/projects.js} +58 -28
  15. package/lib/cloud-api/registry.js +98 -0
  16. package/lib/{cloud-run-services.js → cloud-api/run.js} +103 -21
  17. package/lib/cloud-api/storage.js +123 -0
  18. package/lib/deployment/constants.js +54 -0
  19. package/lib/deployment/deployer.js +621 -0
  20. package/lib/deployment/helpers.js +194 -0
  21. package/lib/deployment/source-processor.js +124 -0
  22. package/lib/util/archive.js +104 -0
  23. package/lib/util/helpers.js +42 -0
  24. package/mcp-server.js +21 -10
  25. package/package.json +8 -5
  26. package/test/local/cloud-api/build.test.js +436 -0
  27. package/test/local/cloud-api/projects.test.js +18 -0
  28. package/test/local/deployment-helpers.test.js +231 -0
  29. package/test/local/mcp-server-stdio.test.js +59 -0
  30. package/test/local/mcp-server-streamable-http.test.js +61 -0
  31. package/test/local/notifications.test.js +9 -6
  32. package/test/local/source-processor.test.js +290 -0
  33. package/test/local/test-utils.js +51 -0
  34. package/test/local/tools.test.js +114 -70
  35. package/test/need-gcp/cloud-run-services.test.js +44 -44
  36. package/test/need-gcp/{cloud-run-deploy.test.js → deployer.test.js} +3 -56
  37. package/test/need-gcp/gcp-auth-check.test.js +1 -1
  38. package/test/need-gcp/gcp-projects.test.js +3 -15
  39. package/test/need-gcp/test-helpers.js +175 -0
  40. package/test/need-gcp/workflows/deployment-workflows.test.js +100 -0
  41. package/tools/register-tools.js +106 -97
  42. package/tools/tools.js +2 -9
  43. package/lib/cloud-run-deploy.js +0 -937
  44. package/lib/gcp-billing-accounts.js +0 -98
  45. /package/lib/{gcp-auth-check.js → cloud-api/auth.js} +0 -0
  46. /package/lib/{gcp-metadata.js → cloud-api/metadata.js} +0 -0
@@ -15,42 +15,74 @@ limitations under the License.
15
15
  */
16
16
 
17
17
  import protofiles from 'google-proto-files';
18
- import { callWithRetry, ensureApisEnabled } from './gcp-api-helpers.js';
18
+ import { callWithRetry, ensureApisEnabled } from './helpers.js';
19
+ import { logAndProgress } from '../util/helpers.js';
19
20
 
20
21
  let runClient;
22
+ let serviceUsageClient;
21
23
  let loggingClient;
22
24
 
25
+ async function listCloudRunLocations(projectId) {
26
+ const listLocationsRequest = {
27
+ name: `projects/${projectId}`,
28
+ };
29
+
30
+ const availableLocations = [];
31
+ try {
32
+ console.log('Listing Cloud Run supported locations:');
33
+ const iterable = runClient.listLocationsAsync(listLocationsRequest);
34
+ for await (const location of iterable) {
35
+ if (location.labels.initialized) {
36
+ console.log(`${location.locationId}: ${location.name}`);
37
+ availableLocations.push(location.locationId);
38
+ }
39
+ }
40
+ } catch (err) {
41
+ console.error('Error listing locations:', err);
42
+ throw err;
43
+ }
44
+ return availableLocations;
45
+ }
46
+
23
47
  /**
24
- * Lists all Cloud Run services in a given project and location.
48
+ * Lists all Cloud Run services in a given project.
25
49
  * @param {string} projectId - The Google Cloud project ID.
26
- * @param {string} location - The Google Cloud location (e.g., 'europe-west1').
27
- * @returns {Promise<Array<object>>} - A promise that resolves to an array of service objects.
50
+ * @returns {Promise<object>} - A promise that resolves to an object mapping region to list of service objects in that region.
28
51
  */
29
- export async function listServices(projectId, location) {
30
- // TODO make location optional, if not provided, list all services from all regions
52
+ export async function listServices(projectId) {
31
53
  if (!runClient) {
32
54
  const { v2 } = await import('@google-cloud/run');
33
55
  const { ServicesClient } = v2;
56
+ const { ServiceUsageClient } = await import('@google-cloud/service-usage');
34
57
  runClient = new ServicesClient({ projectId });
58
+ serviceUsageClient = new ServiceUsageClient({ projectId });
35
59
  }
60
+ const context = {
61
+ runClient: runClient,
62
+ serviceUsageClient: serviceUsageClient,
63
+ };
64
+ await ensureApisEnabled(context, projectId, ['run.googleapis.com']);
65
+ const locations = await listCloudRunLocations(projectId);
36
66
 
37
- await ensureApisEnabled(projectId, ['run.googleapis.com']);
67
+ const allServices = {};
68
+ for (const location of locations) {
69
+ const parent = runClient.locationPath(projectId, location);
38
70
 
39
- const parent = runClient.locationPath(projectId, location);
40
-
41
- try {
42
- console.log(
43
- `Listing Cloud Run services in project ${projectId}, location ${location}...`
44
- );
45
- const [services] = await callWithRetry(
46
- () => runClient.listServices({ parent }),
47
- 'listServices'
48
- );
49
- return services;
50
- } catch (error) {
51
- console.error(`Error listing Cloud Run services:`, error);
52
- throw error;
71
+ try {
72
+ console.log(
73
+ `Listing Cloud Run services in project ${projectId}, location ${location}...`
74
+ );
75
+ const [services] = await callWithRetry(
76
+ () => runClient.listServices({ parent }),
77
+ 'listServices'
78
+ );
79
+ allServices[location] = services;
80
+ } catch (error) {
81
+ console.error(`Error listing Cloud Run services:`, error);
82
+ throw error;
83
+ }
53
84
  }
85
+ return allServices;
54
86
  }
55
87
 
56
88
  /**
@@ -191,3 +223,53 @@ function formatLogEntry(entry) {
191
223
  }
192
224
  return `[${timestampStr}] [${severity}] ${responseData} ${data}`;
193
225
  }
226
+
227
+ /**
228
+ * Checks if a Cloud Run service already exists.
229
+ *
230
+ * @async
231
+ * @param {object} context - The context object containing clients and other parameters.
232
+ * @param {string} projectId - The Google Cloud project ID.
233
+ * @param {string} location - The Google Cloud region where the service is located.
234
+ * @param {string} serviceId - The ID of the Cloud Run service.
235
+ * @param {function(object): void} [progressCallback] - Optional callback for progress updates.
236
+ * @returns {Promise<boolean>} A promise that resolves to true if the service exists, false otherwise.
237
+ * @throws {Error} If there's an error checking the service (other than not found).
238
+ */
239
+ export async function checkCloudRunServiceExists(
240
+ context,
241
+ projectId,
242
+ location,
243
+ serviceId,
244
+ progressCallback
245
+ ) {
246
+ const parent = context.runClient.locationPath(projectId, location);
247
+ const servicePath = context.runClient.servicePath(
248
+ projectId,
249
+ location,
250
+ serviceId
251
+ );
252
+ try {
253
+ await callWithRetry(
254
+ () => context.runClient.getService({ name: servicePath }),
255
+ `getService ${serviceId}`
256
+ );
257
+ await logAndProgress(
258
+ `Cloud Run service ${serviceId} already exists.`,
259
+ progressCallback
260
+ );
261
+ return true;
262
+ } catch (error) {
263
+ if (error.code === 5) {
264
+ await logAndProgress(
265
+ `Cloud Run service ${serviceId} does not exist.`,
266
+ progressCallback
267
+ );
268
+ return false;
269
+ }
270
+ const errorMessage = `Error checking Cloud Run service ${serviceId}: ${error.message}`;
271
+ console.error(`Error checking Cloud Run service ${serviceId}:`, error);
272
+ await logAndProgress(errorMessage, progressCallback, 'error');
273
+ throw error;
274
+ }
275
+ }
@@ -0,0 +1,123 @@
1
+ /*
2
+ Copyright 2025 Google LLC
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ import { callWithRetry } from './helpers.js';
18
+ import { logAndProgress } from '../util/helpers.js';
19
+
20
+ /**
21
+ * Ensures that a Google Cloud Storage bucket exists.
22
+ * If the bucket does not exist, it attempts to create it in the specified location.
23
+ *
24
+ * @async
25
+ * @param {object} context - The context object containing clients and other parameters.
26
+ * @param {string} bucketName - The name of the storage bucket.
27
+ * @param {string} [location='us'] - The location to create the bucket in if it doesn't exist. Defaults to 'us'.
28
+ * @param {function(object): void} [progressCallback] - Optional callback for progress updates.
29
+ * @returns {Promise<import('@google-cloud/storage').Bucket>} A promise that resolves with the GCS Bucket object.
30
+ * @throws {Error} If there's an error checking or creating the bucket.
31
+ */
32
+ export async function ensureStorageBucketExists(
33
+ context,
34
+ bucketName,
35
+ location = 'us',
36
+ progressCallback
37
+ ) {
38
+ const bucket = context.storage.bucket(bucketName);
39
+ try {
40
+ const [exists] = await callWithRetry(
41
+ () => bucket.exists(),
42
+ `storage.bucket.exists ${bucketName}`
43
+ );
44
+ if (exists) {
45
+ await logAndProgress(
46
+ `Bucket ${bucketName} already exists.`,
47
+ progressCallback
48
+ );
49
+ return bucket;
50
+ } else {
51
+ await logAndProgress(
52
+ `Bucket ${bucketName} does not exist. Creating in location ${location}...`,
53
+ progressCallback
54
+ );
55
+ try {
56
+ const [createdBucket] = await callWithRetry(
57
+ () =>
58
+ context.storage.createBucket(bucketName, { location: location }),
59
+ `storage.createBucket ${bucketName}`
60
+ );
61
+ await logAndProgress(
62
+ `Storage bucket ${createdBucket.name} created successfully in ${location}.`,
63
+ progressCallback
64
+ );
65
+ return createdBucket;
66
+ } catch (createError) {
67
+ const errorMessage = `Failed to create storage bucket ${bucketName}. Error details: ${createError.message}`;
68
+ console.error(
69
+ `Failed to create storage bucket ${bucketName}. Error details:`,
70
+ createError
71
+ );
72
+ await logAndProgress(errorMessage, progressCallback, 'error');
73
+ throw createError;
74
+ }
75
+ }
76
+ } catch (error) {
77
+ const errorMessage = `Error checking/creating bucket ${bucketName}: ${error.message}`;
78
+ console.error(`Error checking/creating bucket ${bucketName}:`, error);
79
+ await logAndProgress(errorMessage, progressCallback, 'error');
80
+ throw error;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Uploads a buffer to a specified Google Cloud Storage bucket and blob name.
86
+ *
87
+ * @async
88
+ * @param {object} context - The context object containing clients and other parameters.
89
+ * @param {import('@google-cloud/storage').Bucket} bucket - The Google Cloud Storage bucket object.
90
+ * @param {Buffer} buffer - The buffer containing the data to upload.
91
+ * @param {string} destinationBlobName - The name for the blob in the bucket.
92
+ * @param {function(object): void} [progressCallback] - Optional callback for progress updates.
93
+ * @returns {Promise<import('@google-cloud/storage').File>} A promise that resolves with the GCS File object representing the uploaded blob.
94
+ * @throws {Error} If the upload fails.
95
+ */
96
+ export async function uploadToStorageBucket(
97
+ context,
98
+ bucket,
99
+ buffer,
100
+ destinationBlobName,
101
+ progressCallback
102
+ ) {
103
+ try {
104
+ await logAndProgress(
105
+ `Uploading buffer to gs://${bucket.name}/${destinationBlobName}...`,
106
+ progressCallback
107
+ );
108
+ await callWithRetry(
109
+ () => bucket.file(destinationBlobName).save(buffer),
110
+ `storage.bucket.file.save ${destinationBlobName}`
111
+ );
112
+ await logAndProgress(
113
+ `File ${destinationBlobName} uploaded successfully to gs://${bucket.name}/${destinationBlobName}.`,
114
+ progressCallback
115
+ );
116
+ return bucket.file(destinationBlobName);
117
+ } catch (error) {
118
+ const errorMessage = `Error uploading buffer: ${error.message}`;
119
+ console.error(`Error uploading buffer:`, error);
120
+ await logAndProgress(errorMessage, progressCallback, 'error');
121
+ throw error;
122
+ }
123
+ }
@@ -0,0 +1,54 @@
1
+ /*
2
+ Copyright 2025 Google LLC
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ export const DEPLOYMENT_CONFIG = {
18
+ REPO_NAME: 'mcp-cloud-run-deployments',
19
+ ZIP_FILE_NAME: 'source.zip',
20
+ TARGZ_FILE_NAME: 'source.tar.gz',
21
+ IMAGE_TAG: 'latest',
22
+ LABEL_CREATED_BY: 'cloud-run-mcp',
23
+ DEFAULT_NODE_BASE_IMAGE: 'nodejs22',
24
+ NO_BUILD_IMAGE_TYPE: 'scratch',
25
+ };
26
+
27
+ export const TEMP_PATHS = {
28
+ BASE: '.cloud-run-mcp',
29
+ SUBDIR: 'source',
30
+ };
31
+
32
+ export const REQUIRED_APIS = {
33
+ SOURCE_DEPLOY: [
34
+ 'serviceusage.googleapis.com',
35
+ 'iam.googleapis.com',
36
+ 'storage.googleapis.com',
37
+ 'cloudbuild.googleapis.com',
38
+ 'artifactregistry.googleapis.com',
39
+ 'run.googleapis.com',
40
+ ],
41
+ IMAGE_DEPLOY: ['serviceusage.googleapis.com', 'run.googleapis.com'],
42
+ };
43
+
44
+ export const DEPLOYMENT_TYPES = {
45
+ NO_BUILD: 'no-build',
46
+ IMAGE: 'image',
47
+ WITH_BUILD: 'with-build',
48
+ };
49
+
50
+ export const RUNTIMES = {
51
+ NODEJS: 'nodejs',
52
+ };
53
+
54
+ export const MAX_ALLOWED_DIRECT_SOURCE_SIZE_BYTES = 250 * 1024 * 1024; // 250 MiB