MCP Server: Demo

by jubalm

307 downloads
Not rated
GitHub

About

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

Details

Author
jubalm
Downloads
307
Categories
Search

- Provides a query tool for sending SQL queries to the database.
- Includes a schema-introspection tool to help the LLM understand database structure.
- Implemented as a Cloudflare Durable Object with SSE transport.
- SQL migration files (up.sql, down.sql, seed.sql) for schema management.
- Built with the @modelcontextprotocol/sdk and MCPAgent.

Clone the repository, install dependencies with npm install, then run npm run dev for local development or npm run deploy to deploy to Cloudflare Workers. Integration tests are available with npm run test.

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.

npm run test

Project Structure

πŸ“¦ 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:

   npm install

Development

- Start the development server:

  npm run dev

Deployment

- Deploy to Cloudflare Workers:

  npm run deploy

License

This project is licensed under the MIT License.

No reviews yet β€” be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.