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

相关推荐
中间件XL8 小时前
ai-agent框架spring ai alibaba (三)外部调用II-1 MCP
ai agent·spring ai·mcp·ai agent框架
丁劲犇15 小时前
使用TraeAI开发Web页面测试MSYS2 ucrt64 Qt MCP服务器
服务器·前端·c++·qt·mcp
nix.gnehc1 天前
从范式到工程:Plan & Execute + Nacos MCP 构建 AI Agent 的实践之路
人工智能·agent·mcp
一条泥憨鱼2 天前
能够让AI做事的“Skill“有什么奥秘
人工智能·ai·agent·rag·skill·mcp
深念Y2 天前
Claude Code 搜索工具失灵,用 MCP + 提示词注入绕过 tavily
网络·搜索引擎·mcp·claudecode·中转站·tavily·搜索服务器
ZengLiangYi3 天前
MCP Server 集成:让 AI Agent 自动调用知识库
ai编程·mcp
ZengLiangYi3 天前
MCP + Claude Code:新对话自动回忆历史经验
ai编程·mcp
winlife_4 天前
把 Godot 编辑器接入 AI:Funplay MCP for Godot 介绍
人工智能·编辑器·godot·ai编程·游戏开发·mcp
倾颜4 天前
做 AI 应用时,Agent、RAG、Tool、Skill、MCP 这些概念怎么分工?
agent·ai编程·mcp
掉鱼的猫4 天前
用 Solon AI 从零构建 MCP 工具服务:让 AI Agent 拥有真实世界的能力
java·llm·mcp