icode-mcp-adapter 1.0.1 → 1.0.3
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "icode-mcp-adapter",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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: err.message || err.sqlMessage || 'Failed to get DB connection' });
|
|
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
|
}
|