Files
FastapiAdmin/backend/app/api/v1/module_application/ai/service.py
T
zhangtao c2ca6d19ac refactor: 优化代码注释和文档字符串格式
style: 统一代码风格和格式

docs: 完善函数和方法的文档字符串

refactor(base_model): 移除冗余的表名和表参数生成方法

refactor(constant): 更新返回码注释格式

refactor(router_class): 添加路由处理器的详细文档

refactor(database): 完善数据库连接函数的文档

refactor(security): 添加认证类和方法的详细文档

refactor(validator): 更新验证器函数的文档格式

refactor(serialize): 优化序列化工具类的文档

refactor(response): 完善响应类的文档字符串

refactor(dependencies): 添加依赖函数的详细文档

refactor(initialize): 完善初始化脚本的文档

refactor(plugin): 添加生命周期和中间件注册的文档

refactor(service): 完善服务层方法的文档

refactor(controller): 添加控制器方法的详细文档

refactor(crud): 完善CRUD操作的文档字符串

refactor(schema): 简化模型类并移除冗余字段

refactor(param): 更新查询参数类的注释格式

refactor(template): 优化代码生成模板的格式

refactor(console): 添加控制台输出功能的实现

refactor(util): 完善工具函数的文档字符串
2025-10-18 16:31:28 +08:00

131 lines
4.5 KiB
Python

# -*- coding: utf-8 -*-
from typing import List, Dict, Optional, Any
from app.core.exceptions import CustomException
from app.api.v1.module_system.auth.schema import AuthSchema
from app.utils.ai_util import AIClient
from .schema import McpCreateSchema, McpUpdateSchema, McpOutSchema, ChatQuerySchema
from .param import McpQueryParam
from .crud import McpCRUD
class McpService:
"""MCP服务层"""
@classmethod
async def detail_service(cls, auth: AuthSchema, id: int) -> Dict[str, Any]:
"""
获取MCP服务器详情
参数:
- auth (AuthSchema): 认证信息模型
- id (int): MCP服务器ID
返回:
- Dict[str, Any]: MCP服务器详情字典
"""
obj = await McpCRUD(auth).get_by_id_crud(id=id)
if not obj:
raise CustomException(msg='MCP 服务器不存在')
return McpOutSchema.model_validate(obj).model_dump()
@classmethod
async def list_service(cls, auth: AuthSchema, search: Optional[McpQueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> List[Dict[str, Any]]:
"""
列表查询MCP服务器
参数:
- auth (AuthSchema): 认证信息模型
- search (Optional[McpQueryParam]): 查询参数模型
- order_by (Optional[List[Dict[str, str]]]): 排序参数列表
返回:
- List[Dict[str, Any]]: MCP服务器详情字典列表
"""
if order_by:
order_by = eval(str(order_by))
obj_list = await McpCRUD(auth).get_list_crud(search=search.__dict__ if search else {}, order_by=order_by)
return [McpOutSchema.model_validate(obj).model_dump() for obj in obj_list]
@classmethod
async def create_service(cls, auth: AuthSchema, data: McpCreateSchema) -> Dict[str, Any]:
"""
创建MCP服务器
参数:
- auth (AuthSchema): 认证信息模型
- data (McpCreateSchema): 创建MCP服务器模型
返回:
- Dict[str, Any]: 创建的MCP服务器详情字典
"""
obj = await McpCRUD(auth).get_by_name_crud(name=data.name)
if obj:
raise CustomException(msg='创建失败,MCP 服务器已存在')
obj = await McpCRUD(auth).create_crud(data=data)
return McpOutSchema.model_validate(obj).model_dump()
@classmethod
async def update_service(cls, auth: AuthSchema, id: int, data: McpUpdateSchema) -> Dict[str, Any]:
"""
更新MCP服务器
参数:
- auth (AuthSchema): 认证信息模型
- id (int): MCP服务器ID
- data (McpUpdateSchema): 更新MCP服务器模型
返回:
- Dict[str, Any]: 更新的MCP服务器详情字典
"""
obj = await McpCRUD(auth).get_by_id_crud(id=id)
if not obj:
raise CustomException(msg='更新失败,该数据不存在')
exist_obj = await McpCRUD(auth).get_by_name_crud(name=data.name)
if exist_obj and exist_obj.id != id:
raise CustomException(msg='更新失败,MCP 服务器名称重复')
obj = await McpCRUD(auth).update_crud(id=id, data=data)
return McpOutSchema.model_validate(obj).model_dump()
@classmethod
async def delete_service(cls, auth: AuthSchema, ids: List[int]) -> None:
"""
批量删除MCP服务器
参数:
- auth (AuthSchema): 认证信息模型
- ids (List[int]): MCP服务器ID列表
返回:
- None
"""
if len(ids) < 1:
raise CustomException(msg='删除失败,删除对象不能为空')
for id in ids:
obj = await McpCRUD(auth).get_by_id_crud(id=id)
if not obj:
raise CustomException(msg='删除失败,该数据不存在')
await McpCRUD(auth).delete_crud(ids=ids)
@classmethod
async def chat_query(cls, query: ChatQuerySchema):
"""
处理聊天查询
参数:
- query (ChatQuerySchema): 聊天查询模型
返回:
- AsyncGenerator[str, None]: 异步生成器,每次返回一个聊天响应
"""
# 创建MCP客户端实例
mcp_client = AIClient()
try:
# 处理消息
async for response in mcp_client.process(query.message):
yield response
finally:
# 确保关闭客户端连接
await mcp_client.close()