MCP实现Agentic RAG server案例

使用dify的知识库 api,fastapi创建mcp server


代码

复制代码
@mcp.tool()
async def rag_retrieve(query: str) -> dict:
    """
    Name:
        ***产品业务服务等相关客服问答知识检索服务

    Description:
        提供****业务服务等相关客服问答知识检索服务,根据输入query返回结果。
    
    Args:
        query: 搜索的内容,参数必传,例如 'query=客服电话'
    """
    url = "http://117.-dbb9-4cb9-a172-482c6571aab8/retrieve"
    token = "dataset-VVqUSZNMzMrVLiB4wQbvlYCW"  # 你的token

    headers = {
        "Authorization": f"Bearer {token}",
        # 使用 json= 参数时会自动设置 Content-Type,此处保留以匹配原始请求
        "Content-Type": "application/json"
    }

    payload = {
        "query": query,
        "retrieval_model": {
            "search_method": "semantic_search",
            "reranking_enable": False,  # false → False
            "reranking_mode": None,  # null → None
            "reranking_model": {
                "reranking_provider_name": "",
                "reranking_model_name": ""
            },
            "weights": None,  # null → None
            "top_k": 2,
            "score_threshold_enabled": False,  # false → False
            "score_threshold": None,  # null → None
            
        }
    }
    logging.info(f"rag_retrieve query:{query}")
    # ✅ 使用全局连接池
    client = await get_http_client()
    
    try:
        response = await client.post(url, headers=headers, json=payload)
        response.raise_for_status()
        result = response.json()
        logging.info(f"🔍 rag_retrieve 完成")
        return result
    except Exception as e:
        logging.error(f"❌ rag_retrieve 失败: {e}")
        raise ValueError(f"调用API失败: {e}")

cherry studio客服端访问

相关推荐
qdprobot1 天前
AIcam智能ESP32视觉摄像头体识万物
人工智能·esp32s3·图形化编程·mcp·mixly小智ai
TickDB1 天前
统一行情 API 查 A 股、港股、美股和数字货币:code=0 不代表 symbol 一个没少
人工智能·python·websocket·mcp·行情数据 api
星野云联AIoT技术洞察1 天前
AG-UI 在 IoT 控制台里怎么落地:设备状态、命令确认与人机协同
ai agent·技术方案·mcp·ag-ui·平台架构·命令确认·iot设备控制
砍光二叉树2 天前
一文打通 AI 认知:LLM、Agent、MCP、Skill 完整体系
人工智能·llm·agent·skill·mcp
MageGojo2 天前
R-Shell开源项目实战解析:用Rust打造命令行SSH工具,支持连接管理、远程执行、SFTP与MCP
运维·rust·开源项目·命令行工具·ssh客户端·mcp
deepdata_cn2 天前
AI大模型通用上下文标准(MCP)
人工智能·mcp
宋哥转AI3 天前
@Tool写了但tools/list为空?Spring AI MCP Server注册的两种路径和四个坑
java·agent·mcp
console.log('npc')3 天前
将 Figma 接入 Codex MCP:从 `/plugins` 到本地插件配置的完整教程
前端·人工智能·python·figma·code·codex·mcp
Filwaod3 天前
MCP 接入模式对比:Agent - Gateway - 业务项目 vs Agent - Adapter - 业务项目
java·agent·mcp
用户2018792831673 天前
CodeGraph 如何节省 Token:一个让 AI 不再“翻遍所有抽屉”的智能帮手
mcp