grain-tools 0.0.1 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Mehul Fadnavis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,23 +1,377 @@
1
- # grain-tools
1
+ # Grain
2
2
 
3
- Reserved for [Grain](https://grain.tools) — a local SQL execution layer that coding
4
- agents can drive over MCP, a CLI, or the VS Code extension.
3
+ **A fast SQL workspace for VS Code and MCP-enabled AI agents.**
5
4
 
6
- **Nothing is published here yet.** This version reserves the name while the first
7
- release is prepared. When it ships, this package will provide the stdio MCP server:
5
+ Run SQL in VS Code, manage database profiles securely, inspect results in a
6
+ high-performance grid, and expose safe, visible database tools to Claude, Cursor,
7
+ Copilot, and other Model Context Protocol clients.
8
+
9
+ [![VS Code Marketplace](https://vsmarketplacebadges.dev/version/pattrnlabs.grain.svg)](https://marketplace.visualstudio.com/items?itemName=pattrnlabs.grain)
10
+ [![Installs](https://vsmarketplacebadges.dev/installs/pattrnlabs.grain.svg)](https://marketplace.visualstudio.com/items?itemName=pattrnlabs.grain)
11
+ [![OpenVSX](https://img.shields.io/open-vsx/v/pattrnlabs/grain?label=OpenVSX&color=a60ee5)](https://open-vsx.org/extension/pattrnlabs/grain)
12
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
13
+
14
+ ---
15
+
16
+ ## Why Grain?
17
+
18
+ Grain is built for the workflow where humans and agents both need real
19
+ database context:
20
+
21
+ - **Query from the editor**: open a `.sql` file, press `Cmd+Enter` or
22
+ `Ctrl+Enter`, and inspect results without leaving VS Code.
23
+ - **Manage real connection profiles**: create, test, select, and migrate
24
+ database connections from the Grain panel.
25
+ - **Give agents safe, visible access**: expose read-only MCP tools for running
26
+ queries, listing schemas and tables, describing columns, and inspecting shared
27
+ result tabs. Agent queries are restricted to read-only SQL by default, and an
28
+ agent attached through the packaged bridge cannot change your saved connections
29
+ or ask for your stored credentials.
30
+ - **Stay local-first**: there is no hosted Grain service and no bundled
31
+ AI model. Credentials are never kept in a configuration file; the service
32
+ picks an operating system credential helper, the OS keychain, VS Code's Secret
33
+ Storage, or — only when none is available — a local file protected by
34
+ filesystem permissions, in that order of preference.
35
+
36
+ ---
37
+
38
+ ## Highlights
39
+
40
+ - **Connection manager** with dynamic connector forms, active profile selection,
41
+ legacy settings migration, and per-connection password actions.
42
+ - **Tabbed result grid** powered by AG Grid with sorting, filtering, column type
43
+ display, adjustable density, TSV copy, and full export to CSV, TSV, or JSON
44
+ that re-runs the query to fetch every row.
45
+ - **Daemon-backed execution** so heavy queries do not block the editor and
46
+ results can be shared across VS Code, browser, and MCP surfaces.
47
+ - **MCP server** for Claude Desktop, Cursor, Copilot, and other MCP clients,
48
+ with safe mode enabled by default for read-only operation.
49
+ - **Multi-surface sessions** so agent-triggered and editor-triggered work can be
50
+ inspected through the same local daemon without tab ID collisions.
51
+ - **Transparent telemetry** that follows your VS Code telemetry setting and can
52
+ be turned off at any time. Crash reports carry redacted stack traces, with
53
+ file paths reduced to their extension and usernames removed. It never sends
54
+ SQL text, results, credentials, hostnames, database names,
55
+ schema/table/column names, workspace names, or raw driver error messages. The
56
+ separately controlled replay capability masks every rendered character and
57
+ input.
58
+
59
+ ---
60
+
61
+ ## Supported Databases
62
+
63
+ Grain ships connector metadata and setup forms for:
64
+
65
+ | Database | Connector |
66
+ | -------------- | --------------- |
67
+ | PostgreSQL | `postgres` |
68
+ | MySQL | `mysql` |
69
+ | SQLite | `sqlite` |
70
+ | DuckDB | `duckdb` |
71
+ | Trino / Presto | `trino` |
72
+ | Elasticsearch | `elasticsearch` |
73
+ | Snowflake | `snowflake` |
74
+ | BigQuery | `bigquery` |
75
+ | SQL Server | `mssql` |
76
+
77
+ Trino/Presto and PostgreSQL are bundled into the extension build. Native and
78
+ cloud-provider connectors are lazy-loaded and may require their driver runtime in
79
+ the active environment; Grain reports guided missing-driver errors instead
80
+ of failing silently.
81
+
82
+ ---
83
+
84
+ ## Quick Start: VS Code
85
+
86
+ 1. Install Grain from the
87
+ [VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=pattrnlabs.grain)
88
+ or [OpenVSX](https://open-vsx.org/extension/pattrnlabs/grain).
89
+ 2. Run **Grain: Manage Connections** from the Command Palette.
90
+ 3. Add a connection profile, set its password, test it, and make it active.
91
+ 4. Open a `.sql` file and press **`Cmd+Enter`** on macOS or **`Ctrl+Enter`** on
92
+ Windows/Linux.
93
+
94
+ Results open in the **Grain** panel at the bottom of the editor. To force a
95
+ new tab, run **Grain: Run Query in New Tab**.
96
+
97
+ ### Migrating from Legacy Settings
98
+
99
+ If you previously configured `sqlPreview.host`, `sqlPreview.port`,
100
+ `sqlPreview.user`, `sqlPreview.catalog`, `sqlPreview.schema`, or
101
+ `sqlPreview.databasePath`, open **Grain: Show Welcome Guide** or
102
+ **Grain: Manage Connections**. Grain can import those settings into a
103
+ named connection profile and keep passwords in VS Code Secret Storage.
104
+
105
+ ---
106
+
107
+ ## Quick Start: MCP Agents
108
+
109
+ ### One command
110
+
111
+ ```
112
+ npx -y grain-tools --stdio
113
+ ```
114
+
115
+ That is the command every MCP client is given, on every platform. There is no
116
+ second setup path.
117
+
118
+ 1. Run **Grain: Manage Connections** and confirm your active connection.
119
+ 2. Open the Grain settings panel and **enable the MCP server** (`grain.mcpEnabled`).
120
+ This is required and ships **off** — the daemon serves no MCP surface until you turn it on.
121
+ 3. Keep safe mode enabled unless you explicitly want agents to run mutating SQL.
122
+ 4. Give your client the command:
123
+ - **Claude Code**: `claude mcp add grain -- npx -y grain-tools --stdio`
124
+ - **Cursor**: Settings → MCP Servers → Add new MCP server, type `command`,
125
+ command `npx -y grain-tools --stdio`
126
+ - **VS Code**: open the Extensions view, search `@mcp Grain`, and install it
127
+ into your user profile or the current workspace — the gallery entry carries
128
+ the command
129
+ - **Claude Desktop and other file-configured clients**: see
130
+ [the setup guide](docs/engineering/guides/mcp-client-setup.md), which
131
+ transcribes the same command into that client's config file
132
+
133
+ This bridges to the daemon the VS Code extension is already running, so agent
134
+ queries and sessions show up in the results grid alongside anything you run from
135
+ the editor. It works with every MCP client — including ones that cannot send
136
+ custom HTTP headers, such as Claude Desktop — and it needs no credential in your
137
+ config, because the bridge reads the daemon's access token itself.
138
+
139
+ `-y` is not decoration: an MCP client starts the server with no terminal
140
+ attached, so npm's install prompt would have nobody to answer it.
141
+
142
+ Enabling `grain.mcpEnabled` in the Grain settings panel is required
143
+ for this stdio bridge too. Launching the bridge without it produces a clear
144
+ refusal rather than a silent failure. If you want to refuse MCP everywhere, including
145
+ this bridge, set `"mcp": { "enabled": false }` in `~/.grain/daemon.json`.
146
+
147
+ ### Advanced: HTTP
148
+
149
+ The daemon requires an authorization token on every non-public route, so an HTTP
150
+ client must send it. Use this when your client supports custom headers (Cursor
151
+ and VS Code do):
8
152
 
9
153
  ```json
10
154
  {
11
155
  "mcpServers": {
12
156
  "grain": {
13
- "command": "npx",
14
- "args": ["-y", "grain-tools", "--stdio"]
157
+ "url": "http://127.0.0.1:PORT/mcp",
158
+ "headers": {
159
+ "Authorization": "Bearer YOUR_TOKEN"
160
+ }
15
161
  }
16
162
  }
17
163
  }
18
164
  ```
19
165
 
20
- The primary install path will be a standalone binary from
21
- [grain.tools](https://grain.tools), which needs no Node.js and reduces the
22
- configuration above to `"command": "grain"`. This package is the fallback channel
23
- for anyone who would rather use npm.
166
+ **`PORT` is not a fixed number.** The daemon asks the operating system for a free
167
+ port and writes the address it got to `~/.grain/endpoint.json`, so that two
168
+ accounts on one machine, two editors, or a container never compete for the same
169
+ one. Read the current port from that file, or copy the whole snippet — port and
170
+ token already filled in — from the Grain settings panel, which is the
171
+ intended route.
172
+
173
+ Because the port changes when the daemon restarts, a hand-written HTTP config
174
+ goes stale. **The stdio setup above needs no port at all and is the recommended
175
+ path for that reason.**
176
+
177
+ To pin a port deliberately — a deployed host, or a client that cannot be
178
+ reconfigured — start the daemon with `--port <number>`, or set
179
+ `GRAIN_PORT`. A pinned port is used exactly as given and never reassigned.
180
+
181
+ The token also lives at `~/.grain/auth-token`; treat it like a password,
182
+ since it authorizes access to your connections and query results.
183
+
184
+ > **Upgrading from 0.6.x?** Three things changed. A bare
185
+ > `{"url": "http://localhost:8414/mcp"}` config no longer connects — switch to the
186
+ > stdio config above (recommended) or add the `Authorization` header. **`8414` is
187
+ > also no longer the port**: the daemon takes a free one and publishes it, so an
188
+ > HTTP config that names any fixed port needs either the current port from
189
+ > `~/.grain/endpoint.json` or an explicit `--port` to pin one. The stdio
190
+ > config avoids the question entirely. And
191
+ > `grain.mcpEnabled` now genuinely gates the surface: with it off, `/mcp`
192
+ > returns **404**, because the route is not registered rather than being
193
+ > registered and refused. Previously the setting only decided whether the daemon
194
+ > started eagerly, so HTTP MCP answered whether or not you had enabled it.
195
+
196
+ ### Standalone / stdio Server with its own connections
197
+
198
+ For CI jobs or any client that should not share the editor's daemon, the same
199
+ command carries its own connection profiles through the environment:
200
+
201
+ ```bash
202
+ GRAIN_CONNECTIONS='[{"id":"analytics","name":"Analytics","type":"postgres","host":"localhost","port":5432,"user":"analyst","database":"warehouse","password":"YOUR_PASSWORD"}]' \
203
+ npx -y grain-tools --stdio
204
+ ```
205
+
206
+ The same variable goes in an `env` block for a file-configured client; see
207
+ [the setup guide](docs/engineering/guides/mcp-client-setup.md).
208
+
209
+ `--stdio` first looks for a shared daemon — for example, one started by the VS
210
+ Code extension — and, if it finds one, bridges to it, so the agent's queries and
211
+ sessions still appear in the VS Code grid exactly like the HTTP form above. It
212
+ resolves the address in one order: an explicit `--port`/`--host`, then
213
+ `GRAIN_MCP_PORT`, `GRAIN_PORT` or `MCP_PORT`, and otherwise the
214
+ address the daemon published in `~/.grain/endpoint.json`. There is no
215
+ fallback port to guess at: if nothing is pinned and nothing is published, there is
216
+ no daemon to attach to. If no shared daemon is reachable, it falls back to
217
+ starting its own embedded, isolated daemon exactly as before. Pass
218
+ `--embedded` or set `GRAIN_EMBEDDED=1` to force the isolated daemon
219
+ unconditionally, which is recommended for CI and other headless runs that
220
+ shouldn't depend on (or interfere with) a locally running instance.
221
+ The MCP `initialize` response explicitly reports `Grain isolation mode:
222
+ shared` or `isolated` in its standard `instructions` field. Agents can use that
223
+ protocol signal to tell whether their query sessions are visible in the user's
224
+ editor; stderr is not required for detection.
225
+
226
+ The extension and the daemon now agree on the address by publication rather than
227
+ by both guessing the same default: the daemon writes where it actually bound, and
228
+ the extension reads it. The endpoint-mismatch warning still fires when a pinned
229
+ port disagrees with a running daemon's published one. It remains detection, not
230
+ automatic correction: align the settings or remove the custom port.
231
+
232
+ Two accounts on one machine, two editors, and a container each get their own
233
+ daemon and their own address, because the address lives in each config directory
234
+ rather than being one number for the whole machine. Setting `GRAIN_HOME`
235
+ selects which config directory — and therefore which daemon — a client uses.
236
+
237
+ The MCP server exposes tools for:
238
+
239
+ | Tool | What it does |
240
+ | ------------------ | --------------------------------------------------- |
241
+ | `run_query` | Execute SQL and return typed JSON rows |
242
+ | `list_connectors` | Return supported connector types and setup schemas |
243
+ | `list_connections` | List configured connection profiles without secrets |
244
+ | `save_connection` | Add or update a connection profile |
245
+ | `test_connection` | Validate saved or unsaved connection settings |
246
+ | `list_schemas` | List schemas for a connection |
247
+ | `list_tables` | List tables within a schema |
248
+ | `describe_table` | Return column names and types |
249
+ | `get_tab_info` | Inspect daemon result tab state |
250
+ | `cancel_query` | Cancel a running query |
251
+ | `close_tab` | Close a daemon result tab |
252
+
253
+ Safe mode is on by default and restricts agents to read-only statements such as
254
+ `SELECT`, `SHOW`, `DESCRIBE`, `EXPLAIN`, `WITH`, and connector-specific metadata
255
+ queries.
256
+
257
+ See the full [MCP client setup guide](docs/engineering/guides/mcp-client-setup.md)
258
+ and [daemon URL reference](docs/engineering/guides/daemon-url-reference.md) for
259
+ advanced configuration.
260
+
261
+ ---
262
+
263
+ ## Configuration
264
+
265
+ Most users should use **Grain: Manage Connections** instead of editing
266
+ settings JSON by hand.
267
+
268
+ | Setting | Default | Description |
269
+ | -------------------------- | -------------------------- | -------------------------------------------------------------------------------------- |
270
+ | `grain.activeConnectionId` | empty | Connection profile used for VS Code query execution |
271
+ | `grain.maxRowsToDisplay` | `500` | Max rows shown in the grid; full export is still available |
272
+ | `grain.fontSize` | `0` | Results grid font size in px; `0` inherits from the editor |
273
+ | `grain.rowHeight` | `normal` | Grid density: `compact`, `normal`, or `comfortable` |
274
+ | `grain.tabNaming` | `file-sequential` | Result tab naming strategy |
275
+ | `grain.alwaysRunInNewTab` | `false` | Always open query results in a new tab |
276
+ | `grain.mcpEnabled` | `false` | Serve the local MCP surface for agent access. Off means `/mcp` is not registered (404) |
277
+ | `grain.mcpSafeMode` | `true` | Restrict MCP query execution to read-only statements |
278
+ | `grain.telemetry.enabled` | `false` | Opt in to anonymous product telemetry |
279
+ | `grain.telemetryHost` | `https://us.i.posthog.com` | Advanced PostHog-compatible telemetry endpoint |
280
+
281
+ Legacy connection settings such as `sqlPreview.host`, `sqlPreview.port`,
282
+ `sqlPreview.user`, `sqlPreview.catalog`, `sqlPreview.schema`, and
283
+ `sqlPreview.databasePath` remain available for migration and backward
284
+ compatibility.
285
+
286
+ ### Passwords and Secrets
287
+
288
+ Passwords entered through the VS Code connection manager are stored per
289
+ connection in VS Code Secret Storage, backed by the operating system keychain.
290
+ They are not written to `settings.json` or sent to the webview.
291
+
292
+ Use these commands when needed:
293
+
294
+ - **Grain: Set Connection Password**
295
+ - **Grain: Clear Connection Password**
296
+ - **Grain: Manage Connections**
297
+
298
+ ### Managed Machines
299
+
300
+ On most machines Grain runs as one background service the system keeps
301
+ alive. On a machine whose policy forbids that, Grain starts on demand
302
+ instead. The practical difference is that the first request after a pause
303
+ takes a moment longer; nothing else changes.
304
+
305
+ ### Telemetry
306
+
307
+ Grain telemetry is enabled by default when VS Code telemetry is enabled,
308
+ and you can turn it off at any time with `grain.telemetry.enabled`. Events
309
+ are anonymous and allowlisted, and are used to improve setup, connector
310
+ reliability, query workflows, MCP adoption, and product quality.
311
+
312
+ When something crashes, Grain sends a **redacted stack trace** so the
313
+ failure can be diagnosed without asking you for logs. Before anything leaves
314
+ your machine, file paths are reduced to their extension (`<path>/*.sql`) and
315
+ usernames are removed; paths that point at code keep only the part from
316
+ `node_modules/` or the build directory onward. If redaction fails for any
317
+ reason, the report is dropped rather than sent.
318
+
319
+ Grain never sends SQL text, query results, credentials, hostnames,
320
+ database/schema/table/column names, workspace names, environment variables,
321
+ process arguments, or raw driver error messages. A driver's error message can
322
+ embed table names, column names, and literal data values, so crash reports
323
+ carry the error's type and its stack — not the message text.
324
+
325
+ Session replay is separately feature-flagged and disabled by default. When it is
326
+ enabled, it covers only the VS Code results webview and masks every rendered
327
+ character and input; it does not record SQL, results, or other displayed content.
328
+
329
+ Use **Grain: Show Telemetry Status** to inspect the effective telemetry
330
+ state.
331
+
332
+ ---
333
+
334
+ ## Architecture
335
+
336
+ Grain uses a local daemon model:
337
+
338
+ ```text
339
+ VS Code extension / MCP client / browser surface
340
+ |
341
+ v
342
+ Grain daemon on localhost
343
+ |
344
+ v
345
+ Database connector
346
+ |
347
+ v
348
+ Your database
349
+ ```
350
+
351
+ The daemon owns connection profiles, query execution, result tabs, MCP
352
+ transports, and browser/VS Code projections. That lets editor sessions and agent
353
+ sessions share the same local state while keeping credentials and execution on
354
+ your machine.
355
+
356
+ ---
357
+
358
+ ## Release Notes
359
+
360
+ See [Changelog.md](Changelog.md) for the full release history.
361
+
362
+ ---
363
+
364
+ ## Contributing
365
+
366
+ Contributions are welcome, especially new connector support, reliability
367
+ improvements, and documentation fixes. See [CONTRIBUTING.md](CONTRIBUTING.md)
368
+ for setup instructions and development workflow.
369
+
370
+ Found a bug? Open an issue on
371
+ [GitHub](https://github.com/pattrnlabs/grain/issues).
372
+
373
+ ---
374
+
375
+ ## License
376
+
377
+ MIT (c) 2026 [Mehul Fadnavis](https://github.com/fadnavismehul)