@@ -155,103 +155,107 @@ async function syncToolSchemasToWorkflows(
155155 }
156156}
157157
158- export const POST =
159- withRouteHandler ( withMcpAuth < { id : string } ) >
160- 'read' ( async ( request : NextRequest , { userId, workspaceId, requestId } , { params } ) => {
161- const { id : serverId } = await params
162-
163- try {
164- logger . info ( `[${ requestId } ] Refreshing MCP server: ${ serverId } ` )
165-
166- const [ server ] = await db
167- . select ( )
168- . from ( mcpServers )
169- . where (
170- and (
171- eq ( mcpServers . id , serverId ) ,
172- eq ( mcpServers . workspaceId , workspaceId ) ,
173- isNull ( mcpServers . deletedAt )
158+ export const POST = withRouteHandler (
159+ withMcpAuth < { id : string } > ( 'read' ) (
160+ async ( request : NextRequest , { userId, workspaceId, requestId } , { params } ) => {
161+ const { id : serverId } = await params
162+
163+ try {
164+ logger . info ( `[${ requestId } ] Refreshing MCP server: ${ serverId } ` )
165+
166+ const [ server ] = await db
167+ . select ( )
168+ . from ( mcpServers )
169+ . where (
170+ and (
171+ eq ( mcpServers . id , serverId ) ,
172+ eq ( mcpServers . workspaceId , workspaceId ) ,
173+ isNull ( mcpServers . deletedAt )
174+ )
174175 )
175- )
176- . limit ( 1 )
177-
178- if ( ! server ) {
179- return createMcpErrorResponse (
180- new Error ( 'Server not found or access denied' ) ,
181- 'Server not found' ,
182- 404
183- )
184- }
176+ . limit ( 1 )
177+
178+ if ( ! server ) {
179+ return createMcpErrorResponse (
180+ new Error ( 'Server not found or access denied' ) ,
181+ 'Server not found' ,
182+ 404
183+ )
184+ }
185185
186- let connectionStatus : 'connected' | 'disconnected' | 'error' = 'error'
187- let toolCount = 0
188- let lastError : string | null = null
189- let syncResult : SyncResult = { updatedCount : 0 , updatedWorkflowIds : [ ] }
190- let discoveredTools : McpTool [ ] = [ ]
186+ let connectionStatus : 'connected' | 'disconnected' | 'error' = 'error'
187+ let toolCount = 0
188+ let lastError : string | null = null
189+ let syncResult : SyncResult = { updatedCount : 0 , updatedWorkflowIds : [ ] }
190+ let discoveredTools : McpTool [ ] = [ ]
191+
192+ const currentStatusConfig : McpServerStatusConfig =
193+ ( server . statusConfig as McpServerStatusConfig | null ) ?? {
194+ consecutiveFailures : 0 ,
195+ lastSuccessfulDiscovery : null ,
196+ }
191197
192- const currentStatusConfig : McpServerStatusConfig =
193- ( server . statusConfig as McpServerStatusConfig | null ) ?? {
194- consecutiveFailures : 0 ,
195- lastSuccessfulDiscovery : null ,
198+ try {
199+ discoveredTools = await mcpService . discoverServerTools ( userId , serverId , workspaceId )
200+ connectionStatus = 'connected'
201+ toolCount = discoveredTools . length
202+ logger . info ( `[${ requestId } ] Discovered ${ toolCount } tools from server ${ serverId } ` )
203+
204+ syncResult = await syncToolSchemasToWorkflows (
205+ workspaceId ,
206+ serverId ,
207+ discoveredTools ,
208+ requestId ,
209+ { url : server . url ?? undefined , name : server . name ?? undefined }
210+ )
211+ } catch ( error ) {
212+ connectionStatus = 'error'
213+ lastError =
214+ error instanceof Error
215+ ? error . message . split ( '\n' ) [ 0 ] . slice ( 0 , 200 )
216+ : 'Connection failed'
217+ logger . warn ( `[${ requestId } ] Failed to connect to server ${ serverId } :` , error )
196218 }
197219
198- try {
199- discoveredTools = await mcpService . discoverServerTools ( userId , serverId , workspaceId )
200- connectionStatus = 'connected'
201- toolCount = discoveredTools . length
202- logger . info ( `[${ requestId } ] Discovered ${ toolCount } tools from server ${ serverId } ` )
203-
204- syncResult = await syncToolSchemasToWorkflows (
205- workspaceId ,
206- serverId ,
207- discoveredTools ,
208- requestId ,
209- { url : server . url ?? undefined , name : server . name ?? undefined }
210- )
211- } catch ( error ) {
212- connectionStatus = 'error'
213- lastError =
214- error instanceof Error ? error . message . split ( '\n' ) [ 0 ] . slice ( 0 , 200 ) : 'Connection failed'
215- logger . warn ( `[${ requestId } ] Failed to connect to server ${ serverId } :` , error )
216- }
220+ const now = new Date ( )
221+ const newStatusConfig =
222+ connectionStatus === 'connected'
223+ ? { consecutiveFailures : 0 , lastSuccessfulDiscovery : now . toISOString ( ) }
224+ : {
225+ consecutiveFailures : currentStatusConfig . consecutiveFailures + 1 ,
226+ lastSuccessfulDiscovery : currentStatusConfig . lastSuccessfulDiscovery ,
227+ }
228+
229+ const [ refreshedServer ] = await db
230+ . update ( mcpServers )
231+ . set ( {
232+ lastToolsRefresh : now ,
233+ connectionStatus,
234+ lastError,
235+ lastConnected : connectionStatus === 'connected' ? now : server . lastConnected ,
236+ toolCount,
237+ statusConfig : newStatusConfig ,
238+ updatedAt : now ,
239+ } )
240+ . where ( eq ( mcpServers . id , serverId ) )
241+ . returning ( )
242+
243+ if ( connectionStatus === 'connected' ) {
244+ await mcpService . clearCache ( workspaceId )
245+ }
217246
218- const now = new Date ( )
219- const newStatusConfig =
220- connectionStatus === 'connected'
221- ? { consecutiveFailures : 0 , lastSuccessfulDiscovery : now . toISOString ( ) }
222- : {
223- consecutiveFailures : currentStatusConfig . consecutiveFailures + 1 ,
224- lastSuccessfulDiscovery : currentStatusConfig . lastSuccessfulDiscovery ,
225- }
226-
227- const [ refreshedServer ] = await db
228- . update ( mcpServers )
229- . set ( {
230- lastToolsRefresh : now ,
231- connectionStatus,
232- lastError,
233- lastConnected : connectionStatus === 'connected' ? now : server . lastConnected ,
247+ return createMcpSuccessResponse ( {
248+ status : connectionStatus ,
234249 toolCount,
235- statusConfig : newStatusConfig ,
236- updatedAt : now ,
250+ lastConnected : refreshedServer ?. lastConnected ?. toISOString ( ) || null ,
251+ error : lastError ,
252+ workflowsUpdated : syncResult . updatedCount ,
253+ updatedWorkflowIds : syncResult . updatedWorkflowIds ,
237254 } )
238- . where ( eq ( mcpServers . id , serverId ) )
239- . returning ( )
240-
241- if ( connectionStatus === 'connected' ) {
242- await mcpService . clearCache ( workspaceId )
255+ } catch ( error ) {
256+ logger . error ( `[${ requestId } ] Error refreshing MCP server:` , error )
257+ return createMcpErrorResponse ( toError ( error ) , 'Failed to refresh MCP server' , 500 )
243258 }
244-
245- return createMcpSuccessResponse ( {
246- status : connectionStatus ,
247- toolCount,
248- lastConnected : refreshedServer ?. lastConnected ?. toISOString ( ) || null ,
249- error : lastError ,
250- workflowsUpdated : syncResult . updatedCount ,
251- updatedWorkflowIds : syncResult . updatedWorkflowIds ,
252- } )
253- } catch ( error ) {
254- logger . error ( `[${ requestId } ] Error refreshing MCP server:` , error )
255- return createMcpErrorResponse ( toError ( error ) , 'Failed to refresh MCP server' , 500 )
256259 }
257- } )
260+ )
261+ )
0 commit comments