n8n¶
n8n MCP 服务器将你的 ADK 智能体连接到 n8n,一个可扩展的工作流自动化工具。此集成允许你的智能体安全地连接到 n8n 实例,直接通过自然语言界面搜索、检查和触发工作流。
替代方案:工作流级别的 MCP 服务器
本页的配置指南涵盖实例级别的 MCP 访问,它将你的智能体连接到已启用工作流的中央枢纽。或者,你可以使用 MCP 服务器触发器节点使单个工作流充当其自己的独立 MCP 服务器。如果你想制作特定的服务器行为或公开隔离到一个工作流的工具,此方法很有用。
使用场景¶
-
执行复杂工作流:直接从智能体触发 n8n 中定义的多步骤业务流程,利用可靠的分支逻辑、循环和错误处理来确保一致性。
-
连接到外部应用:通过 n8n 访问预构建的集成,无需为每个服务编写自定义工具,无需管理 API 身份验证、标头或样板代码。
-
数据处理:将复杂的数据转换任务卸载到 n8n 工作流,例如将自然语言转换为 API 调用或抓取和总结网页,利用自定义 Python 或 JavaScript 节点进行精确的数据塑形。
前置条件¶
- 活跃的 n8n 实例
- 在设置中启用 MCP 访问
- 有效的 MCP 访问令牌
有关详细的设置说明,请参阅 n8n MCP 文档。
与智能体一起使用¶
from google.adk.agents import Agent
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from mcp import StdioServerParameters
N8N_INSTANCE_URL = "https://localhost:5678"
N8N_MCP_TOKEN = "YOUR_N8N_MCP_TOKEN"
root_agent = Agent(
model="gemini-2.5-pro",
name="n8n_agent",
instruction="帮助用户管理和执行 n8n 中的工作流",
tools=[
McpToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx",
args=[
"-y",
"supergateway",
"--streamableHttp",
f"{N8N_INSTANCE_URL}/mcp-server/http",
"--header",
f"authorization:Bearer {N8N_MCP_TOKEN}"
]
),
timeout=300,
),
)
],
)
from google.adk.agents import Agent
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPServerParams
N8N_INSTANCE_URL = "https://localhost:5678"
N8N_MCP_TOKEN = "YOUR_N8N_MCP_TOKEN"
root_agent = Agent(
model="gemini-2.5-pro",
name="n8n_agent",
instruction="帮助用户管理和执行 n8n 中的工作流",
tools=[
McpToolset(
connection_params=StreamableHTTPServerParams(
url=f"{N8N_INSTANCE_URL}/mcp-server/http",
headers={
"Authorization": f"Bearer {N8N_MCP_TOKEN}",
},
),
)
],
)
可用工具¶
| 工具 | 描述 |
|---|---|
search_workflows |
搜索可用的工作流 |
execute_workflow |
执行特定工作流 |
get_workflow_details |
检索工作流的元数据和架构信息 |
配置¶
要使工作流可供智能体访问,它们必须满足以下条件:
-
处于活跃状态:工作流必须在 n8n 中激活。
-
支持的触发器:包含 Webhook、Schedule、Chat 或 Form 触发器节点。
-
为 MCP 启用:你必须在工作流设置中切换"在 MCP 中可用",或从工作流卡片菜单中选择"启用 MCP 访问"。