|
| 1 | +import { |
| 2 | + Client, |
| 3 | + StreamableHTTPClientTransport, |
| 4 | +} from '@modelcontextprotocol/client'; |
| 5 | +import { |
| 6 | + CLIENT_CAPABILITIES_META_KEY, |
| 7 | + PROTOCOL_VERSION_META_KEY, |
| 8 | +} from '@modelcontextprotocol/server'; |
| 9 | +import { http, HttpResponse } from 'msw'; |
| 10 | +import type { SetupServer } from 'msw/node'; |
| 11 | +import { afterEach, beforeEach, describe, expect, test } from 'vitest'; |
| 12 | + |
| 13 | +import { |
| 14 | + ACCESS_TOKEN, |
| 15 | + API_URL, |
| 16 | + MCP_CLIENT_NAME, |
| 17 | + MCP_CLIENT_VERSION, |
| 18 | + setupMockApis, |
| 19 | +} from '../../test/mocks.js'; |
| 20 | +import { createSupabaseApiPlatform } from '../platform/api-platform.js'; |
| 21 | +import { createSupabaseMcpHandler } from './http.js'; |
| 22 | + |
| 23 | +// https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/ |
| 24 | +const MODERN_PROTOCOL_VERSION = '2026-07-28'; |
| 25 | +const MCP_ENDPOINT = new URL('https://mcp.test'); |
| 26 | + |
| 27 | +let mockServer!: SetupServer; |
| 28 | +const cleanups: Array<() => Promise<void>> = []; |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + mockServer = setupMockApis(); |
| 32 | +}); |
| 33 | + |
| 34 | +afterEach(async () => { |
| 35 | + try { |
| 36 | + for (const cleanup of cleanups.splice(0).reverse()) { |
| 37 | + await cleanup(); |
| 38 | + } |
| 39 | + } finally { |
| 40 | + mockServer.close(); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +function createHandler() { |
| 45 | + const handler = createSupabaseMcpHandler({ |
| 46 | + platform: createSupabaseApiPlatform({ |
| 47 | + accessToken: ACCESS_TOKEN, |
| 48 | + apiUrl: API_URL, |
| 49 | + }), |
| 50 | + readOnly: true, |
| 51 | + }); |
| 52 | + |
| 53 | + cleanups.push(() => handler.close()); |
| 54 | + |
| 55 | + return handler; |
| 56 | +} |
| 57 | + |
| 58 | +async function setupModernClient() { |
| 59 | + const handler = createHandler(); |
| 60 | + const transport = new StreamableHTTPClientTransport(MCP_ENDPOINT, { |
| 61 | + fetch: (url, init) => handler.fetch(new Request(url, init)), |
| 62 | + }); |
| 63 | + const client = new Client( |
| 64 | + { |
| 65 | + name: MCP_CLIENT_NAME, |
| 66 | + version: MCP_CLIENT_VERSION, |
| 67 | + }, |
| 68 | + { |
| 69 | + capabilities: {}, |
| 70 | + versionNegotiation: { |
| 71 | + mode: { pin: MODERN_PROTOCOL_VERSION }, |
| 72 | + }, |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + await client.connect(transport); |
| 77 | + cleanups.push(() => client.close()); |
| 78 | + |
| 79 | + return { client, handler }; |
| 80 | +} |
| 81 | + |
| 82 | +function jsonRequest(body: unknown) { |
| 83 | + return new Request(MCP_ENDPOINT, { |
| 84 | + method: 'POST', |
| 85 | + headers: { 'content-type': 'application/json' }, |
| 86 | + body: JSON.stringify(body), |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +function deferred() { |
| 91 | + let resolve!: () => void; |
| 92 | + const promise = new Promise<void>((resolvePromise) => { |
| 93 | + resolve = resolvePromise; |
| 94 | + }); |
| 95 | + |
| 96 | + return { promise, resolve }; |
| 97 | +} |
| 98 | + |
| 99 | +describe('createSupabaseMcpHandler', () => { |
| 100 | + test('serves discovery and tools/list to a client pinned to 2026-07-28', async () => { |
| 101 | + const { client } = await setupModernClient(); |
| 102 | + |
| 103 | + const { tools } = await client.listTools(); |
| 104 | + |
| 105 | + expect(client.getProtocolEra()).toBe('modern'); |
| 106 | + expect(client.getNegotiatedProtocolVersion()).toBe(MODERN_PROTOCOL_VERSION); |
| 107 | + expect(client.getDiscoverResult()?.supportedVersions).toContain( |
| 108 | + MODERN_PROTOCOL_VERSION |
| 109 | + ); |
| 110 | + expect(tools.map((tool) => tool.name)).toContain('list_projects'); |
| 111 | + }); |
| 112 | + |
| 113 | + test('calls the same registered read-only business tool', async () => { |
| 114 | + const { client } = await setupModernClient(); |
| 115 | + |
| 116 | + const result = await client.callTool({ |
| 117 | + name: 'search_docs', |
| 118 | + arguments: { |
| 119 | + graphql_query: |
| 120 | + '{ searchDocs(query: "typescript") { nodes { title href } } }', |
| 121 | + }, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result.isError).not.toBe(true); |
| 125 | + expect(result.content).toEqual([ |
| 126 | + { |
| 127 | + type: 'text', |
| 128 | + text: JSON.stringify({ result: { dummy: true } }), |
| 129 | + }, |
| 130 | + ]); |
| 131 | + }); |
| 132 | + |
| 133 | + test('rejects a claim-less legacy request', async () => { |
| 134 | + const handler = createHandler(); |
| 135 | + |
| 136 | + const response = await handler.fetch( |
| 137 | + jsonRequest({ |
| 138 | + jsonrpc: '2.0', |
| 139 | + id: 1, |
| 140 | + method: 'tools/list', |
| 141 | + params: {}, |
| 142 | + }) |
| 143 | + ); |
| 144 | + |
| 145 | + expect(response.status).toBe(400); |
| 146 | + await expect(response.json()).resolves.toMatchObject({ |
| 147 | + jsonrpc: '2.0', |
| 148 | + id: 1, |
| 149 | + error: { |
| 150 | + code: -32022, |
| 151 | + data: { supported: [MODERN_PROTOCOL_VERSION] }, |
| 152 | + }, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + test('returns a modern validation error for a malformed claimed envelope', async () => { |
| 157 | + const handler = createHandler(); |
| 158 | + |
| 159 | + const response = await handler.fetch( |
| 160 | + jsonRequest({ |
| 161 | + jsonrpc: '2.0', |
| 162 | + id: 2, |
| 163 | + method: 'tools/list', |
| 164 | + params: { |
| 165 | + _meta: { |
| 166 | + [PROTOCOL_VERSION_META_KEY]: MODERN_PROTOCOL_VERSION, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }) |
| 170 | + ); |
| 171 | + |
| 172 | + expect(response.status).toBe(400); |
| 173 | + await expect(response.json()).resolves.toMatchObject({ |
| 174 | + jsonrpc: '2.0', |
| 175 | + id: 2, |
| 176 | + error: { |
| 177 | + code: -32602, |
| 178 | + data: { |
| 179 | + envelope: { |
| 180 | + key: CLIENT_CAPABILITIES_META_KEY, |
| 181 | + problem: 'missing', |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + test('close releases an in-flight request', async () => { |
| 189 | + const requestStarted = deferred(); |
| 190 | + const releaseRequest = deferred(); |
| 191 | + mockServer.use( |
| 192 | + http.get(`${API_URL}/v1/projects`, async () => { |
| 193 | + requestStarted.resolve(); |
| 194 | + await releaseRequest.promise; |
| 195 | + return HttpResponse.json([]); |
| 196 | + }) |
| 197 | + ); |
| 198 | + const { client, handler } = await setupModernClient(); |
| 199 | + const callOutcome = client |
| 200 | + .callTool({ name: 'list_projects', arguments: {} }) |
| 201 | + .then( |
| 202 | + () => ({ status: 'resolved' as const }), |
| 203 | + (error: unknown) => ({ status: 'rejected' as const, error }) |
| 204 | + ); |
| 205 | + |
| 206 | + try { |
| 207 | + await requestStarted.promise; |
| 208 | + await handler.close(); |
| 209 | + |
| 210 | + await expect(callOutcome).resolves.toMatchObject({ |
| 211 | + status: 'rejected', |
| 212 | + }); |
| 213 | + } finally { |
| 214 | + releaseRequest.resolve(); |
| 215 | + } |
| 216 | + }); |
| 217 | +}); |
0 commit comments