-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(mcp): Address auth issues with Firebase Studio environment #8871
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
Changes from 3 commits
b990534
0a8f315
c8e607d
e2458f3
54322f6
13a2850
afd223b
beb291b
cac5a1e
4d7203f
ba4a63e
758b7d0
196baca
473a5b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { dirExistsSync } from "./fsutils"; | ||
|
||
let googleIdxFolderExists: boolean | undefined; | ||
export function isFirebaseStudio() { | ||
Check warning on line 4 in src/env.ts
|
||
if (googleIdxFolderExists === true || process.env.MONOSPACE_ENV) return true; | ||
googleIdxFolderExists = dirExistsSync("/google/idx"); | ||
return googleIdxFolderExists; | ||
} | ||
|
||
export function isFirebaseMcp() { | ||
Check warning on line 10 in src/env.ts
|
||
return !!process.env.IS_FIREBASE_MCP; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; | ||
import { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js"; | ||
import { appendFileSync } from "fs"; | ||
|
||
export class LoggingStdioServerTransport extends StdioServerTransport { | ||
path: string; | ||
|
||
constructor(path: string) { | ||
super(); | ||
this.path = path; | ||
appendFileSync(path, "--- new process start ---\n"); | ||
const origOnData = this._ondata; | ||
this._ondata = (chunk: Buffer) => { | ||
origOnData(chunk); | ||
appendFileSync(path, chunk.toString(), { encoding: "utf8" }); | ||
}; | ||
} | ||
|
||
async send(message: JSONRPCMessage) { | ||
await super.send(message); | ||
appendFileSync(this.path, JSON.stringify(message) + "\n"); | ||
} | ||
mbleigh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,9 @@ | |
import { Tokens, TokensWithExpiration, User } from "./types/auth"; | ||
import { setRefreshToken, setActiveAccount, setGlobalDefaultAccount, isExpired } from "./auth"; | ||
import type { Options } from "./options"; | ||
import { isFirebaseMcp, isFirebaseStudio } from "./env"; | ||
import { timeoutError } from "./timeout"; | ||
import { timeouts } from "retry"; | ||
|
||
const AUTH_ERROR_MESSAGE = `Command requires authentication, please run ${clc.bold( | ||
"firebase login", | ||
|
@@ -44,13 +47,19 @@ | |
|
||
let clientEmail; | ||
try { | ||
const credentials = await client.getCredentials(); | ||
const timeoutSeconds = isFirebaseMcp() ? 5000 : 15000; // shorter timeout for MCP | ||
Check failure on line 50 in src/requireAuth.ts
|
||
joehan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const credentials = await timeoutError( | ||
client.getCredentials(), | ||
new FirebaseError( | ||
`Authenticating with default credentials timed out after ${timeouts} seconds. Please try running \`firebase login\` instead.`, | ||
), | ||
); | ||
mbleigh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
clientEmail = credentials.client_email; | ||
} catch (e) { | ||
// Make sure any error here doesn't block the CLI, but log it. | ||
logger.debug(`Error getting account credentials.`); | ||
} | ||
if (process.env.MONOSPACE_ENV && token && clientEmail) { | ||
if (isFirebaseStudio() && token && clientEmail) { | ||
// Within monospace, this a OAuth token for the user, so we make it the active user. | ||
const activeAccount = { | ||
user: { email: clientEmail }, | ||
|
@@ -82,7 +91,10 @@ | |
* if the user is not authenticated | ||
* @param options CLI options. | ||
*/ | ||
export async function requireAuth(options: any): Promise<string | null> { | ||
export async function requireAuth( | ||
options: any, | ||
skipAutoAuth: boolean = false, | ||
): Promise<string | null> { | ||
lastOptions = options; | ||
api.setScopes([scopes.CLOUD_PLATFORM, scopes.FIREBASE_PLATFORM]); | ||
options.authScopes = api.getScopes(); | ||
|
@@ -104,6 +116,8 @@ | |
); | ||
} else if (user && (!isExpired(tokens) || tokens?.refresh_token)) { | ||
logger.debug(`> authorizing via signed-in user (${user.email})`); | ||
} else if (skipAutoAuth) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have monospace specific logic in autoAuth that sets the user from the returned access token as the active user (so that it appears like the user is logged in). I'm concerned about the case where these tokens are present but expired - MCP server will tell them to login, but I think I think we probably need to clean up the auth strategy overall ASAP - what you have here is probably still an improvement over the current state tho |
||
return null; | ||
} else { | ||
try { | ||
return await autoAuth(options, options.authScopes); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Races a promise against a timer, returns a fallback value (without rejecting) when time expires. | ||
*/ | ||
export async function timeoutFallback<T, V>( | ||
promise: Promise<T>, | ||
value: V, | ||
timeoutMillis = 2000, | ||
): Promise<T | V> { | ||
return Promise.race([ | ||
promise, | ||
new Promise<V>((resolve) => setTimeout(() => resolve(value), timeoutMillis)), | ||
]); | ||
} | ||
|
||
export async function timeoutError<T>( | ||
promise: Promise<T>, | ||
error?: string | Error, | ||
timeoutMillis = 5000, | ||
): Promise<T> { | ||
if (typeof error === "string") error = new Error(error); | ||
return Promise.race<T>([ | ||
promise, | ||
new Promise((resolve, reject) => { | ||
setTimeout(() => reject(error || new Error("Operation timed out.")), timeoutMillis); | ||
}), | ||
]); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.