mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 04:37:31 +08:00
add mcp servers (#2076)
This commit is contained in:
102
plugins/wasm-go/mcp-servers/mcp-scripts/create_api_directories.sh
Executable file
102
plugins/wasm-go/mcp-servers/mcp-scripts/create_api_directories.sh
Executable file
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Function to display usage
|
||||
usage() {
|
||||
echo "Usage: $0 [options] [api_code1 api_code2 ...]"
|
||||
echo "Options:"
|
||||
echo " -h, --help Display this help message"
|
||||
echo ""
|
||||
echo "If no api_codes are specified, all APIs will be processed."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Parse command line options
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-h|--help)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
# Collect all remaining arguments as API codes
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Define the API data with English translations
|
||||
api_codes=("cmapi00065924" "cmapi00066017" "cmapi00054907" "cmapi012364" "cmapi029030" "cmapi011240" "cmapi011517" "cmapi011178" "cmapi010845" "cmapi011212" "cmapi00045093" "cmapi00067564" "cmapi011529" "cmapi022144" "cmapi026966" "cmapi011221" "cmapi00066410" "cmapi00048162" "cmapi00050817" "cmapi00044839" "cmapi00049059" "cmapi00046930" "cmapi00050226" "cmapi00069588" "cmapi022105" "cmapi00066353" "cmapi00065113" "cmapi027789" "cmapi00062739" "cmapi022081" "cmapi011032" "cmapi011138" "cmapi00066399" "cmapi00047480" "cmapi00067671")
|
||||
server_names=("stock-helper 股票助手" "calendar-holiday-helper 日历/假期助手" "ip-query ip查询" "weather-query 墨迹天气查询" "business-info-query 工商信息查询" "train-ticket-query 火车票查询" "today-in-history 历史上的今天" "hot-news 热门新闻" "stock-history-data 股票历史数据" "heavenly-stems-and-earthly-branches-query 天干地支查询" "recipe-query 菜谱查询" "business-credit-rating 企业信用评级" "zodiac-analysis 星座分析" "taobao-hot-words 淘宝热词" "fund-data-query 基金数据查询" "exchange-rate-query 汇率查询" "national-bid-query 全国招中标查询" "logistics-tracking-query 物流轨迹查询" "parking-lot-query 停车场查询" "agricultural-product-price-query 农产品价格查询" "business-patent-query 企业专利查询" "vehicle-info-query 车辆信息查询" "invoice-verification 发票查验" "traditional-chinese-medicine-tongue-diagnosis 中医舌诊" "tourist-attraction-query 旅游景点查询" "book-query 图书查询" "route-planning 路径规划" "global-financial-news 全球财经快讯" "oil-price-query 油价查询" "jd-hot-words 京东热词" "product-barcode-query 商品条码查询" "vehicle-restriction-query 车辆限行查询" "resume-analysis 简历解析" "deadbeat-query 老赖查询" "document-conversion 文档转换")
|
||||
|
||||
# If specific API codes are provided, filter the arrays
|
||||
if [[ $# -gt 0 ]]; then
|
||||
# Create temporary arrays
|
||||
declare -a filtered_api_codes
|
||||
declare -a filtered_server_names
|
||||
|
||||
for requested_api_code in "$@"; do
|
||||
for i in "${!api_codes[@]}"; do
|
||||
if [[ "${api_codes[$i]}" == "$requested_api_code" ]]; then
|
||||
filtered_api_codes+=("${api_codes[$i]}")
|
||||
filtered_server_names+=("${server_names[$i]}")
|
||||
break
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# Check if any API codes were found
|
||||
if [[ ${#filtered_api_codes[@]} -eq 0 ]]; then
|
||||
echo "Error: None of the specified API codes were found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Replace the original arrays with the filtered ones
|
||||
api_codes=("${filtered_api_codes[@]}")
|
||||
server_names=("${filtered_server_names[@]}")
|
||||
|
||||
echo "Processing ${#api_codes[@]} specified API(s)"
|
||||
else
|
||||
echo "Processing all ${#api_codes[@]} APIs"
|
||||
fi
|
||||
|
||||
# Function to process a single API
|
||||
process_api() {
|
||||
local api_code=$1
|
||||
local server_name=$2
|
||||
local english_name=$(echo "$server_name" | awk '{print $1}')
|
||||
local chinese_name=$(echo "$server_name" | awk '{print $2}')
|
||||
|
||||
echo "Processing $english_name ($api_code)..."
|
||||
|
||||
# Create directory
|
||||
mkdir -p "../$english_name"
|
||||
|
||||
# Generate mcp-server.yaml
|
||||
$GOPATH/bin/openapi-to-mcp --input "../$english_name/api.json" --output "../$english_name/mcp-server.yaml" --server-name "$english_name" --template yunmarket-tmpl.yaml
|
||||
|
||||
# Create README_ZH.md
|
||||
echo "# $chinese_name" > "../$english_name/README_ZH.md"
|
||||
# Add API details to README.md
|
||||
echo -e "\nAPI认证需要的APP Code请在阿里云API市场申请: https://market.aliyun.com/apimarket/detail/$api_code" >> "../$english_name/README_ZH.md"
|
||||
|
||||
# Generate Markdown documentation from YAML and append to README_ZH.md
|
||||
if [ -f "../$english_name/mcp-server.yaml" ]; then
|
||||
echo -e "\n" >> "../$english_name/README_ZH.md"
|
||||
python3 ./yaml_to_markdown.py "../$english_name/mcp-server.yaml" | cat >> "../$english_name/README_ZH.md"
|
||||
echo "Generated Markdown documentation for $english_name"
|
||||
fi
|
||||
|
||||
# Translate README_ZH.md to README.md
|
||||
if [ -f "../$english_name/README_ZH.md" ]; then
|
||||
python3 ./translate_readme.py "../$english_name/README_ZH.md" "../$english_name/README.md"
|
||||
echo "Translated README_ZH.md to README.md for $english_name"
|
||||
fi
|
||||
|
||||
echo "Completed processing $english_name"
|
||||
}
|
||||
|
||||
# Process APIs sequentially for now (simpler implementation)
|
||||
for i in "${!api_codes[@]}"; do
|
||||
process_api "${api_codes[$i]}" "${server_names[$i]}"
|
||||
done
|
||||
|
||||
echo "All API processing completed"
|
||||
29
plugins/wasm-go/mcp-servers/mcp-scripts/mcp-server-docs.md
Normal file
29
plugins/wasm-go/mcp-servers/mcp-scripts/mcp-server-docs.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# MCP服务器功能与工具简介
|
||||
|
||||
本文档旨在提供关于MCP服务器及其集成工具的功能概述,帮助开发者快速理解如何利用这些工具来实现PPT生成、获取下载链接以及检查生成状态等任务。
|
||||
|
||||
## 功能简介
|
||||
MCP服务器被命名为`ppt-generation`,主要负责处理与PPT创建相关的请求。它支持多种操作,包括根据用户提供的信息自动生成PPT文件、查询已生成PPT的状态及直接获取PPT文件的下载链接。通过配置特定的应用代码(appCode),用户可以安全地访问阿里云市场上的相关API服务以完成上述功能。此外,每个请求都包含了必要的认证信息如`Authorization`头和唯一的非重复标识符`X-Ca-Nonce`,确保了请求的安全性和唯一性。
|
||||
|
||||
## 工具简介
|
||||
|
||||
### 描述生成PPT
|
||||
此工具允许用户基于指定参数自定义生成PPT的内容与样式。用户可以通过调整复杂度参数来控制PPT的设计复杂程度,并且能够指定作者名称等个人信息。
|
||||
- **使用场景**: 当需要为会议、报告或演示准备个性化PPT时非常有用。
|
||||
- **参数说明**:
|
||||
- `complex`: 定义PPT的设计复杂度等级 (1-简单, 2-中等, 3-复杂)。
|
||||
- `text`: 用户希望包含在PPT中的文本内容。
|
||||
- `user_name`: PPT制作者的名字。
|
||||
|
||||
### 获取PPT下载链接
|
||||
一旦PPT生成完毕,该工具可以帮助用户获得对应的下载链接,以便于从网络上直接下载到本地设备。
|
||||
- **使用场景**: 在完成了PPT制作后,如果需要将其保存下来或者分享给他人时可使用此功能。
|
||||
- **参数说明**:
|
||||
- `id`: 唯一标识一个PPT生成任务的ID号。
|
||||
- `type`: 指定想要下载的文件格式类型。
|
||||
|
||||
### 获取PPT生成结果
|
||||
用于查询某个特定PPT生成任务的状态,了解其是否已完成以及是否有任何错误发生。
|
||||
- **使用场景**: 当提交了一个PPT生成请求之后,可以通过这个接口来跟踪进度并确认最终成果。
|
||||
- **参数说明**:
|
||||
- `id`: 代表某一具体PPT生成任务的唯一识别码。
|
||||
89
plugins/wasm-go/mcp-servers/mcp-scripts/translate_readme.py
Executable file
89
plugins/wasm-go/mcp-servers/mcp-scripts/translate_readme.py
Executable file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import requests
|
||||
|
||||
def read_file(file_path):
|
||||
"""Read file and return its content as a string."""
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
return file.read()
|
||||
except Exception as e:
|
||||
print(f"Error reading file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def call_openai_api(content, base_url):
|
||||
"""Call OpenAI API to translate content from Chinese to English."""
|
||||
url = f"http://{base_url}/chat/completions"
|
||||
|
||||
# Prepare the prompt for OpenAI
|
||||
prompt = f"""
|
||||
请将以下中文文档翻译成英文。保持原始的Markdown格式,包括标题、列表、代码块等。
|
||||
确保翻译准确、专业,并且保持技术术语的正确性。
|
||||
|
||||
以下是需要翻译的中文文档:
|
||||
|
||||
{content}
|
||||
"""
|
||||
|
||||
# Prepare the API request
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
data = {
|
||||
"model": "gpt-4o",
|
||||
"messages": [
|
||||
{"role": "system", "content": "你是一个专业的技术文档翻译助手,擅长将中文技术文档翻译成英文。"},
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
"temperature": 0.3
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
if "choices" in result and len(result["choices"]) > 0:
|
||||
return result["choices"][0]["message"]["content"]
|
||||
else:
|
||||
print("Error: Unexpected API response format")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error calling OpenAI API: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def save_markdown(markdown_content, output_file):
|
||||
"""Save the Markdown content to a file."""
|
||||
try:
|
||||
with open(output_file, 'w', encoding='utf-8') as file:
|
||||
file.write(markdown_content)
|
||||
except Exception as e:
|
||||
print(f"Error saving Markdown file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python translate_readme.py <input_file_path> [output_file_path]")
|
||||
sys.exit(1)
|
||||
|
||||
input_file = sys.argv[1]
|
||||
output_file = sys.argv[2] if len(sys.argv) > 2 else "README.md"
|
||||
base_url = "127.0.0.1:8080/v1"
|
||||
|
||||
# Read the Chinese content
|
||||
chinese_content = read_file(input_file)
|
||||
|
||||
# Translate to English
|
||||
english_content = call_openai_api(chinese_content, base_url)
|
||||
|
||||
# Save the translated content
|
||||
save_markdown(english_content, output_file)
|
||||
|
||||
# Print the translated content to stdout
|
||||
print(english_content)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
86
plugins/wasm-go/mcp-servers/mcp-scripts/yaml_to_markdown.py
Executable file
86
plugins/wasm-go/mcp-servers/mcp-scripts/yaml_to_markdown.py
Executable file
@@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import requests
|
||||
|
||||
def read_yaml_file(file_path):
|
||||
"""Read YAML file and return its content as a string."""
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
return file.read()
|
||||
except Exception as e:
|
||||
print(f"Error reading YAML file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def call_openai_api(yaml_content, base_url):
|
||||
"""Call OpenAI API to transform YAML to Markdown."""
|
||||
url = f"http://{base_url}/chat/completions"
|
||||
|
||||
# Prepare the prompt for OpenAI
|
||||
prompt = f"""
|
||||
请将以下MCP服务器YAML配置转换为Markdown格式的功能简介文档。
|
||||
文档应包含两个二级标题:
|
||||
1. ## 功能简介 - 概述该MCP服务器的主要功能和用途
|
||||
2. ## 工具简介 - 概括介绍每个工具的用途和使用场景
|
||||
|
||||
以下是YAML配置内容:
|
||||
|
||||
{yaml_content}
|
||||
"""
|
||||
|
||||
# Prepare the API request
|
||||
headers = {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
data = {
|
||||
"model": "gpt-4",
|
||||
"messages": [
|
||||
{"role": "system", "content": "你是一个专业的技术文档编写助手,擅长将技术配置文件转换为易于理解的文档。"},
|
||||
{"role": "user", "content": prompt}
|
||||
],
|
||||
"temperature": 0.7
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
response.raise_for_status()
|
||||
|
||||
result = response.json()
|
||||
if "choices" in result and len(result["choices"]) > 0:
|
||||
return result["choices"][0]["message"]["content"]
|
||||
else:
|
||||
print("Error: Unexpected API response format")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"Error calling OpenAI API: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def save_markdown(markdown_content, output_file):
|
||||
"""Save the Markdown content to a file."""
|
||||
try:
|
||||
with open(output_file, 'w', encoding='utf-8') as file:
|
||||
file.write(markdown_content)
|
||||
except Exception as e:
|
||||
print(f"Error saving Markdown file: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python yaml_to_markdown.py <yaml_file_path> [output_file_path]")
|
||||
sys.exit(1)
|
||||
|
||||
yaml_file = sys.argv[1]
|
||||
output_file = sys.argv[2] if len(sys.argv) > 2 else "mcp-server-docs.md"
|
||||
base_url = "127.0.0.1:8080/v1"
|
||||
|
||||
yaml_content = read_yaml_file(yaml_file)
|
||||
markdown_content = call_openai_api(yaml_content, base_url)
|
||||
save_markdown(markdown_content, output_file)
|
||||
|
||||
# Print the Markdown content to stdout
|
||||
print(markdown_content)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
11
plugins/wasm-go/mcp-servers/mcp-scripts/yunmarket-tmpl.yaml
Normal file
11
plugins/wasm-go/mcp-servers/mcp-scripts/yunmarket-tmpl.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
config:
|
||||
appCode: ""
|
||||
|
||||
tools:
|
||||
requestTemplate:
|
||||
headers:
|
||||
- key: Authorization
|
||||
value: "APPCODE {{.config.appCode}}"
|
||||
- key: X-Ca-Nonce
|
||||
value: "{{uuidv4}}"
|
||||
Reference in New Issue
Block a user