Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mcp-server-supabase/src/content-api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class GraphQLClient {
*/
async query(
request: GraphQLRequest,
options: QueryOptions = { validateSchema: true }
options: QueryOptions = { validateSchema: false }
) {
try {
// Check that this is a valid GraphQL query
Expand Down
15 changes: 6 additions & 9 deletions packages/mcp-server-supabase/src/content-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const contentApiSchemaResponseSchema = z.object({
});

export type ContentApiClient = {
schema: string;
loadSchema: () => Promise<string>;
query: QueryFn;
setUserAgent: (userAgent: string) => void;
};
Expand All @@ -18,18 +18,15 @@ export async function createContentApiClient(
const graphqlClient = new GraphQLClient({
url,
headers,
});

return {
// Content API provides schema string via `schema` query
loadSchema: async ({ query }) => {
const response = await query({ query: '{ schema }' });
loadSchema: async () => {
const response = await graphqlClient.query({ query: '{ schema }' });
const { schema } = contentApiSchemaResponseSchema.parse(response);
return schema;
},
});

const { source } = await graphqlClient.schemaLoaded;

return {
schema: source,
async query(request: GraphQLRequest) {
return graphqlClient.query(request);
},
Expand Down
25 changes: 25 additions & 0 deletions packages/mcp-server-supabase/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ACCESS_TOKEN,
API_URL,
contentApiMockSchema,
mockContentApiSchemaLoadCount,
createOrganization,
createProject,
createBranch,
Expand All @@ -31,6 +32,7 @@ beforeEach(async () => {
mockOrgs.clear();
mockProjects.clear();
mockBranches.clear();
mockContentApiSchemaLoadCount.value = 0;

const server = setupServer(...mockContentApi, ...mockManagementApi);
server.listen({ onUnhandledRequest: 'error' });
Expand Down Expand Up @@ -2943,4 +2945,27 @@ describe('docs tools', () => {

expect(tool.description.includes(contentApiMockSchema)).toBe(true);
});

test('schema is only loaded when listing tools', async () => {
const { client, callTool } = await setup();

expect(mockContentApiSchemaLoadCount.value).toBe(0);

// "tools/list" requests fetch the schema
await client.listTools();
expect(mockContentApiSchemaLoadCount.value).toBe(1);

// "tools/call" should not fetch the schema again
await callTool({
name: 'search_docs',
arguments: {
graphql_query: '{ searchDocs(query: "test") { nodes { title } } }',
},
});
expect(mockContentApiSchemaLoadCount.value).toBe(1);

// Additional "tools/list" requests fetch the schema again
await client.listTools();
expect(mockContentApiSchemaLoadCount.value).toBe(2);
});
});
16 changes: 9 additions & 7 deletions packages/mcp-server-supabase/src/tools/docs-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export type DocsToolsOptions = {
export function getDocsTools({ contentApiClient }: DocsToolsOptions) {
return {
search_docs: tool({
description: source`
Search the Supabase documentation using GraphQL. Must be a valid GraphQL query.
description: async () => {
const schema = await contentApiClient.loadSchema();

You should default to calling this even if you think you already know the answer, since the documentation is always being updated.

Below is the GraphQL schema for the Supabase docs endpoint:
${contentApiClient.schema}
`,
return source`
Search the Supabase documentation using GraphQL. Must be a valid GraphQL query.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related with this PR but I've noticed that this tool is only called when I start my prompts with "How can I... or How do I..." (at least for me). Sometimes, if it bumps into any problem while calling other tool, the LLM will search for the docs online instead of calling this tool.

If anyone else is experiencing this, my suggestion is:

  1. We write explicitly in the tool description "If you face any issue while calling other Supabase tool, use the search_docs tool to get the most up-to-date documentation to help you debug"
  2. We put this disclaimer on every tool description

I prefer the first one because we only have to maintain this instruction once.

PS: I would only change this behavior once we have evals in place

You should default to calling this even if you think you already know the answer, since the documentation is always being updated.
Below is the GraphQL schema for the Supabase docs endpoint:
${schema}
`;
},
annotations: {
title: 'Search docs',
readOnlyHint: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/mcp-server-supabase/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export const mockOrgs = new Map<string, Organization>();
export const mockProjects = new Map<string, MockProject>();
export const mockBranches = new Map<string, MockBranch>();

export const mockContentApiSchemaLoadCount = { value: 0 };

export const mockContentApi = [
http.post(CONTENT_API_URL, async ({ request }) => {
const json = await request.json();
Expand All @@ -96,6 +98,7 @@ export const mockContentApi = [
const [queryName] = getQueryFields(document);

if (queryName === 'schema') {
mockContentApiSchemaLoadCount.value++;
return HttpResponse.json({
data: {
schema: contentApiMockSchema,
Expand Down
39 changes: 22 additions & 17 deletions packages/mcp-utils/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type Tool<
Params extends z.ZodObject<any> = z.ZodObject<any>,
Result = unknown,
> = {
description: string;
description: Prop<string>;
annotations?: Annotations;
parameters: Params;
execute(params: z.infer<Params>): Promise<Result>;
Expand Down Expand Up @@ -436,24 +436,30 @@ export function createMcpServer(options: McpServerOptions) {
ListToolsRequestSchema,
async (): Promise<ListToolsResult> => {
const tools = await getTools();
return {
tools: Object.entries(tools).map(
([name, { description, annotations, parameters }]) => {
const inputSchema = zodToJsonSchema(parameters);

if (!('properties' in inputSchema)) {
throw new Error('tool parameters must be a ZodObject');
return {
tools: await Promise.all(
Object.entries(tools).map(
async ([name, { description, annotations, parameters }]) => {
const inputSchema = zodToJsonSchema(parameters);

if (!('properties' in inputSchema)) {
throw new Error('tool parameters must be a ZodObject');
}

return {
name,
description:
typeof description === 'function'
? await description()
: description,
annotations,
inputSchema,
};
}

return {
name,
description,
annotations,
inputSchema,
};
}
)
),
};
} satisfies ListToolsResult;
}
);

Expand All @@ -471,7 +477,6 @@ export function createMcpServer(options: McpServerOptions) {
if (!tool) {
throw new Error('tool not found');
}

const args = tool.parameters
.strict()
.parse(request.params.arguments ?? {});
Expand Down