mcp-server-db2i 1.2.1 → 1.3.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.
Files changed (62) hide show
  1. package/README.md +90 -308
  2. package/dist/auth/authMiddleware.d.ts +66 -0
  3. package/dist/auth/authMiddleware.d.ts.map +1 -0
  4. package/dist/auth/authMiddleware.js +217 -0
  5. package/dist/auth/authMiddleware.js.map +1 -0
  6. package/dist/auth/index.d.ts +9 -0
  7. package/dist/auth/index.d.ts.map +1 -0
  8. package/dist/auth/index.js +10 -0
  9. package/dist/auth/index.js.map +1 -0
  10. package/dist/auth/tokenManager.d.ts +114 -0
  11. package/dist/auth/tokenManager.d.ts.map +1 -0
  12. package/dist/auth/tokenManager.js +255 -0
  13. package/dist/auth/tokenManager.js.map +1 -0
  14. package/dist/auth/types.d.ts +103 -0
  15. package/dist/auth/types.d.ts.map +1 -0
  16. package/dist/auth/types.js +10 -0
  17. package/dist/auth/types.js.map +1 -0
  18. package/dist/config.d.ts +128 -0
  19. package/dist/config.d.ts.map +1 -1
  20. package/dist/config.js +185 -0
  21. package/dist/config.js.map +1 -1
  22. package/dist/db/connection.d.ts +53 -3
  23. package/dist/db/connection.d.ts.map +1 -1
  24. package/dist/db/connection.js +154 -15
  25. package/dist/db/connection.js.map +1 -1
  26. package/dist/db/queries.d.ts +29 -6
  27. package/dist/db/queries.d.ts.map +1 -1
  28. package/dist/db/queries.js +35 -12
  29. package/dist/db/queries.js.map +1 -1
  30. package/dist/index.d.ts +9 -0
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +97 -42
  33. package/dist/index.js.map +1 -1
  34. package/dist/openapi.d.ts +49 -0
  35. package/dist/openapi.d.ts.map +1 -0
  36. package/dist/openapi.js +634 -0
  37. package/dist/openapi.js.map +1 -0
  38. package/dist/server.d.ts +26 -2
  39. package/dist/server.d.ts.map +1 -1
  40. package/dist/server.js +68 -22
  41. package/dist/server.js.map +1 -1
  42. package/dist/tools/metadata.d.ts +10 -0
  43. package/dist/tools/metadata.d.ts.map +1 -1
  44. package/dist/tools/metadata.js +10 -6
  45. package/dist/tools/metadata.js.map +1 -1
  46. package/dist/tools/query.d.ts +4 -0
  47. package/dist/tools/query.d.ts.map +1 -1
  48. package/dist/tools/query.js +5 -3
  49. package/dist/tools/query.js.map +1 -1
  50. package/dist/transports/http.d.ts +26 -0
  51. package/dist/transports/http.d.ts.map +1 -0
  52. package/dist/transports/http.js +552 -0
  53. package/dist/transports/http.js.map +1 -0
  54. package/dist/transports/index.d.ts +8 -0
  55. package/dist/transports/index.d.ts.map +1 -0
  56. package/dist/transports/index.js +8 -0
  57. package/dist/transports/index.js.map +1 -0
  58. package/dist/transports/sessionManager.d.ts +106 -0
  59. package/dist/transports/sessionManager.d.ts.map +1 -0
  60. package/dist/transports/sessionManager.js +260 -0
  61. package/dist/transports/sessionManager.js.map +1 -0
  62. package/package.json +9 -5
package/README.md CHANGED
@@ -1,7 +1,49 @@
1
1
  # mcp-server-db2i
2
2
 
