Skip to content

Commit 5b79891

Browse files
teetanghnithishr
andauthored
(feat) couchbase doc crud operations (#3)
* Add document management functions to MCP server - Implemented `get_document` to retrieve a document by ID. - Added `upsert_document` for inserting or updating documents. - Created `delete_document` to remove documents by ID. - Enhanced error handling and logging for each operation. * Add function to list documents in a specified collection - Introduced `list_documents_in_collection` to retrieve document IDs and content from a specified collection. - Implemented error handling for cluster availability and query execution. - Added logging for query operations to enhance traceability. * fixed feedback based on comments * Refactor document management functions in MCP server - Renamed `get_document` to `get_document_by_id` for clarity. - Renamed `upsert_document` to `upsert_document_by_id` to specify operation by ID. - Renamed `delete_document` to `delete_document_by_id` for consistency. - Updated docstrings to reflect changes and improve documentation. - Enhanced error logging by removing type information for cleaner output. * Update README.md to reflect enhanced document management features - Added descriptions for new functionalities: retrieving, upserting, and deleting documents by ID from specified scopes and collections. --------- Co-authored-by: Nithish Raghunandanan <[email protected]>
1 parent 2f701f3 commit 5b79891

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ An [MCP](https://modelcontextprotocol.io/) server implementation of Couchbase th
44

55
## Features
66

7-
- Get a list of all the scopes and collections in the specified bucket in a Couchbase cluster
8-
- Get the structure for a collection in a Couchbase cluster
9-
- Run a [SQL++ query](https://www.couchbase.com/sqlplusplus/) against data in Couchbase cluster bucket
7+
- Get a list of all the scopes and collections in the specified bucket
8+
- Get the structure for a collection
9+
- Get a document by ID from a specified scope and collection
10+
- Upsert a document by ID to a specified scope and collection
11+
- Delete a document by ID from a specified scope and collection
12+
- Run a [SQL++ query](https://www.couchbase.com/sqlplusplus/) on a specified scope
1013

1114
## Prerequisites
1215

src/mcp_server.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,59 @@ def get_schema_for_collection(
122122
raise
123123

124124

125+
@mcp.tool()
126+
def get_document_by_id(
127+
ctx: Context, scope_name: str, collection_name: str, document_id: str
128+
) -> dict[str, Any]:
129+
"""Get a document by its ID from the specified scope and collection."""
130+
bucket = ctx.request_context.lifespan_context.bucket
131+
try:
132+
collection = bucket.scope(scope_name).collection(collection_name)
133+
result = collection.get(document_id)
134+
return result.content_as[dict]
135+
except Exception as e:
136+
logger.error(f"Error getting document {document_id}: {e}")
137+
raise
138+
139+
140+
@mcp.tool()
141+
def upsert_document_by_id(
142+
ctx: Context,
143+
scope_name: str,
144+
collection_name: str,
145+
document_id: str,
146+
document_content: dict[str, Any],
147+
) -> bool:
148+
"""Insert or update a document by its ID.
149+
Returns True on success, False on failure."""
150+
bucket = ctx.request_context.lifespan_context.bucket
151+
try:
152+
collection = bucket.scope(scope_name).collection(collection_name)
153+
collection.upsert(document_id, document_content)
154+
logger.info(f"Successfully upserted document {document_id}")
155+
return True
156+
except Exception as e:
157+
logger.error(f"Error upserting document {document_id}: {e}")
158+
return False
159+
160+
161+
@mcp.tool()
162+
def delete_document_by_id(
163+
ctx: Context, scope_name: str, collection_name: str, document_id: str
164+
) -> bool:
165+
"""Delete a document by its ID.
166+
Returns True on success, False on failure."""
167+
bucket = ctx.request_context.lifespan_context.bucket
168+
try:
169+
collection = bucket.scope(scope_name).collection(collection_name)
170+
collection.remove(document_id)
171+
logger.info(f"Successfully deleted document {document_id}")
172+
return True
173+
except Exception as e:
174+
logger.error(f"Error deleting document {document_id}: {e}")
175+
return False
176+
177+
125178
@mcp.tool()
126179
def run_sql_plus_plus_query(
127180
ctx: Context, scope_name: str, query: str

0 commit comments

Comments
 (0)