构建自定义SQLite MCP服务器
高级
这是一个Building Blocks, AI领域的自动化工作流,包含 16 个节点。主要使用 Code, Switch, ToolCode, McpTrigger, ToolWorkflow 等节点,结合人工智能技术实现智能自动化。 构建自定义SQLite MCP服务器
前置要求
- •AI 服务 API Key(如 OpenAI、Anthropic 等)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
"meta": {
"instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9"
},
"nodes": [
{
"id": "fcbf7023-7e12-49d8-9c7d-4cb431c79905",
"name": "当由另一个工作流执行时",
"type": "n8n-nodes-base.executeWorkflowTrigger",
"position": [
460,
260
],
"parameters": {
"workflowInputs": {
"values": [
{
"name": "operation"
},
{
"name": "tableName"
},
{
"name": "values",
"type": "object"
},
{
"name": "where",
"type": "object"
}
]
}
},
"typeVersion": 1.1
},
{
"id": "58c93321-ded9-48c1-812f-c35d160e257b",
"name": "操作",
"type": "n8n-nodes-base.switch",
"position": [
640,
260
],
"parameters": {
"rules": {
"values": [
{
"outputKey": "READ",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "81b134bc-d671-4493-b3ad-8df9be3f49a6",
"operator": {
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.operation }}",
"rightValue": "read"
}
]
},
"renameOutput": true
},
{
"outputKey": "INSERT",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "8d57914f-6587-4fb3-88e0-aa1de6ba56c1",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.operation }}",
"rightValue": "insert"
}
]
},
"renameOutput": true
},
{
"outputKey": "UPDATE",
"conditions": {
"options": {
"version": 2,
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "7c38f238-213a-46ec-aefe-22e0bcb8dffc",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json.operation }}",
"rightValue": "update"
}
]
},
"renameOutput": true
}
]
},
"options": {}
},
"typeVersion": 3.2
},
{
"id": "865ae43a-14ec-4aac-9396-d0aef1ab4a75",
"name": "便签",
"type": "n8n-nodes-base.stickyNote",
"position": [
-340,
-100
],
"parameters": {
"color": 7,
"width": 680,
"height": 660,
"content": "## 1. 设置 MCP 服务器触发器"
},
"typeVersion": 1
},
{
"id": "35551851-319a-47cf-87cd-a63b128300cc",
"name": "便签1",
"type": "n8n-nodes-base.stickyNote",
"position": [
360,
-100
],
"parameters": {
"color": 7,
"width": 820,
"height": 720,
"content": "## 2. 通过防止原始 SQL 语句确保安全"
},
"typeVersion": 1
},
{
"id": "95c35568-e447-4634-afe8-c902ba5c7d2f",
"name": "便签 3",
"type": "n8n-nodes-base.stickyNote",
"position": [
-340,
-220
],
"parameters": {
"color": 5,
"width": 380,
"height": 100,
"content": "### 始终验证您的服务器!"
},
"typeVersion": 1
},
{
"id": "2d0f98f8-043a-459c-8b77-634e06ee0f57",
"name": "SQLite MCP 服务器",
"type": "@n8n/n8n-nodes-langchain.mcpTrigger",
"position": [
-160,
60
],
"webhookId": "3124a4cd-4e93-4c1b-b4db-b5599f4889b1",
"parameters": {
"path": "3124a4cd-4e93-4c1b-b4db-b5599f4889b1"
},
"typeVersion": 1
},
{
"id": "6f313137-eb8f-429b-a6c9-7b17e067dc5e",
"name": "创建记录",
"type": "n8n-nodes-base.code",
"position": [
940,
260
],
"parameters": {
"jsCode": "const sqlite3 = require('sqlite3').verbose();\nconst { promisify } = require('util');\n\nconst db = new sqlite3.Database('/home/node/test.db');\nconst run = promisify(db.run.bind(db));\n\nconst { json } = $input.first();\n\n\nlet output = '';\nconst statement = [\n `INSERT INTO ${json.tableName}`,\n ` (${Object.keys(json.values).join(',')})`,\n `VALUES`,\n ` (${Object.keys(json.values).map(_ => '?').join(',')})`\n].join(' ');\nconst params = Object.values(json.values);\n\ntry {\n await run(statement.trim(), params);\n output = { output: 'ok', error: null };\n} catch (err) {\n output = { output: null, error: err };\n} finally {\n await db.close();\n}\n\nreturn output;"
},
"typeVersion": 2
},
{
"id": "b2530656-bbf4-4316-8b8e-c5d27865e45f",
"name": "更新记录",
"type": "n8n-nodes-base.code",
"position": [
940,
440
],
"parameters": {
"jsCode": "const sqlite3 = require('sqlite3').verbose();\nconst { promisify } = require('util');\n\nconst db = new sqlite3.Database('/home/node/test.db');\nconst run = promisify(db.run.bind(db));\n\nconst { json } = $input.first();\n\nlet output = '';\nconst statement = [\n `UPDATE ${json.tableName}`,\n `SET`,\n `${Object.keys(json.values)\n .map(key => `${key} = ?`)\n .join(',')}`,\n `WHERE`,\n `${Object.keys(json.where)\n .map((key,idx) => `${key} = ?`)\n .join(' AND ')}`\n].join(' ');\nconst params = [ ...Object.values(json.values), ...Object.values(json.where)];\n\ntry {\n await run(statement, params);\n output = { output: 'ok', error: null };\n} catch (err) {\n output = { output: null, error: err };\n} finally {\n await db.close();\n}\n\nreturn output;"
},
"typeVersion": 2
},
{
"id": "8c1b8bcb-20f1-4ef9-b646-9d89177651dd",
"name": "读取记录",
"type": "n8n-nodes-base.code",
"position": [
940,
80
],
"parameters": {
"jsCode": "const sqlite3 = require('sqlite3').verbose();\nconst { promisify } = require('util');\n\nconst db = new sqlite3.Database('/home/node/test.db');\nconst all = promisify(db.all.bind(db));\n\nconst { json } = $input.first();\n\nlet output = '';\nconst statement = [\n `SELECT * FROM ${json.tableName}`,\n json?.where && Object.keys(json?.where).length > 0\n ? `WHERE ` + Object.keys(json.where)\n .map((key,idx) => `${key} = $${idx+1}`)\n .join(' AND ')\n : ''\n].join(' ');\nconst params = json.where ? Object.values(json.where) : undefined;\n\ntry {\n \n const results = await all(statement.trim(), params);\n\n output = { output: [].concat(results), error: null };\n} catch (err) {\n output = { output: null, error: err };\n} finally {\n await db.close();\n}\n\nreturn output"
},
"typeVersion": 2
},
{
"id": "87df3eed-b4d5-4a9c-bd82-0ad455449cd2",
"name": "描述表",
"type": "@n8n/n8n-nodes-langchain.toolCode",
"position": [
-160,
340
],
"parameters": {
"name": "describeTable",
"jsCode": "const sqlite3 = require('sqlite3').verbose();\nconst { promisify } = require('util');\n\nconst db = new sqlite3.Database('/home/node/test.db');\nconst all = promisify(db.all.bind(db));\n\nlet output = '';\ntry {\n const rows = await all(`PRAGMA table_info(${query.tableName})`);\n const results = rows.map((col) => (\n `${col.name} | ${col.type} | NOT NULL: ${col.notnull} | Default: ${col.dflt_value}`\n )).join('\\n');\n \n output = { output: [].concat(results), error: null };\n} catch (err) {\n output = { output: null, error: err };\n} finally {\n await db.close();\n}\n\nreturn JSON.stringify(output);",
"schemaType": "manual",
"description": "调用此工具来描述表的架构。",
"inputSchema": "{\n \"type\": \"object\",\n \"required\": [\"tableName\"],\n \"properties\": {\n \"tableName\": {\n \"type\": \"string\",\n \"description\": \"Name of the table\"\n }\n }\n}",
"specifyInputSchema": true
},
"typeVersion": 1.1
},
{
"id": "4a0ba0d0-4955-44fd-92de-ad031ebb64cb",
"name": "列出数据表",
"type": "@n8n/n8n-nodes-langchain.toolCode",
"position": [
-260,
240
],
"parameters": {
"name": "listTables",
"jsCode": "const sqlite3 = require('sqlite3').verbose();\nconst { promisify } = require('util');\n\nconst db = new sqlite3.Database('/home/node/test.db');\nconst all = promisify(db.all.bind(db));\n\nlet output = '';\ntry {\n const rows = await all(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`, []);\n const results = rows.map((row) => row.name).join('\\n');\n \n output = { output: [].concat(results), error: null };\n} catch (err) {\n output = { output: null, error: err };\n} finally {\n await db.close();\n}\n\nreturn JSON.stringify(output);",
"description": "调用此工具来列出 SQLite 数据库中所有可用的表。"
},
"typeVersion": 1.1
},
{
"id": "69e8e720-7e91-4b46-8db5-1afdf1f3dbe0",
"name": "创建记录",
"type": "@n8n/n8n-nodes-langchain.toolWorkflow",
"position": [
-40,
440
],
"parameters": {
"name": "CreateRecords",
"workflowId": {
"__rl": true,
"mode": "id",
"value": "={{ $workflow.id }}"
},
"description": "调用此工具在 SQLite 表中创建一行。",
"workflowInputs": {
"value": {
"where": "={{ {} }}",
"values": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('values', `An object of key-value pair where key represents the column name.`, 'string') }}",
"operation": "insert",
"tableName": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('tableName', `table to insert into`, 'string') }}"
},
"schema": [
{
"id": "operation",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "operation",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "tableName",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "tableName",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "values",
"type": "object",
"display": true,
"removed": false,
"required": false,
"displayName": "values",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "where",
"type": "object",
"display": true,
"removed": false,
"required": false,
"displayName": "where",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": [],
"attemptToConvertTypes": false,
"convertFieldsToString": false
}
},
"typeVersion": 2.1
},
{
"id": "f2e18ae5-89a0-4d61-805b-e777f11300a2",
"name": "更新行",
"type": "@n8n/n8n-nodes-langchain.toolWorkflow",
"position": [
100,
360
],
"parameters": {
"name": "updateRows",
"workflowId": {
"__rl": true,
"mode": "id",
"value": "={{ $workflow.id }}"
},
"description": "调用此工具在表中创建一行。",
"workflowInputs": {
"value": {
"where": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('where', `An object of key-value pair where key represents the column name.`, 'string') }}",
"values": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('values', `An object of key-value pair where key represents the column name.`, 'string') }}",
"operation": "update",
"tableName": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('tableName', `table to update`, 'string') }}"
},
"schema": [
{
"id": "operation",
"type": "string",
"display": true,
"required": false,
"displayName": "operation",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "tableName",
"type": "string",
"display": true,
"required": false,
"displayName": "tableName",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "values",
"type": "object",
"display": true,
"required": false,
"displayName": "values",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "where",
"type": "object",
"display": true,
"required": false,
"displayName": "where",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": [],
"attemptToConvertTypes": false,
"convertFieldsToString": false
}
},
"typeVersion": 2.1
},
{
"id": "22645721-1b66-4a36-9be5-f1e5edde30f8",
"name": "读取行",
"type": "@n8n/n8n-nodes-langchain.toolWorkflow",
"position": [
180,
240
],
"parameters": {
"name": "readRows",
"workflowId": {
"__rl": true,
"mode": "id",
"value": "={{ $workflow.id }}"
},
"description": "调用此工具读取表中的一行或多行",
"workflowInputs": {
"value": {
"where": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('where', `An object of key-value pair where key represents the column name.`, 'string') }}",
"values": "={}",
"operation": "read",
"tableName": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('tableName', `table to read from`, 'string') }}"
},
"schema": [
{
"id": "operation",
"type": "string",
"display": true,
"required": false,
"displayName": "operation",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "tableName",
"type": "string",
"display": true,
"required": false,
"displayName": "tableName",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "values",
"type": "object",
"display": true,
"required": false,
"displayName": "values",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "where",
"type": "object",
"display": true,
"required": false,
"displayName": "where",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": [],
"attemptToConvertTypes": false,
"convertFieldsToString": false
}
},
"typeVersion": 2.1
},
{
"id": "2176742a-5a28-41c6-9cd7-ac3229ddcdb6",
"name": "便签 2",
"type": "n8n-nodes-base.stickyNote",
"position": [
-820,
-800
],
"parameters": {
"width": 440,
"height": 1360,
"content": "## 试试看!"
},
"typeVersion": 1
},
{
"id": "5a9a4763-2952-4d95-8f35-25238affa049",
"name": "便签 4",
"type": "n8n-nodes-base.stickyNote",
"position": [
-340,
-340
],
"parameters": {
"color": 3,
"width": 380,
"height": 100,
"content": "### 仅限自托管"
},
"typeVersion": 1
}
],
"pinData": {},
"connections": {
"ReadRows": {
"ai_tool": [
[
{
"node": "SQLite MCP Server",
"type": "ai_tool",
"index": 0
}
]
]
},
"Operation": {
"main": [
[
{
"node": "ReadRecords",
"type": "main",
"index": 0
}
],
[
{
"node": "CreateRecord",
"type": "main",
"index": 0
}
],
[
{
"node": "UpdateRecord",
"type": "main",
"index": 0
}
]
]
},
"ListTables": {
"ai_tool": [
[
{
"node": "SQLite MCP Server",
"type": "ai_tool",
"index": 0
}
]
]
},
"UpdateRows": {
"ai_tool": [
[
{
"node": "SQLite MCP Server",
"type": "ai_tool",
"index": 0
}
]
]
},
"CreateRecords": {
"ai_tool": [
[
{
"node": "SQLite MCP Server",
"type": "ai_tool",
"index": 0
}
]
]
},
"DescribeTables": {
"ai_tool": [
[
{
"node": "SQLite MCP Server",
"type": "ai_tool",
"index": 0
}
]
]
},
"When Executed by Another Workflow": {
"main": [
[
{
"node": "Operation",
"type": "main",
"index": 0
}
]
]
}
}
}常见问题
如何使用这个工作流?
复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。
这个工作流适合什么场景?
高级 - 构建模块, 人工智能
需要付费吗?
本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。
相关工作流推荐
构建自定义Qdrant向量存储MCP服务器
构建自定义Qdrant向量存储MCP服务器
If
Set
Code
+14
44 节点Jimleuk
构建模块
构建自定义YouTube MCP服务器
构建自定义YouTube MCP服务器
Set
Switch
Aggregate
+5
20 节点Jimleuk
构建模块
构建自定义API MCP服务器
构建自定义API MCP服务器
If
Set
Filter
+8
25 节点Jimleuk
工程
构建自定义GitHub MCP服务器
构建自定义GitHub MCP服务器
Set
Github
Switch
+6
19 节点Jimleuk
工程
构建自定义Google Drive MCP服务器
构建自定义Google Drive MCP服务器
Set
Switch
Google Drive
+7
17 节点Jimleuk
构建模块
构建自定义PostgreSQL MCP服务器
构建自定义PostgreSQL MCP服务器
Switch
Postgres
Postgres Tool
+4
15 节点Jimleuk
构建模块
工作流信息
难度等级
高级
节点数量16
分类2
节点类型7
作者
Jimleuk
@jimleukFreelance consultant based in the UK specialising in AI-powered automations. I work with select clients tackling their most challenging projects. For business enquiries, send me an email at hello@jimle.uk LinkedIn: https://www.linkedin.com/in/jimleuk/ X/Twitter: https://x.com/jimle_uk
外部链接
在 n8n.io 查看 →
分享此工作流