Skip to main content
Glama

Cloudflare D1 Database MCP Server

server.ts3.22 kB
import { Server } from "@modelcontextprotocol/sdk/server/index.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js"; import { D1Client } from "./client.js"; export interface D1ServerOptions { client: D1Client; } export function createD1Server(options: D1ServerOptions) { const { client } = options; const server = new Server( { name: "cloudflare-d1-mcp", version: "1.0.0", }, { capabilities: { tools: {}, }, } ); // Register the tools/list handler server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: "d1_query", description: "Run a SQL query against the configured Cloudflare D1 database.", inputSchema: { type: "object", properties: { sql: { type: "string", description: "The SQL statement to execute." }, bindings: { type: "array", description: "Optional positional bindings for the SQL statement.", items: { type: ["string", "number", "boolean", "null"] } } }, required: ["sql"] } }, { name: "d1_list_tables", description: "List tables available in the Cloudflare D1 database.", inputSchema: { type: "object", properties: {}, additionalProperties: false } } ] }; }); // Register the tools/call handler server.setRequestHandler(CallToolRequestSchema, async (request: any) => { const { name, arguments: args } = request.params; try { if (name === "d1_query") { const { sql, bindings } = args as { sql: string; bindings?: unknown[]; }; const result = await client.executeQuery(sql, bindings); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } else if (name === "d1_list_tables") { const tables = await client.listTables(); return { content: [ { type: "text", text: JSON.stringify(tables, null, 2) } ] }; } else { throw new Error(`Unknown tool: ${name}`); } } catch (error) { return { content: [ { type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }); return { server, async start() { // Use the proper stdio transport from MCP SDK const transport = new StdioServerTransport(); await server.connect(transport); console.error('Cloudflare D1 MCP server started'); } }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/seek-your-way-out/cloudflare-sywo-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server