Codex or Claude Code owns the conversation and tool-calling loop. This optional
stdio server forwards calls to Lang2SQL's existing tool registry; it does not run
agent_loop, instantiate an LLM provider, or require an OpenAI API key.
The CLI/Discord harness, core ports, SQL safety pipeline and existing tools are
unchanged. HarnessContext is reused with an inert session and an LLM that raises
if called. Concrete dependencies are assembled separately under tenancy/tool_runtime.py.
From the checkout:
uv sync --extra mcp
# Add the matching existing DB extra for other databases, e.g.:
# uv sync --extra mcp --extra postgresOne server process uses one DB connection and one fixed identity. Set the existing
LANG2SQL_DB_URL (and optionally LANG2SQL_DB_SCHEMA) or the existing D1 variables.
The server fails at startup if no connection is configured; it does not use the
CLI's stub DB. Configure a read-only source DB account. Metadata changes still
write to the separate SQLite or Oracle metadata store.
Replace the paths below with absolute paths. The SQLite source database must already exist. These commands register a server in the client; installation of Lang2SQL itself does not change client configuration.
Codex CLI:
codex mcp add lang2sql --env LANG2SQL_DB_URL=sqlite:////absolute/path/analytics.db -- \
/absolute/path/Lang2SQL/.venv/bin/lang2sql-mcp \
--user-id analyst --org-id demo --team-id finance \
--store /absolute/path/lang2sql-metadata.dbClaude Code:
claude mcp add --transport stdio lang2sql \
--env LANG2SQL_DB_URL=sqlite:////absolute/path/analytics.db -- \
/absolute/path/Lang2SQL/.venv/bin/lang2sql-mcp \
--user-id analyst --org-id demo --team-id finance \
--store /absolute/path/lang2sql-metadata.dbCLI argument forms were checked against the locally installed clients' mcp add --help. Protocol integration is tested using the official Python MCP client;
interactive Codex/Claude conversations are not an automated test requirement.
The parent directory for --store must exist. Without --store, metadata and audit
are in-memory. Pass --admin only when the local operator should be able to write
org-level definitions. User/org/team/admin values come from startup arguments,
not model-supplied tool arguments. This is a local, single-identity adapter, not
remote authentication or a multi-tenant HTTP server. Use a distinct metadata store
per DB to avoid mixing definitions between databases. Shared user-level definitions
retain the existing federation semantics across teams within an organization.
| Name | Behavior |
|---|---|
explore_schema |
Discover tables, views, column descriptions and keys. Pass tables to describe several objects together. |
resolve_business_terms |
Check all business terms in the question against the current scope. Pass an empty array for a fully specified request with no business terms. |
run_sql |
Pass the single-use definition_check_id returned by the latest successful check. Optionally validate ordered expected_columns and expected_rows. |
term_custom, context_candidates |
Browse confirmed definitions or stage a user-supplied definition for operator review. |
remember, recall_memory |
Store and retrieve notes for the current user and team. Provide a persistent metadata store to retain them across restarts. |
save_column_notes |
Save confirmed column descriptions using an administrator connection. |
calendar_interval_sql, fixed_decimal_sql, integer_ratio_sql |
Generate SQL for calendar intervals and exact numeric calculations in the supported dialect. |
search_schema |
Find candidate tables when the optional Qdrant index is configured. Verify their columns with explore_schema. |
Resolve terms again after each attempted query, including a failed query. If a business definition is missing, ask for the definition and scope. Use lang2sql-tools decide-context to approve or reject a staged candidate. For an approved Oracle view metric, operators can bind its contract with lang2sql-tools bind-oracle-view. The client then supplies the metric and date interval to run_sql.
Source queries are read-only by default. To let the operator-configured identity execute a single explicitly requested write, start the MCP process with --allow-write and a DB account with the necessary grants. The normal definition check also applies to writes. The separate --admin flag controls metadata administration.
- Tool schemas come from existing
ToolSpec; the MCP boundary rejects extra arguments and the SDK validates declared types before dispatch. - Existing
ToolResult.contentbecomes MCP text content;is_errorbecomesisError. SQL output remains the existing text table (first 50 rows shown), not a new structured result or export API. - SQL
NEEDS CONFIRMATIONis forwarded as existing text without executing the query. This adapter adds no approval bypass or automatic resume mechanism. - Calls share one registry/store and are serialized in a worker thread. Existing synchronous SQLite KV operations do not block the transport event loop. Cancellation of a request does not guarantee cancellation of an executing DB query; the worker retains the store lock until completion.
- Logs use stderr; stdout carries only MCP messages. No HTTP server or listener is started. Environment credentials are not advertised as tool parameters.
- Existing DB adapter and SQL safety limitations remain; this PR does not add database-side timeout guarantees or a new SQL parser.
uv run --extra mcp pytest -q
uv run --extra mcp python bench/ecommerce_demo.pyThe stdio tests launch the actual server process and check handshake, tool listing, schema discovery, SQL execution, blocked DDL, input errors, unavailable tools, metadata permission checks, team scoping, persistence and process-local notes.
For direct command-line calls using the same runtime, see tool CLI.