Skip to content

Commit 97db7ce

Browse files
committed
MCP: Add example about CrateDB MCP
1 parent 94a055f commit 97db7ce

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

framework/mcp/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ program.
4646
and includes advanced client programs for Claude and Gemini that work out of the box.
4747
It is written in Python, optionally to be invoked with `uv` or `uvx`.
4848

49+
- `example_cratedb_mcp.py`:
50+
The [CrateDB MCP Server] specialises on advanced CrateDB SQL operations by blending in
51+
knowledge base resources from CrateDB's documentation about query optimizations.
52+
It is written in Python, optionally to be invoked with `uv` or `uvx`.
53+
4954
## Resources
5055

5156
- Read a [brief introduction to MCP] by ByteByteGo.
@@ -134,6 +139,7 @@ unlocking more details and features.
134139
[Claude Desktop configuration]: https://github.com/modelcontextprotocol/servers?tab=readme-ov-file#using-an-mcp-client
135140
[connecting to an already running MCP server]: https://github.com/modelcontextprotocol/python-sdk/issues/145
136141
[CrateDB]: https://cratedb.com/database
142+
[CrateDB MCP Server]: https://github.com/crate/cratedb-mcp
137143
[CrateDB SQLAlchemy dialect]: https://cratedb.com/docs/sqlalchemy-cratedb/
138144
[DBHub]: https://github.com/bytebase/dbhub
139145
[Introduction to MCP]: https://modelcontextprotocol.io/introduction

framework/mcp/backlog.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- https://github.com/crate/crate/issues/11757
1717
- https://github.com/crate/crate/issues/12544
1818
- PG-MCP: Fix `/rowcount endpoint`
19+
https://github.com/stuzero/pg-mcp-server/issues/10
1920

2021
## Iteration +2
2122
- General: Evaluate all connectors per `stdio` and `sse`, where possible

framework/mcp/example_cratedb_mcp.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# CrateDB MCP Server
2+
# https://github.com/crate/cratedb-mcp
3+
#
4+
# Derived from:
5+
# https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov-file#writing-mcp-clients
6+
from cratedb_toolkit.util import DatabaseAdapter
7+
from mcp import ClientSession, StdioServerParameters
8+
from mcp.client.stdio import stdio_client
9+
10+
from mcp_utils import McpDatabaseConversation
11+
12+
# Create server parameters for stdio connection.
13+
server_params = StdioServerParameters(
14+
command="cratedb-mcp",
15+
args=[],
16+
env={
17+
"CRATEDB_MCP_HTTP_URL": "http://localhost:4200",
18+
"CRATEDB_MCP_TRANSPORT": "stdio",
19+
},
20+
)
21+
22+
23+
async def run():
24+
async with stdio_client(server_params) as (read, write):
25+
async with ClientSession(
26+
read, write
27+
) as session:
28+
# Initialize the connection
29+
await session.initialize()
30+
31+
client = McpDatabaseConversation(session)
32+
await client.inquire()
33+
34+
print("## MCP server conversations")
35+
print()
36+
37+
# Provision database.
38+
db = DatabaseAdapter("crate://crate@localhost:4200/")
39+
db.run_sql("CREATE TABLE IF NOT EXISTS public.mcp_builtin (id INT, data TEXT)")
40+
db.run_sql("INSERT INTO public.mcp_builtin (id, data) VALUES (42, 'Hotzenplotz')")
41+
db.refresh_table("public.mcp_builtin")
42+
43+
# Call a few tools.
44+
await client.call_tool("query_sql", arguments={"query": "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3"})
45+
await client.call_tool("get_table_metadata", arguments={})
46+
await client.call_tool("get_health", arguments={})
47+
48+
49+
if __name__ == "__main__":
50+
import asyncio
51+
52+
asyncio.run(run())

framework/mcp/requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
cratedb-mcp @ git+https://github.com/crate/cratedb-mcp@packaging-adjustments
12
cratedb-toolkit
2-
mcp<1.6
3+
mcp<1.7
34
mcp-alchemy @ git+https://github.com/runekaagaard/mcp-alchemy.git@b85aae6; python_version>='3.12'
45
sqlalchemy-cratedb>=0.42.0.dev1
56
where

framework/mcp/test.py

+32
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,35 @@ def test_pg_mcp():
190190

191191
assert b"Getting prompt: nl_to_sql_prompt" in p.stdout
192192
assert b"You are an expert PostgreSQL database query assistant" in p.stdout
193+
194+
195+
def test_cratedb_mcp():
196+
"""
197+
Validate the CrateDB MCP server works well.
198+
199+
It is written in Python and uses HTTP.
200+
https://github.com/crate/cratedb-mcp
201+
"""
202+
p = run(f"{sys.executable} example_cratedb_mcp.py")
203+
assert p.returncode == 0
204+
205+
# Validate output specific to the MCP server.
206+
assert b"Processing request of type" in p.stderr
207+
assert b"ListPromptsRequest" in p.stderr
208+
assert b"ListResourcesRequest" in p.stderr
209+
assert b"ListToolsRequest" in p.stderr
210+
assert b"CallToolRequest" in p.stderr
211+
212+
# Validate output specific to CrateDB.
213+
assert b"Calling tool: query_sql" in p.stdout
214+
assert b"cols:" in p.stdout
215+
assert b"rows:" in p.stdout
216+
assert b"Mont Blanc" in p.stdout
217+
218+
assert b"Calling tool: get_table_metadata" in p.stdout
219+
assert b"- information_schema" in p.stdout
220+
assert b"total_missing_shards: null" in p.stdout
221+
assert b"table_name: table_partitions" in p.stdout
222+
223+
assert b"Calling tool: get_health" in p.stdout
224+
assert b"underreplicated_shards" in p.stdout

0 commit comments

Comments
 (0)