Skip to content

fixed MCPClient to reuse session across tool calls#4034

Open
omChauhanDev wants to merge 2 commits intopipecat-ai:mainfrom
omChauhanDev:fix/mcp-persistent-session
Open

fixed MCPClient to reuse session across tool calls#4034
omChauhanDev wants to merge 2 commits intopipecat-ai:mainfrom
omChauhanDev:fix/mcp-persistent-session

Conversation

@omChauhanDev
Copy link
Contributor

@omChauhanDev omChauhanDev commented Mar 15, 2026

Please describe the changes in your PR. If it is addressing an issue, please reference that as well.

Fixes #3950

Issue : MCPClient was opening a new connection & re-initializing the MCP session on every single tool call. This caused unnecessary latency and repeated handshakes.

Solution : This PR makes MCPClient maintain a persistent session using AsyncExitStack, reused across all tool calls & schema requests until the client is closed.

MCPClient now requires an explicit lifecycle : either async with or start()/close().

Changes :

  • Replaced 6 transport-specific methods with a single start()/close() lifecycle & unified _tool_wrapper
  • Updated all MCP examples (39, 39a, 39b, 39c) to use async with

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 2.32558% with 42 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/pipecat/services/mcp_service.py 2.32% 42 Missing ⚠️
Files with missing lines Coverage Δ
src/pipecat/services/mcp_service.py 10.94% <2.32%> (+2.46%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP SSE transport: New SSE session created on every tool call

1 participant