Skip to content

Commit 48834b6

Browse files
committed
fix: eszip module resolution during bundling for tests
1 parent e8f725d commit 48834b6

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

packages/mcp-server-supabase/src/edge-function.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { codeBlock } from 'common-tags';
2+
13
/**
24
* Gets the deployment ID for an Edge Function.
35
*/
@@ -15,3 +17,20 @@ export function getDeploymentId(
1517
export function getPathPrefix(deploymentId: string) {
1618
return `/tmp/user_fn_${deploymentId}/`;
1719
}
20+
21+
export const edgeFunctionExample = codeBlock`
22+
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
23+
24+
Deno.serve(async (req: Request) => {
25+
const data = {
26+
message: "Hello there!"
27+
};
28+
29+
return new Response(JSON.stringify(data), {
30+
headers: {
31+
'Content-Type': 'application/json',
32+
'Connection': 'keep-alive'
33+
}
34+
});
35+
});
36+
`;

packages/mcp-server-supabase/src/eszip.ts

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,59 @@ export async function bundleFiles(files: File[], pathPrefix: string = '/') {
8484
(file) => `file://${join(pathPrefix, file.name)}`
8585
);
8686
const eszip = await build(specifiers, async (specifier: string) => {
87-
if (specifier.startsWith('file://')) {
88-
const file = files.find(
89-
(file) => `file://${join(pathPrefix, file.name)}` === specifier
90-
);
87+
const url = new URL(specifier);
88+
const scheme = url.protocol;
9189

92-
if (!file) {
93-
throw new Error(`File not found: ${specifier}`);
94-
}
90+
switch (scheme) {
91+
case 'file:': {
92+
const file = files.find(
93+
(file) => `file://${join(pathPrefix, file.name)}` === specifier
94+
);
95+
96+
if (!file) {
97+
throw new Error(`File not found: ${specifier}`);
98+
}
9599

96-
return {
97-
kind: 'module',
98-
specifier,
99-
headers: {
100+
const headers = {
100101
'content-type': file.type,
101-
},
102-
content: await file.text(),
103-
};
102+
};
103+
104+
const content = await file.text();
105+
106+
return {
107+
kind: 'module',
108+
specifier,
109+
headers,
110+
content,
111+
};
112+
}
113+
case 'http:':
114+
case 'https:': {
115+
const response = await fetch(specifier);
116+
if (!response.ok) {
117+
throw new Error(`Failed to fetch ${specifier}: ${response.status}`);
118+
}
119+
120+
// Header keys must be lower case
121+
const headers = Object.fromEntries(
122+
Array.from(response.headers.entries()).map(([key, value]) => [
123+
key.toLowerCase(),
124+
value,
125+
])
126+
);
127+
128+
const content = await response.text();
129+
130+
return {
131+
kind: 'module',
132+
specifier,
133+
headers,
134+
content,
135+
};
136+
}
137+
default: {
138+
throw new Error(`Unsupported scheme: ${scheme}`);
139+
}
104140
}
105141
});
106142

packages/mcp-server-supabase/src/server.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { createMcpServer, tool } from '@supabase/mcp-utils';
22
import { fileURLToPath } from 'node:url';
33
import { z } from 'zod';
44
import packageJson from '../package.json' with { type: 'json' };
5-
import { getDeploymentId, getPathPrefix } from './edge-function.js';
5+
import {
6+
edgeFunctionExample,
7+
getDeploymentId,
8+
getPathPrefix,
9+
} from './edge-function.js';
610
import { extractFiles } from './eszip.js';
711
import { getLogQuery } from './logs.js';
812
import {
@@ -465,8 +469,7 @@ export function createSupabaseMcpServer(options: SupabaseMcpServerOptions) {
465469
},
466470
}),
467471
deploy_edge_function: tool({
468-
description:
469-
'Deploys an Edge Function to a Supabase project. If the function already exists, this will create a new version.',
472+
description: `Deploys an Edge Function to a Supabase project. If the function already exists, this will create a new version. Example:\n\n${edgeFunctionExample}`,
470473
parameters: z.object({
471474
project_id: z.string(),
472475
name: z.string().describe('The name of the function'),

packages/mcp-server-supabase/test/llm.e2e.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ describe('llm tests', () => {
8989
.sql`create table inventory (id serial, name text)`;
9090

9191
const toolCalls: ToolCallUnion<ToolSet>[] = [];
92+
const tools = await client.tools();
9293

9394
const { text } = await generateText({
9495
model,
95-
tools: await client.tools(),
96+
tools,
9697
messages: [
9798
{
9899
role: 'system',
@@ -104,7 +105,7 @@ describe('llm tests', () => {
104105
content: 'What tables do I have?',
105106
},
106107
],
107-
maxSteps: 2,
108+
maxSteps: 3,
108109
async onStepFinish({ toolCalls: tools }) {
109110
toolCalls.push(...tools);
110111
},
@@ -140,10 +141,11 @@ describe('llm tests', () => {
140141
});
141142

142143
const toolCalls: ToolCallUnion<ToolSet>[] = [];
144+
const tools = await client.tools();
143145

144146
const { text } = await generateText({
145147
model,
146-
tools: await client.tools(),
148+
tools,
147149
messages: [
148150
{
149151
role: 'system',
@@ -156,7 +158,7 @@ describe('llm tests', () => {
156158
},
157159
],
158160
maxSteps: 2,
159-
async onStepFinish({ text, toolCalls: tools, toolResults }) {
161+
async onStepFinish({ toolCalls: tools }) {
160162
toolCalls.push(...tools);
161163
},
162164
});
@@ -206,10 +208,11 @@ describe('llm tests', () => {
206208
);
207209

208210
const toolCalls: ToolCallUnion<ToolSet>[] = [];
211+
const tools = await client.tools();
209212

210213
const { text } = await generateText({
211214
model,
212-
tools: await client.tools(),
215+
tools,
213216
messages: [
214217
{
215218
role: 'system',
@@ -222,11 +225,8 @@ describe('llm tests', () => {
222225
},
223226
],
224227
maxSteps: 3,
225-
async onStepFinish({ text, toolCalls: tools, toolResults }) {
228+
async onStepFinish({ toolCalls: tools }) {
226229
toolCalls.push(...tools);
227-
console.dir(text, { depth: null });
228-
console.dir(tools, { depth: null });
229-
console.dir(toolResults, { depth: null });
230230
},
231231
});
232232

0 commit comments

Comments
 (0)