美文网首页
n8n SSE Endpoint 方式调用自己本地mcp服务

n8n SSE Endpoint 方式调用自己本地mcp服务

作者: ndxs2008 | 来源:发表于2025-06-05 09:33 被阅读0次

1 n8n流程图


image.png
image.png
{
  "name": "n8n-mcp-xx书",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ],
      "id": "de641539-794b-45d6-b5f8-f2ed38033b63",
      "name": "When chat message received",
      "webhookId": "efb120d9-332f-40f3-a074-7873dc9fcc94"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=你是交底书生成专家,根据用户问题内容{{$json.chatInput }}自动选择使用交底书mcp生成交底书",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.9,
      "position": [
        260,
        0
      ],
      "id": "1c2b31e7-014e-4730-a786-e487dc69d4fc",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "connectionType": "sse",
        "operation": "executeTool",
        "toolName": "={{$fromAI(\"tool\")}}",
        "toolParameters": "={{ $fromAI('Tool_Parameters', ``, 'json') }}"
      },
      "type": "n8n-nodes-mcp.mcpClientTool",
      "typeVersion": 1,
      "position": [
        440,
        220
      ],
      "id": "5828c51c-2cd3-45f1-94a8-c643ed0d5fc2",
      "name": "MCP Client",
      "credentials": {
        "mcpClientSseApi": {
          "id": "zGmxUyCqAcRxk9P6",
          "name": "MCP Client -魔塔mcp"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatDeepSeek",
      "typeVersion": 1,
      "position": [
        160,
        220
      ],
      "id": "4372c908-4a5b-41d9-881f-513927d32016",
      "name": "DeepSeek Chat Model",
      "credentials": {
        "deepSeekApi": {
          "id": "jMMXQvLh3LN8OMZu",
          "name": "DeepSeek account 2"
        }
      }
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        300,
        220
      ],
      "id": "31342492-241f-4af2-ab41-a84e35c69398",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "sseEndpoint": "=http://122.145.14.238:8000/sse"
      },
      "type": "@n8n/n8n-nodes-langchain.mcpClientTool",
      "typeVersion": 1,
      "position": [
        640,
        220
      ],
      "id": "4c4b651c-52c1-4b50-9db2-16a6ab9da838",
      "name": "交底书撰写"
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MCP Client": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "DeepSeek Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "交底书撰写": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0c6fb444-986d-4236-893a-0db4a6fefd97",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "cc70799ffa116d6456cbbb8995bdbd0d368f3995d3046992ffaaab7036cf839d"
  },
  "id": "S2wHsf0OnFBL2xCo",
  "tags": []
}

2 本地 mcp 服务代码
代码部署在服务器上

# from mcp.server.fastmcp import FastMCP
from fastmcp import FastMCP
import requests

mcp = FastMCP(
    name="generatetechdisclosure",
    host="0.0.0.0",
    port=8000,
    description="xx撰写MCP服务",
    sse_path='/sse'
)

@mcp.tool()
def generate_tech_disclosure() -> str:
    """生成xx"""
    url = "https://ai-xxx.openai.azure.com/openai/deployments/gpt-4.1/chat/completions"
    params = {
        "api-version": "2025-xxx"
    }
    headers = {
        "api-key": "5Oxxxx",
        "Content-Type": "application/json",
        "Authorization": "Bearer xxxx"
    }
    data = {
        "messages": [
            {
                "role": "system",
                "content": [
                    {
                        "type": "text",
                        "text": "你是一个帮助用户查找信息的 AI 助手。请帮我生成一份不少于4000字的 xxx"
                    }
                ]
            }
        ],
        "temperature": 1,
        "top_p": 1,
        "max_tokens": 4000
    }
    
    response = requests.post(url, params=params, headers=headers, json=data)
    if response.status_code == 200:
        return response.json()['choices'][0]['message']['content']
    else:
        raise Exception(f"API请求失败,状态码:{response.status_code}")


if __name__ == "__main__":
    try:
        print("Starting server...")
        # mcp.run(transport='stdio')
        mcp.run(transport="sse", host="0.0.0.0", port=8000)
    except Exception as e:
        print(f"Error: {e}")
    

相关文章

  • golang RPC

    1、RPC流水线工程 ① Client以本地调用的方式调用服务 ② Client Stub接收到调用后,把服务调用...

  • 2019-08-03 图解RPC

    一次 RPC 调用流程如下: • 服务消费者(Client 客户端)通过本地调用的方式调用服务。 •客户端存根(C...

  • rpc-dubbo

    如果有一种方式能让我们像调用本地服务一样调用远程服务,而让调用者对网络通信这些细节透明。这种方式其实就是RPC(R...

  • 本地服务调用远程dubbo服务的一种解决方案

    本地服务调用远程dubbo服务的一种解决方案 本地能调用远端的dubbo服务需要满足两个条件: 本地服务需要连接到...

  • iOS应用架构谈 组件化方案笔记

    调用方式 先说本地应用调用,本地组件A在某处调用[[CTMediator sharedInstance] perf...

  • 微服务从放弃到精通

    从0开始学微服务 概念 单体应用 不足:业务扩张导致系统膨胀 模块间调用方式:本地调用 微服务 特性拆分粒度一定程...

  • 令牌 Endpoint

    layout: docs-default 令牌 Endpoint 程序可以调用令牌endpoint来请求和刷新令牌...

  • Kubernetes-服务连接和暴露

    1 endpoint 1.1 endpoint介绍   服务和pod不是直接连接,而是通过Endpoint资源进行...

  • 分布式服务框架--第十章:服务发布和引用

    服务提供者需要通过配置、注解、API调用等方式,把本地接口发布成远程服务;对于消费者,可以通过对等的方式引用远程服...

  • 【SpringBoot WEB 系列】SSE 服务器发送事件详解

    【SpringBoot WEB系列】SSE 服务器发送事件详解 SSE 全称Server Sent Event,直...

网友评论

      本文标题:n8n SSE Endpoint 方式调用自己本地mcp服务

      本文链接:https://www.haomeiwen.com/subject/wtphwjtx.html