-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
224 lines (202 loc) · 6.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
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 fetch from "node-fetch";
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import {
CreateProjectInputSchema,
ListProjectsInputSchema,
GetProjectInputSchema,
DeleteProjectInputSchema,
type SupabaseResponse,
type Project,
type Organization,
ListOrganizationsInputSchema,
GetOrganizationInputSchema,
CreateOrganizationInputSchema,
UpdateOrganizationInputSchema,
ProjectApiKey,
GetProjectApiKeysInputSchema,
} from './schemas.js';
// Configuration
const SUPABASE_API_URL = 'https://api.supabase.com/v1';
class SupabaseAPI {
private apiKey: string;
constructor(apiKey: string) {
this.apiKey = apiKey;
}
private async makeRequest(endpoint: string, method: string = 'GET', body?: any): Promise<any> {
const response = await fetch(`${SUPABASE_API_URL}${endpoint}`, {
method,
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
});
if (!response.ok) {
throw new Error(`Supabase API error: ${response.statusText}`);
}
return response.json();
}
async listProjects(ref?: string): Promise<Project[]> {
return this.makeRequest('/projects' + (ref ? `?ref=${ref}` : ''));
}
async getProject(ref: string): Promise<Project> {
return this.makeRequest(`/projects/${ref}`);
}
async createProject(data: any): Promise<Project> {
return this.makeRequest('/projects', 'POST', data);
}
async deleteProject(ref: string): Promise<void> {
return this.makeRequest(`/projects/${ref}`, 'DELETE');
}
async listOrganizations(): Promise<Organization[]> {
return this.makeRequest('/organizations');
}
async getOrganization(slug: string): Promise<Organization> {
return this.makeRequest(`/organizations/${slug}`);
}
async createOrganization(data: any): Promise<Organization> {
return this.makeRequest('/organizations', 'POST', data);
}
async getProjectApiKeys(ref: string): Promise<ProjectApiKey[]> {
return this.makeRequest(`/projects/${ref}/api-keys`);
}
}
// Initialize server and API client
const apiKey = process.env.SUPABASE_API_KEY;
if (!apiKey) {
throw new Error('SUPABASE_API_KEY environment variable is required');
}
const supabase = new SupabaseAPI(apiKey);
const server = new Server({
name: "supabase-mcp",
version: "1.0.0",
}, {
capabilities: {
tools: {}
}
});
// List available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "list_projects",
description: "List all Supabase projects",
inputSchema: zodToJsonSchema(ListProjectsInputSchema)
},
{
name: "get_project",
description: "Get details of a specific Supabase project",
inputSchema: zodToJsonSchema(GetProjectInputSchema)
},
{
name: "create_project",
description: "Create a new Supabase project",
inputSchema: zodToJsonSchema(CreateProjectInputSchema)
},
{
name: "delete_project",
description: "Delete a Supabase project",
inputSchema: zodToJsonSchema(DeleteProjectInputSchema)
},
{
name: "list_organizations",
description: "List all organizations",
inputSchema: zodToJsonSchema(ListOrganizationsInputSchema)
},
{
name: "get_organization",
description: "Get details of a specific organization",
inputSchema: zodToJsonSchema(GetOrganizationInputSchema)
},
{
name: "create_organization",
description: "Create a new organization",
inputSchema: zodToJsonSchema(CreateOrganizationInputSchema)
},
{
name: "get_project_api_keys",
description: "Get API keys for a specific Supabase project",
inputSchema: zodToJsonSchema(GetProjectApiKeysInputSchema)
}
]
};
});
// Handle tool calls
server.setRequestHandler(CallToolRequestSchema, async (request) => {
try {
if (!request.params.arguments) {
throw new Error("Arguments are required");
}
switch (request.params.name) {
case "list_projects": {
const args = ListProjectsInputSchema.parse(request.params.arguments);
const result = await supabase.listProjects(args.ref);
return { toolResult: result };
}
case "get_project": {
const args = GetProjectInputSchema.parse(request.params.arguments);
const result = await supabase.getProject(args.ref);
return { toolResult: result };
}
case "create_project": {
const args = CreateProjectInputSchema.parse(request.params.arguments);
const result = await supabase.createProject(args);
return { toolResult: result };
}
case "delete_project": {
const args = DeleteProjectInputSchema.parse(request.params.arguments);
await supabase.deleteProject(args.ref);
return { toolResult: { success: true } };
}
case "list_organizations": {
const result = await supabase.listOrganizations();
return { toolResult: result };
}
case "get_organization": {
const args = GetOrganizationInputSchema.parse(request.params.arguments);
const result = await supabase.getOrganization(args.slug);
return { toolResult: result };
}
case "create_organization": {
const args = CreateOrganizationInputSchema.parse(request.params.arguments);
const result = await supabase.createOrganization(args);
return { toolResult: result };
}
case "get_project_api_keys": {
const args = GetProjectApiKeysInputSchema.parse(request.params.arguments);
const result = await supabase.getProjectApiKeys(args.ref);
if (args.name) {
const filtered = result.filter(key => key.name === args.name);
return { toolResult: filtered };
}
return { toolResult: result };
}
default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
} catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Invalid arguments: ${error.message}`);
}
throw error;
}
});
// Start the server
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Supabase MCP Server running on stdio");
}
runServer().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});