|
| 1 | +--- |
| 2 | +slug: /use-cases/AI/MCP/ai-agent-libraries/microsoft-agent-framework |
| 3 | +sidebar_label: 'Integrate Microsoft Agent Framework' |
| 4 | +title: 'How to build an AI Agent with Microsoft Agent Framework and the ClickHouse MCP Server' |
| 5 | +pagination_prev: null |
| 6 | +pagination_next: null |
| 7 | +description: 'Learn how build an AI Agent with Microsoft Agent Framework and the ClickHouse MCP Server' |
| 8 | +keywords: ['ClickHouse', 'MCP', 'Microsoft'] |
| 9 | +show_related_blogs: true |
| 10 | +doc_type: 'guide' |
| 11 | +--- |
| 12 | + |
| 13 | +# How to build an AI Agent with Microsoft Agent Framework and the ClickHouse MCP Server |
| 14 | + |
| 15 | +In this guide you'll learn how to build a [Microsoft Agent Framework](https://github.com/microsoft/agent-framework) AI agent that can interact with |
| 16 | +[ClickHouse's SQL playground](https://sql.clickhouse.com/) using [ClickHouse's MCP Server](https://github.com/ClickHouse/mcp-clickhouse). |
| 17 | + |
| 18 | +:::note Example notebook |
| 19 | +This example can be found as a notebook in the [examples repository](https://github.com/ClickHouse/examples/blob/main/ai/mcp/microsoft-agent-framework/microsoft-agent-framework.ipynb). |
| 20 | +::: |
| 21 | + |
| 22 | +## Prerequisites {#prerequisites} |
| 23 | +- You'll need to have Python installed on your system. |
| 24 | +- You'll need to have `pip` installed on your system. |
| 25 | +- You'll need an OpenAI API key |
| 26 | + |
| 27 | +You can run the following steps either from your Python REPL or via script. |
| 28 | + |
| 29 | +<VerticalStepper headerLevel="h2"> |
| 30 | + |
| 31 | +## Install libraries {#install-libraries} |
| 32 | + |
| 33 | +Install the Microsoft Agent Framework library by running the following commands: |
| 34 | + |
| 35 | +```python |
| 36 | +!pip install -q --upgrade pip |
| 37 | +!pip install -q agent-framework --pre |
| 38 | +!pip install -q ipywidgets |
| 39 | +``` |
| 40 | + |
| 41 | +## Setup credentials {#setup-credentials} |
| 42 | + |
| 43 | +Next, you'll need to provide your OpenAI API key: |
| 44 | + |
| 45 | +```python |
| 46 | +import os, getpass |
| 47 | +os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter OpenAI API Key:") |
| 48 | +``` |
| 49 | + |
| 50 | +```response title="Response" |
| 51 | +Enter OpenAI API Key: ········ |
| 52 | +``` |
| 53 | + |
| 54 | +Next, define the credentials needed to connect to the ClickHouse SQL playground: |
| 55 | + |
| 56 | +```python |
| 57 | +env = { |
| 58 | + "CLICKHOUSE_HOST": "sql-clickhouse.clickhouse.com", |
| 59 | + "CLICKHOUSE_PORT": "8443", |
| 60 | + "CLICKHOUSE_USER": "demo", |
| 61 | + "CLICKHOUSE_PASSWORD": "", |
| 62 | + "CLICKHOUSE_SECURE": "true" |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Initialize MCP Server and Microsoft Agent Framework agent {#initialize-mcp-and-agent} |
| 67 | + |
| 68 | +Now configure the ClickHouse MCP Server to point at the ClickHouse SQL playground |
| 69 | +and also initialize our agent and ask it a question: |
| 70 | + |
| 71 | +```python |
| 72 | +from agent_framework import ChatAgent, MCPStdioTool |
| 73 | +from agent_framework.openai import OpenAIResponsesClient |
| 74 | +``` |
| 75 | + |
| 76 | +```python |
| 77 | +clickhouse_mcp_server = MCPStdioTool( |
| 78 | + name="clickhouse", |
| 79 | + command="uv", |
| 80 | + args=[ |
| 81 | + "run", |
| 82 | + "--with", |
| 83 | + "mcp-clickhouse", |
| 84 | + "--python", |
| 85 | + "3.10", |
| 86 | + "mcp-clickhouse" |
| 87 | + ], |
| 88 | + env=env |
| 89 | +) |
| 90 | + |
| 91 | + |
| 92 | +async with ChatAgent( |
| 93 | + chat_client=OpenAIResponsesClient(model_id="gpt-5-mini-2025-08-07"), |
| 94 | + name="HousePricesAgent", |
| 95 | + instructions="You are a helpful assistant that can help query a ClickHouse database", |
| 96 | + tools=clickhouse_mcp_server, |
| 97 | +) as agent: |
| 98 | + query = "Tell me about UK property prices over the last five years" |
| 99 | + print(f"User: {query}") |
| 100 | + async for chunk in agent.run_stream(query): |
| 101 | + print(chunk.text, end="", flush=True) |
| 102 | + print("\n\n") |
| 103 | +``` |
| 104 | + |
| 105 | +The output of running this script is shown below: |
| 106 | + |
| 107 | +```response title="Response" |
| 108 | +User: Tell me about UK property prices over the last five years |
| 109 | +I looked at monthly UK sold-price records in the uk.uk_price_paid_simple_partitioned table for the last five years (toStartOfMonth(date), from Oct 2020 → Aug 2025). Summary and key points: |
| 110 | +
|
| 111 | +What I measured |
| 112 | +- Metrics: monthly median price, mean price, and transaction count (price paid records). |
| 113 | +- Period covered: months starting 2020-10-01 through 2025-08-01 (last five years from today). |
| 114 | +
|
| 115 | +High-level findings |
| 116 | +- Median price rose from £255,000 (2020-10) to £294,500 (2025-08) — an increase of about +15.4% over five years. |
| 117 | + - Equivalent compound annual growth rate (CAGR) for the median ≈ +2.9% per year. |
| 118 | +- Mean price fell slightly from about £376,538 (2020-10) to £364,653 (2025-08) — a decline of ≈ −3.2% over five years. |
| 119 | + - Mean-price CAGR ≈ −0.6% per year. |
| 120 | +- The divergence (median up, mean slightly down) suggests changes in the mix of transactions (fewer very-high-value sales or other compositional effects), since the mean is sensitive to outliers while the median is not. |
| 121 | +
|
| 122 | +Notable patterns and events in the data |
| 123 | +- Strong rises in 2020–2021 (visible in both median and mean), consistent with the post‑pandemic / stamp‑duty / demand-driven market surge seen in that period. |
| 124 | +- Peaks in mean prices around mid‑2022 (mean values ~£440k), then a general softening through 2022–2023 and stabilisation around 2023–2024. |
| 125 | +- Some months show large volatility or unusual counts (e.g., June 2021 and June 2021 had very high transaction counts; March 2025 shows a high median but April–May 2025 show lower counts). Recent months (mid‑2025) have much lower transaction counts in the table — this often indicates incomplete reporting for the most recent months and means recent monthly figures should be treated cautiously. |
| 126 | +
|
| 127 | +Example datapoints (from the query) |
| 128 | +- 2020-10: median £255,000, mean £376,538, transactions 89,125 |
| 129 | +- 2022-08: mean peak ~£441,209 (median ~£295,000) |
| 130 | +- 2025-03: median ~£314,750 (one of the highest medians) |
| 131 | +- 2025-08: median £294,500, mean £364,653, transactions 18,815 (low count — likely incomplete) |
| 132 | +
|
| 133 | +Caveats |
| 134 | +- These are transaction prices (Price Paid dataset) — actual house “values” may differ. |
| 135 | +- Mean is sensitive to composition and outliers. Changes in the types of properties sold (e.g., mix of flats vs detached houses, regional mix) will affect mean and median differently. |
| 136 | +- Recent months can be incomplete; months with unusually low transaction counts should be treated with caution. |
| 137 | +- This is a national aggregate — regional differences can be substantial. |
| 138 | +
|
| 139 | +If you want I can: |
| 140 | +- Produce a chart of median and mean over time. |
| 141 | +- Compare year-on-year or compute CAGR for a different start/end month. |
| 142 | +- Break the analysis down by region/county/town, property type (flat, terraced, semi, detached), or by price bands. |
| 143 | +- Show a table of top/bottom regions for price growth over the last 5 years. |
| 144 | +
|
| 145 | +Which follow-up would you like? |
| 146 | +
|
| 147 | +``` |
| 148 | + |
| 149 | +</VerticalStepper> |
0 commit comments