@@ -373,6 +373,7 @@ import {
373373 ListWebhooksSchema ,
374374 ListWebhookEventsSchema ,
375375 GetWebhookEventSchema ,
376+ HealthCheckSchema ,
376377} from "./schemas.js" ;
377378
378379import { randomUUID } from "node:crypto" ;
@@ -1254,6 +1255,15 @@ function buildAuthHeaders(): Record<string, string> {
12541255 return { } ;
12551256}
12561257
1258+ function usesJobTokenHeader ( ) : boolean {
1259+ if ( GITLAB_JOB_TOKEN ) return true ;
1260+ if ( REMOTE_AUTHORIZATION || GITLAB_MCP_OAUTH ) {
1261+ const ctx = sessionAuthStore . getStore ( ) ;
1262+ return ctx ?. header === "JOB-TOKEN" ;
1263+ }
1264+ return false ;
1265+ }
1266+
12571267/**
12581268 * Get the effective GitLab API URL for the current request
12591269 * In REMOTE_AUTHORIZATION mode with ENABLE_DYNAMIC_API_URL, reads from session context
@@ -7623,9 +7633,23 @@ async function createCommitStatus(
76237633async function getCurrentUser ( ) : Promise < GitLabUser > {
76247634 const response = await fetch ( `${ getEffectiveApiUrl ( ) } /user` , getFetchConfig ( ) ) ;
76257635
7636+ if ( response . ok ) {
7637+ const data = await response . json ( ) ;
7638+ return GitLabUserSchema . parse ( data ) ;
7639+ }
7640+
7641+ if ( ( response . status === 401 || response . status === 403 ) && usesJobTokenHeader ( ) ) {
7642+ const jobResponse = await fetch ( `${ getEffectiveApiUrl ( ) } /job` , getFetchConfig ( ) ) ;
7643+ if ( jobResponse . ok ) {
7644+ const jobData = await jobResponse . json ( ) as { user ?: { username ?: string ; id ?: number ; name ?: string } } ;
7645+ if ( jobData . user ) {
7646+ return GitLabUserSchema . parse ( jobData . user ) ;
7647+ }
7648+ }
7649+ }
7650+
76267651 await handleGitLabError ( response ) ;
7627- const data = await response . json ( ) ;
7628- return GitLabUserSchema . parse ( data ) ;
7652+ throw new Error ( `GitLab API error: ${ response . status } ${ response . statusText } ` ) ;
76297653}
76307654
76317655/**
@@ -10341,6 +10365,21 @@ async function handleToolCall(params: any) {
1034110365 } ;
1034210366 }
1034310367
10368+ case "health_check ": {
10369+ HealthCheckSchema . parse ( params . arguments ) ;
10370+ const url = new URL ( `${ getEffectiveApiUrl ( ) } /user` ) ;
10371+ const response = await fetch ( url . toString ( ) , getFetchConfig ( ) ) ;
10372+ let authenticated = response . ok ;
10373+ if ( ! authenticated && ( response . status === 401 || response . status === 403 ) && ( GITLAB_JOB_TOKEN || usesJobTokenHeader ( ) ) ) {
10374+ const jobUrl = new URL ( `${ getEffectiveApiUrl ( ) } /job` ) ;
10375+ const jobResponse = await fetch ( jobUrl . toString ( ) , getFetchConfig ( ) ) ;
10376+ authenticated = jobResponse . ok ;
10377+ }
10378+ return {
10379+ content : [ { type : "text" , text : JSON . stringify ( { status : authenticated ? "ok" : "error" , authenticated, gitlab_url : getEffectiveApiUrl ( ) } ) } ] ,
10380+ } ;
10381+ }
10382+
1034410383 default :
1034510384 throw new Error ( `Unknown tool: ${ params . name } ` ) ;
1034610385 }
0 commit comments