内容
# Higress OPS MCP Server
一个模型上下文协议(MCP)服务器实现,能够全面配置和管理 [Higress](https://higress.cn/)。该仓库还提供了一个基于 [LangGraph](https://www.langchain.com/langgraph) 和 [LangChain MCP Adapters](https://github.com/langchain-ai/langchain-mcp-adapters) 的MCP客户端,便于通过精心设计的代理流架构与Higress MCP服务器进行交互。
## 演示
https://github.com/user-attachments/assets/bae66b77-a158-452e-9196-98060bac0df7
## 配置环境变量
将 `.env.example` 文件复制为 `.env` 并填写相应的值。
## 启动 MCP 客户端和 MCP 服务器
在标准输入输出模式下,MCP服务器进程由MCP客户端程序启动。运行以下命令以启动MCP客户端和MCP服务器:
```python
uv run client.py
```
## 添加新工具
**步骤 1:创建一个新的工具类或扩展现有的工具类**
- 如果添加一个全新的工具类别,请在工具目录中创建一个新文件
- 或者如果您的工具适合现有类别,则将其添加到现有类中
```python
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]:
"""
您的工具描述。
参数:
arg1: arg1 的描述
arg2: arg2 的描述
返回:
返回值的描述
引发:
ValueError: 如果请求失败
"""
# 使用 self.higress_client 实现 API 调用
return self.higress_client.your_api_method(arg1, arg2)
```
**步骤 2:如果您的工具需要与Higress控制台API交互,请在HigressClient中添加新方法**
- 在 utils/higress_client.py 中添加封装API调用的方法
- 使用现有的HTTP方法(get、put、post)进行实际的API通信
```python
def your_api_method(self, arg1: str, arg2: int) -> List[Dict]:
"""
此API方法的功能描述。
参数:
arg1: arg1 的描述
arg2: arg2 的描述
返回:
响应数据
引发:
ValueError: 如果请求失败
"""
path = "/v1/your/api/endpoint"
data = {"arg1": arg1, "arg2": arg2}
return self.put(path, data) # 或 self.get(path) 或 self.post(path, data)
```
**步骤 3:在服务器中注册您的工具类**
- 将您的工具类添加到 server.py 中的 tool_classes 列表
- 该列表由 ToolsRegister 用于实例化和注册所有工具
- ToolsRegister 将自动设置 logger 和 higress_client 属性
```python
tool_classes = [
CommonTools,
RequestBlockTools,
RouteTools,
ServiceSourceTools,
YourTools # 在此处添加您的工具类
]
```
**步骤 4:如果您的工具需要人工确认,请将其添加到 `SENSITIVE_TOOLS`**
- 此列表中的工具在执行前需要人工确认
```python
# 定义需要人工确认的写操作
SENSITIVE_TOOLS = [
"add_route",
"add_service_source",
"update_route",
"update_request_block_plugin",
"update_service_source",
"your_tool_function" # 如果需要确认,请在此处添加您的工具名称
]
```