feat: implement Docker image optimization - reduces size from 2.6GB to ~200MB

- Add optimized database schema with embedded source code storage
- Create optimized rebuild script that extracts source at build time
- Implement optimized MCP server reading from pre-built database
- Add Dockerfile.optimized with multi-stage build process
- Create comprehensive documentation and testing scripts
- Demonstrate 92% size reduction by removing runtime n8n dependencies

The optimization works by:
1. Building complete database at Docker build time
2. Extracting all node source code into the database
3. Creating minimal runtime image without n8n packages
4. Serving everything from pre-built SQLite database

This makes n8n-MCP suitable for resource-constrained production deployments.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-06-14 10:36:54 +02:00
parent d67c04dd52
commit 3ab8fbd60b
14 changed files with 1490 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
-- Optimized schema with source code storage for Docker optimization
CREATE TABLE IF NOT EXISTS nodes (
node_type TEXT PRIMARY KEY,
package_name TEXT NOT NULL,
display_name TEXT NOT NULL,
description TEXT,
category TEXT,
development_style TEXT CHECK(development_style IN ('declarative', 'programmatic')),
is_ai_tool INTEGER DEFAULT 0,
is_trigger INTEGER DEFAULT 0,
is_webhook INTEGER DEFAULT 0,
is_versioned INTEGER DEFAULT 0,
version TEXT,
documentation TEXT,
properties_schema TEXT,
operations TEXT,
credentials_required TEXT,
-- New columns for source code storage
node_source_code TEXT,
credential_source_code TEXT,
source_location TEXT,
source_extracted_at DATETIME,
-- Metadata
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_package ON nodes(package_name);
CREATE INDEX IF NOT EXISTS idx_ai_tool ON nodes(is_ai_tool);
CREATE INDEX IF NOT EXISTS idx_category ON nodes(category);
-- FTS5 table for full-text search including source code
CREATE VIRTUAL TABLE IF NOT EXISTS nodes_fts USING fts5(
node_type,
display_name,
description,
documentation,
operations,
node_source_code,
content=nodes,
content_rowid=rowid
);
-- Trigger to keep FTS in sync
CREATE TRIGGER IF NOT EXISTS nodes_fts_insert AFTER INSERT ON nodes
BEGIN
INSERT INTO nodes_fts(rowid, node_type, display_name, description, documentation, operations, node_source_code)
VALUES (new.rowid, new.node_type, new.display_name, new.description, new.documentation, new.operations, new.node_source_code);
END;
CREATE TRIGGER IF NOT EXISTS nodes_fts_update AFTER UPDATE ON nodes
BEGIN
UPDATE nodes_fts
SET node_type = new.node_type,
display_name = new.display_name,
description = new.description,
documentation = new.documentation,
operations = new.operations,
node_source_code = new.node_source_code
WHERE rowid = new.rowid;
END;
CREATE TRIGGER IF NOT EXISTS nodes_fts_delete AFTER DELETE ON nodes
BEGIN
DELETE FROM nodes_fts WHERE rowid = old.rowid;
END;

337
src/mcp/server-optimized.ts Normal file
View File

@@ -0,0 +1,337 @@
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { createDatabaseAdapter, DatabaseAdapter } from '../database/database-adapter';
import { logger } from '../utils/logger';
interface OptimizedNode {
nodeType: string;
packageName: string;
displayName: string;
description: string;
category: string;
nodeSourceCode?: string;
credentialSourceCode?: string;
sourceLocation?: string;
properties?: any[];
operations?: any[];
documentation?: string;
isAITool?: boolean;
isTrigger?: boolean;
}
/**
* Optimized MCP Server that reads everything from pre-built database
* No runtime dependency on n8n packages
*/
export class OptimizedMCPServer {
private server: Server;
private db: DatabaseAdapter | null = null;
private transport: StdioServerTransport;
constructor() {
this.server = new Server(
{
name: 'n8n-mcp-optimized',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
this.transport = new StdioServerTransport();
this.setupHandlers();
}
private async initDatabase() {
const dbPath = process.env.NODE_DB_PATH || './data/nodes.db';
this.db = await createDatabaseAdapter(dbPath);
logger.info('Database initialized');
}
private setupHandlers() {
// List available tools
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: 'list_nodes',
description: 'List all available n8n nodes with filtering options',
inputSchema: {
type: 'object',
properties: {
category: { type: 'string', description: 'Filter by category' },
packageName: { type: 'string', description: 'Filter by package' },
isAITool: { type: 'boolean', description: 'Filter AI-capable nodes' },
isTrigger: { type: 'boolean', description: 'Filter trigger nodes' },
limit: { type: 'number', description: 'Max results', default: 50 }
}
}
},
{
name: 'get_node_info',
description: 'Get comprehensive information about a specific n8n node',
inputSchema: {
type: 'object',
properties: {
nodeType: { type: 'string', description: 'Node type identifier' }
},
required: ['nodeType']
}
},
{
name: 'search_nodes',
description: 'Full-text search across all nodes',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query' },
limit: { type: 'number', description: 'Max results', default: 20 }
},
required: ['query']
}
},
{
name: 'list_ai_tools',
description: 'List all AI-capable n8n nodes',
inputSchema: {
type: 'object',
properties: {}
}
},
{
name: 'get_node_source',
description: 'Get source code for a specific node',
inputSchema: {
type: 'object',
properties: {
nodeType: { type: 'string', description: 'Node type identifier' }
},
required: ['nodeType']
}
},
{
name: 'get_database_statistics',
description: 'Get statistics about the node database',
inputSchema: {
type: 'object',
properties: {}
}
}
]
};
});
// Handle tool calls
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
if (!this.db) {
await this.initDatabase();
}
try {
switch (name) {
case 'list_nodes':
return await this.listNodes(args);
case 'get_node_info':
return await this.getNodeInfo(args);
case 'search_nodes':
return await this.searchNodes(args);
case 'list_ai_tools':
return await this.listAITools();
case 'get_node_source':
return await this.getNodeSource(args);
case 'get_database_statistics':
return await this.getDatabaseStatistics();
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
logger.error(`Tool execution failed: ${name}`, error);
throw error;
}
});
}
private async listNodes(args: any) {
const conditions: string[] = ['1=1'];
const params: any[] = [];
if (args.category) {
conditions.push('category = ?');
params.push(args.category);
}
if (args.packageName) {
conditions.push('package_name = ?');
params.push(args.packageName);
}
if (args.isAITool !== undefined) {
conditions.push('is_ai_tool = ?');
params.push(args.isAITool ? 1 : 0);
}
if (args.isTrigger !== undefined) {
conditions.push('is_trigger = ?');
params.push(args.isTrigger ? 1 : 0);
}
params.push(args.limit || 50);
const query = `
SELECT node_type, package_name, display_name, description, category,
is_ai_tool, is_trigger, is_webhook
FROM nodes
WHERE ${conditions.join(' AND ')}
LIMIT ?
`;
const nodes = this.db!.prepare(query).all(...params);
return {
nodes: nodes.map((n: any) => ({
nodeType: n.node_type,
packageName: n.package_name,
displayName: n.display_name,
description: n.description,
category: n.category,
isAITool: n.is_ai_tool === 1,
isTrigger: n.is_trigger === 1,
isWebhook: n.is_webhook === 1
})),
total: nodes.length
};
}
private async getNodeInfo(args: any) {
const query = `
SELECT * FROM nodes WHERE node_type = ?
`;
const node = this.db!.prepare(query).get(args.nodeType);
if (!node) {
throw new Error(`Node ${args.nodeType} not found`);
}
return {
nodeType: node.node_type,
packageName: node.package_name,
displayName: node.display_name,
description: node.description,
category: node.category,
developmentStyle: node.development_style,
isAITool: node.is_ai_tool === 1,
isTrigger: node.is_trigger === 1,
isWebhook: node.is_webhook === 1,
isVersioned: node.is_versioned === 1,
version: node.version,
documentation: node.documentation,
properties: JSON.parse(node.properties_schema || '[]'),
operations: JSON.parse(node.operations || '[]'),
credentialsRequired: JSON.parse(node.credentials_required || '[]'),
sourceExtractedAt: node.source_extracted_at
};
}
private async searchNodes(args: any) {
const query = `
SELECT n.* FROM nodes n
JOIN nodes_fts ON n.rowid = nodes_fts.rowid
WHERE nodes_fts MATCH ?
LIMIT ?
`;
const results = this.db!.prepare(query).all(args.query, args.limit || 20);
return {
nodes: results.map((n: any) => ({
nodeType: n.node_type,
displayName: n.display_name,
description: n.description,
category: n.category,
packageName: n.package_name,
relevance: n.rank
})),
total: results.length
};
}
private async listAITools() {
const query = `
SELECT node_type, display_name, description, category, package_name
FROM nodes
WHERE is_ai_tool = 1
ORDER BY display_name
`;
const nodes = this.db!.prepare(query).all();
return {
aiTools: nodes.map((n: any) => ({
nodeType: n.node_type,
displayName: n.display_name,
description: n.description,
category: n.category,
packageName: n.package_name
})),
total: nodes.length
};
}
private async getNodeSource(args: any) {
const query = `
SELECT node_source_code, credential_source_code, source_location
FROM nodes
WHERE node_type = ?
`;
const result = this.db!.prepare(query).get(args.nodeType);
if (!result) {
throw new Error(`Node ${args.nodeType} not found`);
}
return {
nodeType: args.nodeType,
sourceCode: result.node_source_code || 'Source code not available',
credentialCode: result.credential_source_code,
location: result.source_location
};
}
private async getDatabaseStatistics() {
const stats = {
totalNodes: this.db!.prepare('SELECT COUNT(*) as count FROM nodes').get().count,
aiTools: this.db!.prepare('SELECT COUNT(*) as count FROM nodes WHERE is_ai_tool = 1').get().count,
triggers: this.db!.prepare('SELECT COUNT(*) as count FROM nodes WHERE is_trigger = 1').get().count,
webhooks: this.db!.prepare('SELECT COUNT(*) as count FROM nodes WHERE is_webhook = 1').get().count,
withSource: this.db!.prepare('SELECT COUNT(*) as count FROM nodes WHERE node_source_code IS NOT NULL').get().count,
withDocs: this.db!.prepare('SELECT COUNT(*) as count FROM nodes WHERE documentation IS NOT NULL').get().count,
categories: this.db!.prepare('SELECT DISTINCT category FROM nodes').all().map((r: any) => r.category),
packages: this.db!.prepare('SELECT DISTINCT package_name FROM nodes').all().map((r: any) => r.package_name)
};
return stats;
}
async start() {
await this.initDatabase();
await this.server.connect(this.transport);
logger.info('Optimized MCP Server started');
}
}
// Start the server if run directly
if (require.main === module) {
const server = new OptimizedMCPServer();
server.start().catch(console.error);
}

