@@ -14,12 +14,11 @@ import {
1414 ReadResourceRequestSchema ,
1515 Resource ,
1616 RootsListChangedNotificationSchema ,
17- SetLevelRequestSchema ,
1817 SubscribeRequestSchema ,
1918 Tool ,
2019 ToolSchema ,
2120 UnsubscribeRequestSchema ,
22- type Root ,
21+ type Root
2322} from "@modelcontextprotocol/sdk/types.js" ;
2423import { z } from "zod" ;
2524import { zodToJsonSchema } from "zod-to-json-schema" ;
@@ -174,58 +173,50 @@ export const createServer = () => {
174173 let subsUpdateInterval : NodeJS . Timeout | undefined ;
175174 let stdErrUpdateInterval : NodeJS . Timeout | undefined ;
176175
177- let logLevel : LoggingLevel = "debug" ;
178176 let logsUpdateInterval : NodeJS . Timeout | undefined ;
179177 // Store client capabilities
180178 let clientCapabilities : ClientCapabilities | undefined ;
181179
182180 // Roots state management
183181 let currentRoots : Root [ ] = [ ] ;
184182 let clientSupportsRoots = false ;
185- const messages = [
186- { level : "debug" , data : "Debug-level message" } ,
187- { level : "info" , data : "Info-level message" } ,
188- { level : "notice" , data : "Notice-level message" } ,
189- { level : "warning" , data : "Warning-level message" } ,
190- { level : "error" , data : "Error-level message" } ,
191- { level : "critical" , data : "Critical-level message" } ,
192- { level : "alert" , data : "Alert level-message" } ,
193- { level : "emergency" , data : "Emergency-level message" } ,
194- ] ;
195-
196- const isMessageIgnored = ( level : LoggingLevel ) : boolean => {
197- const currentLevel = messages . findIndex ( ( msg ) => logLevel === msg . level ) ;
198- const messageLevel = messages . findIndex ( ( msg ) => level === msg . level ) ;
199- return messageLevel < currentLevel ;
200- } ;
201-
202- // Function to start notification intervals when a client connects
203- const startNotificationIntervals = ( ) => {
204- if ( ! subsUpdateInterval ) {
205- subsUpdateInterval = setInterval ( ( ) => {
206- for ( const uri of subscriptions ) {
207- server . notification ( {
208- method : "notifications/resources/updated" ,
209- params : { uri } ,
210- } ) ;
211- }
212- } , 10000 ) ;
213- }
183+ let sessionId : string | undefined ;
184+
185+ // Function to start notification intervals when a client connects
186+ const startNotificationIntervals = ( sid ?: string | undefined ) => {
187+ sessionId = sid ;
188+ if ( ! subsUpdateInterval ) {
189+ subsUpdateInterval = setInterval ( ( ) => {
190+ for ( const uri of subscriptions ) {
191+ server . notification ( {
192+ method : "notifications/resources/updated" ,
193+ params : { uri } ,
194+ } ) ;
195+ }
196+ } , 10000 ) ;
197+ }
214198
215- if ( ! logsUpdateInterval ) {
216- logsUpdateInterval = setInterval ( ( ) => {
217- let message = {
218- method : "notifications/message" ,
219- params : messages [ Math . floor ( Math . random ( ) * messages . length ) ] ,
220- } ;
221- if ( ! isMessageIgnored ( message . params . level as LoggingLevel ) )
222- server . notification ( message ) ;
223- } , 20000 ) ;
199+ console . log ( sessionId )
200+ const maybeAppendSessionId = sessionId ? ` - SessionId ${ sessionId } ` : "" ;
201+ const messages : { level : LoggingLevel ; data : string } [ ] = [
202+ { level : "debug" , data : `Debug-level message${ maybeAppendSessionId } ` } ,
203+ { level : "info" , data : `Info-level message${ maybeAppendSessionId } ` } ,
204+ { level : "notice" , data : `Notice-level message${ maybeAppendSessionId } ` } ,
205+ { level : "warning" , data : `Warning-level message${ maybeAppendSessionId } ` } ,
206+ { level : "error" , data : `Error-level message${ maybeAppendSessionId } ` } ,
207+ { level : "critical" , data : `Critical-level message${ maybeAppendSessionId } ` } ,
208+ { level : "alert" , data : `Alert level-message${ maybeAppendSessionId } ` } ,
209+ { level : "emergency" , data : `Emergency-level message${ maybeAppendSessionId } ` } ,
210+ ] ;
211+
212+ if ( ! logsUpdateInterval ) {
213+ console . error ( "Starting logs update interval" ) ;
214+ logsUpdateInterval = setInterval ( async ( ) => {
215+ await server . sendLoggingMessage ( messages [ Math . floor ( Math . random ( ) * messages . length ) ] , sessionId ) ;
216+ } , 15000 ) ;
224217 }
225218 } ;
226219
227-
228-
229220 // Helper method to request sampling from client
230221 const requestSampling = async (
231222 context : string ,
@@ -918,23 +909,6 @@ export const createServer = () => {
918909 throw new Error ( `Unknown reference type` ) ;
919910 } ) ;
920911
921- server . setRequestHandler ( SetLevelRequestSchema , async ( request ) => {
922- const { level } = request . params ;
923- logLevel = level ;
924-
925- // Demonstrate different log levels
926- await server . notification ( {
927- method : "notifications/message" ,
928- params : {
929- level : "debug" ,
930- logger : "test-server" ,
931- data : `Logging level set to: ${ logLevel } ` ,
932- } ,
933- } ) ;
934-
935- return { } ;
936- } ) ;
937-
938912 // Roots protocol handlers
939913 server . setNotificationHandler ( RootsListChangedNotificationSchema , async ( ) => {
940914 try {
@@ -944,24 +918,18 @@ export const createServer = () => {
944918 currentRoots = response . roots ;
945919
946920 // Log the roots update for demonstration
947- await server . notification ( {
948- method : "notifications/message" ,
949- params : {
921+ await server . sendLoggingMessage ( {
950922 level : "info" ,
951923 logger : "everything-server" ,
952924 data : `Roots updated: ${ currentRoots . length } root(s) received from client` ,
953- } ,
954- } ) ;
925+ } , sessionId ) ;
955926 }
956927 } catch ( error ) {
957- await server . notification ( {
958- method : "notifications/message" ,
959- params : {
928+ await server . sendLoggingMessage ( {
960929 level : "error" ,
961930 logger : "everything-server" ,
962931 data : `Failed to request roots from client: ${ error instanceof Error ? error . message : String ( error ) } ` ,
963- } ,
964- } ) ;
932+ } , sessionId ) ;
965933 }
966934 } ) ;
967935
@@ -976,43 +944,31 @@ export const createServer = () => {
976944 if ( response && 'roots' in response ) {
977945 currentRoots = response . roots ;
978946
979- await server . notification ( {
980- method : "notifications/message" ,
981- params : {
947+ await server . sendLoggingMessage ( {
982948 level : "info" ,
983949 logger : "everything-server" ,
984950 data : `Initial roots received: ${ currentRoots . length } root(s) from client` ,
985- } ,
986- } ) ;
951+ } , sessionId ) ;
987952 } else {
988- await server . notification ( {
989- method : "notifications/message" ,
990- params : {
953+ await server . sendLoggingMessage ( {
991954 level : "warning" ,
992955 logger : "everything-server" ,
993956 data : "Client returned no roots set" ,
994- } ,
995- } ) ;
957+ } , sessionId ) ;
996958 }
997959 } catch ( error ) {
998- await server . notification ( {
999- method : "notifications/message" ,
1000- params : {
960+ await server . sendLoggingMessage ( {
1001961 level : "error" ,
1002962 logger : "everything-server" ,
1003963 data : `Failed to request initial roots from client: ${ error instanceof Error ? error . message : String ( error ) } ` ,
1004- } ,
1005- } ) ;
964+ } , sessionId ) ;
1006965 }
1007966 } else {
1008- await server . notification ( {
1009- method : "notifications/message" ,
1010- params : {
967+ await server . sendLoggingMessage ( {
1011968 level : "info" ,
1012969 logger : "everything-server" ,
1013970 data : "Client does not support MCP roots protocol" ,
1014- } ,
1015- } ) ;
971+ } , sessionId ) ;
1016972 }
1017973 } ;
1018974
0 commit comments