dataset skill

This commit is contained in:
cyc 2026-06-01 09:08:49 +08:00
parent 709709ca5c
commit b9d67d09eb
18 changed files with 3408 additions and 0 deletions

View File

@ -0,0 +1,4 @@
{
"API_BASE_URL": "http://172.20.32.121:31213/",
"MCP_TRANSPORT": "stdio"
}

View File

@ -0,0 +1,25 @@
import json
import os
def load_config():
config_path = os.path.join(os.path.dirname(__file__), "config.json")
default_config = {
"API_BASE_URL": "http://172.20.32.121:31213",
"MCP_TRANSPORT": "stdio"
}
try:
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
default_config.update(config)
except Exception as e:
print("读取配置文件失败: {}".format(e))
return default_config
config = load_config()
API_BASE_URL = config.get("API_BASE_URL")
TRANSPORT_MODE = config.get("MCP_TRANSPORT")

View File

@ -0,0 +1,58 @@
import json
import os
import httpx
from mcp.server.fastmcp import FastMCP
def load_config():
config_path = os.path.join(os.path.dirname(__file__), "config.json")
default_config = {
"API_BASE_URL": "http://172.20.32.121:31213",
"MCP_TRANSPORT": "stdio"
}
try:
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
default_config.update(config)
except Exception as e:
print("读取配置文件失败: {}".format(e))
return default_config
config = load_config()
API_BASE_URL = config.get("API_BASE_URL")
TRANSPORT_MODE = config.get("MCP_TRANSPORT")
mcp = FastMCP("login mcp server")
@mcp.tool()
async def login(
username: str,
password: str
) -> str:
url = "{}/api/auth/login".format(API_BASE_URL)
headers = {
"Content-Type": "application/json; charset=UTF-8"
}
payload = {
"username": username,
"password": password
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return "登录失败: {}".format(str(e))
if __name__ == "__main__":
print("启动登录 MCP 服务,传输模式: {}".format(TRANSPORT_MODE))
mcp.run(transport=TRANSPORT_MODE)

View File

@ -0,0 +1,89 @@
---
name: create-dataset
description: 当用户需要新增数据集时,请使用此技能。
triggers:
- "新增数据集"
- "创建数据集"
metadata:
api-base: http://172.20.32.121:31213
---
# 创建数据集(调用接口)
## 何时使用
- 用户需要创建一个新的数据集
- 用户提到“创建数据集”“新建数据集”“调用创建数据集 API”
## 执行流程Agent 必须遵守)
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- 数据集名称 `name`
- 数据集标签 `data_tag`
- 数据类型 `data_type`(可选,默认为"通用数据"
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
- 调用`common/login_mcp_server.login`方法
- 参数: `username`, `password`
- 获取 `access_token`
- 保存为临时变量 `token`
3. **创建数据集**
- 调用`scripts/dataset_mcp_server.create_dataset`方法
- 参数:`token`(来自步骤2), `name`, `data_tag`, `data_type`
4. **反馈结果**
- ✅ 成功:返回数据集名称和创建结果
- ❌ 失败:返回错误码和错误信息
5. **询问是否创建数据集版本**
- 如果创建数据集成功,则继续询问用户是否需要创建数据集版本
- 用户回答是则进行以下步骤,否则终止。
6. **输入版本描述**
- 输入版本描述version_desc
7. **上传文件**
- 调用`scripts/upload_file.upload_file`方法分片上传文件
8. **获取最新的版本号**
- 调用`scripts/dataset_mcp_server.query_next_version`方法获取最新的版本号
9. **创建数据集版本**
- 调用`scripts/dataset_mcp_server.create_dataset_version`方法
- 参数:`token`(来自步骤2), `git_id`(来自步骤3), `id`(来自步骤3), `identifier`(来自步骤3), `file_path`(来自步骤7的输入), `file_data`(来自步骤7的结果), `name`:name, `owner`:username, `version`(来自步骤8), `version_desc`(来自步骤6)
10. **反馈结果**
- 打印第9步的参数
- ✅ 成功:返回创建数据集版本结果
- ❌ 失败:返回错误码和错误信息
---
## 示例对话
**用户:**
> 帮我创建一个数据集,名字叫 test121标签是 test数据类型是通用数据
**Agent 行为:**
1. 询问用户名和密码(如未知)
2. 调用登录接口获取token
3. 调用创建数据集
4. 返回:
> ✅ 数据集 `test121` 创建成功
5. 询问是否创建数据集版本
6. 输入版本描述
7. 上传文件
8. 获取最新的版本号
9. 创建数据集版本
10. 反馈结果
---
## 注意事项
- Token 有效期由服务端控制,过期需重新登录
- 不建议将用户名、密码、Token 写入日志
- 创建失败时应明确提示是 **登录失败** 还是 **创建失败**

View File

@ -0,0 +1,139 @@
import httpx
import os
from mcp.server.fastmcp import FastMCP
from common.config import API_BASE_URL, TRANSPORT_MODE
mcp = FastMCP("数据集mcp server")
@mcp.tool()
async def create_dataset(
token: str,
name: str,
data_tag: str,
data_type: str = "通用数据"
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/addDataset"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
payload = {
"name": name,
"preview_pic": "http://172.20.32.121:31213/minio/data/mini-model-platform-data/temp/fanshuai/1761528061144/dataset/电学材料.png",
"dataset_source": "add",
"data_type": data_type,
"data_tag": data_tag,
"is_public": False,
"is_hot_stone": False
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 创建数据集失败: {str(e)}"
@mcp.tool()
async def query_next_version(
token: str,
identifier: str,
owner: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/queryNextVersion"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
payload = {
"identifier": identifier,
"owner": owner
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 获取版本号失败: {str(e)}"
@mcp.tool()
async def create_dataset_version(
token: str,
git_id: int,
id: int,
identifier: str,
file_path: str,
file_data: dict,
name: str,
owner: str,
version: str,
version_desc: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/addVersion"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
dataset_version_vos = [{"file_name": os.path.basename(file_path), "file_size": os.path.getsize(file_path), "url": file_data.get("location")}]
payload = {
"git_id": git_id,
"id": id,
"identifier": identifier,
"is_public": False,
"owner": owner,
"dataset_version_vos": dataset_version_vos,
"name": name,
"version": version,
"version_desc": version_desc,
"dataset_source": "add"
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 创建数据集版本失败: {str(e)}"
@mcp.tool()
async def query_asset_icon(
token: str,
category_id: int = 1
) -> str:
url = f"{API_BASE_URL}/api/mmp/assetIcon"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"page": 0,
"size": 10000,
"category_id": category_id
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 查询数据集分类失败: {str(e)}"
if __name__ == "__main__":
print(f"🚀 启动数据集 MCP 服务,传输模式: {TRANSPORT_MODE}")
mcp.run(transport=TRANSPORT_MODE)

View File

@ -0,0 +1,166 @@
import asyncio
import hashlib
import json
import os
import sys
import time
import httpx
API_BASE_URL = "http://172.20.32.121:31213"
DEFAULT_SIZE = 10 * 1024 * 1024 # 10MB
def compute_md5(file_path: str) -> str:
file_size = os.path.getsize(file_path)
chunk_size = min(DEFAULT_SIZE, file_size)
with open(file_path, "rb") as f:
data = f.read(chunk_size)
md5 = hashlib.md5(data).hexdigest()
filename = os.path.basename(file_path)
name_bytes = filename.encode('utf-8')
combined = md5.encode('utf-8') + name_bytes
return hashlib.md5(combined).hexdigest()
async def get_upload_task(token: str, params: dict) -> dict:
url = f"{API_BASE_URL}/api/mmp/uploader/chunk"
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
return response.json()
async def upload_chunk(token: str, file_path: str, part_number: int, total_chunks: int, identifier: str) -> dict:
url = f"{API_BASE_URL}/api/mmp/uploader/chunk"
file_size = os.path.getsize(file_path)
filename = os.path.basename(file_path)
start = DEFAULT_SIZE * (part_number - 1)
end = min(start + DEFAULT_SIZE, file_size)
current_chunk_size = end - start
with open(file_path, "rb") as f:
f.seek(start)
blob_data = f.read(current_chunk_size)
files = {
"chunkNumber": (None, str(part_number)),
"chunkSize": (None, str(DEFAULT_SIZE)),
"currentChunkSize": (None, str(current_chunk_size)),
"filename": (None, filename),
"relativePath": (None, filename),
"identifier": (None, identifier),
"totalChunks": (None, str(total_chunks)),
"totalSize": (None, str(file_size)),
"upfile": (filename, blob_data),
}
headers = {"Authorization": f"Bearer {token}"}
async with httpx.AsyncClient(timeout=600.0) as client:
response = await client.post(url, files=files, headers=headers)
response.raise_for_status()
return response.json()
async def merge_chunks(token: str, file_path: str, identifier: str) -> dict:
url = f"{API_BASE_URL}/api/mmp/uploader/mergeFile"
file_size = os.path.getsize(file_path)
filename = os.path.basename(file_path)
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json; charset=UTF-8",
}
payload = {
"fileType": "application/zip",
"name": filename,
"relativePath": filename,
"size": file_size,
"uniqueIdentifier": identifier,
"refProjectId": "123456789",
}
async with httpx.AsyncClient(timeout=600.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
return response.json()
async def get_merge_status(token: str, filename: str, identifier: str) -> dict:
url = f"{API_BASE_URL}/api/mmp/uploader/selectFile"
headers = {"Authorization": f"Bearer {token}"}
params = {"filename": filename, "identifier": identifier}
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
return response.json()
async def upload_file(token: str, file_path: str) -> dict:
file_size = os.path.getsize(file_path)
filename = os.path.basename(file_path)
total_chunks = max(1, (file_size + DEFAULT_SIZE - 1) // DEFAULT_SIZE)
identifier = compute_md5(file_path)
print(f"File: {filename}, Size: {file_size}, Chunks: {total_chunks}, MD5: {identifier}")
task_params = {
"chunkNumber": 1,
"chunkSize": DEFAULT_SIZE,
"currentChunkSize": min(DEFAULT_SIZE, file_size),
"totalSize": file_size,
"identifier": identifier,
"filename": filename,
"relativePath": filename,
"totalChunks": total_chunks,
}
task_result = await get_upload_task(token, task_params)
print(f"Task result: {json.dumps(task_result, ensure_ascii=False)}")
if task_result.get("code") != 200:
raise Exception(f"Get upload task failed: {task_result}")
task = task_result.get("data", {})
if task.get("skip_upload"):
print("File already uploaded, skipping upload")
return task
for part in range(1, total_chunks + 1):
print(f"Uploading chunk {part}/{total_chunks}...")
result = await upload_chunk(token, file_path, part, total_chunks, identifier)
print(f" Chunk {part} result: {json.dumps(result, ensure_ascii=False)}")
print("Merging chunks...")
merge_result = await merge_chunks(token, file_path, identifier)
print(f"Merge result: {json.dumps(merge_result, ensure_ascii=False)}")
if merge_result.get("code") != 200:
raise Exception(f"Merge failed: {merge_result}")
merge_data = merge_result.get("data", {})
if merge_data.get("state") == "Succeeded":
print("Merge succeeded immediately!")
return merge_data
if merge_data.get("location"):
print("Merge has location, returning immediately despite state:", merge_data.get("state"))
return merge_data
for i in range(30):
await asyncio.sleep(3)
status_result = await get_merge_status(token, filename, identifier)
status_data = status_result.get("data", {})
state = status_data.get("state")
print(f" Merge status poll #{i+1}: {state}")
if state == "Succeeded":
print("Merge succeeded!")
return status_data
elif state == "Failed":
if status_data.get("location"):
print("Merge has location despite Failed state, using it")
return status_data
raise Exception(f"Merge failed: {status_result}")
raise Exception("Merge status polling timed out")
async def main():
token = sys.argv[1]
file_path = sys.argv[2]
result = await upload_file(token, file_path)
print(f"\nFinal result:\n{json.dumps(result, ensure_ascii=False, indent=2)}")
if __name__ == "__main__":
asyncio.run(main())

View File

@ -0,0 +1,75 @@
---
name: delete-dataset
description: 当用户需要删除数据集或版本时,请使用此技能。
triggers:
- "删除数据集"
- "删除数据集版本"
metadata:
api-base: http://172.20.32.121:31213
---
# 删除数据集(调用接口)
## 何时使用
- 用户需要删除数据集版本
- 用户需要删除整个数据集
## 执行流程Agent 必须遵守)
### 删除数据集版本
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- git_id `git_id`
- owner `owner`
- identifier `identifier`
- relative_paths `relative_paths`
- version `version`
- 若缺失用户名和密码,必须先向用户询问
2. **调用登录接口获取token**
- 调用`common/login_mcp_server.login`方法
- 获取 `access_token`
3. **删除版本**
- 调用`scripts/delete_mcp_server.delete_dataset_version`方法
4. **反馈结果**
- ✅ 成功:返回操作成功信息
- ❌ 失败:返回错误码和错误信息
### 删除数据集
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- id `id`
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
3. **删除数据集**
- 调用`scripts/delete_mcp_server.delete_dataset`方法
4. **反馈结果**
---
## 示例对话
**用户:**
> 帮我删除数据集id是46
**Agent 行为:**
1. 询问用户名和密码
2. 调用登录接口获取token
3. 调用删除数据集接口
4. 返回删除结果
---
## 注意事项
- Token 有效期由服务端控制,过期需重新登录
- 不建议将用户名、密码、Token 写入日志
- 删除操作不可逆,请谨慎操作

View File

@ -0,0 +1,63 @@
import httpx
from mcp.server.fastmcp import FastMCP
from common.config import API_BASE_URL, TRANSPORT_MODE
mcp = FastMCP("删除数据集mcp server")
@mcp.tool()
async def delete_dataset_version(
token: str,
git_id: int,
owner: str,
identifier: str,
relative_paths: str,
version: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/deleteDatasetVersion"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"git_id": git_id,
"owner": owner,
"identifier": identifier,
"relative_paths": relative_paths,
"version": version
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.delete(url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 删除数据集版本失败: {str(e)}"
@mcp.tool()
async def delete_dataset(
token: str,
id: int
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/deleteDataset/{id}"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.delete(url, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 删除数据集失败: {str(e)}"
if __name__ == "__main__":
print(f"🚀 启动删除数据集 MCP 服务,传输模式: {TRANSPORT_MODE}")
mcp.run(transport=TRANSPORT_MODE)

View File

@ -0,0 +1,77 @@
---
name: download-dataset
description: 当用户需要下载数据集版本文件时,请使用此技能。
triggers:
- "下载数据集"
- "下载数据集版本"
- "下载数据集文件"
metadata:
api-base: http://172.20.32.121:31213
---
# 下载数据集版本文件(调用接口)
## 何时使用
- 用户需要下载数据集当前版本的所有文件
- 用户需要下载数据集当前版本的单个文件
## 执行流程Agent 必须遵守)
### 下载当前版本所有文件打包
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- name `name`
- git_id `git_id`
- version `version`
- identifier `identifier`
- owner `owner`
- is_public `is_public`
- 若缺失用户名和密码,必须先向用户询问
2. **调用登录接口获取token**
- 调用`common/login_mcp_server.login`方法
- 获取 `access_token`
3. **下载文件**
- 调用`scripts/download_mcp_server.download_all_files`方法
4. **反馈结果**
- ✅ 成功:返回文件内容
- ❌ 失败:返回错误码和错误信息
### 下载当前版本选中文件
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- url `url`
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
3. **下载文件**
- 调用`scripts/download_mcp_server.download_single_file`方法
4. **反馈结果**
---
## 示例对话
**用户:**
> 帮我下载数据集的所有文件name是test121git_id是125version是v1identifier是fanshuai_dataset_20260519104635owner是fanshuaiis_public是false
**Agent 行为:**
1. 询问用户名和密码
2. 调用登录接口获取token
3. 调用下载所有文件接口
4. 返回文件内容
---
## 注意事项
- Token 有效期由服务端控制,过期需重新登录
- 不建议将用户名、密码、Token 写入日志
- 下载文件可能较大,请确保网络连接稳定

View File

@ -0,0 +1,66 @@
import httpx
from mcp.server.fastmcp import FastMCP
from common.config import API_BASE_URL, TRANSPORT_MODE
mcp = FastMCP("下载数据集mcp server")
@mcp.tool()
async def download_all_files(
token: str,
name: str,
git_id: int,
version: str,
identifier: str,
owner: str,
is_public: bool
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/downloadAllFiles"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"name": name,
"git_id": git_id,
"version": version,
"identifier": identifier,
"owner": owner,
"is_public": is_public
}
try:
async with httpx.AsyncClient(timeout=300.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
return f"✅ 下载成功,文件大小: {len(response.content)} 字节"
except Exception as e:
return f"❌ 下载所有文件失败: {str(e)}"
@mcp.tool()
async def download_single_file(
token: str,
url: str
) -> str:
api_url = f"{API_BASE_URL}/api/mmp/newdataset/downloadSingleFile"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"url": url
}
try:
async with httpx.AsyncClient(timeout=300.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
return f"✅ 下载成功,文件大小: {len(response.content)} 字节"
except Exception as e:
return f"❌ 下载单个文件失败: {str(e)}"
if __name__ == "__main__":
print(f"🚀 启动下载数据集 MCP 服务,传输模式: {TRANSPORT_MODE}")
mcp.run(transport=TRANSPORT_MODE)

View File

@ -0,0 +1,97 @@
---
name: query-dataset
description: 当用户需要查询数据集信息时,请使用此技能。
triggers:
- "查询数据集"
- "查询数据集列表"
- "查询数据集详情"
- "发布数据集"
metadata:
api-base: http://172.20.32.121:31213
---
# 查询数据集(调用接口)
## 何时使用
- 用户需要查询数据集列表
- 用户需要查询数据集详情
- 用户需要发布数据集
## 执行流程Agent 必须遵守)
### 查询数据集列表
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- 页码 `page`可选默认0
- 每页大小 `size`可选默认20
- 是否公开 `is_public`(可选)
- 数据类型 `data_type`(可选)
- 是否热门 `is_hot_stone`(可选)
- 若缺失用户名和密码,必须先向用户询问
2. **调用登录接口获取token**
- 调用`common/login_mcp_server.login`方法
- 获取 `access_token`
3. **查询数据集列表**
- 调用`scripts/query_mcp_server.query_datasets`方法
4. **反馈结果**
- ✅ 成功:返回数据集列表
- ❌ 失败:返回错误码和错误信息
### 查询数据集详情
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- git_id `git_id`
- owner `owner`
- name `name`
- identifier `identifier`
- is_public `is_public`
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
3. **查询数据集详情**
- 调用`scripts/query_mcp_server.get_dataset_detail`方法
4. **反馈结果**
### 发布数据集
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- id `id`
- name `name`
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
3. **发布数据集**
- 调用`scripts/query_mcp_server.publish_dataset`方法
4. **反馈结果**
---
## 示例对话
**用户:**
> 帮我查询数据集列表
**Agent 行为:**
1. 询问用户名和密码
2. 调用登录接口获取token
3. 调用查询数据集列表接口
4. 返回数据集列表
---
## 注意事项
- Token 有效期由服务端控制,过期需重新登录
- 不建议将用户名、密码、Token 写入日志

View File

@ -0,0 +1,129 @@
import httpx
from mcp.server.fastmcp import FastMCP
from common.config import API_BASE_URL, TRANSPORT_MODE
mcp = FastMCP("查询数据集mcp server")
@mcp.tool()
async def query_datasets(
token: str,
page: int = 0,
size: int = 20,
is_public: bool = None,
data_type: str = "",
is_hot_stone: bool = None
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/queryDatasets"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"page": page,
"size": size
}
if is_public is not None:
params["is_public"] = is_public
if data_type:
params["data_type"] = data_type
if is_hot_stone is not None:
params["is_hot_stone"] = is_hot_stone
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 查询数据集列表失败: {str(e)}"
@mcp.tool()
async def get_dataset_detail(
token: str,
git_id: int,
owner: str,
name: str,
identifier: str,
is_public: bool
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/getDatasetDetail"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"git_id": git_id,
"owner": owner,
"name": name,
"identifier": identifier,
"is_public": is_public
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 查询数据集详情失败: {str(e)}"
@mcp.tool()
async def publish_dataset(
token: str,
id: int,
name: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/publish"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
payload = {
"id": id,
"name": name
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.post(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 发布数据集失败: {str(e)}"
@mcp.tool()
async def get_version_list(
token: str,
owner: str,
identifier: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/getVersionList"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
params = {
"owner": owner,
"identifier": identifier
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.get(url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 获取版本列表失败: {str(e)}"
if __name__ == "__main__":
print(f"🚀 启动查询数据集 MCP 服务,传输模式: {TRANSPORT_MODE}")
mcp.run(transport=TRANSPORT_MODE)

View File

@ -0,0 +1,85 @@
---
name: update-dataset
description: 当用户需要修改数据集信息时,请使用此技能。
triggers:
- "修改数据集"
- "编辑数据集"
- "修改数据集简介"
metadata:
api-base: http://172.20.32.121:31213
---
# 修改数据集(调用接口)
## 何时使用
- 用户需要修改数据集信息
- 用户需要修改数据集简介
## 执行流程Agent 必须遵守)
### 修改数据集
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- id `id`
- name `name`
- identifier `identifier`
- description `description`(可选)
- is_public `is_public`
- data_type `data_type`
- data_tag `data_tag`
- owner `owner`
- dataset_source `dataset_source`
- relative_paths `relative_paths`
- is_hot_stone `is_hot_stone`
- git_id `git_id`
- preview_pic `preview_pic`
- 若缺失用户名和密码,必须先向用户询问
2. **调用登录接口获取token**
- 调用`common/login_mcp_server.login`方法
- 获取 `access_token`
3. **修改数据集**
- 调用`scripts/update_mcp_server.update_dataset`方法
4. **反馈结果**
- ✅ 成功:返回修改后的数据集信息
- ❌ 失败:返回错误码和错误信息
### 修改数据集简介
1. **确认参数**
- 是否已提供:
- 用户名 `username`
- 密码 `password`
- git_id `git_id`
- identifier `identifier`
- description `description`
- 若缺失,必须先向用户询问
2. **调用登录接口获取token**
3. **修改数据集简介**
- 调用`scripts/update_mcp_server.update_dataset_desc`方法
4. **反馈结果**
---
## 示例对话
**用户:**
> 帮我修改数据集简介git_id是125identifier是fanshuai_dataset_20260519104635简介是新的描述内容
**Agent 行为:**
1. 询问用户名和密码
2. 调用登录接口获取token
3. 调用修改数据集简介接口
4. 返回修改结果
---
## 注意事项
- Token 有效期由服务端控制,过期需重新登录
- 不建议将用户名、密码、Token 写入日志

View File

@ -0,0 +1,91 @@
import httpx
from mcp.server.fastmcp import FastMCP
from common.config import API_BASE_URL, TRANSPORT_MODE
mcp = FastMCP("修改数据集mcp server")
@mcp.tool()
async def update_dataset(
token: str,
id: int,
name: str,
identifier: str,
description: str = "",
is_public: bool = False,
data_type: str = "通用数据",
data_tag: str = "",
owner: str = "",
dataset_source: str = "add",
relative_paths: str = "",
is_hot_stone: bool = False,
git_id: int = 0,
preview_pic: str = ""
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/updateDataset"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
payload = {
"id": id,
"name": name,
"identifier": identifier,
"description": description,
"is_public": is_public,
"data_type": data_type,
"data_tag": data_tag,
"praises_count": 0,
"praised": False,
"create_by": owner,
"update_time": "",
"owner": owner,
"dataset_source": dataset_source,
"relative_paths": relative_paths,
"is_hot_stone": is_hot_stone,
"git_id": git_id,
"preview_pic": preview_pic,
"type": 0
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.put(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 修改数据集失败: {str(e)}"
@mcp.tool()
async def update_dataset_desc(
token: str,
git_id: int,
identifier: str,
description: str
) -> str:
url = f"{API_BASE_URL}/api/mmp/newdataset/updateDesc"
headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": f"Bearer {token}"
}
payload = {
"git_id": git_id,
"identifier": identifier,
"description": description
}
try:
async with httpx.AsyncClient(timeout=30.0) as client:
response = await client.put(url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return result
except Exception as e:
return f"❌ 修改数据集简介失败: {str(e)}"
if __name__ == "__main__":
print(f"🚀 启动修改数据集 MCP 服务,传输模式: {TRANSPORT_MODE}")
mcp.run(transport=TRANSPORT_MODE)

108
dataset/test_dataset.json Normal file
View File

@ -0,0 +1,108 @@
{
"add_dataset": {
"name": "测试数据集",
"preview_pic": "http://172.20.32.121:31213/minio/data/mini-model-platform-data/temp/test/test.png",
"dataset_source": "add",
"data_type": "通用数据",
"data_tag": "test",
"is_public": false,
"is_hot_stone": false
},
"get_asset_icon": {
"page": 0,
"size": 100,
"category_id": 1
},
"upload_chunk": {
"chunkNumber": 1,
"chunkSize": 10485760,
"currentChunkSize": 9577,
"totalSize": 9577,
"identifier": "e174a5dd7c5357a2efc3416ee0465e3c",
"filename": "test.zip",
"relativePath": "test.zip",
"totalChunks": 1
},
"add_version": {
"git_id": 125,
"id": 47,
"identifier": "test_dataset_20260520100000",
"is_public": false,
"owner": "test_user",
"name": "测试数据集",
"version": "v1",
"version_desc": "初始版本",
"dataset_source": "add",
"dataset_version_vos": [
{
"file_name": "test.zip",
"file_size": 9577,
"url": "/home/resource/temp/test/e174a5dd7c5357a2efc3416ee0465e3c/test.zip"
}
]
},
"update_dataset": {
"id": 47,
"name": "测试数据集_修改",
"identifier": "test_dataset_20260520100000",
"description": "测试描述",
"is_public": false,
"data_type": "通用数据",
"data_tag": "test",
"praises_count": 0,
"praised": false,
"create_by": "test_user",
"update_time": "2026-05-20 10:00:00",
"owner": "test_user",
"dataset_source": "add",
"relative_paths": "test/datasets/125/test_dataset_20260520100000/origin/dataset",
"is_hot_stone": false,
"git_id": 125,
"preview_pic": "http://172.20.32.121:31213/minio/data/mini-model-platform-data/temp/test/test.png",
"type": 0
},
"update_desc": {
"git_id": 125,
"identifier": "test_dataset_20260520100000",
"description": "更新后的数据集简介"
},
"publish_dataset": {
"id": 47,
"name": "测试数据集"
},
"download_all_files": {
"name": "测试数据集",
"git_id": 125,
"version": "v1",
"identifier": "test_dataset_20260520100000",
"owner": "test_user",
"is_public": false
},
"download_single_file": {
"url": "/home/resource/test/datasets/125/test_dataset_20260520100000/v1/dataset/test.zip"
},
"delete_version": {
"git_id": 125,
"owner": "test_user",
"identifier": "test_dataset_20260520100000",
"relative_paths": "test/datasets/125/test_dataset_20260520100000/v1/dataset",
"version": "v1"
},
"delete_dataset": {
"id": 47
},
"query_datasets": {
"page": 0,
"size": 20,
"is_public": false,
"data_type": "",
"is_hot_stone": false
},
"get_dataset_detail": {
"git_id": 74,
"owner": "test_user",
"name": "测试数据集",
"identifier": "test_dataset_20260520100000",
"is_public": false
}
}

146
test/test_dataset.py Normal file
View File

@ -0,0 +1,146 @@
import asyncio
import json
import sys
import os
import time
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from tools.dataset_mcp_server import (
login,
add_dataset,
get_asset_icon,
upload_chunk,
add_version,
update_dataset,
update_desc,
publish_dataset,
download_all_files,
download_single_file,
delete_version,
delete_dataset,
query_datasets,
get_dataset_detail
)
async def run_test(test_name, func, params, retries=1):
"""运行单个测试用例"""
print(f"\n{'='*60}")
print(f"🔧 测试: {test_name}")
print(f"{'='*60}")
print(f"参数: {json.dumps(params, indent=2, ensure_ascii=False)}")
try:
result = await func(**params)
print(f"\n✅ 成功!")
print(f"返回结果: {result[:500]}{'...' if len(result) > 500 else ''}")
return result, True
except Exception as e:
if retries > 0:
print(f"\n⚠️ 失败,重试中 ({retries}次剩余): {e}")
return await run_test(test_name, func, params, retries - 1)
print(f"\n❌ 失败: {e}")
return None, False
async def main():
print("🚀 开始测试数据集 MCP 服务...")
try:
with open("../dataset/test_dataset.json", "r", encoding="utf-8") as f:
test_data = json.load(f)
except FileNotFoundError:
print("❌ 错误:找不到 test_dataset.json 文件")
return
except json.JSONDecodeError as e:
print(f"❌ 错误test_dataset.json 格式不正确: {e}")
return
token = None
login_result, success = await run_test("登录获取Token", login, {})
if success:
try:
login_data = json.loads(login_result)
token = login_data.get("data", {}).get("access_token")
print(f"\n📋 获取到Token: {token[:20]}...")
except:
print("❌ 解析Token失败")
return
else:
print("❌ 登录失败,无法继续测试")
return
test_cases = [
("查询数据集分类", get_asset_icon, {"token": token, **test_data["get_asset_icon"]}),
("查询数据集列表", query_datasets, {"token": token, **test_data["query_datasets"]}),
]
passed = 1
failed = 0
for test_name, func, params in test_cases:
_, success = await run_test(test_name, func, params)
if success:
passed += 1
else:
failed += 1
new_dataset_params = {"token": token, **test_data["add_dataset"]}
dataset_name = test_data["add_dataset"]["name"]
add_result, success = await run_test("新增数据集", add_dataset, new_dataset_params)
while not success or (add_result and "项目名称已被使用" in add_result):
if add_result and "项目名称已被使用" in add_result:
timestamp = int(time.time())
new_name = f"{dataset_name}_{timestamp}"
print(f"\n⚠️ 项目名称已被使用,尝试新名称: {new_name}")
new_dataset_params["name"] = new_name
add_result, success = await run_test("新增数据集", add_dataset, new_dataset_params)
else:
break
if success:
passed += 1
try:
add_result_data = json.loads(add_result)
dataset_id = add_result_data.get("data", {}).get("id", test_data["add_version"]["id"])
print(f"\n📋 创建的数据集ID: {dataset_id}")
except:
dataset_id = test_data["add_version"]["id"]
else:
failed += 1
dataset_id = test_data["add_version"]["id"]
remaining_tests = [
("上传文件分片", upload_chunk, {"token": token, **test_data["upload_chunk"]}),
("新增版本", add_version, {"token": token, **{"id": dataset_id, **test_data["add_version"]}}),
("修改数据集", update_dataset, {"token": token, **{"id": dataset_id, **test_data["update_dataset"]}}),
("编辑数据集简介", update_desc, {"token": token, **test_data["update_desc"]}),
("发布数据集", publish_dataset, {"token": token, **{"id": dataset_id, **test_data["publish_dataset"]}}),
("下载全部文件", download_all_files, {"token": token, **test_data["download_all_files"]}),
("下载单个文件", download_single_file, {"token": token, **test_data["download_single_file"]}),
("删除版本", delete_version, {"token": token, **test_data["delete_version"]}),
("删除数据集", delete_dataset, {"token": token, **{"id": dataset_id}}),
("查询数据集详情", get_dataset_detail, {"token": token, **test_data["get_dataset_detail"]})
]
for test_name, func, params in remaining_tests:
_, success = await run_test(test_name, func, params)
if success:
passed += 1
else:
failed += 1
print(f"\n{'='*60}")
print("📊 测试结果汇总")
print(f"{'='*60}")
print(f"通过: {passed}")
print(f"失败: {failed}")
print(f"成功率: {passed / (passed + failed) * 100:.1f}%")
if __name__ == "__main__":
asyncio.run(main())

730
tools/dataset_mcp_server.py Normal file
View File

@ -0,0 +1,730 @@
import httpx
from mcp.server.fastmcp import FastMCP
import os
import json
def load_config():
config_path = os.path.join(os.path.dirname(__file__), "../config/config.json")
default_config = {
"DATASET_API_BASE_URL": "http://172.20.32.121:31213",
"DATASET_DEFAULT_USERNAME": "fanshuai",
"DATASET_DEFAULT_PASSWORD": "h1n2x3j4y5@",
"MCP_TRANSPORT": "stdio"
}
try:
if os.path.exists(config_path):
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
default_config.update(config)
print(f"✅ 已加载配置文件: {config_path}")
else:
print(f"⚠️ 配置文件不存在,使用默认配置: {config_path}")
except Exception as e:
print(f"❌ 读取配置文件失败: {e},使用默认配置")
return default_config
config = load_config()
API_BASE_URL = config.get("DATASET_API_BASE_URL")
DEFAULT_USERNAME = config.get("DATASET_DEFAULT_USERNAME")
DEFAULT_PASSWORD = config.get("DATASET_DEFAULT_PASSWORD")
TRANSPORT_MODE = config.get("MCP_TRANSPORT")
mcp = FastMCP("Dataset-Service")
@mcp.tool()
async def login(
username: str = "",
password: str = ""
) -> str:
"""
获取访问token
参数说明:
- username: 用户名 (可选默认使用配置文件中的用户名)
- password: 密码 (可选默认使用配置文件中的密码)
返回: 包含access_token和expires_in的JSON字符串
"""
api_url = f"{API_BASE_URL}/api/auth/login"
if not username:
username = DEFAULT_USERNAME
if not password:
password = DEFAULT_PASSWORD
payload = {
"username": username,
"password": password
}
headers = {"Content-Type": "application/json"}
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"登录 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接认证服务: {str(e)}"
except Exception as e:
return f"登录时发生未知错误: {str(e)}"
def build_auth_headers(token: str) -> dict:
"""构建包含token的请求头"""
return {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
}
@mcp.tool()
async def add_dataset(
name: str,
token: str,
preview_pic: str = "",
dataset_source: str = "add",
data_type: str = "通用数据",
data_tag: str = "",
is_public: bool = False,
is_hot_stone: bool = False
) -> str:
"""
新增数据集
参数说明:
- name: 数据集名称
- token: 访问令牌
- preview_pic: 预览图片URL (可选)
- dataset_source: 数据集来源 (默认: add)
- data_type: 数据类型 (默认: 通用数据)
- data_tag: 数据标签 (可选)
- is_public: 是否公开 (默认: False)
- is_hot_stone: 是否热门 (默认: False)
返回: 新增数据集的详细信息
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/addDataset"
payload = {
"name": name,
"preview_pic": preview_pic,
"dataset_source": dataset_source,
"data_type": data_type,
"data_tag": data_tag,
"is_public": is_public,
"is_hot_stone": is_hot_stone
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"新增数据集 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"新增数据集时发生未知错误: {str(e)}"
@mcp.tool()
async def get_asset_icon(
token: str,
page: int = 0,
size: int = 10000,
category_id: int = 1
) -> str:
"""
查询数据集分类接口
参数说明:
- token: 访问令牌
- page: 页码 (默认: 0)
- size: 每页数量 (默认: 10000)
- category_id: 分类ID (默认: 1)
返回: 数据集分类列表
"""
api_url = f"{API_BASE_URL}/api/mmp/assetIcon"
params = {
"page": page,
"size": size,
"category_id": category_id
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"查询数据集分类 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"查询数据集分类时发生未知错误: {str(e)}"
@mcp.tool()
async def upload_chunk(
token: str,
chunkNumber: int,
chunkSize: int,
currentChunkSize: int,
totalSize: int,
identifier: str,
filename: str,
relativePath: str,
totalChunks: int
) -> str:
"""
上传版本文件分片上传
参数说明:
- token: 访问令牌
- chunkNumber: 当前分片序号
- chunkSize: 分片大小
- currentChunkSize: 当前分片实际大小
- totalSize: 总文件大小
- identifier: 文件唯一标识
- filename: 文件名
- relativePath: 相对路径
- totalChunks: 总分片数
返回: 上传结果包含文件location用于新增版本
"""
api_url = f"{API_BASE_URL}/api/mmp/uploader/chunk"
params = {
"chunkNumber": chunkNumber,
"chunkSize": chunkSize,
"currentChunkSize": currentChunkSize,
"totalSize": totalSize,
"identifier": identifier,
"filename": filename,
"relativePath": relativePath,
"totalChunks": totalChunks
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"上传文件 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"上传文件时发生未知错误: {str(e)}"
@mcp.tool()
async def add_version(
token: str,
git_id: int,
id: int,
identifier: str,
is_public: bool,
owner: str,
name: str,
version: str,
version_desc: str,
dataset_source: str = "add",
dataset_version_vos: list = None
) -> str:
"""
新增数据集版本
参数说明:
- token: 访问令牌
- git_id: Git仓库ID
- id: 数据集ID
- identifier: 数据集标识
- is_public: 是否公开
- owner: 所有者
- name: 数据集名称
- version: 版本号 (例如: v1)
- version_desc: 版本描述
- dataset_source: 数据集来源 (默认: add)
- dataset_version_vos: 版本文件列表格式: [{"file_name":"xxx","file_size":xxx,"url":"xxx"}]
返回: 新增版本结果
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/addVersion"
payload = {
"git_id": git_id,
"id": id,
"identifier": identifier,
"is_public": is_public,
"owner": owner,
"name": name,
"version": version,
"version_desc": version_desc,
"dataset_source": dataset_source,
"dataset_version_vos": dataset_version_vos if dataset_version_vos else []
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"新增版本 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"新增版本时发生未知错误: {str(e)}"
@mcp.tool()
async def update_dataset(
token: str,
id: int,
name: str,
identifier: str,
description: str = "",
is_public: bool = False,
data_type: str = "通用数据",
data_tag: str = "",
praises_count: int = 0,
praised: bool = False,
create_by: str = "",
update_time: str = "",
owner: str = "",
dataset_source: str = "add",
relative_paths: str = "",
is_hot_stone: bool = False,
git_id: int = 0,
preview_pic: str = "",
type: int = 0
) -> str:
"""
修改数据集
参数说明:
- token: 访问令牌
- id: 数据集ID
- name: 数据集名称
- identifier: 数据集标识
- description: 描述 (可选)
- is_public: 是否公开 (默认: False)
- data_type: 数据类型 (默认: 通用数据)
- data_tag: 数据标签 (可选)
- praises_count: 点赞数 (默认: 0)
- praised: 是否已点赞 (默认: False)
- create_by: 创建者 (可选)
- update_time: 更新时间 (可选)
- owner: 所有者 (可选)
- dataset_source: 数据集来源 (默认: add)
- relative_paths: 相对路径 (可选)
- is_hot_stone: 是否热门 (默认: False)
- git_id: Git仓库ID (默认: 0)
- preview_pic: 预览图片URL (可选)
- type: 类型 (默认: 0)
返回: 修改后的数据集信息
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/updateDataset"
payload = {
"id": id,
"name": name,
"identifier": identifier,
"description": description,
"is_public": is_public,
"data_type": data_type,
"data_tag": data_tag,
"praises_count": praises_count,
"praised": praised,
"create_by": create_by,
"update_time": update_time,
"owner": owner,
"dataset_source": dataset_source,
"relative_paths": relative_paths,
"is_hot_stone": is_hot_stone,
"git_id": git_id,
"preview_pic": preview_pic,
"type": type
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.put(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"修改数据集 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"修改数据集时发生未知错误: {str(e)}"
@mcp.tool()
async def update_desc(
token: str,
git_id: int,
identifier: str,
description: str
) -> str:
"""
编辑数据集简介
参数说明:
- token: 访问令牌
- git_id: Git仓库ID
- identifier: 数据集标识
- description: 数据集简介
返回: 操作结果
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/updateDesc"
payload = {
"git_id": git_id,
"identifier": identifier,
"description": description
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.put(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"编辑简介 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"编辑简介时发生未知错误: {str(e)}"
@mcp.tool()
async def publish_dataset(
token: str,
id: int,
name: str
) -> str:
"""
发布数据集
参数说明:
- token: 访问令牌
- id: 数据集ID
- name: 数据集名称
返回: 发布后的数据集信息
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/publish"
payload = {
"id": id,
"name": name
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(api_url, json=payload, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"发布数据集 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"发布数据集时发生未知错误: {str(e)}"
@mcp.tool()
async def download_all_files(
token: str,
name: str,
git_id: int,
version: str,
identifier: str,
owner: str,
is_public: bool
) -> str:
"""
当前版本所有文件打包下载
参数说明:
- token: 访问令牌
- name: 数据集名称
- git_id: Git仓库ID
- version: 版本号
- identifier: 数据集标识
- owner: 所有者
- is_public: 是否公开
返回: 文件下载链接或文件内容
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/downloadAllFiles"
params = {
"name": name,
"git_id": git_id,
"version": version,
"identifier": identifier,
"owner": owner,
"is_public": is_public
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
content_type = response.headers.get("content-type", "")
if "application/json" in content_type:
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
else:
return f"文件下载成功,内容长度: {len(response.content)} bytes"
except httpx.HTTPStatusError as e:
return f"下载文件 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"下载文件时发生未知错误: {str(e)}"
@mcp.tool()
async def download_single_file(
token: str,
url: str
) -> str:
"""
当前版本选中文件下载
参数说明:
- token: 访问令牌
- url: 文件路径
返回: 文件下载链接或文件内容
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/downloadSingleFile"
params = {"url": url}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=120.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
content_type = response.headers.get("content-type", "")
if "application/json" in content_type:
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
else:
return f"文件下载成功,内容长度: {len(response.content)} bytes"
except httpx.HTTPStatusError as e:
return f"下载单个文件 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"下载单个文件时发生未知错误: {str(e)}"
@mcp.tool()
async def delete_version(
token: str,
git_id: int,
owner: str,
identifier: str,
relative_paths: str,
version: str
) -> str:
"""
删除当前版本
参数说明:
- token: 访问令牌
- git_id: Git仓库ID
- owner: 所有者
- identifier: 数据集标识
- relative_paths: 相对路径
- version: 版本号
返回: 操作结果
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/deleteDatasetVersion"
params = {
"git_id": git_id,
"owner": owner,
"identifier": identifier,
"relative_paths": relative_paths,
"version": version
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.delete(api_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"删除版本 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"删除版本时发生未知错误: {str(e)}"
@mcp.tool()
async def delete_dataset(
token: str,
id: int
) -> str:
"""
删除数据集
参数说明:
- token: 访问令牌
- id: 数据集ID
返回: 操作结果
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/deleteDataset/{id}"
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.delete(api_url, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"删除数据集 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"删除数据集时发生未知错误: {str(e)}"
@mcp.tool()
async def query_datasets(
token: str,
page: int = 0,
size: int = 20,
is_public: bool = None,
data_type: str = "",
is_hot_stone: bool = None
) -> str:
"""
查询数据集列表
参数说明:
- token: 访问令牌
- page: 页码 (默认: 0)
- size: 每页数量 (默认: 20)
- is_public: 是否公开 (可选)
- data_type: 数据类型 (可选)
- is_hot_stone: 是否热门 (可选)
返回: 数据集列表
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/queryDatasets"
params = {
"page": page,
"size": size
}
if is_public is not None:
params["is_public"] = is_public
if data_type:
params["data_type"] = data_type
if is_hot_stone is not None:
params["is_hot_stone"] = is_hot_stone
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"查询数据集列表 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"查询数据集列表时发生未知错误: {str(e)}"
@mcp.tool()
async def get_dataset_detail(
token: str,
git_id: int,
owner: str,
name: str,
identifier: str,
is_public: bool
) -> str:
"""
查询数据集详情
参数说明:
- token: 访问令牌
- git_id: Git仓库ID
- owner: 所有者
- name: 数据集名称
- identifier: 数据集标识
- is_public: 是否公开
返回: 数据集详细信息
"""
api_url = f"{API_BASE_URL}/api/mmp/newdataset/getDatasetDetail"
params = {
"git_id": git_id,
"owner": owner,
"name": name,
"identifier": identifier,
"is_public": is_public
}
headers = build_auth_headers(token)
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(api_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
return json.dumps(result, indent=2, ensure_ascii=False)
except httpx.HTTPStatusError as e:
return f"查询数据集详情 API 返回错误 ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"无法连接数据集服务: {str(e)}"
except Exception as e:
return f"查询数据集详情时发生未知错误: {str(e)}"
if __name__ == "__main__":
mcp.run(transport=TRANSPORT_MODE)

1260
数据集.txt Normal file

File diff suppressed because it is too large Load Diff