Skip to content

Commit b6fb8e2

Browse files
feat(mcp): support filtering tool results by a jq expression
1 parent d14a6a0 commit b6fb8e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+306
-71
lines changed

packages/mcp-server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dependencies": {
3030
"@flowglad/node": "file:../../dist/",
3131
"@modelcontextprotocol/sdk": "^1.11.5",
32+
"jq-web": "^0.6.2",
3233
"yargs": "^17.7.2",
3334
"@cloudflare/cabidela": "^0.2.4",
3435
"zod": "^3.25.20",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export async function maybeFilter(args: Record<string, unknown> | undefined, response: any): Promise<any> {
2+
const jqFilter = args?.['jq_filter'];
3+
if (jqFilter && typeof jqFilter === 'string') {
4+
return await jq(response, jqFilter);
5+
} else {
6+
return response;
7+
}
8+
}
9+
10+
var jqWeb = require('jq-web');
11+
async function jq(json: any, jqFilter: string) {
12+
return (await jqWeb).json(json, jqFilter);
13+
}

packages/mcp-server/src/tools/catalogs/clone-catalogs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const metadata: Metadata = {
1717

1818
export const tool: Tool = {
1919
name: 'clone_catalogs',
20-
description: 'Clone a Catalog',
20+
description:
21+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nClone a Catalog",
2122
inputSchema: {
2223
type: 'object',
2324
properties: {

packages/mcp-server/src/tools/catalogs/create-catalogs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { maybeFilter } from '@flowglad/node-mcp/filtering';
34
import { asTextContentResult } from '@flowglad/node-mcp/tools/types';
45

56
import { Tool } from '@modelcontextprotocol/sdk/types.js';
@@ -17,7 +18,8 @@ export const metadata: Metadata = {
1718

1819
export const tool: Tool = {
1920
name: 'create_catalogs',
20-
description: 'Create Catalog',
21+
description:
22+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nCreate Catalog\n\n# Response Schema\n```json\n{\n type: 'object',\n properties: {\n catalog: {\n type: 'object',\n properties: {\n id: {\n type: 'string'\n },\n createdAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n createdByCommit: {\n type: 'string'\n },\n isDefault: {\n type: 'boolean'\n },\n livemode: {\n type: 'boolean'\n },\n name: {\n type: 'string'\n },\n organizationId: {\n type: 'string'\n },\n updatedAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n updatedByCommit: {\n type: 'string'\n }\n },\n required: [ 'id',\n 'createdAt',\n 'createdByCommit',\n 'isDefault',\n 'livemode',\n 'name',\n 'organizationId',\n 'updatedAt',\n 'updatedByCommit'\n ]\n }\n },\n required: [ 'catalog'\n ]\n}\n```",
2123
inputSchema: {
2224
type: 'object',
2325
properties: {
@@ -33,13 +35,19 @@ export const tool: Tool = {
3335
},
3436
required: ['name'],
3537
},
38+
jq_filter: {
39+
type: 'string',
40+
title: 'jq Filter',
41+
description:
42+
'A jq filter to apply to the response to include certain fields. Consult the output schema in the tool description to see the fields that are available.\n\nFor example: to include only the `name` field in every object of a results array, you can provide ".results[].name".\n\nFor more information, see the [jq documentation](https://jqlang.org/manual/).',
43+
},
3644
},
3745
},
3846
};
3947

4048
export const handler = async (client: Flowglad, args: Record<string, unknown> | undefined) => {
4149
const body = args as any;
42-
return asTextContentResult(await client.catalogs.create(body));
50+
return asTextContentResult(await maybeFilter(args, await client.catalogs.create(body)));
4351
};
4452

4553
export default { metadata, tool, handler };

packages/mcp-server/src/tools/catalogs/list-catalogs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { maybeFilter } from '@flowglad/node-mcp/filtering';
34
import { asTextContentResult } from '@flowglad/node-mcp/tools/types';
45

56
import { Tool } from '@modelcontextprotocol/sdk/types.js';
@@ -17,7 +18,8 @@ export const metadata: Metadata = {
1718

1819
export const tool: Tool = {
1920
name: 'list_catalogs',
20-
description: 'List Catalogs',
21+
description:
22+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nList Catalogs\n\n# Response Schema\n```json\n{\n type: 'object',\n properties: {\n data: {\n type: 'array',\n items: {\n type: 'object',\n properties: {\n id: {\n type: 'string'\n },\n createdAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n createdByCommit: {\n type: 'string'\n },\n isDefault: {\n type: 'boolean'\n },\n livemode: {\n type: 'boolean'\n },\n name: {\n type: 'string'\n },\n organizationId: {\n type: 'string'\n },\n updatedAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n updatedByCommit: {\n type: 'string'\n }\n },\n required: [ 'id',\n 'createdAt',\n 'createdByCommit',\n 'isDefault',\n 'livemode',\n 'name',\n 'organizationId',\n 'updatedAt',\n 'updatedByCommit'\n ]\n }\n },\n hasMore: {\n type: 'boolean'\n },\n total: {\n type: 'number'\n },\n currentCursor: {\n type: 'string'\n },\n nextCursor: {\n type: 'string'\n }\n },\n required: [ 'data',\n 'hasMore',\n 'total'\n ]\n}\n```",
2123
inputSchema: {
2224
type: 'object',
2325
properties: {
@@ -27,13 +29,19 @@ export const tool: Tool = {
2729
limit: {
2830
type: 'number',
2931
},
32+
jq_filter: {
33+
type: 'string',
34+
title: 'jq Filter',
35+
description:
36+
'A jq filter to apply to the response to include certain fields. Consult the output schema in the tool description to see the fields that are available.\n\nFor example: to include only the `name` field in every object of a results array, you can provide ".results[].name".\n\nFor more information, see the [jq documentation](https://jqlang.org/manual/).',
37+
},
3038
},
3139
},
3240
};
3341

3442
export const handler = async (client: Flowglad, args: Record<string, unknown> | undefined) => {
3543
const body = args as any;
36-
return asTextContentResult(await client.catalogs.list(body));
44+
return asTextContentResult(await maybeFilter(args, await client.catalogs.list(body)));
3745
};
3846

3947
export default { metadata, tool, handler };

packages/mcp-server/src/tools/catalogs/retrieve-catalogs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const metadata: Metadata = {
1717

1818
export const tool: Tool = {
1919
name: 'retrieve_catalogs',
20-
description: 'Get Catalog',
20+
description:
21+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nGet Catalog",
2122
inputSchema: {
2223
type: 'object',
2324
properties: {

packages/mcp-server/src/tools/catalogs/retrieve-default-catalogs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const metadata: Metadata = {
1717

1818
export const tool: Tool = {
1919
name: 'retrieve_default_catalogs',
20-
description: 'Get Default Catalog for Organization',
20+
description:
21+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nGet Default Catalog for Organization",
2122
inputSchema: {
2223
type: 'object',
2324
properties: {},

packages/mcp-server/src/tools/catalogs/update-catalogs.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
import { maybeFilter } from '@flowglad/node-mcp/filtering';
34
import { asTextContentResult } from '@flowglad/node-mcp/tools/types';
45

56
import { Tool } from '@modelcontextprotocol/sdk/types.js';
@@ -17,7 +18,8 @@ export const metadata: Metadata = {
1718

1819
export const tool: Tool = {
1920
name: 'update_catalogs',
20-
description: 'Update Catalog',
21+
description:
22+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nUpdate Catalog\n\n# Response Schema\n```json\n{\n type: 'object',\n properties: {\n catalog: {\n type: 'object',\n properties: {\n id: {\n type: 'string'\n },\n createdAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n createdByCommit: {\n type: 'string'\n },\n isDefault: {\n type: 'boolean'\n },\n livemode: {\n type: 'boolean'\n },\n name: {\n type: 'string'\n },\n organizationId: {\n type: 'string'\n },\n updatedAt: {\n anyOf: [ {\n type: 'string',\n format: 'date-time'\n },\n {\n type: 'string'\n }\n ],\n description: 'safeZodDate'\n },\n updatedByCommit: {\n type: 'string'\n }\n },\n required: [ 'id',\n 'createdAt',\n 'createdByCommit',\n 'isDefault',\n 'livemode',\n 'name',\n 'organizationId',\n 'updatedAt',\n 'updatedByCommit'\n ]\n }\n },\n required: [ 'catalog'\n ]\n}\n```",
2123
inputSchema: {
2224
type: 'object',
2325
properties: {
@@ -39,13 +41,19 @@ export const tool: Tool = {
3941
},
4042
required: ['id'],
4143
},
44+
jq_filter: {
45+
type: 'string',
46+
title: 'jq Filter',
47+
description:
48+
'A jq filter to apply to the response to include certain fields. Consult the output schema in the tool description to see the fields that are available.\n\nFor example: to include only the `name` field in every object of a results array, you can provide ".results[].name".\n\nFor more information, see the [jq documentation](https://jqlang.org/manual/).',
49+
},
4250
},
4351
},
4452
};
4553

4654
export const handler = async (client: Flowglad, args: Record<string, unknown> | undefined) => {
4755
const { id, ...body } = args as any;
48-
return asTextContentResult(await client.catalogs.update(id, body));
56+
return asTextContentResult(await maybeFilter(args, await client.catalogs.update(id, body)));
4957
};
5058

5159
export default { metadata, tool, handler };

packages/mcp-server/src/tools/checkout-sessions/create-checkout-sessions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const metadata: Metadata = {
1717

1818
export const tool: Tool = {
1919
name: 'create_checkout_sessions',
20-
description: 'Create Checkout Session',
20+
description:
21+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nCreate Checkout Session",
2122
inputSchema: {
2223
type: 'object',
2324
properties: {

packages/mcp-server/src/tools/checkout-sessions/list-checkout-sessions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export const metadata: Metadata = {
1717

1818
export const tool: Tool = {
1919
name: 'list_checkout_sessions',
20-
description: 'List Checkout Sessions',
20+
description:
21+
"When using this tool, always use the `jq_filter` parameter to reduce the response size and improve performance.\n\nOnly omit if you're sure you don't need the data.\n\nList Checkout Sessions",
2122
inputSchema: {
2223
type: 'object',
2324
properties: {

0 commit comments

Comments
 (0)