1111
1212class CustomMessageHandler (MaggMessageHandler ):
1313 """Custom message handler that logs all notifications."""
14-
14+
1515 def __init__ (self ):
1616 super ().__init__ ()
1717 self .notification_count = 0
18-
18+
1919 async def on_message (self , message ):
2020 """Called for all messages."""
2121 self .notification_count += 1
2222 print (f"📥 Received message #{ self .notification_count } : { type (message ).__name__ } " )
23-
23+
2424 async def on_tool_list_changed (self , notification : mcp .types .ToolListChangedNotification ):
2525 """Called when tool list changes."""
2626 print ("🔧 Tool list changed! Available tools may have been updated." )
27-
27+
2828 async def on_resource_list_changed (self , notification : mcp .types .ResourceListChangedNotification ):
2929 """Called when resource list changes."""
3030 print ("📁 Resource list changed! Available resources may have been updated." )
31-
31+
3232 async def on_progress (self , notification : mcp .types .ProgressNotification ):
3333 """Called for progress updates."""
3434 if notification .params :
@@ -39,7 +39,7 @@ async def on_progress(self, notification: mcp.types.ProgressNotification):
3939 print (f"⏳ Progress: { progress } /{ total } ({ percentage :.1f} %)" )
4040 else :
4141 print (f"⏳ Progress: { progress } " )
42-
42+
4343 async def on_logging_message (self , notification : mcp .types .LoggingMessageNotification ):
4444 """Called for log messages from servers."""
4545 if notification .params :
@@ -51,38 +51,38 @@ async def on_logging_message(self, notification: mcp.types.LoggingMessageNotific
5151async def callback_example ():
5252 """Example using callback-based message handler."""
5353 print ("🚀 Starting callback-based message handler example..." )
54-
54+
5555 def on_tool_change (notification ):
5656 print ("🔧 [Callback] Tools changed!" )
57-
57+
5858 def on_progress (notification ):
5959 if notification .params and notification .params .progress is not None :
6060 print (f"⏳ [Callback] Progress: { notification .params .progress } " )
61-
61+
6262 # Create handler with callbacks
6363 handler = MaggMessageHandler (
6464 on_tool_list_changed = on_tool_change ,
6565 on_progress = on_progress
6666 )
67-
67+
6868 # Create client with message handler
6969 client = MaggClient (
7070 "http://localhost:8000/mcp/" , # MCP endpoint with trailing slash
7171 message_handler = handler
7272 )
73-
73+
7474 try :
7575 async with client :
7676 print ("✅ Connected to Magg server with message handling" )
77-
77+
7878 # List tools to see what's available
7979 tools = await client .list_tools ()
8080 print (f"📋 Found { len (tools )} tools available" )
81-
81+
8282 # Keep connection open to receive notifications
8383 print ("👂 Listening for notifications... (press Ctrl+C to stop)" )
8484 await asyncio .sleep (30 ) # Listen for 30 seconds
85-
85+
8686 except KeyboardInterrupt :
8787 print ("\n 🛑 Stopped listening for notifications" )
8888 except Exception as e :
@@ -92,36 +92,36 @@ def on_progress(notification):
9292async def class_example ():
9393 """Example using class-based message handler."""
9494 print ("🚀 Starting class-based message handler example..." )
95-
95+
9696 # Create custom handler
9797 handler = CustomMessageHandler ()
98-
98+
9999 # Create client with message handler
100100 client = MaggClient (
101101 "http://localhost:8000/mcp/" , # MCP endpoint with trailing slash
102102 message_handler = handler
103103 )
104-
104+
105105 try :
106106 async with client :
107107 print ("✅ Connected to Magg server with custom message handler" )
108-
108+
109109 # List available capabilities
110110 tools = await client .list_tools ()
111111 resources = await client .list_resources ()
112112 prompts = await client .list_prompts ()
113-
113+
114114 print (f"📋 Available capabilities:" )
115115 print (f" 🔧 Tools: { len (tools )} " )
116116 print (f" 📁 Resources: { len (resources )} " )
117117 print (f" 💬 Prompts: { len (prompts )} " )
118-
118+
119119 # Keep connection open to receive notifications
120120 print ("👂 Listening for notifications... (press Ctrl+C to stop)" )
121121 await asyncio .sleep (30 ) # Listen for 30 seconds
122-
122+
123123 print (f"📊 Total notifications received: { handler .notification_count } " )
124-
124+
125125 except KeyboardInterrupt :
126126 print ("\n 🛑 Stopped listening for notifications" )
127127 except Exception as e :
@@ -136,17 +136,17 @@ async def main():
136136 print ("This example demonstrates Magg's real-time messaging capabilities." )
137137 print ("Make sure you have a Magg server running at http://localhost:8000" )
138138 print ()
139-
139+
140140 # Run callback example
141141 await callback_example ()
142142 print ()
143-
143+
144144 # Wait a bit between examples
145145 await asyncio .sleep (2 )
146-
146+
147147 # Run class example
148148 await class_example ()
149-
149+
150150 print ()
151151 print ("✨ Examples completed!" )
152152 print ()
@@ -157,4 +157,4 @@ async def main():
157157
158158
159159if __name__ == "__main__" :
160- asyncio .run (main ())
160+ asyncio .run (main ())
0 commit comments