8
n8n 中文网amn8n.com

构建自定义文件系统MCP服务器

中级

这是一个自动化工作流,包含 14 个节点。主要使用 Switch, ExecuteCommand, ExecuteCommandTool, McpTrigger, ToolWorkflow 等节点。 构建自定义文件系统MCP服务器

前置要求
  • 无特殊前置要求,导入即可使用

分类

-
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9"
  },
  "nodes": [
    {
      "id": "24be8907-684e-4b57-9642-6f4a45ca7af3",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -380,
        -280
      ],
      "parameters": {
        "color": 7,
        "width": 680,
        "height": 660,
        "content": "## 1. 设置 MCP 服务器触发器"
      },
      "typeVersion": 1
    },
    {
      "id": "d5845d0a-648f-4bc1-b087-bc0d17506ed3",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -380,
        -400
      ],
      "parameters": {
        "color": 5,
        "width": 380,
        "height": 100,
        "content": "### 始终验证您的服务器!"
      },
      "typeVersion": 1
    },
    {
      "id": "fe9f1c8e-8334-4732-be3a-5ee49036e11e",
      "name": "文件系统 MCP 服务器",
      "type": "@n8n/n8n-nodes-langchain.mcpTrigger",
      "position": [
        -180,
        -140
      ],
      "webhookId": "0d93cfd5-2fbf-457e-9535-5bfc9a73ba9e",
      "parameters": {
        "path": "0d93cfd5-2fbf-457e-9535-5bfc9a73ba9e"
      },
      "typeVersion": 1
    },
    {
      "id": "fb49782f-d8de-480b-a470-e37adb2e3036",
      "name": "列出目录",
      "type": "n8n-nodes-base.executeCommandTool",
      "position": [
        -300,
        60
      ],
      "parameters": {
        "command": "=ls /home/node/{{ $fromAI('path', 'optional, leave blank for project root directory.') }}",
        "toolDescription": "List directories under the project root folder. The project root directory is /home/node/"
      },
      "typeVersion": 1
    },
    {
      "id": "8fa93054-bcf5-4fbc-9825-df16be063eb2",
      "name": "创建目录",
      "type": "n8n-nodes-base.executeCommandTool",
      "position": [
        -200,
        160
      ],
      "parameters": {
        "command": "=mkdir -p /home/node/{{ $fromAI('filename', 'name of directory. Will be scoped under the /home/node/ project root directory. Optionally use path to create within subdirectories') }}",
        "toolDescription": "Create directories under the project root folder. The project root folder is /home/node."
      },
      "typeVersion": 1
    },
    {
      "id": "aafe884d-0e6e-476a-92fe-b2111f624417",
      "name": "当被另一个工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        400,
        40
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "operation"
            },
            {
              "name": "filenames",
              "type": "array"
            },
            {
              "name": "contents",
              "type": "array"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "d85925b6-d58d-43b5-a6ca-3e43cbc81121",
      "name": "操作",
      "type": "n8n-nodes-base.switch",
      "position": [
        580,
        40
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "writeOneOrMultipleFiles",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "c1da2138-e2df-46d4-b1f4-97525c05e778",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.operation }}",
                    "rightValue": "writeOneOrMultipleFiles"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "readOneOrMultipleFiles",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "cc02a5a2-609c-4dbe-bdb6-45f145947e47",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.operation }}",
                    "rightValue": "readOneOrMultipleFiles"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "e9ec2928-5e33-4213-a53a-92b7d840d49e",
      "name": "读取一个或多个文件",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        840,
        140
      ],
      "parameters": {
        "command": "=cat {{ $json.filenames.join(' ') }}"
      },
      "typeVersion": 1
    },
    {
      "id": "77ba2a48-b4b9-4a23-818d-e028a7762514",
      "name": "读取文件",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        40,
        160
      ],
      "parameters": {
        "name": "readFil",
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "description": "=调用此工具读取文件内容。需包含文件扩展名",
        "workflowInputs": {
          "value": {
            "contents": "[]",
            "filenames": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('filenames', `An array of filenames`, 'string') }}",
            "operation": "readOneOrMultipleFiles"
          },
          "schema": [
            {
              "id": "operation",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "operation",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "filenames",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "filenames",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "contents",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "contents",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "2ddf9a9a-cade-41c0-a068-482345452d4b",
      "name": "写入文件",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        140,
        60
      ],
      "parameters": {
        "name": "write_file",
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "description": "调用此工具将内容写入一个或多个文件。文件名和内容通过各自的数组索引匹配。例如:要写入单个文件,使用 { filenames: [<文件名1>,<文件名2>], contents: [<内容1>,<内容2>] }",
        "workflowInputs": {
          "value": {
            "contents": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('contents', `An array of strings for content to be written`, 'string') }}",
            "filenames": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('filenames', `An array of strings for filenames`, 'string') }}",
            "operation": "writeOneOrMultipleFiles"
          },
          "schema": [
            {
              "id": "operation",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "operation",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "filenames",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "filenames",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "contents",
              "type": "array",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "contents",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "a5d9e11b-0583-4c67-b30b-be1d4185b891",
      "name": "写入一个或多个文件",
      "type": "n8n-nodes-base.executeCommand",
      "position": [
        840,
        -60
      ],
      "parameters": {
        "command": "={{\n$json.filenames.map((filename,idx) =>\n  `echo \"${$json.contents[idx] ?? ''}\" > /home/node/${filename}`\n).join('\\n')\n}}"
      },
      "typeVersion": 1
    },
    {
      "id": "de2f715c-b6d1-4702-9d39-2527108b5706",
      "name": "搜索目录",
      "type": "n8n-nodes-base.executeCommandTool",
      "position": [
        -80,
        240
      ],
      "parameters": {
        "command": "=find /home/node/ -name \"{{ $fromAI('filename', 'A name search paramter for the linux find tool') }}\"\n",
        "toolDescription": "Search the project folder for a file by name. The project root directory is /home/node/"
      },
      "typeVersion": 1
    },
    {
      "id": "a4918bb1-8882-45c8-a05c-a3e22912cc0f",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        320,
        -280
      ],
      "parameters": {
        "color": 7,
        "width": 740,
        "height": 660,
        "content": "## 2. 使用自定义工作流工具处理更复杂的命令"
      },
      "typeVersion": 1
    },
    {
      "id": "ebf6c15b-e4e0-4db0-bb4e-36e204fb6a47",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -880,
        -740
      ],
      "parameters": {
        "width": 460,
        "height": 1120,
        "content": "## 试试看!"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Operation": {
      "main": [
        [
          {
            "node": "writeOneOrMultipleFiles",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "readOneOrMultipleFiles",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ReadFiles": {
      "ai_tool": [
        [
          {
            "node": "FileSystem MCP Server",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "WriteFiles": {
      "ai_tool": [
        [
          {
            "node": "FileSystem MCP Server",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "ListDirectory": {
      "ai_tool": [
        [
          {
            "node": "FileSystem MCP Server",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "CreateDirectory": {
      "ai_tool": [
        [
          {
            "node": "FileSystem MCP Server",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "SearchDirectory": {
      "ai_tool": [
        [
          {
            "node": "FileSystem MCP Server",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Operation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量14
分类-
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Jimleuk

Jimleuk

@jimleuk

Freelance 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 查看

分享此工作流