Skip to content

how to cleanup mcp servers correctly? #1393

@DeoLeung

Description

@DeoLeung

#840 similar to this one

@router.post(
  '/chat/completions',
  response_class=JSONResponse,
  response_model=ChatCompletion,
)
async def chat_completions(
  request: ChatCompletionRequest, account: Account, bg: BackgroundTasks
):
  """Handle chat completions for the agent."""
  start_agent, server_1, server_2 =  ...
  # server_2 = MCPServerStreamableHttp(
  #  MCPServerStreamableHttpParams(
   #   url='http://...',
   # ),
    #cache_tools_list=True,
    #tool_filter=create_static_tool_filter(allowed_tool_names=tools),
  #)
  await server_1.connect()
  await server_2.connect()

  bg.add_task(server_1.cleanup)
  bg.add_task(server_2.cleanup)
  result = Runner.run_streamed(
    start_agent,
    input=input_messages,
    run_config=RunConfig(
      trace_id='trace_' + request.request_id,
      workflow_name='abc',
      trace_metadata={},
    ),
    session=session,
  )

  helper = ChatCompletionChunkHelper(model=request.model)

  return StreamingResponse(
    process_messages(result, helper, start_agent.name, request.config),
    media_type='text/event-stream',
  )

using fastapi to serve the result, the request is ok, but cleanup the server i got

server 320 2025-08-07 21:29:42,789 - ERROR - Error cleaning up server: Attempted to exit cancel scope in a different task than it was entered in
server 320 2025-08-07 21:29:42,789 - ERROR - Error cleaning up server: Attempted to exit cancel scope in a different task than it was entered in

how should i handle the cleanup correctly?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions