LlamaIndex AI 에이전트를 ClickHouse MCP 서버로 빌드하기

LlamaIndex AI 에이전트를 ClickHouse MCP 서버로 빌드하기

이 가이드에서는 ClickHouse의 MCP 서버를 사용해 ClickHouse의 SQL 플레이그라운드와 상호작용할 수 있는 LlamaIndex AI 에이전트를 빌드하는 방법을 배워요.

출처: 문서

본문

이 가이드에서는 ClickHouse의 MCP 서버를 사용해 ClickHouse의 SQL 플레이그라운드와 상호작용할 수 있는 LlamaIndex AI 에이전트를 빌드하는 방법을 배워요.

예제 노트북 이 예제는 examples 리포지토리에서 노트북으로 찾아볼 수 있어요.

전제 조건 (Prerequisites)

  • 시스템에 Python이 설치되어 있어야 해요.
  • 시스템에 pip이 설치되어 있어야 해요.
  • Anthropic API 키 또는 다른 LLM 제공자의 API 키가 있어야 해요.

다음 단계는 Python REPL 또는 스크립트를 통해 실행할 수 있어요.

1. 라이브러리 설치하기

다음 명령을 실행해 필요한 라이브러리를 설치하세요:

pip install -q --upgrade pip
pip install -q llama-index clickhouse-connect llama-index-llms-anthropic llama-index-tools-mcp

2. 자격 증명 설정하기

다음으로 Anthropic API 키를 제공해야 해요:

import os, getpass
os.environ["ANTHROPIC_API_KEY"] = getpass.getpass("Enter Anthropic API Key:")

응답:

Enter Anthropic API Key: ········

다른 LLM 제공자 사용하기 Anthropic API 키가 없고 다른 LLM 제공자를 사용하려면 LlamaIndex "LLMs" 문서에서 자격 증명 설정 지침을 찾을 수 있어요.

3. MCP 서버 초기화하기

이제 ClickHouse MCP 서버가 ClickHouse SQL 플레이그라운드를 가리키도록 구성해요. 이 파이썬 함수들을 Llama Index 도구로 변환해야 해요:

from llama_index.tools.mcp import BasicMCPClient, McpToolSpec

mcp_client = BasicMCPClient(
    "uv",
    args=[
        "run",
        "--with", "mcp-clickhouse",
        "--python", "3.13",
        "mcp-clickhouse"
    ],
    env={
        "CLICKHOUSE_HOST": "sql-clickhouse.clickhouse.com",
        "CLICKHOUSE_PORT": "8443",
        "CLICKHOUSE_USER": "demo",
        "CLICKHOUSE_PASSWORD": "",
        "CLICKHOUSE_SECURE": "true"
    }
)

mcp_tool_spec = McpToolSpec(
    client=mcp_client,
)

tools = await mcp_tool_spec.to_tool_list_async()

4. 에이전트 만들기

이제 해당 도구들에 접근할 수 있는 에이전트를 만들 준비가 됐어요. 한 번 실행에서 최대 도구 호출 수를 10으로 설정해요. 원하면 이 파라미터를 수정할 수 있어요:

from llama_index.core.agent import AgentRunner, FunctionCallingAgentWorker

agent_worker = FunctionCallingAgentWorker.from_tools(
    tools=tools,
    llm=llm, verbose=True, max_function_calls=10
)
agent = AgentRunner(agent_worker)

5. LLM 초기화하기

다음 코드로 Claude Sonnet 4.0 모델을 초기화해요:

from llama_index.llms.anthropic import Anthropic
llm = Anthropic(model="claude-sonnet-4-0")

6. 에이전트 실행하기

마지막으로 에이전트에게 질문할 수 있어요:

response = agent.query("What's the most popular repository?")

응답이 길어서 아래 예제 응답에서는 잘라냈어요:

응답:

Added user message to memory: What's the most popular repository?
=== LLM Response ===
I'll help you find the most popular repository. Let me first explore the available databases and tables to understand the data structure.
=== Calling Function ===
Calling function: list_databases with args: {}
=== Function Output ===
meta=None content=[TextContent(type='text', text='amazon\nbluesky\ncountry\ncovid\ndefault\ndns\nenvironmental\nfood\nforex\ngeo\ngit\ngithub\nhackernews\nimdb\nlogs\nmetrica\nmgbench\nmta\nnoaa\nnyc_taxi\nnypd\nontime\nopensky\notel\notel_v2\npypi\nrandom\nreddit\nrubygems\nstackoverflow\nstar_schema\nstock\nsystem\ntw_weather\ntwitter\nuk\nwiki\nwords\nyoutube', annotations=None)] isError=False
=== LLM Response ===
I can see there's a `github` database which likely contains repository data. Let me explore the tables in that database.
=== Calling Function ===
Calling function: list_tables with args: {"database": "github"}
=== Function Output ===
...
...
...
=== LLM Response ===
Based on the GitHub data, **the most popular repository is `sindresorhus/awesome`** with **402,292 stars**.

Here are the top 10 most popular repositories by star count:

1. **sindresorhus/awesome** - 402,292 stars
2. **996icu/996.ICU** - 388,413 stars  
3. **kamranahmedse/developer-roadmap** - 349,097 stars
4. **donnemartin/system-design-primer** - 316,524 stars
5. **jwasham/coding-interview-university** - 313,767 stars
6. **public-apis/public-apis** - 307,227 stars
7. **EbookFoundation/free-programming-books** - 298,890 stars
8. **facebook/react** - 286,034 stars
9. **vinta/awesome-python** - 269,320 stars
10. **freeCodeCamp/freeCodeCamp** - 261,824 stars

The `sindresorhus/awesome` repository is a curated list of awesome lists, which explains its popularity as it serves as a comprehensive directory of resources across many different topics in software development.

더 알아보기 (Learn more)