index.ts93 lines · main
| 1 | import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js' |
| 2 | import { createSupabaseMcpServer, SupabasePlatform } from '@supabase/mcp-server-supabase' |
| 3 | import { stripIndent } from 'common-tags' |
| 4 | import { NextApiRequest, NextApiResponse } from 'next' |
| 5 | import { z } from 'zod' |
| 6 | |
| 7 | import { |
| 8 | commaSeparatedStringIntoArray, |
| 9 | fromNodeHeaders, |
| 10 | zBooleanString, |
| 11 | } from '@/lib/api/apiHelpers' |
| 12 | import { |
| 13 | getDatabaseOperations, |
| 14 | getDebuggingOperations, |
| 15 | getDevelopmentOperations, |
| 16 | } from '@/lib/api/self-hosted/mcp' |
| 17 | import { DEFAULT_PROJECT } from '@/lib/constants/api' |
| 18 | |
| 19 | const supportedFeatureGroupSchema = z.enum(['docs', 'database', 'development', 'debugging']) |
| 20 | |
| 21 | const mcpQuerySchema = z.object({ |
| 22 | features: z |
| 23 | .string() |
| 24 | .transform(commaSeparatedStringIntoArray) |
| 25 | .optional() |
| 26 | .describe( |
| 27 | stripIndent` |
| 28 | A comma-separated list of feature groups to filter tools by. If not provided, all tools are available. |
| 29 | |
| 30 | The following feature groups are supported: ${supportedFeatureGroupSchema.options.map((group) => `\`${group}\``).join(', ')}. |
| 31 | ` |
| 32 | ) |
| 33 | .pipe(z.array(supportedFeatureGroupSchema).optional()), |
| 34 | read_only: zBooleanString() |
| 35 | .default('false') |
| 36 | .describe( |
| 37 | 'Indicates whether or not the MCP server should operate in read-only mode. This prevents write operations on any of your databases by executing SQL as a read-only Postgres user.' |
| 38 | ), |
| 39 | }) |
| 40 | |
| 41 | const handler = async (req: NextApiRequest, res: NextApiResponse) => { |
| 42 | switch (req.method) { |
| 43 | case 'POST': |
| 44 | return handlePost(req, res) |
| 45 | default: |
| 46 | res.setHeader('Allow', ['POST']) |
| 47 | return res.status(405).json({ error: { message: `Method ${req.method} Not Allowed` } }) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | async function handlePost(req: NextApiRequest, res: NextApiResponse) { |
| 52 | const { error, data } = mcpQuerySchema.safeParse(req.query) |
| 53 | |
| 54 | if (error) { |
| 55 | return res.status(400).json({ error: error.flatten().fieldErrors }) |
| 56 | } |
| 57 | |
| 58 | const { features, read_only } = data |
| 59 | const headers = fromNodeHeaders(req.headers) |
| 60 | |
| 61 | const platform: SupabasePlatform = { |
| 62 | database: getDatabaseOperations({ headers }), |
| 63 | development: getDevelopmentOperations({ headers }), |
| 64 | debugging: getDebuggingOperations({ headers }), |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | const server = createSupabaseMcpServer({ |
| 69 | platform, |
| 70 | projectId: DEFAULT_PROJECT.ref, |
| 71 | features, |
| 72 | readOnly: read_only, |
| 73 | }) |
| 74 | |
| 75 | const transport = new StreamableHTTPServerTransport({ |
| 76 | sessionIdGenerator: undefined, // Stateless, don't use session management |
| 77 | enableJsonResponse: true, // Stateless, discourage SSE streams |
| 78 | }) |
| 79 | |
| 80 | await server.connect(transport) |
| 81 | await transport.handleRequest(req, res, req.body) |
| 82 | } catch (error) { |
| 83 | // Errors at this point will be due MCP setup issues |
| 84 | // Future errors will be handled at the JSON-RPC level within the MCP protocol |
| 85 | if (error instanceof Error) { |
| 86 | return res.status(400).json({ error: error.message }) |
| 87 | } |
| 88 | |
| 89 | return res.status(500).json({ error: 'Unable to process MCP request', cause: error }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | export default handler |