MCP tool provider for tinywasm/orm.
Exposes four tools — db_schema, db_query, db_exec, db_export_schema — to any MCP client (Claude Code, etc.).
The JSON Schema for each tool's inputSchema is generated by tinywasm/mcp from Tool.Args; sqlmcp does not produce JSON Schema itself.
import "github.com/tinywasm/sqlmcp"| Tool | Action | Args | Description |
|---|---|---|---|
db_schema |
read | — | Lists all tables with columns, types, and constraints |
db_query |
read | SQL (string) |
Executes a SELECT/WITH query and returns results as text |
db_exec |
write | SQL (string) |
Executes INSERT, UPDATE, DELETE, DDL statements |
db_export_schema |
read | — | Exports full CREATE TABLE DDL via the caller-supplied ExportFunc |
Use Provider when you already have a *orm.DB at startup. orm.New takes a single
storage.Conn (e.g. from sqlite.Open(dsn) or postgres.Open(dsn)); db_export_schema has no
model registry to draw on (orm dropped Open/Register), so the caller supplies the DDL export
logic explicitly as an ExportFunc:
conn, _ := sqlite.Open(dsn)
db := orm.New(conn)
exportFn := func() (string, error) { return ormcGenerator.ExportSQL(rootDir, sqlt.NewCompiler()) }
provider := sqlmcp.NewProvider(db, exportFn)
srv, _ := mcp.NewServer(cfg, nil)
for _, tool := range provider.Tools() {
srv.AddTool(tool)
}
db_schemais only included whendb.RawConn()implementsddl.SchemaInspector.
Use DaemonProvider when the DB connection is wired at runtime (e.g. a dev daemon that connects after the MCP server starts):
provider := sqlmcp.NewDaemonProvider()
srv, _ := mcp.NewServer(cfg, nil)
for _, tool := range provider.Tools() { // registered at startup, DB not required yet
srv.AddTool(tool)
}
// later, when the project starts:
provider.SetDB(db)
provider.SetExportFunc(exportFn)
// when the project stops:
provider.SetDB(nil)
provider.SetExportFunc(nil)DaemonProvider is goroutine-safe. All four tools are always registered; calls before SetDB return "no database configured".
QueryArgs and ExecArgs are generated by ormc from models.go and expose Schema() []model.Field, which mcp uses to build the JSON Schema:
type QueryArgs struct { Sql string } // field name "SQL" in JSON
type ExecArgs struct { Sql string } // field name "SQL" in JSONBoth models include a full SQL whitelist in their Permitted set (letters, numbers, operators, punctuation) so model.Text() validation passes for real SQL strings.
db_query rejects any statement that does not begin with SELECT or WITH:
db_query only accepts SELECT or WITH statements; use db_exec for mutations
db_exec accepts any statement; enforce access control at the MCP layer via mcp.Config.Authorize.