33#
44# Derived from:
55# https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov-file#writing-mcp-clients
6- import shlex
7-
6+ from cratedb_toolkit .util import DatabaseAdapter
87from mcp import ClientSession , StdioServerParameters
98from mcp .client .stdio import stdio_client
10- from pprint import pprint
119
10+ from mcp_utils import McpDatabaseConversation
1211
1312# Create server parameters for stdio connection.
1413server_params_npx = StdioServerParameters (
2423 env = None ,
2524)
2625
27- docker_command = """
28- docker run --rm --init \
29- --name dbhub \
30- --publish 8080:8080 \
31- bytebase/dbhub:latest \
32- --transport sse \
33- --port 8080 \
34- --dsn="postgres://[email protected] :5432/doc?sslmode=disable"35- """
36- server_params_docker = StdioServerParameters (
37- command = "npx" ,
38- args = shlex .split (docker_command ),
39- env = None ,
40- )
41-
42-
4326async def run ():
4427 async with stdio_client (server_params_npx ) as (read , write ):
4528 async with ClientSession (
@@ -48,40 +31,30 @@ async def run():
4831 # Initialize the connection
4932 await session .initialize ()
5033
51- # List available prompts
52- print ("Prompts:" )
53- pprint (await session .list_prompts ())
54- print ()
55-
56- # List available resources
57- print ("Resources:" )
58- pprint (await session .list_resources ())
59- print ()
34+ client = McpDatabaseConversation (session )
35+ await client .inquire ()
6036
61- # List available tools
62- print ("Tools:" )
63- pprint (await session .list_tools ())
37+ print ("## MCP server conversations" )
6438 print ()
6539
66- print ("Calling tool: run_query" )
67- result = await session .call_tool ("run_query" , arguments = {"query" : "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3" })
68- pprint (result )
69- print ()
40+ # Call a few tools.
41+ await client .call_tool ("run_query" , arguments = {"query" : "SELECT * FROM sys.summits ORDER BY height DESC LIMIT 3" })
42+ await client .call_tool ("list_connectors" , arguments = {})
7043
71- print ("Calling tool: list_connectors" )
72- result = await session .call_tool ("list_connectors" , arguments = {})
73- pprint (result )
74- print ()
44+ # Validate database content.
45+ db = DatabaseAdapter ("crate://crate@localhost:4200/" )
46+ db .run_sql ("CREATE TABLE IF NOT EXISTS public.testdrive (id INT, data TEXT)" )
47+ db .run_sql ("INSERT INTO public.testdrive (id, data) VALUES (42, 'Hotzenplotz')" )
48+ db .refresh_table ("public.testdrive" )
7549
76- # Read a resource
77- # FIXME: Does not work , because the PostgreSQL adapters hard-codes `WHERE table_schema = 'public'`.
50+ # Read a few resources.
51+ # FIXME: Only works on schema=public , because the PostgreSQL adapter hard-codes `WHERE table_schema = 'public'`.
7852 # https://github.com/bytebase/dbhub/blob/09424c8513c8c7bef7f66377b46a2b93a69a57d2/src/connectors/postgres/index.ts#L89-L107
79- """
80- print("Reading resource: tables")
81- content, mime_type = await session.read_resource("db://tables")
82- print("MIME type:", mime_type)
83- pprint(content)
84- """
53+ await client .read_resource ("db://tables" )
54+
55+ # Get a few prompts.
56+ await client .get_prompt ("generate_sql" , arguments = {"description" : "Please enumerate the highest five mountains." , "dialect" : "postgres" })
57+ await client .get_prompt ("explain_db" , arguments = {"target" : "testdrive" })
8558
8659
8760if __name__ == "__main__" :
0 commit comments