> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 在 LibreChat 中使用 ClickHouse MCP 服务器

> 本指南介绍如何使用 Docker 为 LibreChat 配置 ClickHouse MCP 服务器。

export const Image = ({img, alt, size = "lg"}) => {
  const normalizedSize = ["sm", "md", "lg"].includes(size) ? size : "lg";
  return <div className={`ch-image-${normalizedSize}`}>
      <Frame>
        <img src={img} alt={alt} />
      </Frame>
    </div>;
};

> 本指南介绍如何使用 Docker 为 LibreChat 配置 ClickHouse MCP 服务器，
> 并将其连接到 ClickHouse 示例数据集。

<Steps>
  <Step title="安装 Docker" id="install-docker">
    你需要使用 Docker 来运行 LibreChat 和 MCP Server。获取 Docker 的步骤如下：

    1. 访问 [docker.com](https://www.docker.com/products/docker-desktop)
    2. 下载适用于你所用操作系统的 Docker Desktop
    3. 按照对应操作系统的说明安装 Docker
    4. 打开 Docker Desktop，并确认其已启动运行

    <br />

    更多信息请参阅 [Docker 文档](https://docs.docker.com/get-docker/)。
  </Step>

  <Step title="克隆 LibreChat 代码仓库" id="clone-librechat-repo">
    打开终端 (命令提示符、终端或 PowerShell) ，然后使用以下命令克隆
    LibreChat 代码仓库：

    ```bash theme={null}
    git clone https://github.com/danny-avila/LibreChat.git
    cd LibreChat
    ```
  </Step>

  <Step title="创建并编辑 .env 文件" id="create-and-edit-env-file">
    将示例配置文件 `.env.example` 复制为 `.env`：

    ```bash theme={null}
    cp .env.example .env
    ```

    用你喜欢的文本编辑器打开 `.env` 文件。你会看到其中包含许多常用 LLM 提供商的配置部分，例如 OpenAI、Anthropic、AWS Bedrock 等，比如：

    ```text title=".venv" highlight={4} theme={null}
    #============#
    # Anthropic  #
    #============#
    ANTHROPIC_API_KEY=user_provided
    # ANTHROPIC_MODELS=claude-opus-4-20250514,claude-sonnet-4-20250514,claude-3-7-sonnet-20250219,claude-3-5-sonnet-20241022,claude-3-5-haiku-20241022,claude-3-opus-20240229,claude-3-sonnet-20240229,claude-3-haiku-20240307
    # ANTHROPIC_REVERSE_PROXY=
    ```

    将 `user_provided` 替换为你要使用的 LLM 提供商的 API 密钥。

    <Info>
      **使用本地 LLM**

      如果你没有 API 密钥，也可以使用 Ollama 之类的本地 LLM。具体做法会在后面的[“安装 Ollama”](#add-local-llm-using-ollama)步骤中介绍。现在先不要修改 .env 文件，继续执行后续步骤。
    </Info>
  </Step>

  <Step title="创建 librechat.yaml 文件" id="create-librechat-yaml-file">
    运行以下命令，创建新的 `librechat.yaml` 文件：

    ```bash theme={null}
    cp librechat.example.yaml librechat.yaml
    ```

    这将为 LibreChat 创建主[配置文件](https://www.librechat.ai/docs/configuration/librechat_yaml)。
  </Step>

  <Step title="将 ClickHouse MCP 服务器添加到 Docker Compose" id="add-clickhouse-mcp-server-to-docker-compose">
    接下来，我们将 ClickHouse MCP 服务器添加到 LibreChat 的 Docker Compose 文件中，
    以便 LLM 能够与
    [ClickHouse SQL playground](https://sql.clickhouse.com/)
    进行交互。

    创建一个名为 `docker-compose.override.yml` 的文件，并在其中添加以下配置：

    ```yml title="docker-compose.override.yml" theme={null}
    services:
      api:
        volumes:
          - ./librechat.yaml:/app/librechat.yaml
      mcp-clickhouse:
        image: mcp/clickhouse
        container_name: mcp-clickhouse
        ports:
          - 8001:8000
        extra_hosts:
          - "host.docker.internal:host-gateway"
        environment:
          - CLICKHOUSE_HOST=sql-clickhouse.clickhouse.com
          - CLICKHOUSE_USER=demo
          - CLICKHOUSE_PASSWORD=
          - CLICKHOUSE_MCP_SERVER_TRANSPORT=sse
          - CLICKHOUSE_MCP_BIND_HOST=0.0.0.0
    ```

    如果你想探索自己的数据，可以使用你自己的 ClickHouse Cloud 服务的 [host、username 和 password](/docs/zh/get-started/setup/cloud#connect-with-your-app)。

    <Card title="开始使用 ClickHouse Cloud" href="https://cloud.clickhouse.com/" icon="cloud">
      如果你还没有 Cloud 账户，立即开始使用 ClickHouse Cloud，即可获得 300 美元额度。30 天免费试用结束后，你可以继续选择按使用量付费方案，或联系我们，进一步了解基于用量的折扣。详情请访问我们的定价页面。
    </Card>
  </Step>

  <Step title="在 librechat.yaml 中配置 MCP 服务器" id="configure-mcp-server-in-librechat-yaml">
    打开 `librechat.yaml`，并将以下配置添加到文件末尾：

    ```yml theme={null}
    mcpServers:
      clickhouse-playground:
        type: sse
        url: http://host.docker.internal:8001/sse
    ```

    这会将 LibreChat 配置为连接到在 Docker 中运行的 MCP Server。

    找到以下这一行：

    ```text title="librechat.yaml" theme={null}
    socialLogins: ['github', 'google', 'discord', 'openid', 'facebook', 'apple', 'saml']
    ```

    为简化操作，我们暂时先不要求身份验证：

    ```text title="librechat.yaml" theme={null}
    socialLogins: []
    ```
  </Step>

  <Step title="使用 Ollama 添加本地 LLM (可选)" id="add-local-llm-using-ollama">
    ### 安装 Ollama

    前往 [Ollama 官网](https://ollama.com/download)，安装适用于你系统的 Ollama。

    安装完成后，你可以像这样运行一个模型：

    ```bash theme={null}
    ollama run qwen3:32b
    ```

    如果本地尚无该模型，这会将其拉取到你的本地机器。

    模型列表请参见 [Ollama library](https://ollama.com/library)

    ### 在 librechat.yaml 中配置 Ollama

    模型下载完成后，在 `librechat.yaml` 中进行配置：

    ```text title="librechat.yaml" theme={null}
    custom:
      - name: "Ollama"
        apiKey: "ollama"
        baseURL: "http://host.docker.internal:11434/v1/"
        models:
          default:
            [
              "qwen3:32b"
            ]
          fetch: false
        titleConvo: true
        titleModel: "current_model"
        summarize: false
        summaryModel: "current_model"
        forcePrompt: false
        modelDisplayLabel: "Ollama"
    ```
  </Step>

  <Step title="启动所有服务" id="start-all-services">
    在 LibreChat 项目文件夹的根目录下，运行以下命令以启动所有服务：

    ```bash theme={null}
    docker compose up
    ```

    等待所有服务全部启动并正常运行。
  </Step>

  <Step title="在浏览器中打开 LibreChat" id="open-librechat-in-browser">
    当所有服务都启动并正常运行后，打开浏览器并访问 `http://localhost:3080/`

    如果你还没有免费的 LibreChat 账户，请先创建一个并登录。现在，你应该
    可以看到已连接到 ClickHouse MCP 服务器，以及可选连接到
    本地 LLM 的 LibreChat 界面。

    在聊天界面中，选择 `clickhouse-playground` 作为你的 MCP 服务器：

    <Image img="https://mintcdn.com/private-7c7dfe99/F7iOqwDUBB9E2S65/images/use-cases/AI_ML/MCP/librechat.webp?fit=max&auto=format&n=F7iOqwDUBB9E2S65&q=85&s=7ee35425c42685cb0f7415cf65cd35ad" alt="选择你的 MCP 服务器" size="md" width="1630" height="918" data-path="images/use-cases/AI_ML/MCP/librechat.webp" />

    现在，你可以让 LLM 探索 ClickHouse 示例数据集。试试看：

    ```text title="Prompt" theme={null}
    What datasets do you have access to?
    ```
  </Step>
</Steps>

<Note>
  如果 LibreChat UI 中没有显示 MCP 服务器 选项，
  请检查你的 `librechat.yaml` 文件中是否已设置正确的权限。
</Note>

如果 `interface` 下 `mcpServers` 部分中的 `use` 设置为 `false`，聊天中就不会显示 MCP 选择下拉菜单：

```yml title="librechat.yaml" theme={null}
interface:
  mcpServers:
    use: true
    share: false
    create: false
    public: false
```
