supabase-mcp / src /server.ts
AbdulElahGwaith's picture
Complete MCP Server implementation with tool handlers
e7f9b17 verified
Raw
History Blame Contribute Delete
1.81 kB
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
import { getSupabaseClient } from './supabase';
import * as tools from './tools';
const server = new Server({
name: "supabase-mcp",
version: "1.0.0",
}, {
capabilities: {
tools: {},
},
});
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: "list_tables",
description: "List all tables in the public schema of your Supabase database",
inputSchema: { type: "object", properties: {} }
},
{
name: "insert_record",
description: "Insert a new record into a specific table",
inputSchema: {
type: "object",
properties: {
table: { type: "string" },
record: { type: "object" }
},
required: ["table", "record"]
}
}
]
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
// Extract Supabase credentials from environment or headers (simulated here)
const url = process.env.SUPABASE_URL || "";
const key = process.env.SUPABASE_KEY || "";
const client = getSupabaseClient(url, key);
switch (request.params.name) {
case "list_tables":
return { content: [{ type: "text", text: JSON.stringify(await tools.listTables(client)) }] };
case "insert_record":
const { table, record } = request.params.arguments as any;
return { content: [{ type: "text", text: JSON.stringify(await tools.insertRecord(client, table, record)) }] };
default:
throw new Error("Tool not found");
}
});
const transport = new StdioServerTransport();
await server.connect(transport);