3
+ [![CI](https://github.com/Strom-Capital/mcp-server-db2i/actions/workflows/ci.yml/badge.svg)](https://github.com/Strom-Capital/mcp-server-db2i/actions/workflows/ci.yml)
4
+ [![npm version](https://img.shields.io/npm/v/mcp-server-db2i)](https://www.npmjs.com/package/mcp-server-db2i)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![MCP](https://img.shields.io/badge/MCP-2025--11--25-green?logo=anthropic&logoColor=white)](https://modelcontextprotocol.io/)
7
+ [![IBM i](https://img.shields.io/badge/IBM%20i-V7R3+-green?logo=ibm&logoColor=white)](https://www.ibm.com/products/ibm-i)
8
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
9
+ [![Node.js](https://img.shields.io/badge/Node.js-≥20.6-green?logo=node.js&logoColor=white)](https://nodejs.org/)
10
+ [![Docker](https://img.shields.io/badge/Docker-supported-blue?logo=docker&logoColor=white)](docs/docker.md)
11
+ [![npm downloads](https://img.shields.io/npm/dm/mcp-server-db2i)](https://www.npmjs.com/package/mcp-server-db2i)
12
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Strom-Capital/mcp-server-db2i/pulls)
13
+ [![GitHub last commit](https://img.shields.io/github/last-commit/Strom-Capital/mcp-server-db2i)](https://github.com/Strom-Capital/mcp-server-db2i/commits/main)
14
+
3
15
  A [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server for IBM DB2 for i (DB2i). This server enables AI assistants like Claude and Cursor to query and inspect IBM i databases using the JT400 JDBC driver.
4
16
 
17
+ ## Architecture
18
+
19
+ AI clients connect to the MCP Server via stdio (IDEs) or HTTP (agents), which executes read-only queries against DB2 for i using the JT400 JDBC driver.
20
+
21
+ ```mermaid
22
+ graph LR
23
+ subgraph clients ["AI Clients"]
24
+ claude("Claude")
25
+ cursor("Cursor IDE")
26
+ agents("Custom Agents")
27
+ end
28
+
29
+ subgraph server ["MCP Server"]
30
+ stdio["stdio"]
31
+ http["HTTP + Auth"]
32
+ tools[["MCP Tools"]]
33
+ jdbc["JT400 JDBC"]
34
+ end
35
+
36
+ subgraph ibmi ["IBM i"]
37
+ db2[("DB2 for i")]
38
+ end
39
+
40
+ claude & cursor -->|MCP Protocol| stdio
41
+ agents -->|REST API| http
42
+ stdio & http --> tools
43
+ tools --> jdbc
44
+ jdbc -->|JDBC| db2
45
+ ```
46
+
5
47
  ## Features
6
48
 
7
49
  - **Read-only SQL queries** - Execute SELECT statements safely with automatic result limiting
@@ -10,190 +52,37 @@ A [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server for IB
10
52
  - **View inspection** - List and explore database views
11
53
  - **Secure by design** - Only SELECT queries allowed, credentials via environment variables
12
54
  - **Docker support** - Run as a container for easy deployment
55
+ - **HTTP Transport** - REST API with token authentication for web/agent integration
56
+ - **Dual Transport** - Run stdio and HTTP simultaneously
13
57
 
14
- ## Available Tools
58
+ ## Quick Start
15
59
 
16
- | Tool | Description |
17
- |------|-------------|
18
- | `execute_query` | Execute read-only SELECT queries |
19
- | `list_schemas` | List schemas/libraries (with optional filter) |
20
- | `list_tables` | List tables in a schema (with optional filter) |
21
- | `describe_table` | Get detailed column information |
22
- | `list_views` | List views in a schema (with optional filter) |
23
- | `list_indexes` | List SQL indexes for a table |
24
- | `get_table_constraints` | Get primary keys, foreign keys, unique constraints |
25
-
26
- > **Note:** `list_indexes` and `get_table_constraints` query the `QSYS2` SQL catalog views and only return SQL-defined objects. Legacy DDS Logical Files and Physical File constraints are not included. This is standard DB2 for i behavior.
27
-
28
- ### Filter Syntax
29
-
30
- The list tools support pattern matching:
31
- - `CUST` - Contains "CUST"
32
- - `CUST*` - Starts with "CUST"
33
- - `*LOG` - Ends with "LOG"
34
- - `ORD*FILE` - Starts with "ORD", ends with "FILE"
35
-
36
- ## Installation
37
-
38
- ### Prerequisites
39
-
40
- - Node.js 18 or higher
41
- - Java Runtime Environment (JRE) 11 or higher (for JDBC)
42
- - Access to an IBM i system
43
-
44
- ### Option 1: npm (recommended)
60
+ ### Installation
45
61
 
46
62
  ```bash
47
63
  npm install -g mcp-server-db2i
48
64
  ```
49
65
 
50
- ### Option 2: From source
51
-
52
- ```bash
53
- git clone https://github.com/Strom-Capital/mcp-server-db2i.git
54
- cd mcp-server-db2i
55
- npm install
56
- npm run build
57
- ```
58
-
59
- ### Option 3: Docker
66
+ Or with Docker:
60
67
 
61
68
  ```bash
62
69
  docker build -t mcp-server-db2i .
63
70
  ```
64
71
 
65
- ## Configuration
72
+ ### Configuration
66
73
 
67
- Create a `.env` file or set environment variables:
74
+ Create a `.env` file with your IBM i credentials:
68
75
 
69
76
  ```env
70
- # Required
71
77
  DB2I_HOSTNAME=your-ibm-i-host.com
72
78
  DB2I_USERNAME=your-username
73
79
  DB2I_PASSWORD=your-password
74
-
75
- # Optional - Database
76
- DB2I_PORT=446 # Default: 446
77
- DB2I_DATABASE=*LOCAL # Default: *LOCAL
78
- DB2I_SCHEMA=your-default-schema # Default schema for all tools (can be overridden per-call)
79
- DB2I_JDBC_OPTIONS=naming=system;date format=iso
80
-
81
- # Optional - Logging
82
- LOG_LEVEL=info # debug, info, warn, error, fatal (default: info)
83
- NODE_ENV=production # production = JSON logs, development = pretty logs
84
- LOG_PRETTY=true # Override: force pretty (true) or JSON (false) logs
85
- LOG_COLORS=true # Override: force colors on/off (auto-detected by default)
86
-
87
- # Optional - Rate Limiting
88
- RATE_LIMIT_WINDOW_MS=900000 # Time window in ms (default: 900000 = 15 minutes)
89
- RATE_LIMIT_MAX_REQUESTS=100 # Max requests per window (default: 100)
90
- RATE_LIMIT_ENABLED=true # Set to 'false' to disable (default: true)
91
-
92
- # Optional - Query Limits
93
- QUERY_DEFAULT_LIMIT=1000 # Default rows returned (default: 1000)
94
- QUERY_MAX_LIMIT=10000 # Maximum rows allowed (default: 10000)
95
- ```
96
-
97
- ### Environment Variables
98
-
99
- | Variable | Required | Default | Description |
100
- |----------|----------|---------|-------------|
101
- | `DB2I_HOSTNAME` | Yes | - | IBM i hostname or IP address |
102
- | `DB2I_USERNAME` | Yes* | - | IBM i user profile |
103
- | `DB2I_PASSWORD` | Yes* | - | User password |
104
- | `DB2I_USERNAME_FILE` | No | - | Path to file containing username (overrides `DB2I_USERNAME`) |
105
- | `DB2I_PASSWORD_FILE` | No | - | Path to file containing password (overrides `DB2I_PASSWORD`) |
106
- | `DB2I_PORT` | No | `446` | JDBC port (446 is standard for IBM i) |
107
- | `DB2I_DATABASE` | No | `*LOCAL` | Database name |
108
- | `DB2I_SCHEMA` | No | - | Default schema/library for tools. If set, you don't need to specify schema in each tool call. |
109
- | `DB2I_JDBC_OPTIONS` | No | - | Additional JDBC options (semicolon-separated) |
110
- | `LOG_LEVEL` | No | `info` | Log level: `debug`, `info`, `warn`, `error`, `fatal` |
111
- | `NODE_ENV` | No | - | Set to `production` for JSON logs, otherwise pretty-printed |
112
- | `LOG_PRETTY` | No | - | Override log format: `true` = pretty, `false` = JSON |
113
- | `LOG_COLORS` | No | auto | Override colors: `true`/`false` (auto-detects TTY by default) |
114
- | `RATE_LIMIT_WINDOW_MS` | No | `900000` | Rate limit time window in milliseconds (15 min) |
115
- | `RATE_LIMIT_MAX_REQUESTS` | No | `100` | Maximum requests allowed per window |
116
- | `RATE_LIMIT_ENABLED` | No | `true` | Set to `false` or `0` to disable rate limiting |
117
- | `QUERY_DEFAULT_LIMIT` | No | `1000` | Default number of rows returned by queries |
118
- | `QUERY_MAX_LIMIT` | No | `10000` | Maximum rows allowed (caps user-provided limits) |
119
-
120
- *Either the environment variable or the corresponding `*_FILE` variable must be set. File-based secrets take priority when both are provided.
121
-
122
- ### JDBC Options
123
-
124
- Common JDBC options for IBM i (JT400/JTOpen driver):
125
-
126
- | Option | Values | Description |
127
- |--------|--------|-------------|
128
- | `naming` | `system`, `sql` | `system` uses `/` for library separator, `sql` uses `.` for schema separator |
129
- | `libraries` | `LIB1,LIB2,...` | Library list for resolving unqualified names |
130
- | `date format` | `iso`, `usa`, `eur`, `jis`, `mdy`, `dmy`, `ymd` | Date format for date fields |
131
- | `time format` | `iso`, `usa`, `eur`, `jis`, `hms` | Time format for time fields |
132
- | `errors` | `full`, `basic` | Level of detail in error messages (`full` helps debugging) |
133
- | `translate binary` | `true`, `false` | Whether to translate binary/CCSID data |
134
- | `secure` | `true`, `false` | Enable SSL/TLS encryption |
135
-
136
- Example: `naming=system;date format=iso;errors=full`
137
-
138
- ## Usage with Cursor
139
-
140
- Add to your Cursor MCP settings (`~/.cursor/mcp.json`):
141
-
142
- ### Using Docker (recommended)
143
-
144
- ```json
145
- {
146
- "mcpServers": {
147
- "db2i": {
148
- "command": "docker",
149
- "args": [
150
- "run", "-i", "--rm",
151
- "-e", "DB2I_HOSTNAME=your-host",
152
- "-e", "DB2I_USERNAME=your-user",
153
- "-e", "DB2I_PASSWORD=your-password",
154
- "mcp-server-db2i:latest"
155
- ]
156
- }
157
- }
158
- }
159
- ```
160
-
161
- ### Using Docker with env file
162
-
163
- ```json
164
- {
165
- "mcpServers": {
166
- "db2i": {
167
- "command": "docker",
168
- "args": [
169
- "run", "-i", "--rm",
170
- "--env-file", "/path/to/your/.env",
171
- "mcp-server-db2i:latest"
172
- ]
173
- }
174
- }
175
- }
80
+ DB2I_SCHEMA=your-default-schema # Optional
176
81
  ```
177
82
 
178
- ### Using docker-compose
83
+ ### Client Setup
179
84
 
180
- Create a `.env` file in the project root, then:
181
-
182
- ```json
183
- {
184
- "mcpServers": {
185
- "db2i": {
186
- "command": "docker-compose",
187
- "args": ["run", "--rm", "mcp-server-db2i"],
188
- "cwd": "/path/to/mcp-server-db2i"
189
- }
190
- }
191
- }
192
- ```
193
-
194
- The `docker-compose.yml` automatically reads from `.env` in the same directory.
195
-
196
- ### Using npx (after npm install)
85
+ Add to your MCP client config (e.g., `~/.cursor/mcp.json`):
197
86
 
198
87
  ```json
199
88
  {
@@ -202,34 +91,39 @@ The `docker-compose.yml` automatically reads from `.env` in the same directory.
202
91
  "command": "npx",
203
92
  "args": ["mcp-server-db2i"],
204
93
  "env": {
205
- "DB2I_HOSTNAME": "your-host",
206
- "DB2I_USERNAME": "your-user",
207
- "DB2I_PASSWORD": "your-password"
94
+ "DB2I_HOSTNAME": "${env:DB2I_HOSTNAME}",
95
+ "DB2I_USERNAME": "${env:DB2I_USERNAME}",
96
+ "DB2I_PASSWORD": "${env:DB2I_PASSWORD}"
208
97
  }
209
98
  }
210
99
  }
211
100
  }
212
101
  ```
213
102
 
214
- ### Local development
103
+ This uses environment variable expansion to keep credentials out of config files. Set the variables in your shell profile (`~/.zshrc` or `~/.bashrc`).
215
104
 
216
- ```json
217
- {
218
- "mcpServers": {
219
- "db2i": {
220
- "command": "npx",
221
- "args": ["tsx", "/path/to/mcp-server-db2i/src/index.ts"],
222
- "env": {
223
- "DB2I_HOSTNAME": "your-host",
224
- "DB2I_USERNAME": "your-user",
225
- "DB2I_PASSWORD": "your-password"
226
- }
227
- }
228
- }
229
- }
230
- ```
105
+ See the [Client Setup Guide](docs/client-setup.md) for Cursor, Claude Desktop, Claude Code, and Docker setup options.
106
+
107
+ ## Available Tools
231
108
 
232
- ## Example Queries
109
+ | Tool | Description |
110
+ |------|-------------|
111
+ | `execute_query` | Execute read-only SELECT queries |
112
+ | `list_schemas` | List schemas/libraries (with optional filter) |
113
+ | `list_tables` | List tables in a schema (with optional filter) |
114
+ | `describe_table` | Get detailed column information |
115
+ | `list_views` | List views in a schema (with optional filter) |
116
+ | `list_indexes` | List SQL indexes for a table |
117
+ | `get_table_constraints` | Get primary keys, foreign keys, unique constraints |
118
+
119
+ ### Filter Syntax
120
+
121
+ The list tools support pattern matching:
122
+ - `CUST` - Contains "CUST"
123
+ - `CUST*` - Starts with "CUST"
124
+ - `*LOG` - Ends with "LOG"
125
+
126
+ ## Example Usage
233
127
 
234
128
  Once connected, you can ask the AI assistant:
235
129
 
@@ -239,141 +133,30 @@ Once connected, you can ask the AI assistant:
239
133
  - "What indexes exist on the ORDERS table?"
240
134
  - "Run this query: SELECT * FROM MYLIB.CUSTOMERS WHERE STATUS = 'A'"
241
135
 
242
- ## Development
243
-
244
- ```bash
245
- # Install dependencies
246
- npm install
247
-
248
- # Run in development mode
249
- npm run dev
250
-
251
- # Build for production
252
- npm run build
253
-
254
- # Run production build
255
- npm start
256
-
257
- # Run tests
258
- npm test
259
-
260
- # Run tests in watch mode
261
- npm run test:watch
262
-
263
- # Lint code
264
- npm run lint
265
-
266
- # Lint and fix
267
- npm run lint:fix
268
-
269
- # Type check
270
- npm run typecheck
271
- ```
272
-
273
- ## Security
274
-
275
- - **Read-only access**: Only SELECT statements are permitted
276
- - **No credentials in code**: All sensitive data via environment variables or file-based secrets
277
- - **Query validation**: AST-based SQL parsing plus regex validation blocks dangerous operations
278
- - **Result limiting**: Default limit of 1000 rows, configurable max limit (default: 10000)
279
- - **Rate limiting**: Configurable request throttling to prevent abuse (100 req/15 min default)
280
- - **Structured logging**: Automatic redaction of sensitive fields like passwords
281
-
282
- ### Rate Limiting
283
-
284
- The server includes built-in rate limiting to protect the IBM i database from excessive queries:
285
-
286
- - **Default**: 100 requests per 15-minute window
287
- - **Scope**: Per server instance (for stdio transport, this effectively means per-client since each MCP client spawns its own server process)
288
- - **Configurable**: Adjust via `RATE_LIMIT_*` environment variables or disable entirely
289
-
290
- When the rate limit is exceeded, queries return an error with `waitTimeSeconds` indicating when to retry.
291
-
292
- ### Credential Management
293
-
294
- The server supports multiple methods for providing credentials, listed from most to least secure:
295
-
296
- #### Option 1: Docker Secrets (Recommended for Production)
297
-
298
- Docker secrets provide the most secure credential management. Secrets are mounted as files and never exposed in environment variables or process listings.
299
-
300
- 1. Create secret files:
301
-
302
- ```bash
303
- mkdir -p ./secrets
304
- echo "your-username" > ./secrets/db2i_username.txt
305
- echo "your-password" > ./secrets/db2i_password.txt
306
- chmod 600 ./secrets/*.txt
307
- ```
308
-
309
- 2. Configure docker-compose.yml to use secrets:
310
-
311
- ```yaml
312
- services:
313
- mcp-server-db2i:
314
- # ... other config ...
315
- environment:
316
- - DB2I_HOSTNAME=${DB2I_HOSTNAME}
317
- - DB2I_USERNAME_FILE=/run/secrets/db2i_username
318
- - DB2I_PASSWORD_FILE=/run/secrets/db2i_password
319
- secrets:
320
- - db2i_username
321
- - db2i_password
322
-
323
- secrets:
324
- db2i_username:
325
- file: ./secrets/db2i_username.txt
326
- db2i_password:
327
- file: ./secrets/db2i_password.txt
328
- ```
329
-
330
- For Docker Swarm or Kubernetes, use their native secret management instead of file-based secrets.
331
-
332
- #### Option 2: External Secret Management
333
-
334
- For enterprise deployments, integrate with secret management systems:
335
-
336
- - **HashiCorp Vault**: Inject secrets at runtime
337
- - **AWS Secrets Manager**: Use IAM roles for access
338
- - **Azure Key Vault**: Integrate with managed identities
339
-
340
- These systems can populate the `*_FILE` environment variables or inject secrets directly.
341
-
342
- #### Option 3: Environment Variables (Development Only)
343
-
344
- Plain environment variables are convenient for development but expose credentials through:
345
- - `docker inspect` output
346
- - Process listings (`ps aux`)
347
- - Shell history
348
- - Log files
349
-
350
- ```bash
351
- # .env file (ensure it's in .gitignore)
352
- DB2I_USERNAME=your-username
353
- DB2I_PASSWORD=your-password
354
- ```
355
-
356
- **Warning**: Never commit `.env` files or credentials to version control.
357
-
358
- ### File-Based Secret Variables
136
+ ## Documentation
359
137
 
360
- | Variable | Description |
361
- |----------|-------------|
362
- | `DB2I_USERNAME_FILE` | Path to file containing username (takes priority over `DB2I_USERNAME`) |
363
- | `DB2I_PASSWORD_FILE` | Path to file containing password (takes priority over `DB2I_PASSWORD`) |
138
+ | Guide | Description |
139
+ |-------|-------------|
140
+ | [HTTP Transport](docs/http-transport.md) | REST API with token authentication |
141
+ | [Configuration](docs/configuration.md) | All environment variables and JDBC options |
142
+ | [Security](docs/security.md) | Credentials, rate limiting, query validation |
143
+ | [Client Setup](docs/client-setup.md) | Cursor, Claude, Claude Code setup |
144
+ | [Docker Guide](docs/docker.md) | Container deployment |
145
+ | [Development](docs/development.md) | Contributing and local setup |
364
146
 
365
147
  ## Compatibility
366
148
 
367
149
  - IBM i V7R3 and later (V7R5 recommended)
368
- - Works with any IBM i system accessible via JDBC over TCP/IP
150
+ - Node.js 20.6 or higher
151
+ - Java Runtime Environment (JRE) 11 or higher
369
152
 
370
153
  ## Related Projects
371
154
 
372
- - **[IBM ibmi-mcp-server](https://github.com/IBM/ibmi-mcp-server)** - IBM's official MCP server for IBM i systems. Offers YAML-based SQL tool definitions, AI agent frameworks, and production deployment options. Requires [Mapepire](https://mapepire-ibmi.github.io/) to be installed on your IBM i system, but if you can manage that prerequisite, it's worth checking out for more advanced use cases.
155
+ - **[IBM ibmi-mcp-server](https://github.com/IBM/ibmi-mcp-server)** - IBM's official MCP server for IBM i systems. Offers YAML-based SQL tool definitions and AI agent frameworks. Requires [Mapepire](https://mapepire-ibmi.github.io/).
373
156
 
374
157
  ## Contributing
375
158
 
376
- Contributions are welcome! Please feel free to submit a Pull Request.
159
+ Contributions are welcome! See the [Development Guide](docs/development.md) for setup instructions.
377
160
 
378
161
  ## License
379
162
 
@@ -384,4 +167,3 @@ MIT License - see [LICENSE](LICENSE) for details.
384
167
  - [node-jt400](https://www.npmjs.com/package/node-jt400) - JT400 JDBC driver wrapper for Node.js
385
168
  - [Model Context Protocol](https://modelcontextprotocol.io/) - The protocol specification
386
169
  - [@modelcontextprotocol/sdk](https://github.com/modelcontextprotocol/typescript-sdk) - Official TypeScript SDK
387
- - [IBM ibmi-mcp-server](https://github.com/IBM/ibmi-mcp-server) - SQL security validation patterns inspired by their approach to AST-based query validation
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Authentication Middleware for HTTP Transport
3
+ *
4
+ * Express middleware to validate Bearer tokens on protected routes.
5
+ * Supports multiple authentication modes:
6
+ * - 'required': Full /auth flow with per-user DB credentials (default)
7
+ * - 'token': Pre-shared static token, uses env DB credentials
8
+ * - 'none': No authentication required, uses env DB credentials
9
+ */
10
+ import type { Request, Response, NextFunction } from 'express';
11
+ import { type AuthMode } from '../config.js';
12
+ import type { TokenSession } from './types.js';
13
+ /**
14
+ * Extended Express Request with auth context
15
+ */
16
+ export interface AuthenticatedRequest extends Request {
17
+ /** The validated token session */
18
+ tokenSession?: TokenSession;
19
+ /** The raw token string */
20
+ authToken?: string;
21
+ }
22
+ /**
23
+ * Authentication middleware for protected routes
24
+ *
25
+ * Behavior depends on MCP_AUTH_MODE:
26
+ * - 'required': Validates Bearer token from /auth flow, attaches token session
27
+ * - 'token': Validates Bearer token against static MCP_AUTH_TOKEN
28
+ * - 'none': Skips authentication entirely
29
+ *
30
+ * @example
31
+ * app.post('/mcp', authMiddleware, (req, res) => {
32
+ * const session = (req as AuthenticatedRequest).tokenSession;
33
+ * // Use session.config for DB connection (only in 'required' mode)
34
+ * });
35
+ */
36
+ export declare function authMiddleware(req: Request, res: Response, next: NextFunction): void;
37
+ /**
38
+ * Get the current auth mode for use in route handlers
39
+ */
40
+ export declare function getAuthModeFromConfig(): AuthMode;
41
+ /**
42
+ * Optional authentication middleware
43
+ *
44
+ * Similar to authMiddleware but doesn't require authentication.
45
+ * If a valid token is provided, attaches the session to the request.
46
+ * If no token or invalid token, continues without error.
47
+ *
48
+ * Useful for endpoints that work with or without authentication.
49
+ */
50
+ export declare function optionalAuthMiddleware(req: Request, res: Response, next: NextFunction): void;
51
+ /**
52
+ * Auth rate limiting middleware
53
+ *
54
+ * Limits authentication attempts per IP to prevent brute force.
55
+ * Should be applied to the /auth endpoint.
56
+ */
57
+ export declare function authRateLimitMiddleware(req: Request, res: Response, next: NextFunction): void;
58
+ /**
59
+ * Record a failed auth attempt for rate limiting
60
+ */
61
+ export declare function recordFailedAuthAttempt(req: Request): void;
62
+ /**
63
+ * Clear rate limit for an IP (on successful auth)
64
+ */
65
+ export declare function clearAuthRateLimit(req: Request): void;
66
+ //# sourceMappingURL=authMiddleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authMiddleware.d.ts","sourceRoot":"","sources":["../../src/auth/authMiddleware.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI/D,OAAO,EAAiB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAI/C;;GAEG;AACH,MAAM,WAAW,oBAAqB,SAAQ,OAAO;IACnD,kCAAkC;IAClC,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,2BAA2B;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAkBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAuGN;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,QAAQ,CAEhD;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAeN;AA8BD;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,OAAO,EACZ,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,YAAY,GACjB,IAAI,CAwBN;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CAa1D;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CAGrD"}