Overview
This example demonstrates integrating a Couchbase database with an AI agent using the Model Context Protocol (MCP). The agent can understand natural language queries, interact with Couchbase to retrieve or manipulate data, and provide meaningful responses about hotels, airports, airlines, routes, and landmarks.Features
- Natural Language Queries - Ask questions in plain English
- SQL++ Generation - Automatically generate Couchbase queries
- Travel Sample Database - Pre-configured with travel data
- MCP Integration - Standard protocol for database access
- Jupyter Notebook - Interactive development environment
- Comprehensive Schema - Hotels, airports, airlines, routes, landmarks
Introduction to Model Context Protocol (MCP)
The Model Context Protocol (MCP) is an open standard designed to standardize how AI assistants and applications connect to and interact with external data sources, tools, and systems.Key Goals and Features
- Standardized Communication - Common language for AI-to-backend communication
- Enhanced Context Management - Efficient handling of LLM context windows
- Secure Data Access - Controlled infrastructure exposure
- Tool Use and Actionability - Retrieve information and trigger actions
- Interoperability - Ecosystem where AI tools work together
- Client-Server Architecture:
- MCP Hosts/Clients: Applications that access data
- MCP Servers: Programs exposing data sources through MCP
Architecture
Prerequisites
- Python 3.8+
- Jupyter Notebook or JupyterLab
- Couchbase Server with
travel-sampledatabase - Nebius API key
mcp-server-couchbaseinstalled
Installation
Database Structure
Couchbase organizes data hierarchically:Collection
Equivalent of a table in relational databasesCollections:
airport, airline, route, landmark, hotelImplementation
Notebook Setup
Createmain.ipynb:
Configure OpenAI Client
Define Query Function
Create Agent with MCP
Main Execution
MCP Integration Details
MCPServerStdio Configuration
Available MCP Tools
Themcp-server-couchbase provides:
mcp_couchbase_run_sql_plus_plus_query
mcp_couchbase_run_sql_plus_plus_query
Execute SQL++ (N1QL) queries against Couchbase.Example:
mcp_couchbase_get_scopes_and_collections_in_bucket
mcp_couchbase_get_scopes_and_collections_in_bucket
List all scopes and collections in the bucket.Returns:
mcp_couchbase_get_document
mcp_couchbase_get_document
Retrieve a specific document by key.Parameters:
collection: Collection namedocument_id: Document key
mcp_couchbase_insert_document
mcp_couchbase_insert_document
Insert a new document.Parameters:
collection: Collection namedocument: JSON document
mcp_couchbase_update_document
mcp_couchbase_update_document
Update an existing document.
mcp_couchbase_delete_document
mcp_couchbase_delete_document
Delete a document by key.
Query Examples
- Database Info
- Top Hotels
- Travel Recommendation
- UK Sightseeing
- Budget Hotel
Query:Agent Response:
SQL++ Query Generation
The agent generates Couchbase SQL++ queries following these rules:Query Format
Backtick Usage
Common Patterns
Workflow
Best Practices
Agent Instructions
- Provide clear database structure
- Specify scope to use
- Define query formatting rules
- Include backtick requirements
Query Design
- Use only collection names in FROM
- Enclose all identifiers in backticks
- Specify scope in instructions
- Use appropriate LIMIT clauses
Error Handling
- Handle connection errors gracefully
- Validate query syntax
- Provide clear error messages
- Log MCP tool errors
Performance
- Use indexes for common queries
- Limit result set sizes
- Cache schema information
- Monitor query execution time
Troubleshooting
Connection Failed
Connection Failed
Check Couchbase credentials in
.env:MCP Server Not Starting
MCP Server Not Starting
Verify paths and permissions:
Query Syntax Errors
Query Syntax Errors
Common issues:
- Missing backticks around identifiers
- Including scope in FROM clause
- Wrong field paths in nested JSON
Empty Results
Empty Results
- Verify travel-sample is loaded
- Check scope/collection names
- Review query filters
- Test query in Couchbase Console
Advanced Features
Custom Collections
Add your own collections:Multi-Scope Support
Extend to multiple scopes:Aggregation Queries
Source Code
View the complete implementation at:~/workspace/source/mcp_ai_agents/couchbase_mcp_server/
Couchbase
Couchbase database platform
MCP Server Couchbase
Official MCP server
Related Examples
Database MCP Agent
GibsonAI database management
Custom MCP Server
Build custom MCP servers