|
| 1 | +# Verify MCP with CrateDB |
| 2 | + |
| 3 | +## About |
| 4 | + |
| 5 | +This folder includes Python programs that use the [Model Context Protocol |
| 6 | +Python SDK], implementing MCP clients that hold conversations with MCP servers |
| 7 | +wrapping database access. |
| 8 | + |
| 9 | +In this case, the focus is on [CrateDB], by using relevant PostgreSQL adapter |
| 10 | +implementations provided by the MCP ecosystem, because CrateDB is compatible |
| 11 | +with PostgreSQL. |
| 12 | + |
| 13 | +[MCP], the Model Context Protocol, is an open protocol that enables seamless |
| 14 | +integration between LLM applications and external data sources and tools. |
| 15 | + |
| 16 | +MCP clients call servers by either invoking them as a subprocess and |
| 17 | +communicate with stdio, or by using SSE, which implements TCP socket |
| 18 | +communication. |
| 19 | + |
| 20 | +## What's Inside |
| 21 | + |
| 22 | +The Python programs have been derived from the [Writing MCP Clients] example |
| 23 | +program. |
| 24 | + |
| 25 | +- `example_builtin.py`: Exercise the basic built-in Model Context Protocol server |
| 26 | + [@modelcontextprotocol/server-postgres] that provides read-only access to |
| 27 | + PostgreSQL databases per `query` tool. This server enables LLMs to inspect |
| 28 | + database schemas and execute read-only queries. It is written in TypeScript, |
| 29 | + to be invoked with [npx]. |
| 30 | + |
| 31 | +- `example_dbhub.py`: Exercise communication using [DBHub], a universal database |
| 32 | + gateway implementing the Model Context Protocol (MCP) server interface. This |
| 33 | + gateway allows MCP-compatible clients to connect to and explore different databases. |
| 34 | + |
| 35 | +- `example_jdbc.py`: Exercise JDBC communication using the [Model Context Protocol |
| 36 | + Server for JDBC] from the [quarkus-mcp-servers] package, providing a range |
| 37 | + of tools. It is written in Java, to be invoked with [JBang]. |
| 38 | + |
| 39 | +## Resources |
| 40 | + |
| 41 | +- Read a [brief introduction to MCP] by ByteByteGo. |
| 42 | +- Read the canonical [Introduction to MCP]. |
| 43 | +- Read about the [MCP Python SDK]. |
| 44 | +- Read about configuring those servers with other MCP clients. |
| 45 | + - [Claude Desktop configuration] |
| 46 | + - [oterm configuration] |
| 47 | +- [Connecting to an already running MCP server] seems to become possible |
| 48 | + with [MCP SSE]. As of 2025, not all servers implement that just yet. |
| 49 | +- [Model Context Protocol (MCP) @ CrateDB] |
| 50 | + |
| 51 | +## Setup |
| 52 | + |
| 53 | +Start a CrateDB instance on your machine. |
| 54 | +```shell |
| 55 | +docker run -it --rm \ |
| 56 | + --publish=4200:4200 --publish=5432:5432 \ |
| 57 | + --env=CRATE_HEAP_SIZE=2g \ |
| 58 | + crate:latest -Cdiscovery.type=single-node |
| 59 | +``` |
| 60 | + |
| 61 | +Install the [uv] package manager and launcher. |
| 62 | +```shell |
| 63 | +{brew,pip} install uv |
| 64 | +``` |
| 65 | + |
| 66 | +Install the [JBang] package manager and launcher. |
| 67 | +```shell |
| 68 | +{asdf,choco,dnf,brew,scoop,sdk} install jbang |
| 69 | +``` |
| 70 | + |
| 71 | +## Install |
| 72 | + |
| 73 | +Acquire sources, set up sandbox, and install packages. |
| 74 | +```bash |
| 75 | +git clone https://github.com/crate/cratedb-examples |
| 76 | +cd cratedb-examples/framework/mcp |
| 77 | +uv pip install -r requirements.txt |
| 78 | +``` |
| 79 | + |
| 80 | +## Synopsis |
| 81 | + |
| 82 | +```shell |
| 83 | +uv run example_builtin.py |
| 84 | +``` |
| 85 | +```shell |
| 86 | +uv run example_dbhub.py |
| 87 | +``` |
| 88 | +```shell |
| 89 | +uv run example_jdbc.py |
| 90 | +``` |
| 91 | + |
| 92 | +## Development |
| 93 | + |
| 94 | +`ctk tail` is a handy command to follow the progress of CrateDB's `sys.jobs_log`, |
| 95 | +which is applicable for all sorts of driver, adapter, and connector explorations. |
| 96 | +After providing authentication information, just use uv's `uvx` launcher to invoke |
| 97 | +CrateDB Toolkit's tail command without installation. |
| 98 | +```shell |
| 99 | +export CRATEDB_SQLALCHEMY_URL=crate://crate@localhost:4200/ |
| 100 | +``` |
| 101 | +```shell |
| 102 | +uvx --from=cratedb-toolkit ctk tail -n 3 --follow --format=log-pretty sys.jobs_log |
| 103 | +``` |
| 104 | + |
| 105 | +## Outlook |
| 106 | + |
| 107 | +A future iteration may provide a premium MCP database adapter for CrateDB, |
| 108 | +unlocking more details and features. |
| 109 | + |
| 110 | + |
| 111 | +[brief introduction to MCP]: https://blog.bytebytego.com/i/159075598/what-is-mcp |
| 112 | +[Claude Desktop configuration]: https://github.com/modelcontextprotocol/servers?tab=readme-ov-file#using-an-mcp-client |
| 113 | +[connecting to an already running MCP server]: https://github.com/modelcontextprotocol/python-sdk/issues/145 |
| 114 | +[CrateDB]: https://cratedb.com/database |
| 115 | +[DBHub]: https://github.com/bytebase/dbhub |
| 116 | +[Introduction to MCP]: https://modelcontextprotocol.io/introduction |
| 117 | +[JBang]: https://www.jbang.dev/ |
| 118 | +[MCP]: https://modelcontextprotocol.io/ |
| 119 | +[MCP Python SDK]: https://github.com/modelcontextprotocol/python-sdk |
| 120 | +[MCP SSE]: https://github.com/sidharthrajaram/mcp-sse |
| 121 | +[Model Context Protocol (MCP) @ CrateDB]: https://github.com/crate/crate-clients-tools/discussions/234 |
| 122 | +[Model Context Protocol Python SDK]: https://pypi.org/project/mcp/ |
| 123 | +[Model Context Protocol Server for JDBC]: https://github.com/quarkiverse/quarkus-mcp-servers/tree/main/jdbc#readme |
| 124 | +[@modelcontextprotocol/server-postgres]: https://www.npmjs.com/package/@modelcontextprotocol/server-postgres |
| 125 | +[npx]: https://docs.npmjs.com/cli/v11/commands/npx |
| 126 | +[oterm configuration]: https://ggozad.github.io/oterm/tools/mcp/ |
| 127 | +[quarkus-mcp-servers]: https://github.com/quarkiverse/quarkus-mcp-servers |
| 128 | +[uv]: https://docs.astral.sh/uv/ |
| 129 | +[Writing MCP Clients]: https://github.com/modelcontextprotocol/python-sdk?tab=readme-ov-file#writing-mcp-clients |
0 commit comments