@@ -306,8 +306,21 @@ export class McpServerWrapper {
306306 * Setup Fastify routes for MCP transport
307307 */
308308 setupRoutes ( fastify : FastifyInstance ) : void {
309+ // Get authentication middleware from server instance
310+ const tokenIntrospectionService = ( fastify as any ) . tokenIntrospectionService ;
311+ const activityTracker = ( fastify as any ) . activityTracker ;
312+
309313 // Handle POST requests for client-to-server communication
310- fastify . post ( '/mcp' , async ( request : FastifyRequest , reply : FastifyReply ) => {
314+ fastify . post ( '/mcp' , {
315+ preValidation : async ( request : FastifyRequest , reply : FastifyReply ) => {
316+ // Apply authentication middleware
317+ if ( tokenIntrospectionService && activityTracker ) {
318+ const { requireAuthentication } = await import ( '../middleware/auth-middleware' ) ;
319+ const authMiddleware = requireAuthentication ( tokenIntrospectionService , activityTracker ) ;
320+ await authMiddleware ( request , reply ) ;
321+ }
322+ }
323+ } , async ( request : FastifyRequest , reply : FastifyReply ) => {
311324 const sessionId = request . headers [ 'mcp-session-id' ] as string | undefined ;
312325 let transport : StreamableHTTPServerTransport ;
313326 let server : Server ;
@@ -374,6 +387,11 @@ export class McpServerWrapper {
374387 return ;
375388 }
376389
390+ // Track activity before handling request (redundant now - auth middleware does this)
391+ // const requestBody = request.body as any;
392+ // const isToolCall = requestBody?.method === 'tools/call';
393+ // await this.trackActivity(request, sessionId, isToolCall);
394+
377395 // Handle the request
378396 await transport . handleRequest ( request . raw , reply . raw , request . body ) ;
379397 } ) ;
0 commit comments