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

相关推荐
CS创新实验室20 小时前
练习项目:基于 LangGraph 和 MCP 服务器的本地语音助手
运维·服务器·ai·aigc·tts·mcp
赵康1 天前
使用 LLM + Atlassian MCP 1小时生成年终总结
ai·llm·mcp
Miku162 天前
LangGraph+BrightData+PaperSearch的研究助理
爬虫·langchain·mcp
却尘4 天前
🚀 MCP基础完全上手指南:让Claude像开挂一样调用外部工具
aigc·ai编程·mcp
CodeLiving4 天前
MCP学习三——MCP相关概念
人工智能·mcp
_大学牲5 天前
从年初DeepSeek的爆火🔥 - 到年底Trae solo 的上线⚙️ :2025 AI 大年,一个普通人的认知指南 ! 🧭
deepseek·mcp·trae
only-code6 天前
MCP驱动的Rgentic RRG(向量数据库+网络搜索)
数据库·python·大模型·函数调用·mcp
only-code6 天前
100% 本地 MCP 客户端 + SQLite 服务器(LlamaIndex + Ollama + Qwen2.5)
python·mcp
伶俜monster7 天前
大模型 “万能接口” MCP 横空出世!打破数据孤岛,重塑 AI 交互新规则
前端·mcp