メインコンテンツまでスキップ
メインコンテンツまでスキップ

ClickHouse MCP サーバーを使用して PydanticAI エージェントを構築する方法

このガイドでは、ClickHouseの SQL プレイグラウンドClickHouseの MCP サーバー を使用してインタラクションできる PydanticAI エージェントを構築する方法を学びます。

例のノートブック

この例は、examples repository のノートブックに含まれています。

前提条件

  • システムに Python がインストールされている必要があります。
  • システムに pip がインストールされている必要があります。
  • Anthropic API キー、または別の LLM プロバイダーからの API キーが必要です。

以下の手順は、Python REPL またはスクリプト経由で実行できます。

ライブラリをインストールする

以下のコマンドを実行して、必要なライブラリをインストールします。

!pip install -q --upgrade pip
!pip install -q "pydantic-ai-slim[mcp]"
!pip install -q "pydantic-ai-slim[anthropic]" # replace with the appropriate package if using a different LLM provider

認証情報を設定する

次に、あなたの Anthropic API キーを提供する必要があります:

import os, getpass
os.environ["ANTHROPIC_API_KEY"] = getpass.getpass("Enter Anthropic API Key:")
Enter Anthropic API Key: ········
別の LLM プロバイダーを使用する

Anthropic API キーをお持ちでない場合や、別の LLM プロバイダーを使用したい場合は、 PydanticAI ドキュメント で認証情報の設定に関する手順を見つけることができます。

次に、ClickHouse SQL プレイグラウンドに接続するために必要な認証情報を定義します:

env = {
    "CLICKHOUSE_HOST": "sql-clickhouse.clickhouse.com",
    "CLICKHOUSE_PORT": "8443",
    "CLICKHOUSE_USER": "demo",
    "CLICKHOUSE_PASSWORD": "",
    "CLICKHOUSE_SECURE": "true"
}

MCP サーバーと PydanticAI エージェントを初期化する

次に、ClickHouse MCP サーバーを ClickHouse SQL プレイグラウンドに指すように構成します:

from pydantic_ai import Agent
from pydantic_ai.mcp import MCPServerStdio
from pydantic_ai.messages import ToolCallPart, ToolReturnPart

server = MCPServerStdio(
    'uv',
    args=[
        'run',
        '--with', 'mcp-clickhouse',
        '--python', '3.13',
        'mcp-clickhouse'
    ],
    env=env
)
agent = Agent('anthropic:claude-sonnet-4-0', mcp_servers=[server])

エージェントに質問する

最後に、エージェントに質問できます:

async with agent.run_mcp_servers():
    result = await agent.run("Who's done the most PRs for ClickHouse?")
    print(result.output)

以下のような応答が返されます:

Based on the data from the ClickHouse GitHub repository, here are the top contributors by number of pull requests created:

**Top contributors to ClickHouse by PRs opened:**

1. **alexey-milovidov** - 3,370 PRs opened
2. **azat** - 1,905 PRs opened  
3. **rschu1ze** - 979 PRs opened
4. **alesapin** - 947 PRs opened
5. **tavplubix** - 896 PRs opened
6. **kssenii** - 871 PRs opened
7. **Avogar** - 805 PRs opened
8. **KochetovNicolai** - 700 PRs opened
9. **Algunenano** - 658 PRs opened
10. **kitaisreal** - 630 PRs opened

**Alexey Milovidov** stands out as by far the most active contributor with over 3,370 pull requests opened, which is significantly more than any other contributor. This makes sense as Alexey Milovidov is one of the founders and lead developers of ClickHouse.

The data also shows that alexey-milovidov has been very active in managing PRs, with 12,818 "closed" events (likely reviewing and closing PRs from other contributors) in addition to creating his own PRs.

It's worth noting that I filtered out various robot/bot accounts that handle automated processes, focusing on human contributors to give you the most meaningful answer about who has contributed the most PRs to ClickHouse.