@@ -10,6 +10,7 @@ import { NextResponse } from 'next/server'
1010import { startMcpOauthContract } from '@/lib/api/contracts/mcp'
1111import { parseRequest } from '@/lib/api/server'
1212import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
13+ import { isMcpUrlRefusal } from '@/lib/mcp/domain-check'
1314import { withMcpAuth } from '@/lib/mcp/middleware'
1415import {
1516 assertSafeOauthServerUrl ,
@@ -173,16 +174,20 @@ export const GET = withRouteHandler(
173174 // OAuth discovery + DCR through the guarded fetch, bounded so a transient stall fails
174175 // fast with a labeled log instead of hanging the popup. `McpOauthRedirectRequired` is
175176 // the SUCCESS signal (a throw carrying the authorize URL), and an authorization-server
176- // refusal is an expected outcome rather than a Sim fault, so both are caught INSIDE the
177- // bounded step and returned as values — otherwise timedStep would error-log them. Only
178- // a real error or a timeout escapes as a throw.
177+ // refusal or an egress refusal of a URL the server named is an expected outcome rather
178+ // than a Sim fault, so all are caught INSIDE the bounded step and returned as values —
179+ // otherwise timedStep would error-log them. Only a real error or a timeout escapes as a
180+ // throw.
179181 const authOutcome = await timedStep ( 'mcpAuthGuarded' , MCP_AUTH_STEP_MS , async ( ) => {
180182 try {
181183 return { kind : 'result' as const , value : await mcpAuthGuarded ( provider , { serverUrl } ) }
182184 } catch ( e ) {
183185 if ( e instanceof McpOauthRedirectRequired ) {
184186 return { kind : 'redirect' as const , authorizationUrl : e . authorizationUrl }
185187 }
188+ if ( isMcpUrlRefusal ( e ) ) {
189+ return { kind : 'refused' as const , error : e }
190+ }
186191 if ( isDynamicClientRegistrationUnsupported ( e ) ) {
187192 return {
188193 kind : 'rejected' as const ,
@@ -209,6 +214,13 @@ export const GET = withRouteHandler(
209214 } )
210215 return createMcpErrorResponse ( authOutcome . error , authOutcome . message , 422 )
211216 }
217+ if ( authOutcome . kind === 'refused' ) {
218+ logger . warn ( 'MCP OAuth URL refused by egress policy' , {
219+ serverId,
220+ error : authOutcome . error . message ,
221+ } )
222+ return createMcpErrorResponse ( authOutcome . error , authOutcome . error . message , 400 )
223+ }
212224 if ( authOutcome . value === 'AUTHORIZED' ) {
213225 return NextResponse . json ( { status : 'already_authorized' } )
214226 }
0 commit comments