Higress OPS MCP 服务器
模型上下文协议 (MCP) 服务器实现,可实现对Higress的全面配置和管理。此存储库还提供了基于LangGraph和LangChain MCP 适配器构建的 MCP 客户端,通过精心设计的代理流架构,方便与 Higress MCP 服务器进行交互。
演示
https://github.com/user-attachments/assets/bae66b77-a158-452e-9196-98060bac0df7
配置环境变量
将.env.example
文件复制到.env
并填写相应的值。
启动 MCP 客户端和 MCP 服务器
在 stdio 模式下,MCP 服务端进程由 MCP 客户端程序启动。运行以下命令启动 MCP 客户端和 MCP 服务端:
添加新工具
步骤 1:创建新的工具类或扩展现有的工具类
- 如果添加一个全新的工具类别,请在工具目录中创建一个新文件
- 或者,如果您的工具适合现有类别,则将其添加到现有类别中
from typing import Dict, List, Any
from fastmcp import FastMCP
class YourTools:
def register_tools(self, mcp: FastMCP):
@mcp.tool()
async def your_tool_function(arg1: str, arg2: int) -> List[Dict]:
"""
Your tool description.
Args:
arg1: Description of arg1
arg2: Description of arg2
Returns:
Description of the return value
Raises:
ValueError: If the request fails
"""
# Implementation using self.higress_client to make API calls
return self.higress_client.your_api_method(arg1, arg2)
步骤 2:如果您的工具需要与 Higress Console API 交互,请向 HigressClient 添加一个新方法
- 向 utils/higress_client.py 添加封装 API 调用的方法
- 使用现有的 HTTP 方法(get、put、post)进行实际 API 通信
def your_api_method(self, arg1: str, arg2: int) -> List[Dict]:
"""
Description of what this API method does.
Args:
arg1: Description of arg1
arg2: Description of arg2
Returns:
Response data
Raises:
ValueError: If the request fails
"""
path = "/v1/your/api/endpoint"
data = {"arg1": arg1, "arg2": arg2}
return self.put(path, data) # or self.get(path) or self.post(path, data)
步骤3:在服务器中注册你的工具类
- 将您的工具类添加到 server.py 中的 tool_classes 列表中
- ToolsRegister 使用此列表来实例化和注册所有工具
- ToolsRegister 将自动设置 logger 和 higress_client 属性
tool_classes = [
CommonTools,
RequestBlockTools,
RouteTools,
ServiceSourceTools,
YourTools # Add your tool class here
]
步骤 4:如果您的工具需要人工确认,请将其添加到SENSITIVE_TOOLS
# Define write operations that require human confirmation
SENSITIVE_TOOLS = [
"add_route",
"add_service_source",
"update_route",
"update_request_block_plugin",
"update_service_source",
"your_tool_function" # Add your tool name here if it requires confirmation
]