Database MCP Server

Database MCP Server

Build an MCP server that lets Claude query and manage your database safely.
typescript
// Safe database MCP with read-only queries
server.tool("query", "Run a read-only SQL query", {
  sql: z.string().describe("SQL SELECT query"),
}, async ({ sql }) => {
  if (!sql.trim().toLowerCase().startsWith("select")) {
    return { isError: true, content: [{ type: "text", text: "Only SELECT queries allowed" }] };
  }
  const results = await db.query(sql);
  return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] };
});

Tip:Always restrict database access in MCP servers. Use read-only connections and whitelist allowed operations.

PreviewTypeScriptRead Only
Copy this code and run it locally with npx tsx server.ts

💬 Got questions? Ask me anything!