> ## 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.

# 如何使用 ClickHouse MCP 服务器构建 OpenAI 智能体。

> 了解如何构建可与 ClickHouse MCP 服务器交互的 OpenAI 智能体。

在本指南中，你将学习如何构建一个 [OpenAI](https://github.com/openai/openai-agents-python) 智能体，使其能够通过 [ClickHouse 的 MCP 服务器](https://github.com/ClickHouse/mcp-clickhouse) 与 [ClickHouse 的 SQL playground](https://sql.clickhouse.com/) 交互。

<Info>
  **示例 Notebook**

  你可以在 [examples repository](https://github.com/ClickHouse/examples/blob/main/ai/mcp/openai-agents/openai-agents.ipynb) 中找到此示例 Notebook。
</Info>

<div id="prerequisites">
  ## 前置条件
</div>

* 你的系统中需要已安装 Python。
* 你的系统中需要已安装 `pip`。
* 你需要一个 OpenAI API key

你可以在 Python REPL 中或通过脚本运行以下步骤。

<Steps>
  <Step title="安装库" id="install-libraries">
    运行以下命令安装所需库：

    ```python theme={null}
    pip install -q --upgrade pip
    pip install -q openai-agents
    ```
  </Step>

  <Step title="配置凭据" id="setup-credentials">
    接下来，您需要提供您的 OpenAI API 密钥：

    ```python theme={null}
    import os, getpass
    os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter OpenAI API Key:")
    ```

    ```response title="Response" theme={null}
    Enter OpenAI API Key: ········
    ```
  </Step>

  <Step title="初始化 MCP Server 与 OpenAI AI 智能体" id="initialize-mcp-and-agent">
    现在，将 ClickHouse MCP 服务器配置为指向 ClickHouse SQL playground，
    初始化您的 OpenAI agent 并向其提问：

    ```python theme={null}
    from agents.mcp import MCPServer, MCPServerStdio
    from agents import Agent, Runner, trace
    import json

    def simple_render_chunk(chunk):
        """Simple version that just filters important events"""

        # Tool calls
        if (hasattr(chunk, 'type') and
                chunk.type == 'run_item_stream_event'):

            if chunk.name == 'tool_called':
                tool_name = chunk.item.raw_item.name
                args = chunk.item.raw_item.arguments
                print(f"🔧 Tool: {tool_name}({args})")

            elif chunk.name == 'tool_output':
                try:
                    # Handle both string and already-parsed output
                    if isinstance(chunk.item.output, str):
                        output = json.loads(chunk.item.output)
                    else:
                        output = chunk.item.output

                    # Handle both dict and list formats
                    if isinstance(output, dict):
                        if output.get('type') == 'text':
                            text = output['text']
                            if 'Error' in text:
                                print(f"❌ Error: {text}")
                            else:
                                print(f"✅ Result: {text[:100]}...")
                    elif isinstance(output, list) and len(output) > 0:
                        # Handle list format
                        first_item = output[0]
                        if isinstance(first_item, dict) and first_item.get('type') == 'text':
                            text = first_item['text']
                            if 'Error' in text:
                                print(f"❌ Error: {text}")
                            else:
                                print(f"✅ Result: {text[:100]}...")
                    else:
                        # Fallback - just print the raw output
                        print(f"✅ Result: {str(output)[:100]}...")

                except (json.JSONDecodeError, AttributeError, KeyError) as e:
                    # Fallback to raw output if parsing fails
                    print(f"✅ Result: {str(chunk.item.output)[:100]}...")

            elif chunk.name == 'message_output_created':
                try:
                    content = chunk.item.raw_item.content
                    if content and len(content) > 0:
                        print(f"💬 Response: {content[0].text}")
                except (AttributeError, IndexError):
                    print(f"💬 Response: {str(chunk.item)[:100]}...")

        # Text deltas for streaming
        elif (hasattr(chunk, 'type') and
              chunk.type == 'raw_response_event' and
              hasattr(chunk, 'data') and
              hasattr(chunk.data, 'type') and
              chunk.data.type == 'response.output_text.delta'):
            print(chunk.data.delta, end='', flush=True)

    async with MCPServerStdio(
            name="ClickHouse SQL Playground",
            params={
                "command": "uv",
                "args": [
                    'run',
                    '--with', 'mcp-clickhouse',
                    '--python', '3.13',
                    'mcp-clickhouse'
                ],
                "env": env
            }, client_session_timeout_seconds = 60
    ) as server:
        agent = Agent(
            name="Assistant",
            instructions="Use the tools to query ClickHouse and answer questions based on those files.",
            mcp_servers=[server],
        )

        message = "What's the biggest GitHub project so far in 2025?"
        print(f"\n\nRunning: {message}")
        with trace("Biggest project workflow"):
            result = Runner.run_streamed(starting_agent=agent, input=message, max_turns=20)
            async for chunk in result.stream_events():
                simple_render_chunk(chunk)
    ```

    ```response title="Response" theme={null}
    Running: What's the biggest GitHub project so far in 2025?
    🔧 Tool: list_databases({})
    ✅ Result: amazon
    bluesky
    country
    covid
    default
    dns
    environmental
    food
    forex
    geo
    git
    github
    hackernews
    imdb
    log...
    🔧 Tool: list_tables({"database":"github"})
    ✅ Result: {
      "database": "github",
      "name": "actors_per_repo",
      "comment": "",
      "columns": [
        {
          "...
    🔧 Tool: run_select_query({"query":"SELECT repo_name, MAX(stars) FROM github.top_repos_mv"})
    ✅ Result: {
      "status": "error",
      "message": "Query failed: HTTPDriver for https://sql-clickhouse.clickhouse....
    🔧 Tool: run_select_query({"query":"SELECT repo_name, stars FROM github.top_repos ORDER BY stars DESC LIMIT 1"})
    ✅ Result: {
      "repo_name": "sindresorhus/awesome",
      "stars": 402893
    }...
    The biggest GitHub project in 2025, based on stars, is "[sindresorhus/awesome](https://github.com/sindresorhus/awesome)" with 402,893 stars.💬 Response: The biggest GitHub project in 2025, based on stars, is "[sindresorhus/awesome](https://github.com/sindresorhus/awesome)" with 402,893 stars.
    ```
  </Step>
</Steps>
