A high-performance SQLite MCP (Model Context Protocol) server with an experimental code generation approach called Codemode that enables LLMs to execute database operations through generated Go code rather than sequential tool calls.
Quickstart
Installation
MCP Codemode based SQLITE server
星數
8
工具
—
可用率
—
go install github.com/imran31415/codemode-sqlite-mcp@latest
Or build from source:
git clone https://github.com/imran31415/codemode-sqlite-mcp.git
cd codemode-sqlite-mcp
go build -o codemode-sqlite-mcp .
Instead of making multiple tool calls, the Codemode agent:
Receives a natural language task (e.g., "Find the top 5 customers by spending")
Generates a complete Go program that accomplishes the task
Executes the program in a sandboxed interpreter (yaegi)
Returns the output
The generated code has access to a registryCall function that invokes database tools:
package main
import "fmt"
func main() {
result, err := registryCall("query", map[string]interface{}{
"sql": `SELECT c.Name, SUM(i.Total) as TotalSpent
FROM customers c
JOIN invoices i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId
ORDER BY TotalSpent DESC
LIMIT 5`,
})
if err != nil {
fmt.Println("Error:", err)
return
}
// Process and format results...
}
Why Code Generation?
Traditional MCP tool calling requires multiple round-trips between the LLM and the tool server:
LLM → list_tables → result → LLM → get_schema → result → LLM → query → result → LLM
Each round-trip adds latency and token overhead. Complex operations may require 5-15 tool calls.
With Codemode, the LLM reasons about the entire task upfront and generates a single program:
LLM → generates code → execute once → result
Benchmark Results
Testing on complex multi-table queries (Chinook database):
Metric
Standard MCP
Codemode
Improvement
Success Rate
100%
100%
-
Avg Latency
18.8s
9.0s
2.1x faster
Avg Tokens
10,940
1,859
5.9x fewer
Tool Calls
5.6 avg
1
-
Token savings of 83% and latency reduction of 52% on complex analytical queries.
package main
import (
"github.com/imran31415/codemode-sqlite-mcp/pkg/mcp"
"github.com/imran31415/codemode-sqlite-mcp/pkg/tools"
)
func main() {
tools.InitDB("./data.db")
defer tools.CloseDB()
registry := tools.NewRegistry()
server := mcp.NewServer(registry)
// Use STDIO transport (for Claude Desktop integration)
transport := mcp.NewStdioTransport(server)
transport.Run()
// Or use HTTP transport
// httpTransport := mcp.NewHTTPTransport(server, "8084")
// httpTransport.Run()
}
Executor: Sandboxed Code Execution
The executor uses yaegi, a Go interpreter, to run LLM-generated code safely:
codemode-sqlite-mcp [OPTIONS]
OPTIONS:
--mode=MODE Server mode: stdio, http, or codemode (default: stdio)
--port=PORT HTTP port for http mode (default: 8084)
--db=PATH Path to SQLite database (default: codemode.db)
--api-key=KEY Anthropic API key (required for codemode mode)
--model=MODEL LLM model to use (optional)
--init-db Initialize database with sample data and exit
--help Show help message
MODES:
stdio MCP server with stdio transport (for Claude Desktop)
http MCP server with HTTP transport
codemode Interactive agent with LLM code generation
Code Validation: Basic structural validation before execution
No Filesystem Access: Generated code cannot access the filesystem directly
For production use, additional review of the validator rules is recommended based on your security requirements.
Limitations
Generated code is limited to available standard library packages
Complex data transformations may require multiple LLM attempts
The interpreter is slower than compiled Go (acceptable for database I/O bound tasks)
Currently supports Anthropic API only (OpenAI support planned)
Examples
The examples/ directory contains runnable examples:
# Basic usage - direct tool calls and Codemode agent
go run ./examples/basic
# Embed MCP server in your own HTTP application
go run ./examples/embed-server
# Custom executor with injected symbols
go run ./examples/custom-executor
Running Benchmarks
# Build the benchmark tool
go build -o bin/benchmark ./cmd/benchmark/...
# Run Chinook database benchmark
ANTHROPIC_API_KEY="your-key" ./bin/benchmark --mode=chinook
# Run simple comparison benchmark
ANTHROPIC_API_KEY="your-key" ./bin/benchmark --mode=comparison