@@ -12,11 +12,9 @@ import type {
1212 ErrorCode ,
1313 InitializeResult ,
1414 JSONRPCRequest ,
15- JSONRPCResponse ,
1615 MCPMethod ,
1716 ServerCapabilities ,
1817 ServerInfo ,
19- ToolCall ,
2018} from './protocol/types' ;
2119import { StdioTransport } from './transport/stdio-transport' ;
2220import type { Transport , TransportMessage } from './transport/transport' ;
@@ -32,9 +30,10 @@ export interface MCPServerConfig {
3230export class MCPServer {
3331 private registry : AdapterRegistry ;
3432 private transport : Transport ;
35- private logger = new ConsoleLogger ( '[MCP Server]' ) ;
33+ private logger = new ConsoleLogger ( '[MCP Server]' , 'debug' ) ; // Enable debug logging
3634 private config : Config ;
3735 private serverInfo : ServerInfo ;
36+ private clientProtocolVersion ?: string ;
3837
3938 constructor ( config : MCPServerConfig ) {
4039 this . config = config . config ;
@@ -105,17 +104,39 @@ export class MCPServer {
105104 * Handle incoming MCP message
106105 */
107106 private async handleMessage ( message : TransportMessage ) : Promise < void > {
108- // Only handle requests (not notifications for now)
107+ this . logger . debug ( 'Raw message received' , {
108+ type : typeof message ,
109+ isRequest : JSONRPCHandler . isRequest ( message ) ,
110+ preview : JSON . stringify ( message ) . substring ( 0 , 200 ) ,
111+ } ) ;
112+
113+ // Handle notifications
109114 if ( ! JSONRPCHandler . isRequest ( message ) ) {
110- this . logger . debug ( 'Ignoring notification' , { method : message . method } ) ;
115+ const method = ( message as { method : string } ) . method ;
116+ this . logger . info ( 'Received notification' , { method } ) ;
117+
118+ // The 'initialized' notification is sent by the client after receiving
119+ // the initialize response. We acknowledge it but don't need to respond.
120+ if ( method === 'initialized' ) {
121+ this . logger . info ( 'Client initialized successfully' ) ;
122+ }
123+
111124 return ;
112125 }
113126
114127 const request = message as JSONRPCRequest ;
128+ this . logger . info ( 'Received request' , { method : request . method , id : request . id } ) ;
115129
116130 try {
117131 const result = await this . routeRequest ( request ) ;
118- const response = JSONRPCHandler . createResponse ( request . id ! , result ) ;
132+ // request.id is guaranteed to be defined for requests (checked by isRequest)
133+ const requestId = request . id ?? 0 ;
134+ const response = JSONRPCHandler . createResponse ( requestId , result ) ;
135+ this . logger . debug ( 'Sending response' , {
136+ id : request . id ,
137+ method : request . method ,
138+ responsePreview : JSON . stringify ( response ) . substring ( 0 , 200 ) ,
139+ } ) ;
119140 await this . transport . send ( response ) ;
120141 } catch ( error ) {
121142 this . logger . error ( 'Request handling failed' , {
@@ -124,7 +145,8 @@ export class MCPServer {
124145 } ) ;
125146
126147 const jsonrpcError = error as { code : ErrorCode ; message : string ; data ?: unknown } ;
127- const errorResponse = JSONRPCHandler . createErrorResponse ( request . id , jsonrpcError ) ;
148+ const requestId = request . id ?? 0 ;
149+ const errorResponse = JSONRPCHandler . createErrorResponse ( requestId , jsonrpcError ) ;
128150 await this . transport . send ( errorResponse ) ;
129151 }
130152 }
@@ -137,7 +159,7 @@ export class MCPServer {
137159
138160 switch ( method ) {
139161 case 'initialize' :
140- return this . handleInitialize ( ) ;
162+ return this . handleInitialize ( request . params as { protocolVersion ?: string } ) ;
141163
142164 case 'tools/list' :
143165 return this . handleToolsList ( ) ;
@@ -161,15 +183,24 @@ export class MCPServer {
161183 /**
162184 * Handle initialize request
163185 */
164- private handleInitialize ( ) : InitializeResult {
186+ private handleInitialize ( params ?: { protocolVersion ?: string } ) : InitializeResult {
187+ // Store the client's protocol version and echo it back
188+ // MCP uses date-based versioning (e.g., "2025-06-18")
189+ this . clientProtocolVersion = params ?. protocolVersion || '1.0' ;
190+
191+ this . logger . debug ( 'Initialize request' , {
192+ clientProtocolVersion : this . clientProtocolVersion ,
193+ clientParams : params ,
194+ } ) ;
195+
165196 const capabilities : ServerCapabilities = {
166197 tools : { supported : true } ,
167198 resources : { supported : false } , // Not yet implemented
168199 prompts : { supported : false } , // Not yet implemented
169200 } ;
170201
171202 return {
172- protocolVersion : '1.0' ,
203+ protocolVersion : this . clientProtocolVersion ,
173204 capabilities,
174205 serverInfo : this . serverInfo ,
175206 } ;
0 commit comments