@@ -12,12 +12,13 @@ import {
1212} from '@/lib/mcp/middleware'
1313import { McpOauthRedirectRequired } from '@/lib/mcp/oauth'
1414import { mcpService } from '@/lib/mcp/service'
15+ import { McpOauthAuthorizationRequiredError , type McpToolDiscoveryResponse } from '@/lib/mcp/types'
1516import {
16- McpConnectionError ,
17- McpOauthAuthorizationRequiredError ,
18- type McpToolDiscoveryResponse ,
19- } from '@/lib/mcp/types'
20- import { categorizeError , createMcpErrorResponse , createMcpSuccessResponse } from '@/lib/mcp/utils'
17+ categorizeError ,
18+ createMcpErrorResponse ,
19+ createMcpSuccessResponse ,
20+ isMcpServerUnavailableError ,
21+ } from '@/lib/mcp/utils'
2122
2223const logger = createLogger ( 'McpToolDiscoveryAPI' )
2324const MCP_REFRESH_DISCOVERY_CONCURRENCY = 5
@@ -28,26 +29,30 @@ export const dynamic = 'force-dynamic'
2829 * Renders a discovery failure that is not an authorization problem.
2930 *
3031 * Discovery talks to a server the workspace registered, so a server that cannot be
31- * reached, times out, or is cooling down after a recent failure is a fault of that
32- * server rather than of Sim: it answers 422 (the request was valid; the dependency it
33- * names cannot serve it), the status this API family already uses for an upstream that
34- * cannot complete the operation, and logs at warn. Anything else keeps
35- * {@link categorizeError}'s status and logs at error only when that status is a 5xx.
32+ * reached, times out on connect or on `tools/list`, answers with a protocol or HTTP
33+ * error, or is cooling down after a recent failure is a fault of that server rather
34+ * than of Sim: it answers 422 (the request was valid; the dependency it names cannot
35+ * serve it), the status this API family already uses for an upstream that cannot
36+ * complete the operation, and logs at warn. Anything else keeps {@link categorizeError}'s
37+ * status and logs at error only when that status is a 5xx.
3638 */
37- function discoveryFailureResponse ( requestId : string , error : unknown , defaultMessage : string ) {
38- if ( error instanceof McpConnectionError ) {
39+ function discoveryFailureResponse ( requestId : string , error : unknown ) {
40+ if ( isMcpServerUnavailableError ( error ) ) {
3941 logger . warn ( `[${ requestId } ] MCP server unavailable for tool discovery` , {
4042 error : getErrorMessage ( error ) ,
4143 } )
42- return createMcpErrorResponse ( error , defaultMessage , 422 )
44+ return createMcpErrorResponse ( error , 'Failed to discover MCP tools' , 422 )
4345 }
4446 const { message, status } = categorizeError ( error )
4547 if ( status >= 500 ) {
46- logger . error ( `[${ requestId } ] ${ defaultMessage } :` , error )
48+ logger . error ( `[${ requestId } ] Error discovering MCP tools :` , error )
4749 } else {
48- logger . warn ( `[${ requestId } ] ${ defaultMessage } ` , { error : getErrorMessage ( error ) , status } )
50+ logger . warn ( `[${ requestId } ] Failed to discover MCP tools` , {
51+ error : getErrorMessage ( error ) ,
52+ status,
53+ } )
4954 }
50- return createMcpErrorResponse ( new Error ( message ) , defaultMessage , status )
55+ return createMcpErrorResponse ( new Error ( message ) , 'Failed to discover MCP tools' , status )
5156}
5257
5358async function settleWithConcurrency < T , R > (
@@ -134,7 +139,7 @@ export const GET = withRouteHandler(
134139 ) {
135140 return createMcpErrorResponse ( error , 'OAuth re-authorization required' , 401 )
136141 }
137- return discoveryFailureResponse ( requestId , error , 'Failed to discover MCP tools' )
142+ return discoveryFailureResponse ( requestId , error )
138143 }
139144 } )
140145)
@@ -199,7 +204,9 @@ export const POST = withRouteHandler(
199204 ) {
200205 return createMcpErrorResponse ( error , 'OAuth re-authorization required' , 401 )
201206 }
202- return discoveryFailureResponse ( requestId , error , 'Failed to refresh tool discovery' )
207+ logger . error ( `[${ requestId } ] Error refreshing tool discovery:` , error )
208+ const { message, status } = categorizeError ( error )
209+ return createMcpErrorResponse ( new Error ( message ) , 'Failed to refresh tool discovery' , status )
203210 }
204211 } )
205212)
0 commit comments