|
| 1 | +# SQLite MCP (Model Context Protocol) Server |
| 2 | + |
| 3 | +A server-side implementation of the Model Context Protocol (MCP) for SQLite databases, enabling AI applications to interact with **multiple SQLite databases** through a standardized protocol. Each database must be registered before use, allowing dynamic database management and multi-database operations. |
| 4 | + |
| 5 | +## Project Structure |
| 6 | + |
| 7 | +``` |
| 8 | +sqlite-mcp-server/ |
| 9 | +├── cmd/ |
| 10 | +│ └── server/ # Main application entry point |
| 11 | +├── internal/ |
| 12 | +│ ├── mcp/ # MCP implementation |
| 13 | +│ │ ├── tools/ # Tool implementations |
| 14 | +│ │ ├── resources/ # Resource implementations |
| 15 | +│ │ └── prompts/ # Prompt templates |
| 16 | +│ └── db/ # Database management |
| 17 | +│ └── migrations/ # Database migrations |
| 18 | +``` |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +### Database Management Tools |
| 23 | +- `db/register_database`: Register a new SQLite database for use |
| 24 | +- `db/list_databases`: List all registered databases |
| 25 | + |
| 26 | +### Database Operation Tools |
| 27 | +- `db/get_table_schema`: Get schema for a specific table in a database |
| 28 | +- `db/insert_record`: Insert a new record into a table |
| 29 | +- `db/query`: Execute a read-only SQL query on a specific database |
| 30 | +- `db/get_tables`: List all tables in a specific database |
| 31 | +- `db/get_schema`: Get full schema of a specific database |
| 32 | + |
| 33 | +### Resources |
| 34 | +- `db/databases`: List of all registered databases |
| 35 | + |
| 36 | +### Prompts |
| 37 | +- `db/multi_database_help`: Overview of multi-database capabilities |
| 38 | +- `db/register_help`: Help for registering databases |
| 39 | +- `db/query_help`: Help text for constructing queries |
| 40 | +- `db/schema_help`: Help text for understanding schemas |
| 41 | +- `db/insert_help`: Help text for inserting records |
| 42 | + |
| 43 | +## Prerequisites |
| 44 | + |
| 45 | +- Go 1.21 or later |
| 46 | +- SQLite 3 |
| 47 | + |
| 48 | +## Installation |
| 49 | + |
| 50 | +```bash |
| 51 | +go install github.com/nipunap/sqlite-mcp-server@latest |
| 52 | +``` |
| 53 | + |
| 54 | +## Usage |
| 55 | + |
| 56 | +### Starting the Server |
| 57 | + |
| 58 | +Run as a local MCP server: |
| 59 | + |
| 60 | +```bash |
| 61 | +# Start with registry only (no default database) |
| 62 | +sqlite-mcp-server --registry registry.db |
| 63 | + |
| 64 | +# Start with registry and register a default database |
| 65 | +sqlite-mcp-server --registry registry.db --db path/to/default.sqlite |
| 66 | +``` |
| 67 | + |
| 68 | +The server communicates via STDIO using JSON-RPC 2.0 messages. |
| 69 | + |
| 70 | +### Multi-Database Workflow |
| 71 | + |
| 72 | +1. **Register a database**: |
| 73 | +```json |
| 74 | +{ |
| 75 | + "jsonrpc": "2.0", |
| 76 | + "id": 1, |
| 77 | + "method": "invoke", |
| 78 | + "params": { |
| 79 | + "name": "db/register_database", |
| 80 | + "params": { |
| 81 | + "name": "users_db", |
| 82 | + "path": "/absolute/path/to/users.sqlite", |
| 83 | + "description": "User management database", |
| 84 | + "readonly": false, |
| 85 | + "owner": "app_user" |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +2. **List registered databases**: |
| 92 | +```json |
| 93 | +{ |
| 94 | + "jsonrpc": "2.0", |
| 95 | + "id": 2, |
| 96 | + "method": "invoke", |
| 97 | + "params": { |
| 98 | + "name": "db/list_databases", |
| 99 | + "params": {} |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +3. **Query a specific database**: |
| 105 | +```json |
| 106 | +{ |
| 107 | + "jsonrpc": "2.0", |
| 108 | + "id": 3, |
| 109 | + "method": "invoke", |
| 110 | + "params": { |
| 111 | + "name": "db/query", |
| 112 | + "params": { |
| 113 | + "database_name": "users_db", |
| 114 | + "query": "SELECT * FROM users WHERE id = ?", |
| 115 | + "args": [1] |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +4. **Get tables from a specific database**: |
| 122 | +```json |
| 123 | +{ |
| 124 | + "jsonrpc": "2.0", |
| 125 | + "id": 4, |
| 126 | + "method": "invoke", |
| 127 | + "params": { |
| 128 | + "name": "db/get_tables", |
| 129 | + "params": { |
| 130 | + "database_name": "users_db" |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +5. **Insert into a specific database**: |
| 137 | +```json |
| 138 | +{ |
| 139 | + "jsonrpc": "2.0", |
| 140 | + "id": 5, |
| 141 | + "method": "invoke", |
| 142 | + "params": { |
| 143 | + "name": "db/insert_record", |
| 144 | + "params": { |
| 145 | + "database_name": "users_db", |
| 146 | + "table_name": "users", |
| 147 | + "data": { |
| 148 | + "name": "John Doe", |
| 149 | + "email": "john@example.com" |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Development |
| 157 | + |
| 158 | +1. Clone the repository: |
| 159 | +```bash |
| 160 | +git clone https://github.com/nipunap/sqlite-mcp-server.git |
| 161 | +cd sqlite-mcp-server |
| 162 | +``` |
| 163 | + |
| 164 | +2. Install dependencies: |
| 165 | +```bash |
| 166 | +go mod download |
| 167 | +``` |
| 168 | + |
| 169 | +3. Run tests: |
| 170 | +```bash |
| 171 | +go test ./... |
| 172 | +``` |
| 173 | + |
| 174 | +## Contributing |
| 175 | + |
| 176 | +1. Fork the repository |
| 177 | +2. Create your feature branch (`git checkout -b feature/amazing-feature`) |
| 178 | +3. Commit your changes (`git commit -m 'Add some amazing feature'`) |
| 179 | +4. Push to the branch (`git push origin feature/amazing-feature`) |
| 180 | +5. Open a Pull Request |
| 181 | + |
| 182 | +## License |
| 183 | + |
| 184 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments