A ClientSession that logs all communication
| 230 | |
| 231 | # Create a custom ClientSession that logs all communication |
| 232 | class LoggingClientSession(ClientSession): |
| 233 | """A ClientSession that logs all communication""" |
| 234 | |
| 235 | async def send_request(self, *args, **kwargs): |
| 236 | """Log and forward requests""" |
| 237 | method = args[0] |
| 238 | params = args[1] if len(args) > 1 else None |
| 239 | log_mcp_message("REQUEST", method, params) |
| 240 | |
| 241 | try: |
| 242 | result = await super().send_request(*args, **kwargs) |
| 243 | log_mcp_message("RESPONSE", method, result=result) |
| 244 | return result |
| 245 | except Exception as e: |
| 246 | log_mcp_message("ERROR", method, error=str(e)) |
| 247 | raise |
| 248 | |
| 249 | async def send_notification(self, *args, **kwargs): |
| 250 | """Log and forward notifications""" |
| 251 | method = args[0] |
| 252 | params = args[1] if len(args) > 1 else None |
| 253 | log_mcp_message("NOTIFICATION", method, params) |
| 254 | |
| 255 | try: |
| 256 | await super().send_notification(*args, **kwargs) |
| 257 | except Exception as e: |
| 258 | log_mcp_message("ERROR", method, error=str(e)) |
| 259 | raise |
| 260 | |
| 261 | class MCPServer: |
| 262 | """Represents a connection to an MCP server""" |
no outgoing calls
no test coverage detected