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客服端访问

相关推荐
EatFan10 小时前
AI Agent 上生产前先加三道闸门:审批、限权、可回放的工程实践
人工智能·python·算法·多智能体·ai agent·mcp·harness
無a伟16 小时前
彻底搞懂ToolCalling、MCP,Skills的核心区别,能力上的层层封装
大数据·agent·mcp·toolcalling·skills
cpolar技术支持18 小时前
服务器上的 Nginx 只想改配置却不敢动?把 Nginx-UI 跑起来,用 MCP 让 AI 助手帮你安全改配置,再用 cpolar 把面板短时开给自己
nginx·ai·cpolar·mcp·nginx-ui
code_Bo1 天前
多 Agent 协作最大的坑不是模型,是失忆
ai编程·cursor·mcp
agent开发分析1 天前
MCP vs Skill vs Plugin:AI工具生态三种形态,一篇讲透怎么选
agent·mcp
杨杨杨大侠1 天前
MCP 到底接在了哪一层?从“Agent 调工具”说起
agent·ai编程·mcp
sarasuki1 天前
为什么 Agent 工具需要一个「协议」而不是「框架」:MCP 成为标准的底层逻辑
设计模式·agent·mcp
蛋先生DX1 天前
AI Agent 真正干活的 Harness 到底是啥,原来就这 7 个子系统?
agent·claude·mcp
啦啦啦!7 天前
RAG初阶学习
学习·ai·知识库·rag