Skip to content

Commit 8c42955

Browse files
feat: add authentication status updates via MCP logging (#172)
* feat: add authentication status updates via MCP logging * Remove a commented out line.
1 parent 5908bab commit 8c42955

2 files changed

Lines changed: 28 additions & 11 deletions

File tree

workspace-server/src/auth/AuthManager.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ interface OauthWebLogin {
3232
export class AuthManager {
3333
private client: Auth.OAuth2Client | null = null;
3434
private scopes: string[];
35+
private onStatusUpdate: ((message: string) => void) | null = null;
3536

3637
constructor(scopes: string[]) {
3738
this.scopes = scopes;
3839
}
3940

41+
public setOnStatusUpdate(callback: (message: string) => void) {
42+
this.onStatusUpdate = callback;
43+
}
44+
4045
private isTokenExpiringSoon(credentials: Auth.Credentials): boolean {
4146
return !!(credentials.expiry_date &&
4247
credentials.expiry_date < Date.now() + TOKEN_EXPIRY_BUFFER_MS);
@@ -156,16 +161,20 @@ export class AuthManager {
156161

157162
const webLogin = await this.authWithWeb(oAuth2Client);
158163
await open(webLogin.authUrl);
159-
console.log('Waiting for authentication...');
164+
const msg = 'Waiting for authentication... Check your browser.';
165+
logToFile(msg);
166+
if (this.onStatusUpdate) {
167+
this.onStatusUpdate(msg);
168+
}
160169

161170
// Add timeout to prevent infinite waiting when browser tab gets stuck
162171
const authTimeout = 5 * 60 * 1000; // 5 minutes timeout
163172
const timeoutPromise = new Promise<never>((_, reject) => {
164173
setTimeout(() => {
165174
reject(
166175
new Error(
167-
'Authentication timed out after 5 minutes. The browser tab may have gotten stuck in a loading state. ' +
168-
'Please try again.',
176+
'User is not authenticated. Authentication timed out after 5 minutes. The user did not complete the login process in the browser. ' +
177+
'Please ask the user to check their browser and try again.',
169178
),
170179
);
171180
}, authTimeout);

workspace-server/src/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,21 @@ async function main() {
5858
}
5959

6060
const authManager = new AuthManager(SCOPES);
61-
// Trigger auth flow immediately on startup
62-
await authManager.getAuthenticatedClient();
61+
62+
// 2. Create the server instance
63+
const server = new McpServer({
64+
name: "google-workspace-server",
65+
version,
66+
});
67+
68+
authManager.setOnStatusUpdate((message) => {
69+
server.sendLoggingMessage({
70+
level: 'info',
71+
data: message
72+
}).catch(err => {
73+
console.error('Failed to send logging message:', err);
74+
});
75+
});
6376

6477
const driveService = new DriveService(authManager);
6578
const docsService = new DocsService(authManager, driveService);
@@ -71,12 +84,7 @@ async function main() {
7184
const slidesService = new SlidesService(authManager);
7285
const sheetsService = new SheetsService(authManager);
7386

74-
// 2. Create the server instance
75-
const server = new McpServer({
76-
name: "google-workspace-server",
77-
version,
78-
});
79-
87+
8088
// 3. Register tools directly on the server
8189
server.registerTool(
8290
"auth.clear",

0 commit comments

Comments
 (0)