-
Notifications
You must be signed in to change notification settings - Fork 16
Support multiple buckets #47
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
Open
nithishr
wants to merge
3
commits into
main
Choose a base branch
from
support_multiple_buckets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−114
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
Tools for server operations. | ||||||||||||||||||||||||||||
This module contains tools for getting the server status, testing the connection, and getting the scopes and collections in the bucket. | ||||||||||||||||||||||||||||
This module contains tools for getting the server status, testing the connection, and getting the buckets in the cluster, the scopes and collections in the bucket. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
import logging | ||||||||||||||||||||||||||||
|
@@ -10,8 +10,9 @@ | |||||||||||||||||||||||||||
from mcp.server.fastmcp import Context | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
from utils.config import get_settings | ||||||||||||||||||||||||||||
from utils.connection import connect_to_bucket | ||||||||||||||||||||||||||||
from utils.constants import MCP_SERVER_NAME | ||||||||||||||||||||||||||||
from utils.context import ensure_bucket_connection | ||||||||||||||||||||||||||||
from utils.context import get_cluster_connection | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
logger = logging.getLogger(f"{MCP_SERVER_NAME}.tools.server") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -26,15 +27,13 @@ def get_server_configuration_status(ctx: Context) -> dict[str, Any]: | |||||||||||||||||||||||||||
configuration = { | ||||||||||||||||||||||||||||
"connection_string": settings.get("connection_string", "Not set"), | ||||||||||||||||||||||||||||
"username": settings.get("username", "Not set"), | ||||||||||||||||||||||||||||
"bucket_name": settings.get("bucket_name", "Not set"), | ||||||||||||||||||||||||||||
"read_only_query_mode": settings.get("read_only_query_mode", True), | ||||||||||||||||||||||||||||
"password_configured": bool(settings.get("password")), | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
app_context = ctx.request_context.lifespan_context | ||||||||||||||||||||||||||||
connection_status = { | ||||||||||||||||||||||||||||
"cluster_connected": app_context.cluster is not None, | ||||||||||||||||||||||||||||
"bucket_connected": app_context.bucket is not None, | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||
|
@@ -45,39 +44,43 @@ def get_server_configuration_status(ctx: Context) -> dict[str, Any]: | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def test_cluster_connection(ctx: Context) -> dict[str, Any]: | ||||||||||||||||||||||||||||
"""Test the connection to Couchbase cluster and bucket. | ||||||||||||||||||||||||||||
def test_cluster_connection( | ||||||||||||||||||||||||||||
ctx: Context, bucket_name: str | None = None | ||||||||||||||||||||||||||||
) -> dict[str, Any]: | ||||||||||||||||||||||||||||
"""Test the connection to Couchbase cluster and optionally to a bucket. | ||||||||||||||||||||||||||||
This tool verifies the connection to the Couchbase cluster and bucket by establishing the connection if it is not already established. | ||||||||||||||||||||||||||||
Returns connection status and basic cluster information. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||
bucket = ensure_bucket_connection(ctx) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# Test basic connectivity by getting bucket name | ||||||||||||||||||||||||||||
bucket_name = bucket.name | ||||||||||||||||||||||||||||
cluster = get_cluster_connection(ctx) | ||||||||||||||||||||||||||||
bucket = connect_to_bucket(cluster, bucket_name) if bucket_name else None | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||
"status": "success", | ||||||||||||||||||||||||||||
"cluster_connected": True, | ||||||||||||||||||||||||||||
"bucket_connected": True, | ||||||||||||||||||||||||||||
"bucket_name": bucket_name, | ||||||||||||||||||||||||||||
"message": "Successfully connected to Couchbase cluster and bucket", | ||||||||||||||||||||||||||||
"cluster_connected": cluster.connected, | ||||||||||||||||||||||||||||
nithishr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
"bucket_connected": bucket is not None, | ||||||||||||||||||||||||||||
"bucket_name": bucket.name if bucket else None, | ||||||||||||||||||||||||||||
"message": "Successfully connected to Couchbase cluster", | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||
"status": "error", | ||||||||||||||||||||||||||||
"cluster_connected": False, | ||||||||||||||||||||||||||||
"bucket_connected": False, | ||||||||||||||||||||||||||||
"bucket_name": None, | ||||||||||||||||||||||||||||
"error": str(e), | ||||||||||||||||||||||||||||
"message": "Failed to connect to Couchbase", | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_scopes_and_collections_in_bucket(ctx: Context) -> dict[str, list[str]]: | ||||||||||||||||||||||||||||
def get_scopes_and_collections_in_bucket( | ||||||||||||||||||||||||||||
ctx: Context, bucket_name: str | ||||||||||||||||||||||||||||
) -> dict[str, list[str]]: | ||||||||||||||||||||||||||||
"""Get the names of all scopes and collections in the bucket. | ||||||||||||||||||||||||||||
Returns a dictionary with scope names as keys and lists of collection names as values. | ||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||
bucket = ensure_bucket_connection(ctx) | ||||||||||||||||||||||||||||
cluster = get_cluster_connection(ctx) | ||||||||||||||||||||||||||||
bucket = connect_to_bucket(cluster, bucket_name) | ||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||
scopes_collections = {} | ||||||||||||||||||||||||||||
collection_manager = bucket.collections() | ||||||||||||||||||||||||||||
|
@@ -89,3 +92,39 @@ def get_scopes_and_collections_in_bucket(ctx: Context) -> dict[str, list[str]]: | |||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||
logger.error(f"Error getting scopes and collections: {e}") | ||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_buckets_in_cluster(ctx: Context) -> list[str]: | ||||||||||||||||||||||||||||
"""Get the names of all the accessible buckets in the cluster.""" | ||||||||||||||||||||||||||||
cluster = get_cluster_connection(ctx) | ||||||||||||||||||||||||||||
bucket_manager = cluster.buckets() | ||||||||||||||||||||||||||||
buckets_with_settings = bucket_manager.get_all_buckets() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
buckets = [] | ||||||||||||||||||||||||||||
for bucket in buckets_with_settings: | ||||||||||||||||||||||||||||
buckets.append(bucket.name) | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
return buckets | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_scopes_in_bucket(ctx: Context, bucket_name: str) -> list[str]: | ||||||||||||||||||||||||||||
"""Get the names of all scopes in the given bucket.""" | ||||||||||||||||||||||||||||
cluster = get_cluster_connection(ctx) | ||||||||||||||||||||||||||||
bucket = connect_to_bucket(cluster, bucket_name) | ||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||
scopes = bucket.collections().get_all_scopes() | ||||||||||||||||||||||||||||
return [scope.name for scope in scopes] | ||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||
logger.error(f"Error getting scopes and collections: {e}") | ||||||||||||||||||||||||||||
raise | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def get_collections_in_scope( | ||||||||||||||||||||||||||||
ctx: Context, bucket_name: str, scope_name: str | ||||||||||||||||||||||||||||
) -> list[str]: | ||||||||||||||||||||||||||||
"""Get the names of all collections in the given scope and bucket.""" | ||||||||||||||||||||||||||||
scopes_and_collections = get_scopes_and_collections_in_bucket(ctx, bucket_name) | ||||||||||||||||||||||||||||
if scope_name not in scopes_and_collections: | ||||||||||||||||||||||||||||
logger.error(f"Scope {scope_name} not found in bucket {bucket_name}") | ||||||||||||||||||||||||||||
raise ValueError(f"Scope {scope_name} not found in bucket {bucket_name}") | ||||||||||||||||||||||||||||
return scopes_and_collections[scope_name] | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function calls get_scopes_and_collections_in_bucket which retrieves all scopes and collections, but only returns collections for a specific scope. This is inefficient as it fetches unnecessary data.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function attempts to connect to a bucket when bucket_name is provided, but there's no validation that bucket_name is not an empty string. An empty string would pass the truthiness check but would likely cause connection issues.
Copilot uses AI. Check for mistakes.