trackmcp
Back to directory
jubalm

mcp-with-d1-data

View on GitHub

Demo MCP Server build using Cloudflare MCPAgent Durable Objects and D1 database

2 stars TypeScriptServers & Infrastructure Updated Jun 20, 2025
cloudflarecloudflare-d1cloudflare-kvcloudflare-workersmcp

Documentation

MCP Server: Demo

Overview

This repository serves as a demonstration of building an MCP (Model Context Protocol) server that can access and query actual database information. Uses

the `@modelcontextprotocol/sdk` library leveraging Cloudflare's `MCPAgent` to implement tools, manage resources, and a D1 database.

Features

Database

The project integrates with a D1 database (`mcp-agent-d1-data`) to retrieve data but you should be able to work with any database with some minor adjustments on the tools. Also included some niceties:

  • Schema Management: SQL migration files (`up.sql`, `down.sql`, `seed.sql`) in the `db/` directory manage the database schema and seed data.

MCP Server (`MyMCP`)

The `MyMCP` class extends the `McpAgent` class from the `@modelcontextprotocol/sdk` library. It acts as the core server implementation, providing server sent events (SSE) as a foundational transport.

  • Durable Object: The server is implemented as a Durable Object, which allows stateful interactions and long process operations. It also comes with hibernation built-in when idle.

Query Tool

The `query` tool is designed to interact with the database by providing an interface where the LLM can send SQL queries onto the database. Use with care in production environments and practice proper authorization safeguards.

Introspection Tool

The `schema-introspection` tool is particularly useful to help the LLM understand the structure of the database to produce a more accurate query design.

Integration Tests

Included integration tests in the `tests/` directory to get started. Validate the functionality of the MCP server and its tools, or use the tests for TDD.

bash
npm run test

Project Structure

code
๐Ÿ“ฆ d1-mcp
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ tsconfig.json
โ”œโ”€โ”€ vitest.config.js
โ”œโ”€โ”€ worker-configuration.d.ts
โ”œโ”€โ”€ wrangler.jsonc
โ”œโ”€โ”€ db
โ”‚   โ”œโ”€โ”€ down.sql
โ”‚   โ”œโ”€โ”€ seed.sql
โ”‚   โ””โ”€โ”€ up.sql
โ”œโ”€โ”€ public
โ”‚   โ””โ”€โ”€ index.html
โ”œโ”€โ”€ src
โ”‚   โ”œโ”€โ”€ index.ts
โ”‚   โ”œโ”€โ”€ metadata.ts
โ”‚   โ”œโ”€โ”€ tools.ts
โ”‚   โ””โ”€โ”€ utils.ts
โ””โ”€โ”€ tests
    โ”œโ”€โ”€ index.test.ts
    โ””โ”€โ”€ tsconfig.json

Getting Started

Prerequisites

  • Node.js
  • Cloudflare Workers CLI (`wrangler`)

Installation

1. Clone the repository.

2. Install dependencies:

bash
npm install

Development

  • Start the development server:
bash
npm run dev

Deployment

  • Deploy to Cloudflare Workers:
bash
npm run deploy

License

This project is licensed under the MIT License.

Frequently asked questions

What is mcp-with-d1-data?

mcp-with-d1-data is Demo MCP Server build using Cloudflare MCPAgent Durable Objects and D1 database

How do I install mcp-with-d1-data?

Open the GitHub repository and follow its README. Most MCP servers are added to your client's MCP config, then called by your agent.

Is mcp-with-d1-data open source?

Yes โ€” it is hosted on GitHub at https://github.com/jubalm/mcp-with-d1-data and has 2 stars.

Related MCP tools

Run your own MCP server? See who uses it and what to fix.

Measure it with TrackMCP