@@ -15,6 +15,7 @@ import { MCPProxy } from './mcp-proxy';
1515import { SubprocessManager } from './subprocess-manager' ;
1616import { extractAllVariables } from '../shared/variable-resolver' ;
1717import { VERSION } from '../version' ;
18+ import { logger } from '../shared/logger' ;
1819
1920export interface ToolValidationResult {
2021 toolId : string ;
@@ -35,6 +36,7 @@ export class CapaMCPServer {
3536 private projectPath : string ;
3637 private sessionId : string | null = null ;
3738 private toolSchemaCache : Map < string , MCPTool > = new Map ( ) ;
39+ private logger = logger . child ( 'MCPHandler' ) ;
3840
3941 constructor (
4042 db : CapaDatabase ,
@@ -391,7 +393,7 @@ export class CapaMCPServer {
391393 const serverDef = capabilities . servers . find ( ( s ) => s . id === serverId ) ;
392394
393395 if ( ! serverDef ) {
394- console . error ( ` Server not found for tool ${ tool . id } : ${ serverId } `) ;
396+ this . logger . failure ( ` Server not found for tool ${ tool . id } : ${ serverId } `) ;
395397 const mcpTool : MCPTool = {
396398 name : tool . id ,
397399 description : `MCP tool: ${ tool . id } (server not found)` ,
@@ -412,7 +414,7 @@ export class CapaMCPServer {
412414 const remoteTool = remoteTools . find ( ( t : any ) => t . name === mcpDef . tool ) ;
413415
414416 if ( remoteTool ) {
415- console . log ( ` ✓ Fetched schema for ${ tool . id } from ${ serverId } `) ;
417+ this . logger . debug ( ` Fetched schema for ${ tool . id } from ${ serverId } `) ;
416418 const mcpTool : MCPTool = {
417419 name : tool . id ,
418420 description : remoteTool . description || `MCP tool: ${ tool . id } ` ,
@@ -424,7 +426,7 @@ export class CapaMCPServer {
424426 this . toolSchemaCache . set ( tool . id , mcpTool ) ;
425427 return mcpTool ;
426428 } else {
427- console . warn ( ` ⚠ Tool ${ mcpDef . tool } not found on server ${ serverId } ` ) ;
429+ this . logger . warn ( `Tool ${ mcpDef . tool } not found on server ${ serverId } ` ) ;
428430 const mcpTool : MCPTool = {
429431 name : tool . id ,
430432 description : `MCP tool: ${ tool . id } (not found on remote server)` ,
@@ -437,7 +439,7 @@ export class CapaMCPServer {
437439 return mcpTool ;
438440 }
439441 } catch ( error : any ) {
440- console . error ( ` ✗ Failed to fetch schema for ${ tool . id } :`, error . message ) ;
442+ this . logger . failure ( ` Failed to fetch schema for ${ tool . id } :`, error . message ) ;
441443 const mcpTool : MCPTool = {
442444 name : tool . id ,
443445 description : `MCP tool: ${ tool . id } ` ,
@@ -462,11 +464,11 @@ export class CapaMCPServer {
462464 async handleMessage ( message : any ) : Promise < any > {
463465 // Handle initialization
464466 if ( message . method === 'initialize' ) {
465- console . log ( ` [MCP Handler] Initialize request` ) ;
467+ this . logger . info ( ' Initialize request' ) ;
466468 // Create session for this connection
467469 const session = this . sessionManager . createSession ( this . projectId ) ;
468470 this . sessionId = session . sessionId ;
469- console . log ( ` Session ID: ${ this . sessionId } `) ;
471+ this . logger . debug ( ` Session ID: ${ this . sessionId } `) ;
470472
471473 return {
472474 jsonrpc : '2.0' ,
@@ -486,7 +488,7 @@ export class CapaMCPServer {
486488
487489 // Handle initialized notification
488490 if ( message . method === 'notifications/initialized' ) {
489- console . log ( ` [MCP Handler] Initialized notification` ) ;
491+ this . logger . debug ( ' Initialized notification' ) ;
490492 return {
491493 jsonrpc : '2.0' ,
492494 result : { } ,
@@ -495,19 +497,19 @@ export class CapaMCPServer {
495497
496498 // Handle tools/list
497499 if ( message . method === 'tools/list' ) {
498- console . log ( ` [MCP Handler] List tools request` ) ;
500+ this . logger . info ( ' List tools request' ) ;
499501 const tools : MCPTool [ ] = [ ] ;
500502 const capabilities = this . sessionManager . getProjectCapabilities ( this . projectId ) ;
501503
502504 // Determine tool exposure mode (default to 'expose-all')
503505 const toolExposureMode = capabilities ?. options ?. toolExposure || 'expose-all' ;
504- console . log ( ` Tool exposure mode: ${ toolExposureMode } `) ;
506+ this . logger . debug ( ` Tool exposure mode: ${ toolExposureMode } `) ;
505507
506508 if ( toolExposureMode === 'expose-all' ) {
507509 // Expose-all mode: Show all tools from all skills immediately
508510 if ( capabilities ) {
509511 const allToolIds = this . sessionManager . getAllRequiredToolsForProject ( this . projectId ) ;
510- console . log ( ` Exposing all ${ allToolIds . length } tool(s) from all skills`) ;
512+ this . logger . debug ( ` Exposing all ${ allToolIds . length } tool(s) from all skills`) ;
511513 for ( const toolId of allToolIds ) {
512514 const tool = capabilities . tools . find ( ( t ) => t . id === toolId ) ;
513515 if ( tool ) {
@@ -539,7 +541,7 @@ export class CapaMCPServer {
539541 if ( this . sessionId ) {
540542 const session = this . sessionManager . getSession ( this . sessionId ) ;
541543 if ( session && session . activeSkills . length > 0 && capabilities ) {
542- console . log ( ` Adding ${ session . availableTools . length } tool(s) from active skills`) ;
544+ this . logger . debug ( ` Adding ${ session . availableTools . length } tool(s) from active skills`) ;
543545 for ( const toolId of session . availableTools ) {
544546 const tool = capabilities . tools . find ( ( t ) => t . id === toolId ) ;
545547 if ( tool ) {
@@ -551,7 +553,7 @@ export class CapaMCPServer {
551553 }
552554 }
553555
554- console . log ( ` Returning ${ tools . length } tool(s): ${ tools . map ( t => t . name ) . join ( ', ' ) } `) ;
556+ this . logger . info ( ` Returning ${ tools . length } tool(s): ${ tools . map ( t => t . name ) . join ( ', ' ) } `) ;
555557 return {
556558 jsonrpc : '2.0' ,
557559 id : message . id ,
@@ -564,8 +566,8 @@ export class CapaMCPServer {
564566 // Handle tools/call
565567 if ( message . method === 'tools/call' ) {
566568 const { name, arguments : args } = message . params ;
567- console . log ( ` [MCP Handler] Call tool: ${ name } `) ;
568- console . log ( ` Arguments: ${ JSON . stringify ( args ) } `) ;
569+ this . logger . info ( ` Call tool: ${ name } `) ;
570+ this . logger . debug ( ` Arguments: ${ JSON . stringify ( args ) } `) ;
569571
570572 // Handle setup_tools
571573 if ( name === 'setup_tools' ) {
@@ -574,13 +576,13 @@ export class CapaMCPServer {
574576 if ( ! this . sessionId ) {
575577 const session = this . sessionManager . createSession ( this . projectId ) ;
576578 this . sessionId = session . sessionId ;
577- console . log ( ` Created session: ${ this . sessionId } `) ;
579+ this . logger . debug ( ` Created session: ${ this . sessionId } `) ;
578580 }
579581
580582 // Setup tools
581- console . log ( ` Activating skills: ${ args . skills . join ( ', ' ) } `) ;
583+ this . logger . info ( ` Activating skills: ${ args . skills . join ( ', ' ) } `) ;
582584 const toolIds = this . sessionManager . setupTools ( this . sessionId , args . skills ) ;
583- console . log ( ` ✓ Loaded ${ toolIds . length } tool(s): ${ toolIds . join ( ', ' ) } `) ;
585+ this . logger . success ( ` Loaded ${ toolIds . length } tool(s): ${ toolIds . join ( ', ' ) } `) ;
584586
585587 return {
586588 jsonrpc : '2.0' ,
@@ -600,7 +602,7 @@ export class CapaMCPServer {
600602 } ,
601603 } ;
602604 } catch ( error : any ) {
603- console . error ( ` ✗ Error: ${ error . message } `) ;
605+ this . logger . failure ( ` Error: ${ error . message } `) ;
604606
605607 // If skill not found, include list of available skills
606608 let errorMessage = error . message || 'Failed to setup tools' ;
@@ -625,7 +627,7 @@ export class CapaMCPServer {
625627
626628 // Handle other tools
627629 if ( ! this . sessionId ) {
628- console . log ( ` ✗ No active session` ) ;
630+ this . logger . warn ( ' No active session' ) ;
629631 return {
630632 jsonrpc : '2.0' ,
631633 id : message . id ,
@@ -638,7 +640,7 @@ export class CapaMCPServer {
638640
639641 const session = this . sessionManager . getSession ( this . sessionId ) ;
640642 if ( ! session ) {
641- console . log ( ` ✗ Session not found` ) ;
643+ this . logger . warn ( ' Session not found' ) ;
642644 return {
643645 jsonrpc : '2.0' ,
644646 id : message . id ,
@@ -655,7 +657,7 @@ export class CapaMCPServer {
655657 // Find tool definition
656658 const toolDef = this . sessionManager . getToolDefinition ( this . projectId , name ) ;
657659 if ( ! toolDef ) {
658- console . log ( ` ✗ Tool not found` ) ;
660+ this . logger . warn ( ' Tool not found' ) ;
659661 return {
660662 jsonrpc : '2.0' ,
661663 id : message . id ,
@@ -666,26 +668,26 @@ export class CapaMCPServer {
666668 } ;
667669 }
668670
669- console . log ( ` Tool type: ${ toolDef . type } `) ;
671+ this . logger . debug ( ` Tool type: ${ toolDef . type } `) ;
670672
671673 // Execute tool based on type
672674 let result : any ;
673675 try {
674676 if ( toolDef . type === 'command' ) {
675- console . log ( ` Executing command tool...` ) ;
677+ this . logger . debug ( ' Executing command tool...' ) ;
676678 const executor = new CommandToolExecutor ( this . db , this . projectId , this . projectPath ) ;
677679 result = await executor . execute (
678680 name ,
679681 toolDef . def as ToolCommandDefinition ,
680682 args as Record < string , any >
681683 ) ;
682- console . log ( ` ✓ Command executed, success: ${ result . success } `) ;
684+ this . logger . debug ( ` Command executed, success: ${ result . success } `) ;
683685 } else if ( toolDef . type === 'mcp' ) {
684- console . log ( ` Executing MCP tool...` ) ;
686+ this . logger . debug ( ' Executing MCP tool...' ) ;
685687 const mcpDef = toolDef . def as ToolMCPDefinition ;
686688 const capabilities = this . sessionManager . getProjectCapabilities ( this . projectId ) ;
687689 if ( ! capabilities ) {
688- console . log ( ` ✗ Project capabilities not found` ) ;
690+ this . logger . warn ( ' Project capabilities not found' ) ;
689691 return {
690692 jsonrpc : '2.0' ,
691693 id : message . id ,
@@ -700,7 +702,7 @@ export class CapaMCPServer {
700702 const serverId = mcpDef . server . replace ( '@' , '' ) ;
701703 const serverDef = capabilities . servers . find ( ( s ) => s . id === serverId ) ;
702704 if ( ! serverDef ) {
703- console . log ( ` ✗ Server not found: ${ serverId } `) ;
705+ this . logger . warn ( ` Server not found: ${ serverId } `) ;
704706 return {
705707 jsonrpc : '2.0' ,
706708 id : message . id ,
@@ -711,9 +713,9 @@ export class CapaMCPServer {
711713 } ;
712714 }
713715
714- console . log ( ` Using MCP server: ${ serverId } `) ;
716+ this . logger . debug ( ` Using MCP server: ${ serverId } `) ;
715717 result = await this . mcpProxy . executeTool ( name , mcpDef , serverDef . def , args as Record < string , any > ) ;
716- console . log ( ` ✓ MCP tool executed` ) ;
718+ this . logger . debug ( ' MCP tool executed' ) ;
717719 }
718720
719721 return {
@@ -729,7 +731,7 @@ export class CapaMCPServer {
729731 } ,
730732 } ;
731733 } catch ( error : any ) {
732- console . error ( ` ✗ Tool execution error: ${ error . message } `) ;
734+ this . logger . failure ( ` Tool execution error: ${ error . message } `) ;
733735 return {
734736 jsonrpc : '2.0' ,
735737 id : message . id ,
@@ -742,7 +744,7 @@ export class CapaMCPServer {
742744 }
743745
744746 // Unknown method
745- console . log ( ` [MCP Handler] ✗ Unknown method: ${ message . method } `) ;
747+ this . logger . warn ( ` Unknown method: ${ message . method } `) ;
746748 return {
747749 jsonrpc : '2.0' ,
748750 id : message . id ,
0 commit comments