@mixio-pro/kalaasetu-mcp 1.0.13 → 1.0.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mixio-pro/kalaasetu-mcp",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "A powerful Model Context Protocol server providing AI tools for content generation and analysis",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -1,6 +1,10 @@
1
1
  import { GoogleAuth } from "google-auth-library";
2
2
  import type { StorageProvider } from "./interface";
3
3
  import * as path from "path";
4
+ import {
5
+ getGoogleAuthClient,
6
+ getGoogleAccessToken,
7
+ } from "../utils/google-auth";
4
8
 
5
9
  export class GCSStorageProvider implements StorageProvider {
6
10
  private bucket: string;
@@ -8,7 +12,7 @@ export class GCSStorageProvider implements StorageProvider {
8
12
 
9
13
  constructor(bucket: string) {
10
14
  this.bucket = bucket;
11
- this.auth = new GoogleAuth({
15
+ this.auth = getGoogleAuthClient({
12
16
  scopes: ["https://www.googleapis.com/auth/cloud-platform"],
13
17
  });
14
18
  }
@@ -26,12 +30,12 @@ export class GCSStorageProvider implements StorageProvider {
26
30
  }
27
31
 
28
32
  private async getAccessToken(): Promise<string> {
29
- const client = await this.auth.getClient();
30
- const token = await client.getAccessToken();
31
- if (!token.token) {
32
- throw new Error("Failed to get GCS access token");
33
- }
34
- return token.token;
33
+ // Use the centralized helper which includes gcloud fallback if needed,
34
+ // although GCSStorageProvider primarily relies on the initialized GoogleAuth.
35
+ // However, if we want to support the same fallback logic as tools:
36
+ return getGoogleAccessToken({
37
+ scopes: ["https://www.googleapis.com/auth/cloud-platform"],
38
+ });
35
39
  }
36
40
 
37
41
  async readFile(filePath: string): Promise<Buffer> {
@@ -5,50 +5,12 @@ import { z } from "zod";
5
5
  import { getStorage } from "../storage";
6
6
  import { generateTimestampedFilename } from "../utils/filename";
7
7
 
8
+ import { getGoogleAccessToken } from "../utils/google-auth";
9
+
8
10
  async function wait(ms: number): Promise<void> {
9
11
  return new Promise((resolve) => setTimeout(resolve, ms));
10
12
  }
11
13
 
12
- async function fetchAccessToken(): Promise<string> {
13
- try {
14
- const auth = new GoogleAuth({
15
- scopes: ["https://www.googleapis.com/auth/cloud-platform"],
16
- });
17
- const client = await auth.getClient();
18
- const token = await client.getAccessToken();
19
- if (!token || typeof token !== "string") {
20
- throw new Error("No token from GoogleAuth");
21
- }
22
- return token;
23
- } catch (e) {
24
- // Fallback to gcloud
25
- return await new Promise((resolve, reject) => {
26
- exec("gcloud auth print-access-token", (err, stdout, stderr) => {
27
- if (err) {
28
- reject(
29
- new Error(
30
- `Failed to fetch an access token (ADC and gcloud): ${
31
- stderr || err.message
32
- }`
33
- )
34
- );
35
- return;
36
- }
37
- const t = (stdout || "").trim();
38
- if (!t) {
39
- reject(
40
- new Error(
41
- "Failed to fetch an access token: empty token from gcloud"
42
- )
43
- );
44
- return;
45
- }
46
- resolve(t);
47
- });
48
- });
49
- }
50
- }
51
-
52
14
  async function fileToBase64(
53
15
  filePath: string
54
16
  ): Promise<{ data: string; mimeType: string }> {
@@ -191,7 +153,7 @@ export const imageToVideo = {
191
153
  durationSeconds = 8;
192
154
  }
193
155
 
194
- const token = await fetchAccessToken();
156
+ const token = await getGoogleAccessToken();
195
157
 
196
158
  const url = `https://${location}-aiplatform.googleapis.com/v1/projects/${projectId}/locations/${location}/publishers/google/models/${modelId}:predictLongRunning`;
197
159
 
@@ -0,0 +1,83 @@
1
+ import { GoogleAuth } from "google-auth-library";
2
+ import { exec } from "child_process";
3
+
4
+ export interface GoogleAuthOptions {
5
+ scopes?: string | string[];
6
+ }
7
+
8
+ /**
9
+ * Returns a GoogleAuth client, prioritizing GOOGLE_SERVICE_ACCOUNT_JSON env var.
10
+ */
11
+ export function getGoogleAuthClient(
12
+ options: GoogleAuthOptions = {}
13
+ ): GoogleAuth {
14
+ const opts: any = {
15
+ scopes: options.scopes || [
16
+ "https://www.googleapis.com/auth/cloud-platform",
17
+ ],
18
+ };
19
+
20
+ if (process.env.GOOGLE_SERVICE_ACCOUNT_JSON) {
21
+ try {
22
+ const credentials = JSON.parse(process.env.GOOGLE_SERVICE_ACCOUNT_JSON);
23
+ opts.credentials = credentials;
24
+ if (credentials.project_id) {
25
+ opts.projectId = credentials.project_id;
26
+ }
27
+ } catch (error) {
28
+ console.warn(
29
+ "Failed to parse GOOGLE_SERVICE_ACCOUNT_JSON, falling back to other methods:",
30
+ error
31
+ );
32
+ }
33
+ } else if (process.env.GOOGLE_SERVICE_ACCOUNT_JSON_PATH) {
34
+ // GoogleAuth natively supports keyFile/keyFilename
35
+ opts.keyFile = process.env.GOOGLE_SERVICE_ACCOUNT_JSON_PATH;
36
+ }
37
+
38
+ return new GoogleAuth(opts);
39
+ }
40
+
41
+ /**
42
+ * Returns an access token, trying GoogleAuth first (JSON > ADC), then gcloud CLI.
43
+ */
44
+ export async function getGoogleAccessToken(
45
+ options: GoogleAuthOptions = {}
46
+ ): Promise<string> {
47
+ try {
48
+ const auth = getGoogleAuthClient(options);
49
+ const client = await auth.getClient();
50
+ const token = await client.getAccessToken();
51
+ if (token.token) {
52
+ return token.token;
53
+ }
54
+ throw new Error("No token returned from GoogleAuth");
55
+ } catch (e) {
56
+ // Fallback to gcloud
57
+ console.warn("GoogleAuth failed, falling back to gcloud CLI:", e);
58
+ return await new Promise((resolve, reject) => {
59
+ exec("gcloud auth print-access-token", (err, stdout, stderr) => {
60
+ if (err) {
61
+ reject(
62
+ new Error(
63
+ `Failed to fetch an access token (Auth Library and gcloud): ${
64
+ stderr || err.message
65
+ }`
66
+ )
67
+ );
68
+ return;
69
+ }
70
+ const t = (stdout || "").trim();
71
+ if (!t) {
72
+ reject(
73
+ new Error(
74
+ "Failed to fetch an access token: empty token from gcloud"
75
+ )
76
+ );
77
+ return;
78
+ }
79
+ resolve(t);
80
+ });
81
+ });
82
+ }
83
+ }