(
request: Request, arguments: Dict[str, Any]
)
| 283 | client_header_forwarding_config=None, |
| 284 | ): |
| 285 | async def call_tool_with_reconnect( |
| 286 | request: Request, arguments: Dict[str, Any] |
| 287 | ) -> CallToolResult: |
| 288 | session_manager = getattr(request.app.state, "session_manager", None) |
| 289 | |
| 290 | async def _invoke(session): |
| 291 | return await session.call_tool(endpoint_name, arguments=arguments) |
| 292 | |
| 293 | if session_manager: |
| 294 | try: |
| 295 | session, _ = await session_manager.ensure_initialized() |
| 296 | except ClosedResourceError: |
| 297 | logger.warning( |
| 298 | "Session closed while initializing '%s'; attempting reconnect", |
| 299 | endpoint_name, |
| 300 | ) |
| 301 | session, _ = await session_manager.reconnect() |
| 302 | request.app.state.session = session |
| 303 | |
| 304 | try: |
| 305 | return await _invoke(session) |
| 306 | except ClosedResourceError: |
| 307 | logger.warning( |
| 308 | "Session closed during call to '%s'; attempting reconnect", |
| 309 | endpoint_name, |
| 310 | ) |
| 311 | session, _ = await session_manager.reconnect() |
| 312 | request.app.state.session = session |
| 313 | return await _invoke(session) |
| 314 | |
| 315 | session = getattr(request.app.state, "session", None) |
| 316 | if not session: |
| 317 | raise HTTPException( |
| 318 | status_code=500, |
| 319 | detail={"message": "MCP session is not available"}, |
| 320 | ) |
| 321 | |
| 322 | return await _invoke(session) |
| 323 | |
| 324 | if form_model_fields: |
| 325 | FormModel = create_model(f"{endpoint_name}_form_model", **form_model_fields) |
no test coverage detected