sqlmcp

package module
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 20, 2026 License: MIT Imports: 8 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExecArgsModel = model.Definition{
	Name: "exec_args",
	Fields: model.Fields{
		{
			Name:      "SQL",
			Type:      model.Text(),
			NotNull:   true,
			Permitted: sqlPermitted,
		},
	},
}
View Source
var QueryArgsModel = model.Definition{
	Name: "query_args",
	Fields: model.Fields{
		{
			Name:      "SQL",
			Type:      model.Text(),
			NotNull:   true,
			Permitted: sqlPermitted,
		},
	},
}

Functions

This section is empty.

Types

type DaemonProvider

type DaemonProvider struct {
	// contains filtered or unexported fields
}

DaemonProvider implements mcp.ToolProvider for the MCP daemon. Tools are registered at startup; SetDB wires the live connection at runtime.

func NewDaemonProvider

func NewDaemonProvider() *DaemonProvider

NewDaemonProvider creates a new DaemonProvider.

func (*DaemonProvider) SetDB

func (p *DaemonProvider) SetDB(db *orm.DB)

SetDB swaps the active DB. Call with nil when the project stops.

func (*DaemonProvider) SetExportFunc added in v0.0.11

func (p *DaemonProvider) SetExportFunc(fn ExportFunc)

SetExportFunc swaps the active DDL export function. Call with nil when the project stops.

func (*DaemonProvider) Tools

func (p *DaemonProvider) Tools() []mcp.Tool

Tools returns db_schema (always), db_query, db_exec — fixed schemas, no DB required.

type ExecArgs

type ExecArgs struct {
	Sql string
}

func (*ExecArgs) DecodeFields

func (m *ExecArgs) DecodeFields(r model.FieldReader)

func (*ExecArgs) EncodeFields

func (m *ExecArgs) EncodeFields(w model.FieldWriter)

func (*ExecArgs) IsNil

func (m *ExecArgs) IsNil() bool

func (*ExecArgs) ModelName

func (m *ExecArgs) ModelName() string

func (*ExecArgs) Pointers

func (m *ExecArgs) Pointers() []any

func (*ExecArgs) Schema

func (m *ExecArgs) Schema() []model.Field

func (*ExecArgs) Validate

func (m *ExecArgs) Validate(action byte) error

type ExecArgsList

type ExecArgsList []*ExecArgs

func (*ExecArgsList) Append

func (s *ExecArgsList) Append() model.Fielder

func (*ExecArgsList) At

func (s *ExecArgsList) At(i int) model.Fielder

func (*ExecArgsList) DecodeFields

func (s *ExecArgsList) DecodeFields(_ model.FieldReader)

func (*ExecArgsList) EncodeFields

func (s *ExecArgsList) EncodeFields(_ model.FieldWriter)

func (*ExecArgsList) IsNil

func (s *ExecArgsList) IsNil() bool

func (*ExecArgsList) Len

func (s *ExecArgsList) Len() int

func (*ExecArgsList) Pointers

func (s *ExecArgsList) Pointers() []any

func (*ExecArgsList) Schema

func (s *ExecArgsList) Schema() []model.Field

type ExportFunc added in v0.0.11

type ExportFunc func() (string, error)

ExportFunc produces the DDL SQL for the currently synced schema. Supplied by the caller (e.g. ormc.Generator.ExportSQL bound to a dialect compiler) — sqlmcp has no model registry of its own to derive this from since orm dropped Open/Register (see orm docs/PLAN.md §2).

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider implements mcp.ToolProvider for a live *orm.DB connection.

func NewProvider

func NewProvider(db *orm.DB, exportFn ExportFunc) *Provider

NewProvider creates a new MCP tool provider wrapping the given DB and DDL export function. exportFn may be nil — db_export_schema then reports it isn't configured.

func (*Provider) Tools

func (p *Provider) Tools() []mcp.Tool

Tools returns the MCP tools available for this DB connection. db_schema is only included if the underlying connection implements ddl.SchemaInspector.

type QueryArgs

type QueryArgs struct {
	Sql string
}

func (*QueryArgs) DecodeFields

func (m *QueryArgs) DecodeFields(r model.FieldReader)

func (*QueryArgs) EncodeFields

func (m *QueryArgs) EncodeFields(w model.FieldWriter)

func (*QueryArgs) IsNil

func (m *QueryArgs) IsNil() bool

func (*QueryArgs) ModelName

func (m *QueryArgs) ModelName() string

func (*QueryArgs) Pointers

func (m *QueryArgs) Pointers() []any

func (*QueryArgs) Schema

func (m *QueryArgs) Schema() []model.Field

func (*QueryArgs) Validate

func (m *QueryArgs) Validate(action byte) error

type QueryArgsList

type QueryArgsList []*QueryArgs

func (*QueryArgsList) Append

func (s *QueryArgsList) Append() model.Fielder

func (*QueryArgsList) At

func (s *QueryArgsList) At(i int) model.Fielder

func (*QueryArgsList) DecodeFields

func (s *QueryArgsList) DecodeFields(_ model.FieldReader)

func (*QueryArgsList) EncodeFields

func (s *QueryArgsList) EncodeFields(_ model.FieldWriter)

func (*QueryArgsList) IsNil

func (s *QueryArgsList) IsNil() bool

func (*QueryArgsList) Len

func (s *QueryArgsList) Len() int

func (*QueryArgsList) Pointers

func (s *QueryArgsList) Pointers() []any

func (*QueryArgsList) Schema

func (s *QueryArgsList) Schema() []model.Field

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL