MCPcopy
hub / github.com/open-webui/mcpo / call_tool_with_reconnect

Function call_tool_with_reconnect

src/mcpo/utils/main.py:285–322  ·  view source on GitHub ↗
(
        request: Request, arguments: Dict[str, Any]
    )

Source from the content-addressed store, hash-verified

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)

Callers 1

toolFunction · 0.85

Calls 3

_invokeFunction · 0.85
ensure_initializedMethod · 0.80
reconnectMethod · 0.80

Tested by

no test coverage detected