11import { and , eq , isNull } from 'drizzle-orm'
2- import { type NextRequest , NextResponse } from 'next/server'
3- import { checkHybridAuth } from '@/lib/auth/hybrid'
2+ import type { NextRequest } from 'next/server'
43import { createLogger } from '@/lib/logs/console/logger'
4+ import { withMcpAuth } from '@/lib/mcp/middleware'
55import { mcpService } from '@/lib/mcp/service'
6- import type { McpApiResponse } from '@/lib/mcp/types'
7- import { getUserEntityPermissions } from '@/lib/permissions/utils'
8- import { generateRequestId } from '@/lib/utils'
6+ import { createMcpErrorResponse , createMcpSuccessResponse } from '@/lib/mcp/utils'
97import { db } from '@/db'
108import { mcpServers } from '@/db/schema'
119
@@ -16,123 +14,86 @@ export const dynamic = 'force-dynamic'
1614/**
1715 * POST - Refresh an MCP server connection (requires any workspace permission)
1816 */
19- export async function POST ( request : NextRequest , { params } : { params : { id : string } } ) {
20- const requestId = generateRequestId ( )
21- const serverId = params . id
17+ export const POST = withMcpAuth ( 'read' ) (
18+ async (
19+ request : NextRequest ,
20+ { userId, workspaceId, requestId } ,
21+ { params } : { params : { id : string } }
22+ ) => {
23+ const serverId = params . id
2224
23- try {
24- const auth = await checkHybridAuth ( request , { requireWorkflowId : false } )
25- if ( ! auth . success || ! auth . userId ) {
26- return NextResponse . json (
27- {
28- success : false ,
29- error : auth . error || 'Authentication required' ,
30- } ,
31- { status : 401 }
32- )
33- }
34-
35- const { searchParams } = new URL ( request . url )
36- const workspaceId = searchParams . get ( 'workspaceId' )
37-
38- if ( ! workspaceId ) {
39- return NextResponse . json (
40- {
41- success : false ,
42- error : 'workspaceId parameter is required' ,
43- } ,
44- { status : 400 }
45- )
46- }
47-
48- // Validate user has permission to refresh MCP servers in this workspace (any permission level)
49- const hasWorkspaceAccess = await getUserEntityPermissions ( auth . userId , 'workspace' , workspaceId )
50- if ( ! hasWorkspaceAccess ) {
51- return NextResponse . json (
25+ try {
26+ logger . info (
27+ `[${ requestId } ] Refreshing MCP server: ${ serverId } in workspace: ${ workspaceId } ` ,
5228 {
53- success : false ,
54- error : 'Access denied to workspace' ,
55- } ,
56- { status : 403 }
29+ userId,
30+ }
5731 )
58- }
5932
60- logger . info ( `[${ requestId } ] Refreshing MCP server: ${ serverId } in workspace: ${ workspaceId } ` , {
61- userId : auth . userId ,
62- } )
63-
64- const [ server ] = await db
65- . select ( )
66- . from ( mcpServers )
67- . where (
68- and (
69- eq ( mcpServers . id , serverId ) ,
70- eq ( mcpServers . workspaceId , workspaceId ) ,
71- isNull ( mcpServers . deletedAt )
33+ const [ server ] = await db
34+ . select ( )
35+ . from ( mcpServers )
36+ . where (
37+ and (
38+ eq ( mcpServers . id , serverId ) ,
39+ eq ( mcpServers . workspaceId , workspaceId ) ,
40+ isNull ( mcpServers . deletedAt )
41+ )
7242 )
73- )
74- . limit ( 1 )
43+ . limit ( 1 )
7544
76- if ( ! server ) {
77- return NextResponse . json (
78- {
79- success : false ,
80- error : 'Server not found or access denied' ,
81- } ,
82- { status : 404 }
83- )
84- }
45+ if ( ! server ) {
46+ return createMcpErrorResponse (
47+ new Error ( 'Server not found or access denied' ) ,
48+ 'Server not found' ,
49+ 404
50+ )
51+ }
8552
86- let connectionStatus : 'connected' | 'disconnected' | 'error' = 'error'
87- let toolCount = 0
88- let lastError : string | null = null
53+ let connectionStatus : 'connected' | 'disconnected' | 'error' = 'error'
54+ let toolCount = 0
55+ let lastError : string | null = null
8956
90- try {
91- const tools = await mcpService . discoverServerTools ( auth . userId , serverId , workspaceId , true ) // Force refresh
92- connectionStatus = 'connected'
93- toolCount = tools . length
94- logger . info (
95- `[${ requestId } ] Successfully connected to server ${ serverId } , discovered ${ toolCount } tools`
96- )
97- } catch ( error ) {
98- connectionStatus = 'error'
99- lastError = error instanceof Error ? error . message : 'Connection test failed'
100- logger . warn ( `[${ requestId } ] Failed to connect to server ${ serverId } :` , error )
101- }
57+ try {
58+ const tools = await mcpService . discoverServerTools ( userId , serverId , workspaceId , true ) // Force refresh
59+ connectionStatus = 'connected'
60+ toolCount = tools . length
61+ logger . info (
62+ `[${ requestId } ] Successfully connected to server ${ serverId } , discovered ${ toolCount } tools`
63+ )
64+ } catch ( error ) {
65+ connectionStatus = 'error'
66+ lastError = error instanceof Error ? error . message : 'Connection test failed'
67+ logger . warn ( `[${ requestId } ] Failed to connect to server ${ serverId } :` , error )
68+ }
10269
103- const [ refreshedServer ] = await db
104- . update ( mcpServers )
105- . set ( {
106- lastToolsRefresh : new Date ( ) ,
107- connectionStatus,
108- lastError,
109- lastConnected : connectionStatus === 'connected' ? new Date ( ) : server . lastConnected ,
110- toolCount,
111- updatedAt : new Date ( ) ,
112- } )
113- . where ( eq ( mcpServers . id , serverId ) )
114- . returning ( )
70+ const [ refreshedServer ] = await db
71+ . update ( mcpServers )
72+ . set ( {
73+ lastToolsRefresh : new Date ( ) ,
74+ connectionStatus,
75+ lastError,
76+ lastConnected : connectionStatus === 'connected' ? new Date ( ) : server . lastConnected ,
77+ toolCount,
78+ updatedAt : new Date ( ) ,
79+ } )
80+ . where ( eq ( mcpServers . id , serverId ) )
81+ . returning ( )
11582
116- const response : McpApiResponse = {
117- success : true ,
118- data : {
83+ logger . info ( `[${ requestId } ] Successfully refreshed MCP server: ${ serverId } ` )
84+ return createMcpSuccessResponse ( {
11985 status : connectionStatus ,
12086 toolCount,
12187 lastConnected : refreshedServer ?. lastConnected ?. toISOString ( ) || null ,
12288 error : lastError ,
123- } ,
89+ } )
90+ } catch ( error ) {
91+ logger . error ( `[${ requestId } ] Error refreshing MCP server:` , error )
92+ return createMcpErrorResponse (
93+ error instanceof Error ? error : new Error ( 'Failed to refresh MCP server' ) ,
94+ 'Failed to refresh MCP server' ,
95+ 500
96+ )
12497 }
125-
126- logger . info ( `[${ requestId } ] Successfully refreshed MCP server: ${ serverId } ` )
127- return NextResponse . json ( response )
128- } catch ( error ) {
129- logger . error ( `[${ requestId } ] Error refreshing MCP server:` , error )
130- return NextResponse . json (
131- {
132- success : false ,
133- error : error instanceof Error ? error . message : 'Failed to refresh MCP server' ,
134- } ,
135- { status : 500 }
136- )
13798 }
138- }
99+ )
0 commit comments