Skip to content

Commit 74ec774

Browse files
committed
mcp-agent and upsonicg
1 parent 0f4faae commit 74ec774

File tree

2 files changed

+663
-0
lines changed

2 files changed

+663
-0
lines changed
Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
---
2+
slug: /use-cases/AI/MCP/ai-agent-libraries/mcp-agent
3+
sidebar_label: 'Integrate mcp-agent'
4+
title: 'How to build an AI Agent with mcp-agent and the ClickHouse MCP Server'
5+
pagination_prev: null
6+
pagination_next: null
7+
description: 'Learn how build an AI Agent with mcp-agent and the ClickHouse MCP Server'
8+
keywords: ['ClickHouse', 'MCP', 'mcp-agent']
9+
show_related_blogs: true
10+
doc_type: 'guide'
11+
---
12+
13+
# How to build an AI Agent with CrewAI and the ClickHouse MCP Server
14+
15+
In this guide you'll learn how to build a [mcp-agent](https://github.com/lastmile-ai/mcp-agent) 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/mcp-agent/mcp-agent.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 mcp-agent library by running the following commands:
34+
35+
```python
36+
pip install -q --upgrade pip
37+
pip install -q mcp-agent openai
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 mcp-agent 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 mcp_agent.app import MCPApp
73+
from mcp_agent.agents.agent import Agent
74+
from mcp_agent.workflows.llm.augmented_llm_openai import OpenAIAugmentedLLM
75+
from mcp_agent.config import Settings, MCPSettings, MCPServerSettings, OpenAISettings
76+
```
77+
78+
```python
79+
settings = Settings(
80+
execution_engine="asyncio",
81+
openai=OpenAISettings(
82+
default_model="gpt-5-mini-2025-08-07",
83+
),
84+
mcp=MCPSettings(
85+
servers={
86+
"clickhouse": MCPServerSettings(
87+
command='uv',
88+
args=[
89+
"run",
90+
"--with", "mcp-clickhouse",
91+
"--python", "3.10",
92+
"mcp-clickhouse"
93+
],
94+
env=env
95+
),
96+
}
97+
),
98+
)
99+
100+
app = MCPApp(name="mcp_basic_agent", settings=settings)
101+
102+
async with app.run() as mcp_agent_app:
103+
logger = mcp_agent_app.logger
104+
data_agent = Agent(
105+
name="database-anayst",
106+
instruction="""You can answer questions with help from a ClickHouse database.""",
107+
server_names=["clickhouse"],
108+
)
109+
110+
async with data_agent:
111+
llm = await data_agent.attach_llm(OpenAIAugmentedLLM)
112+
result = await llm.generate_str(
113+
message="Tell me about UK property prices in 2025. Use ClickHouse to work it out."
114+
)
115+
116+
logger.info(result)
117+
```
118+
119+
```response title="Response"
120+
[10/10/25 11:26:20] INFO Starting MCP server 'mcp-clickhouse' with transport 'stdio' server.py:1502
121+
2025-10-10 11:26:20,183 - mcp.server.lowlevel.server - INFO - Processing request of type ListToolsRequest
122+
2025-10-10 11:26:20,184 - mcp.server.lowlevel.server - INFO - Processing request of type ListPromptsRequest
123+
2025-10-10 11:26:20,185 - mcp.server.lowlevel.server - INFO - Processing request of type ListResourcesRequest
124+
[INFO] 2025-10-10T11:26:20 mcp_agent.workflows.llm.augmented_llm_openai.database-anayst - Using reasoning model 'gpt-5-mini-2025-08-07' with
125+
'medium' reasoning effort
126+
[INFO] 2025-10-10T11:26:23 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
127+
{
128+
"data": {
129+
"progress_action": "Calling Tool",
130+
"tool_name": "list_databases",
131+
"server_name": "clickhouse",
132+
"agent_name": "database-anayst"
133+
}
134+
}
135+
2025-10-10 11:26:23,477 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
136+
2025-10-10 11:26:23,479 - mcp-clickhouse - INFO - Listing all databases
137+
2025-10-10 11:26:23,479 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
138+
2025-10-10 11:26:24,375 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
139+
2025-10-10 11:26:24,551 - mcp-clickhouse - INFO - Found 38 databases
140+
[INFO] 2025-10-10T11:26:26 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
141+
{
142+
"data": {
143+
"progress_action": "Calling Tool",
144+
"tool_name": "list_tables",
145+
"server_name": "clickhouse",
146+
"agent_name": "database-anayst"
147+
}
148+
}
149+
2025-10-10 11:26:26,825 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
150+
2025-10-10 11:26:26,832 - mcp-clickhouse - INFO - Listing tables in database 'uk'
151+
2025-10-10 11:26:26,832 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
152+
2025-10-10 11:26:27,311 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
153+
2025-10-10 11:26:28,738 - mcp-clickhouse - INFO - Found 9 tables
154+
[INFO] 2025-10-10T11:26:48 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
155+
{
156+
"data": {
157+
"progress_action": "Calling Tool",
158+
"tool_name": "run_select_query",
159+
"server_name": "clickhouse",
160+
"agent_name": "database-anayst"
161+
}
162+
}
163+
[INFO] 2025-10-10T11:26:48 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
164+
{
165+
"data": {
166+
"progress_action": "Calling Tool",
167+
"tool_name": "run_select_query",
168+
"server_name": "clickhouse",
169+
"agent_name": "database-anayst"
170+
}
171+
}
172+
[INFO] 2025-10-10T11:26:48 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
173+
{
174+
"data": {
175+
"progress_action": "Calling Tool",
176+
"tool_name": "run_select_query",
177+
"server_name": "clickhouse",
178+
"agent_name": "database-anayst"
179+
}
180+
}
181+
[INFO] 2025-10-10T11:26:48 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
182+
{
183+
"data": {
184+
"progress_action": "Calling Tool",
185+
"tool_name": "run_select_query",
186+
"server_name": "clickhouse",
187+
"agent_name": "database-anayst"
188+
}
189+
}
190+
[INFO] 2025-10-10T11:26:48 mcp_agent.mcp.mcp_aggregator.database-anayst - Requesting tool call
191+
{
192+
"data": {
193+
"progress_action": "Calling Tool",
194+
"tool_name": "run_select_query",
195+
"server_name": "clickhouse",
196+
"agent_name": "database-anayst"
197+
}
198+
}
199+
2025-10-10 11:26:48,366 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
200+
2025-10-10 11:26:48,367 - mcp-clickhouse - INFO - Executing SELECT query: SELECT
201+
count(*) AS transactions,
202+
avg(price) AS avg_price,
203+
quantileExact(0.5)(price) AS median_price,
204+
min(price) AS min_price,
205+
max(price) AS max_price
206+
FROM uk.uk_price_paid_simple_partitioned
207+
WHERE toYear(date)=2025
208+
2025-10-10 11:26:48,367 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
209+
2025-10-10 11:26:49,262 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
210+
2025-10-10 11:26:49,407 - mcp-clickhouse - INFO - Query returned 1 rows
211+
2025-10-10 11:26:49,408 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
212+
2025-10-10 11:26:49,408 - mcp-clickhouse - INFO - Executing SELECT query: SELECT toMonth(date) AS month, count(*) AS transactions, avg(price) AS avg_price, quantileExact(0.5)(price) AS median_price
213+
FROM uk.uk_price_paid_simple_partitioned
214+
WHERE toYear(date)=2025
215+
GROUP BY month
216+
ORDER BY month
217+
2025-10-10 11:26:49,408 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
218+
2025-10-10 11:26:49,857 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
219+
2025-10-10 11:26:50,067 - mcp-clickhouse - INFO - Query returned 8 rows
220+
2025-10-10 11:26:50,068 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
221+
2025-10-10 11:26:50,069 - mcp-clickhouse - INFO - Executing SELECT query: SELECT town, count(*) AS transactions, avg(price) AS avg_price
222+
FROM uk.uk_price_paid_simple_partitioned
223+
WHERE toYear(date)=2025
224+
GROUP BY town
225+
HAVING transactions >= 50
226+
ORDER BY avg_price DESC
227+
LIMIT 10
228+
2025-10-10 11:26:50,069 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
229+
2025-10-10 11:26:50,594 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
230+
2025-10-10 11:26:50,741 - mcp-clickhouse - INFO - Query returned 10 rows
231+
2025-10-10 11:26:50,744 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
232+
2025-10-10 11:26:50,746 - mcp-clickhouse - INFO - Executing SELECT query: SELECT toYear(date) AS year, count(*) AS transactions, avg(price) AS avg_price, quantileExact(0.5)(price) AS median_price
233+
FROM uk.uk_price_paid_simple_partitioned
234+
WHERE toYear(date) IN (2024,2025)
235+
GROUP BY year
236+
ORDER BY year
237+
2025-10-10 11:26:50,747 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
238+
2025-10-10 11:26:51,256 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
239+
2025-10-10 11:26:51,447 - mcp-clickhouse - INFO - Query returned 2 rows
240+
2025-10-10 11:26:51,449 - mcp.server.lowlevel.server - INFO - Processing request of type CallToolRequest
241+
2025-10-10 11:26:51,452 - mcp-clickhouse - INFO - Executing SELECT query: SELECT type, count(*) AS transactions, avg(price) AS avg_price, quantileExact(0.5)(price) AS median_price
242+
FROM uk.uk_price_paid
243+
WHERE toYear(date)=2025
244+
GROUP BY type
245+
ORDER BY avg_price DESC
246+
2025-10-10 11:26:51,452 - mcp-clickhouse - INFO - Creating ClickHouse client connection to sql-clickhouse.clickhouse.com:8443 as demo (secure=True, verify=True, connect_timeout=30s, send_receive_timeout=30s)
247+
2025-10-10 11:26:51,952 - mcp-clickhouse - INFO - Successfully connected to ClickHouse server version 25.8.1.8344
248+
2025-10-10 11:26:52,166 - mcp-clickhouse - INFO - Query returned 5 rows
249+
[INFO] 2025-10-10T11:27:51 mcp_agent.mcp_basic_agent - Summary (TL;DR)
250+
- Based on the UK Price Paid tables in ClickHouse, for transactions recorded in 2025 so far there are 376,633 sales with an average price of
251+
£362,283 and a median price of £281,000. The data appears to include only months Jan–Aug 2025 (so 2025 is incomplete). There are extreme
252+
outliers (min £100, max £127,700,000) that skew the mean.
253+
254+
What I computed (how)
255+
I ran aggregations on the uk.price-paid tables in ClickHouse:
256+
- overall 2025 summary (count, mean, median, min, max) from uk.uk_price_paid_simple_partitioned
257+
- monthly breakdown for 2025 (transactions, mean, median)
258+
- top towns in 2025 by average price (towns with >= 50 transactions)
259+
- year comparison: 2024 vs 2025 (count, mean, median)
260+
- breakdown by property type for 2025 (counts, avg, median) using uk.uk_price_paid
261+
262+
Key numbers (from the dataset)
263+
- Overall 2025 (recorded transactions): transactions = 376,633; mean price = £362,282.66; median price = £281,000; min = £100; max =
264+
£127,700,000.
265+
- By month (2025): (month, transactions, mean price, median price)
266+
- Jan: 53,927, mean £386,053, median £285,000
267+
- Feb: 58,740, mean £371,803, median £285,000
268+
- Mar: 95,274, mean £377,200, median £315,000
269+
- Apr: 24,987, mean £331,692, median £235,000
270+
- May: 39,013, mean £342,380, median £255,000
271+
- Jun: 41,446, mean £334,667, median £268,500
272+
- Jul: 44,431, mean £348,293, median £277,500
273+
- Aug: 18,815, mean £364,653, median £292,999
274+
(Only months 1–8 are present in the dataset.)
275+
- Top towns by average price (2025, towns with ≥50 transactions)
276+
- TRING: 126 txns, avg £1,973,274
277+
- BUCKHURST HILL: 98 txns, avg £1,441,331
278+
- ASCOT: 175 txns, avg £1,300,748
279+
- RADLETT: 69 txns, avg £1,160,217
280+
- COBHAM: 115 txns, avg £1,035,192
281+
- EAST MOLESEY, BEACONSFIELD, ESHER, CHALFONT ST GILES, THAMES DITTON are also in the top 10 (all high-average commuter/affluent towns).
282+
- Year comparison (2024 vs 2025 as recorded)
283+
- 2024: 859,960 transactions, mean £390,879, median £280,000
284+
- 2025: 376,633 transactions, mean £362,283, median £281,000
285+
(2025 counts are much lower because the dataset only includes part of the year.)
286+
- By property type (2025)
287+
- detached: 85,362 txns, avg £495,714, median £415,000
288+
- semi-detached: 107,580 txns, avg £319,922, median £270,000
289+
- flat: 62,975 txns, avg £298,529, median £227,000
290+
- terraced: 112,832 txns, avg £286,616, median £227,000
291+
- other: 7,884 txns, avg £1,087,765 (median £315,000) — note small-group and outlier effect
292+
293+
Important caveats and data quality notes
294+
- The dataset appears partial for 2025 (only months Jan–Aug present). Any “2025” totals are not full-year figures.
295+
- Large outliers exist (e.g., max £127.7M, and min £100). These likely include data-entry errors or non-standard records and inflate the
296+
mean. Median is often a more robust measure here.
297+
- “other” property-type averages are unstable due to low/heterogeneous counts and outliers.
298+
- I did not filter by is_new, duration, or other metadata; those filters can change results (for example excluding new-builds or
299+
leaseholds).
300+
- The tables are Price Paid-style transaction records (recorded sales) — they do not directly represent asking prices or valuations.
301+
302+
Suggested next steps (I can run these)
303+
- Clean out obvious outliers (e.g., prices < £10k or > £10M) and recompute averages/medians.
304+
- Produce regional / county / postcode-area summaries and maps.
305+
- Compute month-on-month or rolling 3-month median to show trend through 2025.
306+
- Produce year-on-year (YoY) growth rates by month (e.g., Mar 2025 vs Mar 2024).
307+
- Forecast for full 2025 using simple extrapolation or time-series modelling (but better after deciding how to handle missing
308+
months/outliers).
309+
310+
If you want, I can:
311+
- Re-run the same aggregations after removing extreme outliers and show cleaned results.
312+
- Produce YoY monthly growth and charts (I can return CSV or JSON aggregates you can chart).
313+
Which would you like me to do next?
314+
[INFO] 2025-10-10T11:27:51 mcp_agent.mcp.mcp_aggregator.database-anayst - Last aggregator closing, shutting down all persistent
315+
connections...
316+
[INFO] 2025-10-10T11:27:51 mcp_agent.mcp.mcp_connection_manager - Disconnecting all persistent server connections...
317+
[INFO] 2025-10-10T11:27:51 mcp_agent.mcp.mcp_connection_manager - clickhouse: Requesting shutdown...
318+
[INFO] 2025-10-10T11:27:51 mcp_agent.mcp.mcp_connection_manager - All persistent server connections signaled to disconnect.
319+
[INFO] 2025-10-10T11:27:52 mcp_agent.mcp.mcp_aggregator.database-anayst - Connection manager successfully closed and removed from context
320+
[INFO] 2025-10-10T11:27:52 mcp_agent.mcp_basic_agent - MCPApp cleanup
321+
{
322+
"data": {
323+
"progress_action": "Finished",
324+
"target": "mcp_basic_agent",
325+
"agent_name": "mcp_application_loop"
326+
}
327+
}
328+
```
329+
330+
</VerticalStepper>

0 commit comments

Comments
 (0)