Skip to content

chore: refactor connections to use the new ConnectionManager to isolate long running processes like OIDC connections MCP-81 #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Aug 6, 2025

Conversation

kmruiz
Copy link
Collaborator

@kmruiz kmruiz commented Aug 5, 2025

Proposed changes

This PR basically proposes consolidating all connections into a single class, ConnectionManager. After this PR, both Atlas tools and MongoDB tools use the same code to connect to MongoDB, independently of the connection type.

Also, the ConnectionManager gets an upgrade on how to handle errors and debugging information:

  • The ConnectionManager keeps track of the latest state and emits events when the connection status changes.
  • There is a new debug resource that contains information about the last connection error.
  • The ConnectionManager infers the authentication type, to provide more information to the user when something goes wrong.
  • The ConnectionManager implements the non-terminal "connecting" state for OIDC flows.

To avoid changing the whole architecture and maybe support multitenancy in the future, the ConnectionManager is hidden and isolated in the Session.

  • Tools still use the session, who acts as a proxy between the ConnectionManager and the tool.
  • Tools still work with the service provider through the Session, and the session ensures the connection is active.

This PR is bigger than usual because the refactor touches some root infrastructure, but hopefully the unit and integration tests I've added, along with the manual testing, ensures everything works correctly.

Checklist

kmruiz added 2 commits August 4, 2025 14:51
Now the connection itself and the user flow is managed
by the new connection manager. There are tests missing
for this class, that will be added in a following commit.
these test should validate that connecting to a cluster is fine
and also the connection mechanism inference is working as expected
until we support both oidc flows.
@kmruiz kmruiz marked this pull request as ready for review August 5, 2025 11:27
@Copilot Copilot AI review requested due to automatic review settings August 5, 2025 11:27
@kmruiz kmruiz requested a review from a team as a code owner August 5, 2025 11:27
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the connection handling architecture by introducing a centralized ConnectionManager class that manages all MongoDB connections, replacing the previous approach where connection logic was distributed across tools and sessions. The main goal is to isolate long-running processes like OIDC connections and provide better error handling and debugging capabilities.

Key changes include:

  • Introduction of ConnectionManager class with state tracking and event emission
  • Consolidation of connection logic from both Atlas and MongoDB tools into a single interface
  • Enhanced error handling and debugging information through connection state management

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/common/connectionManager.ts New centralized connection management class with state tracking and event emission
src/common/session.ts Refactored to use ConnectionManager instead of directly managing ServiceProvider
src/tools/mongodb/mongodbTool.ts Updated to use session's connection state checking methods
src/tools/mongodb/connect/connect.ts Modified to use new connection interface and added connect event listener
src/tools/atlas/connect/connectCluster.ts Updated connection calls to use new ConnectionManager interface
src/server.ts Updated to use new connection interface and moved resource registration earlier
src/resources/common/debug.ts Updated resource naming for MongoDB debugging
tests/unit/common/session.test.ts Updated test calls to match new connection interface
tests/integration/transports/stdio.test.ts Added connection state setup for integration tests
tests/integration/tools/mongodb/connect/connect.test.ts Updated tests with connection state setup and error message expectations
tests/integration/helpers.ts Added ConnectionManager instantiation to test setup
tests/integration/common/connectionManager.test.ts New comprehensive test suite for ConnectionManager functionality

Copy link
Collaborator

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a solid change - I have mostly minor/code style comments, but also a few questions that may be a bit more involved.

kmruiz added 5 commits August 6, 2025 10:15
Also remove the code that forces the reconnection in the stdio
transport test as it's not needed anymore.
But keep deleting users outside, as it's out of scope
of the connection manager as it's not related to MongoDB itself
but to Atlas.
Copy link
Collaborator

@himanshusinghs himanshusinghs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, I left minor suggestions but nothing blocking for the PR.

kmruiz added 2 commits August 6, 2025 13:02
We are not using it, but to be consistent with what we had before
we are keeping it.
Copy link
Collaborator

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - a couple of final nits from me.

…ction state

We used to support only having it when connected, but it might be
useful also when errored
kmruiz added 2 commits August 6, 2025 14:15
Now it doesn't need to be async because the connection itself attemps
to ping, it just needs to check the current connection status and map
the exact values
Copy link
Collaborator

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending lint + test fixes, as well as addressing the comments from the other reviewers, it's good to merge on my end.

@kmruiz kmruiz enabled auto-merge (squash) August 6, 2025 13:47
@kmruiz kmruiz merged commit 1bf59db into main Aug 6, 2025
17 checks passed
@kmruiz kmruiz deleted the chore/mcp-81 branch August 6, 2025 13:48
@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 16778685993

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 192 of 200 (96.0%) changed or added relevant lines in 7 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.7%) to 81.981%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/common/session.ts 31 34 91.18%
src/tools/atlas/connect/connectCluster.ts 32 37 86.49%
Files with Coverage Reduction New Missed Lines %
src/common/session.ts 1 87.39%
src/tools/atlas/connect/connectCluster.ts 6 77.27%
Totals Coverage Status
Change from base Build 16770485719: 0.7%
Covered Lines: 3418
Relevant Lines: 4123

💛 - Coveralls

@kmruiz kmruiz restored the chore/mcp-81 branch August 7, 2025 09:58
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.

5 participants