Skip to main content
Glama
INSTALLATION_GUIDE.md9.99 kB
# Installation Guide - Using SQLite MCP in Your Chatbot Complete guide for installing and using SQLite MCP as a library in your separate chatbot project. ## Project Structure ``` Your Computer: ├── /path/to/sqlite-mcp/ (MCP Server - this project) │ ├── main.py │ ├── mcp_client.py │ ├── setup.py │ └── sqlite_mcp/ │ └── /path/to/your-chatbot/ (Your Chatbot - separate project) ├── venv/ ├── requirements.txt └── chatbot_script.py ``` --- ## Installation in Your Chatbot Project ### Step 1: Verify Your Setup First, verify your chatbot project structure: ```bash # Navigate to your chatbot project cd /path/to/your-chatbot # Verify virtual environment exists ls venv/ # Activate it source venv/bin/activate # macOS/Linux # or venv\Scripts\activate # Windows ``` ### Step 2: Install SQLite MCP Package You have 2 options for installation: #### **Option A: Install from Local Path (Recommended for Development)** ```bash # In your chatbot project with venv activated pip install /path/to/sqlite-mcp # Example: pip install /Users/supriyadi/Projects/MCP/SQL-MCP-SERVER/sqlite-mcp ``` This installs the package in your venv. Updates require reinstalling. #### **Option B: Install in Editable Mode (Best for Development)** ```bash # In your chatbot project with venv activated pip install -e /path/to/sqlite-mcp # Example: pip install -e /Users/supriyadi/Projects/MCP/SQL-MCP-SERVER/sqlite-mcp ``` **Benefits of editable mode (-e flag):** - Changes to sqlite-mcp are immediately reflected - No need to reinstall after changes - Perfect for active development ### Step 3: Verify Installation ```bash # Check what was installed pip list | grep sqlite-mcp # Test import python -c "from mcp_client import MCPClient; print('✅ Import successful')" ``` Expected output: ``` sqlite-mcp 1.0.0 ✅ Import successful ``` --- ## Using MCP in Your Chatbot ### Method 1: Direct Client Access ```python # your_chatbot.py from mcp_client import MCPClient def main(): # Create client client = MCPClient("http://localhost:8000") # Open database client.open_database("./data/sample_data.db") # Query data result = client.execute_query("SELECT * FROM Spam_number LIMIT 5") print(f"Found {result.record_count} records") for row in result.rows: print(f" - {row['mobileNumber']}: {row['Name']}") # Close database client.close_database() if __name__ == "__main__": main() ``` ### Method 2: Using the Chatbot Class ```python # your_chatbot.py from chatbot_example import SQLiteChatbot def main(): # Initialize chatbot chatbot = SQLiteChatbot( api_key="your-sahabat-ai-api-key", base_url="http://your-sahabat-ai-server", mcp_server_url="http://localhost:8000" ) # Chat with database response = chatbot.chat("Show me all spam numbers") print(response) response = chatbot.chat("How many KTP records are there?") print(response) if __name__ == "__main__": main() ``` ### Method 3: Using OpenAI Function Tools ```python # your_chatbot.py from openai_tools import get_openai_tools from openai import OpenAI def main(): # Initialize OpenAI client for sahabat-ai client = OpenAI( api_key="your-sahabat-ai-api-key", base_url="http://your-sahabat-ai-server" ) # Get tools tools = get_openai_tools() # Use with function calling response = client.chat.completions.create( model="sahabat-ai", messages=[{"role": "user", "content": "Query the database"}], tools=tools, tool_choice="auto" ) if __name__ == "__main__": main() ``` --- ## Running Your Chatbot with MCP ### Terminal 1: Start MCP Server ```bash # Navigate to sqlite-mcp directory cd /path/to/sqlite-mcp # Start the server uvicorn main:app --reload # Or python main.py # Expected output: # 🚀 SQLite MCP HTTP API Server Started # INFO: Uvicorn running on http://0.0.0.0:8000 ``` ### Terminal 2: Run Your Chatbot ```bash # Navigate to your chatbot directory cd /path/to/your-chatbot # Activate venv source venv/bin/activate # Set environment variables (if using sahabat-ai) export SAHABAT_AI_API_KEY="your-api-key" export SAHABAT_AI_BASE_URL="http://your-sahabat-ai-server" # Run your chatbot python your_chatbot_script.py ``` --- ## Adding to requirements.txt You can also add the local path to your chatbot's `requirements.txt`: ```txt # requirements.txt in your chatbot project # Your other dependencies fastapi>=0.104.0 uvicorn>=0.24.0 # SQLite MCP (local installation) sqlite-mcp @ file:///path/to/sqlite-mcp # Example: # sqlite-mcp @ file:///Users/supriyadi/Projects/MCP/SQL-MCP-SERVER/sqlite-mcp ``` Then install all requirements: ```bash pip install -r requirements.txt ``` --- ## What Gets Installed When you install sqlite-mcp, the following modules are available in your chatbot: ```python # Direct imports from mcp_client import MCPClient, MCPClientError from mcp_client import QueryResult, InsertResult, UpdateResult, DeleteResult from openai_tools import get_openai_tools, get_tool_by_name, TOOL_MAP from chatbot_example import SQLiteChatbot from sqlite_mcp.db import SQLiteDatabase ``` --- ## Example Chatbot Project Structure ``` your-chatbot/ ├── venv/ │ └── lib/python3.x/site-packages/ │ ├── mcp_client.py │ ├── openai_tools.py │ ├── chatbot_example.py │ ├── sqlite_mcp/ │ └── ... (other packages) │ ├── requirements.txt │ # sqlite-mcp @ file:///path/to/sqlite-mcp │ ├── your_chatbot_script.py │ # from mcp_client import MCPClient │ # from chatbot_example import SQLiteChatbot │ └── config.py ``` --- ## Troubleshooting Installation ### Issue: "No module named 'mcp_client'" ```bash # Solution 1: Verify installation pip list | grep sqlite-mcp # Solution 2: Reinstall pip install --upgrade /path/to/sqlite-mcp # Solution 3: Check venv is activated which python # Should show path to venv/bin/python ``` ### Issue: "Cannot connect to MCP server" ```bash # Solution: Start the MCP server first cd /path/to/sqlite-mcp uvicorn main:app --reload # Then check curl http://localhost:8000/health ``` ### Issue: "FileNotFoundError: sample_data.db" ```python # Use absolute path client.open_database("/Users/supriyadi/Projects/MCP/SQL-MCP-SERVER/sqlite-mcp/data/sample_data.db") # Or relative to your chatbot script import os db_path = os.path.join(os.path.dirname(__file__), "../sqlite-mcp/data/sample_data.db") client.open_database(db_path) ``` ### Issue: "sqlite-mcp @ file://... not found" ```bash # Make sure the path in requirements.txt is absolute and correct # Example: # sqlite-mcp @ file:///Users/supriyadi/Projects/MCP/SQL-MCP-SERVER/sqlite-mcp # Or reinstall manually pip install -e /path/to/sqlite-mcp ``` --- ## Development Workflow ### When You're Developing Both Projects Use editable installation: ```bash # In your chatbot venv pip install -e /path/to/sqlite-mcp ``` Now: - Changes to sqlite-mcp are immediately available - No reinstall needed - Perfect for iterating on both projects ### When You're Only Using SQLite MCP Use regular installation: ```bash # In your chatbot venv pip install /path/to/sqlite-mcp ``` For updates: ```bash pip install --upgrade /path/to/sqlite-mcp ``` --- ## Example: Complete Chatbot Script ```python #!/usr/bin/env python """Example chatbot using SQLite MCP library.""" import os from mcp_client import MCPClient, MCPClientError def main(): """Main chatbot function.""" print("🤖 Chatbot with SQLite MCP") print("=" * 50) try: # Create MCP client client = MCPClient("http://localhost:8000") print("✅ Connected to MCP server") # Open database db_path = "./data/sample_data.db" print(f"\n📂 Opening database: {db_path}") client.open_database(db_path) print("✅ Database opened") # Interactive loop while True: print("\n" + "=" * 50) print("Commands: list, count, query, insert, exit") command = input("Enter command: ").strip().lower() if command == "exit": break elif command == "list": tables = client.list_tables() print(f"Tables: {', '.join(tables)}") elif command == "count": table = input("Table name: ").strip() result = client.execute_query(f"SELECT COUNT(*) as count FROM {table}") count = result.rows[0]["count"] print(f"Total records in {table}: {count}") elif command == "query": query = input("SQL query: ").strip() result = client.execute_query(query) print(f"Found {result.record_count} records:") for row in result.rows[:5]: print(f" {row}") if result.record_count > 5: print(f" ... and {result.record_count - 5} more") elif command == "insert": print("Insert example: INSERT INTO table (col1, col2) VALUES (val1, val2)") # Would implement insert here else: print("Unknown command") # Close database client.close_database() print("\n✅ Database closed") print("Goodbye!") except MCPClientError as e: print(f"❌ MCP Error: {e}") except Exception as e: print(f"❌ Error: {e}") import traceback traceback.print_exc() if __name__ == "__main__": main() ``` --- ## Next Steps 1. ✅ Install sqlite-mcp in your chatbot venv 2. ✅ Start the MCP server in one terminal 3. ✅ Import and use in your chatbot 4. ✅ Run your chatbot in another terminal You're all set! 🎉

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/irpus1603/mcp_sqlite'

If you have feedback or need assistance with the MCP directory API, please join our Discord server