icode-mcp-adapter 1.0.0 → 1.0.2
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/README.md +131 -0
- package/package.json +1 -1
- package/src/engine/McpEngine.js +3 -3
- package/src/transport/fastifyAdapter.js +37 -12
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# icode-mcp-adapter
|
|
2
|
+
|
|
3
|
+
Dynamic MCP server adapter for icode-server. Auto-generates CRUD tools from your existing database tables — column metadata resolved from `INFORMATION_SCHEMA` at runtime.
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Given a lightweight config like this:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
{
|
|
11
|
+
name: 'myapp',
|
|
12
|
+
dbEnv: 'dev',
|
|
13
|
+
tables: {
|
|
14
|
+
users: { operations: ['list', 'get', 'add', 'update'] },
|
|
15
|
+
orders: { operations: ['list', 'get', 'add', 'update', 'delete'] },
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
It auto-generates MCP tools: `list_users`, `get_user`, `add_user`, `update_user`, `list_orders`, `get_order`, `add_order`, `update_order`, `delete_order` — with proper Zod input schemas, SQL queries, and error handling.
|
|
21
|
+
|
|
22
|
+
No manual column definitions. The adapter reads your DB schema and figures out types, PKs, required fields, enums, defaults, and soft delete.
|
|
23
|
+
|
|
24
|
+
## icode-server conventions
|
|
25
|
+
|
|
26
|
+
- **Primary key**: `guid` (string), fallback to `pk`
|
|
27
|
+
- **Soft delete**: `_deleted = 0` (active) / `_deleted = 1` (deleted)
|
|
28
|
+
- **Timestamps**: `_created`, `_time` (epoch ms, auto-managed)
|
|
29
|
+
- **Meta fields**: `_created`, `_createdBy`, `_time`, `_by`, `_deleted`, `pk` — all readOnly
|
|
30
|
+
|
|
31
|
+
## Install
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
npm install icode-mcp-adapter @modelcontextprotocol/sdk zod
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Usage in icode-server
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// instacode.js
|
|
41
|
+
import mcpPlugin from 'icode-mcp-adapter/fastify';
|
|
42
|
+
import { getAppDbService } from './services/connectionService.mjs';
|
|
43
|
+
import paaalConfig from './plugins/mcp/configs/paaal.config.js';
|
|
44
|
+
|
|
45
|
+
await app.register(mcpPlugin, {
|
|
46
|
+
apps: { paaal: paaalConfig },
|
|
47
|
+
getDb: (appName, env) => getAppDbService(appName, env),
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This registers:
|
|
52
|
+
- `POST /v1/mcp/:appName` — MCP protocol endpoint
|
|
53
|
+
- `DELETE /v1/mcp/:appName` — session termination
|
|
54
|
+
- `GET /v1/mcp/:appName/health` — health check
|
|
55
|
+
|
|
56
|
+
## App config format
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
// plugins/mcp/configs/myapp.config.js
|
|
60
|
+
export default {
|
|
61
|
+
name: 'myapp',
|
|
62
|
+
displayName: 'My App',
|
|
63
|
+
version: '1.0.0',
|
|
64
|
+
dbEnv: 'dev',
|
|
65
|
+
tables: {
|
|
66
|
+
users: {
|
|
67
|
+
operations: ['list', 'get', 'add', 'update', 'delete'],
|
|
68
|
+
listOrderBy: '`_created` DESC',
|
|
69
|
+
columns: {
|
|
70
|
+
email: { description: 'User email address' },
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Table options
|
|
78
|
+
|
|
79
|
+
| Option | Default | Description |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `operations` | `['list','get','add','update']` | Which CRUD tools to generate |
|
|
82
|
+
| `pkType` | auto-detected | `'string'` (guid) or `'auto'` (int) |
|
|
83
|
+
| `softDelete` | auto (true if `_deleted` exists) | Soft delete via `_deleted` flag |
|
|
84
|
+
| `singular` | auto-derived (`users`→`user`) | Tool name prefix: `add_user` |
|
|
85
|
+
| `listOrderBy` | `_created DESC` | SQL ORDER BY clause |
|
|
86
|
+
| `columns` | `{}` | Per-column overrides (description, type, filterable) |
|
|
87
|
+
|
|
88
|
+
### Auto-detection from INFORMATION_SCHEMA
|
|
89
|
+
|
|
90
|
+
| DB trait | Detected as |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `guid` column | `primaryKey: 'guid'`, `pkType: 'string'` |
|
|
93
|
+
| `_deleted` column | soft delete: `WHERE _deleted = 0`, delete sets `_deleted = 1` |
|
|
94
|
+
| `_created` | autoSet (epoch ms), readOnly |
|
|
95
|
+
| `_time`, `_by`, `_createdBy`, `pk` | readOnly (meta fields) |
|
|
96
|
+
| `tinyint(1)` | boolean |
|
|
97
|
+
| `ENUM(...)` | enum with parsed values |
|
|
98
|
+
| `IS_NULLABLE = 'NO'` + no default | required |
|
|
99
|
+
| `COLUMN_KEY = 'MUL'` or `*_id` | filterable |
|
|
100
|
+
|
|
101
|
+
## Plugin options
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
await app.register(mcpPlugin, {
|
|
105
|
+
apps: { ... }, // required — app configs keyed by name
|
|
106
|
+
getDb: (name, env) => {}, // required — returns db with .query()
|
|
107
|
+
prefix: '/v1/mcp', // optional — route prefix (default: /v1/mcp)
|
|
108
|
+
public: true, // optional — route visibility for AuthGate (default: true)
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Exports
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
import { createMcpServer, createMcpServerAuto } from 'icode-mcp-adapter/engine';
|
|
116
|
+
import { resolveSchemas, clearSchemaCache } from 'icode-mcp-adapter/schema';
|
|
117
|
+
import { registerTableTools } from 'icode-mcp-adapter/tools';
|
|
118
|
+
import mcpPlugin from 'icode-mcp-adapter/fastify';
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Testing
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
npm test # smoke test (no DB required)
|
|
125
|
+
npm run sandbox # Express server on :3000 (needs .env with DB creds)
|
|
126
|
+
npm run inspector # MCP Inspector UI (needs .env with DB creds)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Full integration guide
|
|
130
|
+
|
|
131
|
+
See [INTEGRATION.md](./INTEGRATION.md) for step-by-step setup, nginx config, Claude Desktop connection, and architecture details.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "icode-mcp-adapter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Dynamic MCP server adapter — auto-generates CRUD tools from schema configs. Plugs into icode-server via Fastify or runs standalone.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/engine/McpEngine.js",
|
package/src/engine/McpEngine.js
CHANGED
|
@@ -11,7 +11,7 @@ import { resolveSchemas } from './SchemaAdapter.js';
|
|
|
11
11
|
*/
|
|
12
12
|
export function createMcpServer(config, db) {
|
|
13
13
|
const server = new McpServer({
|
|
14
|
-
name: `${config.
|
|
14
|
+
name: `${config.appName}-mcp`,
|
|
15
15
|
version: config.version,
|
|
16
16
|
});
|
|
17
17
|
|
|
@@ -44,11 +44,11 @@ export function createMcpServer(config, db) {
|
|
|
44
44
|
*/
|
|
45
45
|
export async function createMcpServerAuto(config, db) {
|
|
46
46
|
const tables = await resolveSchemas(db, config.tables, {
|
|
47
|
-
cacheKey: config.
|
|
47
|
+
cacheKey: config.appName,
|
|
48
48
|
});
|
|
49
49
|
|
|
50
50
|
return createMcpServer({
|
|
51
|
-
|
|
51
|
+
appName: config.appName,
|
|
52
52
|
version: config.version || '1.0.0',
|
|
53
53
|
tables,
|
|
54
54
|
}, db);
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
* getDb: (appName, env) => getAppDbService(appName, env),
|
|
12
12
|
* });
|
|
13
13
|
*
|
|
14
|
+
* Route param format:
|
|
15
|
+
* /v1/mcp/paaal--dev → appName: 'paaal', env: 'dev'
|
|
16
|
+
* /v1/mcp/paaal → appName: 'paaal', env: 'prod' (default)
|
|
17
|
+
*
|
|
14
18
|
* Routes:
|
|
15
19
|
* POST /v1/mcp/:appName — MCP protocol endpoint
|
|
16
20
|
* DELETE /v1/mcp/:appName — Session termination
|
|
@@ -24,35 +28,51 @@ import { createMcpServerAuto } from '../engine/McpEngine.js';
|
|
|
24
28
|
|
|
25
29
|
const sessions = new Map();
|
|
26
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Parse route param: "paaal--dev" → { appName: "paaal", env: "dev" }
|
|
33
|
+
* "paaal" → { appName: "paaal", env: "prod" }
|
|
34
|
+
*/
|
|
35
|
+
function parseAppParam(param) {
|
|
36
|
+
const parts = param.split('--');
|
|
37
|
+
return {
|
|
38
|
+
appName: parts[0],
|
|
39
|
+
env: parts[1] || 'prod',
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
27
43
|
/**
|
|
28
44
|
* @param {import('fastify').FastifyInstance} fastify
|
|
29
45
|
* @param {Object} opts
|
|
30
46
|
* @param {Record<string, import('../engine/types.js').AppConfig>} opts.apps
|
|
31
|
-
* — app configs keyed by
|
|
47
|
+
* — app configs keyed by appName: { paaal: paaalConfig }
|
|
32
48
|
* @param {(appName: string, env: string) => Promise<{ query: Function }>} opts.getDb
|
|
33
49
|
* — DB provider function, e.g. (name, env) => getAppDbService(name, env)
|
|
34
50
|
* @param {string} [opts.prefix='/v1/mcp']
|
|
35
|
-
* — route prefix (default: /v1/mcp)
|
|
36
51
|
* @param {boolean} [opts.public=true]
|
|
37
|
-
* — route visibility for AuthGate (default: public)
|
|
38
52
|
*/
|
|
39
53
|
export default async function mcpPlugin(fastify, opts = {}) {
|
|
40
|
-
const apps = new Map(Object.entries(opts.apps || {}));
|
|
41
54
|
const getDb = opts.getDb;
|
|
42
55
|
const prefix = opts.prefix || '/v1/mcp';
|
|
43
56
|
const isPublic = opts.public ?? true;
|
|
44
57
|
|
|
45
58
|
if (!getDb) throw new Error('icode-mcp-adapter: opts.getDb is required');
|
|
59
|
+
|
|
60
|
+
// Build lookup: key = "appName--env" (or "appName--prod" for default)
|
|
61
|
+
const apps = new Map();
|
|
62
|
+
for (const config of Object.values(opts.apps || {})) {
|
|
63
|
+
const key = `${config.appName}--${config.env || 'prod'}`;
|
|
64
|
+
apps.set(key, config);
|
|
65
|
+
}
|
|
46
66
|
if (!apps.size) throw new Error('icode-mcp-adapter: opts.apps is required (at least one app)');
|
|
47
67
|
|
|
48
68
|
// ── POST — MCP protocol endpoint ───────────────────────────────────────
|
|
49
69
|
fastify.post(`${prefix}/:appName`, {
|
|
50
70
|
config: { public: isPublic },
|
|
51
71
|
}, async (req, reply) => {
|
|
52
|
-
const { appName } = req.params;
|
|
53
|
-
const appConfig = apps.get(appName);
|
|
72
|
+
const { appName, env } = parseAppParam(req.params.appName);
|
|
73
|
+
const appConfig = apps.get(`${appName}--${env}`);
|
|
54
74
|
if (!appConfig) {
|
|
55
|
-
return reply.code(404).send({ ok: false, error: `App '${appName}' not registered` });
|
|
75
|
+
return reply.code(404).send({ ok: false, error: `App '${appName}/${env}' not registered` });
|
|
56
76
|
}
|
|
57
77
|
|
|
58
78
|
const sessionId = req.headers['mcp-session-id'];
|
|
@@ -65,7 +85,12 @@ export default async function mcpPlugin(fastify, opts = {}) {
|
|
|
65
85
|
|
|
66
86
|
// New session (must be initialize request)
|
|
67
87
|
if (!sessionId && isInitializeRequest(req.body)) {
|
|
68
|
-
|
|
88
|
+
let db;
|
|
89
|
+
try {
|
|
90
|
+
db = await getDb(appName, env);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
return reply.code(400).send({ ok: false, error: `No DB config for ${appName}/${env}` });
|
|
93
|
+
}
|
|
69
94
|
const server = await createMcpServerAuto(appConfig, db);
|
|
70
95
|
|
|
71
96
|
const transport = new StreamableHTTPServerTransport({
|
|
@@ -104,9 +129,9 @@ export default async function mcpPlugin(fastify, opts = {}) {
|
|
|
104
129
|
fastify.get(`${prefix}/:appName/health`, {
|
|
105
130
|
config: { public: isPublic },
|
|
106
131
|
}, async (req) => {
|
|
107
|
-
const { appName } = req.params;
|
|
108
|
-
const appConfig = apps.get(appName);
|
|
109
|
-
if (!appConfig) return { ok: false, error:
|
|
110
|
-
return { ok: true, app: appName, tables: Object.keys(appConfig.tables) };
|
|
132
|
+
const { appName, env } = parseAppParam(req.params.appName);
|
|
133
|
+
const appConfig = apps.get(`${appName}--${env}`);
|
|
134
|
+
if (!appConfig) return { ok: false, error: `App '${appName}/${env}' not registered` };
|
|
135
|
+
return { ok: true, app: appName, env, tables: Object.keys(appConfig.tables) };
|
|
111
136
|
});
|
|
112
137
|
}
|