Skip to content

tinywasm/sqlmcp

Repository files navigation

tinywasm/sqlmcp

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

import "github.com/tinywasm/sqlmcp"

Tools

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

Usage

Provider (live connection)

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_schema is only included when db.RawConn() implements ddl.SchemaInspector.

DaemonProvider (hot-swap)

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".

Args models

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 JSON

Both models include a full SQL whitelist in their Permitted set (letters, numbers, operators, punctuation) so model.Text() validation passes for real SQL strings.

SQL validation

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.

About

MCP daemon provider exposing tinywasm/orm databases as MCP tools

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages