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

相关推荐
八苦1 天前
如何用c# 做 mcp/ChatGPT app
c#·mcp
光于前裕于后1 天前
配置钉钉龙虾OpenClaw机器人调用OpenMetadata
机器人·钉钉·数据治理·mcp·openclaw
星野云联AIoT技术洞察1 天前
2026 年 MCP + MQTT:AI Agent 真正控制 IoT 设备的落地路径
数字孪生·ack·ai agent·物联网平台·agentic·mcp·命令服务
API开发2 天前
一个MCP操作所有的数据库
数据库·api·api接口·apisql·mcp·mcpserver·openclaw
ZTrainWilliams2 天前
swagger-mcp-toolkit 让 AI编辑器 更快“读懂并调用”你的接口
前端·后端·mcp
EichKite2 天前
链接智能与工具:深度解析 MCP 接入 LLM 的两大主流实现架构
openai·mcp
sanshanjianke2 天前
claudecode/opencode 数学插件工具的调用
数学建模·mcp·claudecode·vibe
码路飞3 天前
MCP 工具只能返回文字?现在能直接弹出交互式 UI 了,手把手写一个
mcp
丁劲犇3 天前
在Trae Solo模式下用Qt HttpServer和Concurrent升级MCP服务器绘制6G互联网覆盖区域
服务器·开发语言·qt·ai·6g·mcp·trae
安逸sgr3 天前
MCP 协议深度解析(八):Prompts 提示模板与 Sampling 采样机制!
人工智能·分布式·学习·语言模型·协议·mcp