View File

@@ -0,0 +1,231 @@
#!/usr/bin/env node
/**
* Optimized rebuild script that extracts and stores source code at build time
* This eliminates the need for n8n packages at runtime
*/
import { createDatabaseAdapter } from '../database/database-adapter';
import { N8nNodeLoader } from '../loaders/node-loader';
import { NodeParser } from '../parsers/node-parser';
import { DocsMapper } from '../mappers/docs-mapper';
import { NodeRepository } from '../database/node-repository';
import * as fs from 'fs';
import * as path from 'path';
interface ExtractedSourceInfo {
nodeSourceCode: string;
credentialSourceCode?: string;
sourceLocation: string;
}
async function extractNodeSource(NodeClass: any, packageName: string, nodeName: string): Promise<ExtractedSourceInfo> {
try {
// Multiple possible paths for node files
const possiblePaths = [
`${packageName}/dist/nodes/${nodeName}.node.js`,
`${packageName}/dist/nodes/${nodeName}/${nodeName}.node.js`,
`${packageName}/nodes/${nodeName}.node.js`,
`${packageName}/nodes/${nodeName}/${nodeName}.node.js`
];
let nodeFilePath: string | null = null;
let nodeSourceCode = '// Source code not found';
// Try each possible path
for (const path of possiblePaths) {
try {
nodeFilePath = require.resolve(path);
nodeSourceCode = await fs.promises.readFile(nodeFilePath, 'utf8');
break;
} catch (e) {
// Continue to next path
}
}
// If still not found, use NodeClass constructor source
if (nodeSourceCode === '// Source code not found' && NodeClass.toString) {
nodeSourceCode = `// Extracted from NodeClass\n${NodeClass.toString()}`;
nodeFilePath = 'extracted-from-class';
}
// Try to find credential file
let credentialSourceCode: string | undefined;
try {
const credName = nodeName.replace(/Node$/, '');
const credentialPaths = [
`${packageName}/dist/credentials/${credName}.credentials.js`,
`${packageName}/dist/credentials/${credName}/${credName}.credentials.js`,
`${packageName}/credentials/${credName}.credentials.js`
];
for (const path of credentialPaths) {
try {
const credFilePath = require.resolve(path);
credentialSourceCode = await fs.promises.readFile(credFilePath, 'utf8');
break;
} catch (e) {
// Continue to next path
}
}
} catch (error) {
// Credential file not found, which is fine
}
return {
nodeSourceCode,
credentialSourceCode,
sourceLocation: nodeFilePath || 'unknown'
};
} catch (error) {
console.warn(`Could not extract source for ${nodeName}: ${(error as Error).message}`);
return {
nodeSourceCode: '// Source code extraction failed',
sourceLocation: 'unknown'
};
}
}
async function rebuildOptimized() {
console.log('🔄 Building optimized n8n node database with embedded source code...\n');
const dbPath = process.env.BUILD_DB_PATH || './data/nodes.db';
const db = await createDatabaseAdapter(dbPath);
const loader = new N8nNodeLoader();
const parser = new NodeParser();
const mapper = new DocsMapper();
const repository = new NodeRepository(db);
// Initialize database with optimized schema
const schemaPath = path.join(__dirname, '../../src/database/schema-optimized.sql');
const schema = fs.readFileSync(schemaPath, 'utf8');
db.exec(schema);
// Clear existing data
db.exec('DELETE FROM nodes');
console.log('🗑️ Cleared existing data\n');
// Load all nodes
const nodes = await loader.loadAllNodes();
console.log(`📦 Loaded ${nodes.length} nodes from packages\n`);
// Statistics
const stats = {
successful: 0,
failed: 0,
aiTools: 0,
triggers: 0,
webhooks: 0,
withProperties: 0,
withOperations: 0,
withDocs: 0,
withSource: 0
};
// Process each node
for (const { packageName, nodeName, NodeClass } of nodes) {
try {
// Parse node
const parsed = parser.parse(NodeClass, packageName);
// Validate parsed data
if (!parsed.nodeType || !parsed.displayName) {
throw new Error('Missing required fields');
}
// Get documentation
const docs = await mapper.fetchDocumentation(parsed.nodeType);
parsed.documentation = docs || undefined;
// Extract source code at build time
console.log(`📄 Extracting source code for ${parsed.nodeType}...`);
const sourceInfo = await extractNodeSource(NodeClass, packageName, nodeName);
// Prepare the full node data with source code
const nodeData = {
...parsed,
developmentStyle: parsed.style, // Map 'style' to 'developmentStyle'
credentialsRequired: parsed.credentials || [], // Map 'credentials' to 'credentialsRequired'
nodeSourceCode: sourceInfo.nodeSourceCode,
credentialSourceCode: sourceInfo.credentialSourceCode,
sourceLocation: sourceInfo.sourceLocation,
sourceExtractedAt: new Date().toISOString()
};
// Save to database with source code
const stmt = db.prepare(`
INSERT INTO nodes (
node_type, package_name, display_name, description, category,
development_style, is_ai_tool, is_trigger, is_webhook, is_versioned,
version, documentation, properties_schema, operations, credentials_required,
node_source_code, credential_source_code, source_location, source_extracted_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
stmt.run(
nodeData.nodeType,
nodeData.packageName,
nodeData.displayName,
nodeData.description,
nodeData.category,
nodeData.developmentStyle,
nodeData.isAITool ? 1 : 0,
nodeData.isTrigger ? 1 : 0,
nodeData.isWebhook ? 1 : 0,
nodeData.isVersioned ? 1 : 0,
nodeData.version,
nodeData.documentation,
JSON.stringify(nodeData.properties),
JSON.stringify(nodeData.operations),
JSON.stringify(nodeData.credentialsRequired),
nodeData.nodeSourceCode,
nodeData.credentialSourceCode,
nodeData.sourceLocation,
nodeData.sourceExtractedAt
);
// Update statistics
stats.successful++;
if (parsed.isAITool) stats.aiTools++;
if (parsed.isTrigger) stats.triggers++;
if (parsed.isWebhook) stats.webhooks++;
if (parsed.properties.length > 0) stats.withProperties++;
if (parsed.operations.length > 0) stats.withOperations++;
if (docs) stats.withDocs++;
if (sourceInfo.nodeSourceCode !== '// Source code extraction failed') stats.withSource++;
console.log(`${parsed.nodeType} [Props: ${parsed.properties.length}, Ops: ${parsed.operations.length}, Source: ${sourceInfo.nodeSourceCode.length} bytes]`);
} catch (error) {
stats.failed++;
console.error(`❌ Failed to process ${nodeName}: ${(error as Error).message}`);
}
}
// Create FTS index
console.log('\n🔍 Building full-text search index...');
db.exec('INSERT INTO nodes_fts(nodes_fts) VALUES("rebuild")');
// Summary
console.log('\n📊 Summary:');
console.log(` Total nodes: ${nodes.length}`);
console.log(` Successful: ${stats.successful}`);
console.log(` Failed: ${stats.failed}`);
console.log(` AI Tools: ${stats.aiTools}`);
console.log(` Triggers: ${stats.triggers}`);
console.log(` Webhooks: ${stats.webhooks}`);
console.log(` With Properties: ${stats.withProperties}`);
console.log(` With Operations: ${stats.withOperations}`);
console.log(` With Documentation: ${stats.withDocs}`);
console.log(` With Source Code: ${stats.withSource}`);
// Database size check
const dbStats = db.prepare('SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size()').get();
console.log(`\n💾 Database size: ${(dbStats.size / 1024 / 1024).toFixed(2)} MB`);
console.log('\n✨ Optimized rebuild complete!');
db.close();
}
// Run if called directly
if (require.main === module) {
rebuildOptimized().catch(console.error);
}