Skip to content

Commit cc19550

Browse files
committed
Refactor: Rename and implement output schema transformation for tools
1 parent 08276b0 commit cc19550

36 files changed

Lines changed: 27 additions & 113 deletions

File tree

typescript/src/modelcontextprotocol/essentials.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {Tool} from '../types/tools';
1414
import {contextToBulkTools} from '../shared/bulk/tools';
1515
import {DYNAMIC_TOOL_LOADING_THRESHOLD} from '../shared/constants';
1616
import {transformToolOutput} from './transform';
17-
import {transformTitle} from './transform/transformToolOutput';
17+
import {transfromOutputSchema} from './transform/transformToolOutput';
1818

1919
class CommercetoolsAgentEssentials extends McpServer {
2020
private authConfig: AuthConfig;
@@ -174,14 +174,7 @@ class CommercetoolsAgentEssentials extends McpServer {
174174
description: tool.description,
175175
inputSchema: tool.parameters.shape,
176176
...(this.configuration.context?.toolOutputFormat === 'json' &&
177-
tool.outputSchema && {
178-
outputSchema: z.object({
179-
[transformTitle(tool.name)]: z.preprocess(
180-
(val) => JSON.parse(val as string),
181-
tool.outputSchema
182-
),
183-
}).shape,
184-
}),
177+
transfromOutputSchema(tool)),
185178
},
186179
async (args: Record<string, unknown>) => {
187180
const result = await this.commercetoolsAPI.run(method, args, execute);

typescript/src/modelcontextprotocol/transform/transformToolOutput.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {Tool} from '../../types/tools';
12
import {transformPropertyName} from './transformPropertyName';
2-
3+
import {z} from 'zod';
34
const emptyObjectTransformValue = 'no properties';
45
const emptyArrayTransformValue = 'none';
56

@@ -13,6 +14,28 @@ const generateTabs = (tabCount: number) => {
1314

1415
type Format = 'tabular' | 'json';
1516

17+
/**
18+
* Transforms the output schema of a tool to a JSON schema.
19+
*
20+
* @param {Tool} tool - The tool to transform the output schema of.
21+
*
22+
* @returns {Object} The transformed output schema.
23+
*/
24+
export const transfromOutputSchema = (tool: Tool) => {
25+
if (!tool.outputSchema) {
26+
return {};
27+
}
28+
29+
return {
30+
outputSchema: z.object({
31+
[transformTitle(tool.name)]: z.preprocess(
32+
(val) => JSON.parse(val as string),
33+
tool.outputSchema
34+
),
35+
}).shape,
36+
};
37+
};
38+
1639
/**
1740
* A method to strigify tool output into a LLM friendly and optimised format.
1841
*
@@ -58,7 +81,7 @@ export const transformToolOutput = (args: {
5881
transformedData ?? emptyObjectTransformValue);
5982
};
6083

61-
export const transformTitle = (title: string) =>
84+
const transformTitle = (title: string) =>
6285
`${transformPropertyName(title).toUpperCase()}`;
6386

6487
/**

typescript/src/shared/business-unit/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for business-unit
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base BusinessUnit schema
74
export const businessUnitSchema = z.union([z.any(), z.any()]);
85

typescript/src/shared/cart-discount/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for cart-discount
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base CartDiscount schema
74
export const cartDiscountSchema = z
85
.object({

typescript/src/shared/cart/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for cart
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base Cart schema
74
export const cartSchema = z
85
.object({

typescript/src/shared/category/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for category
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base Category schema
74
export const categorySchema = z
85
.object({

typescript/src/shared/channel/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for channel
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base Channel schema
74
export const channelSchema = z
85
.object({

typescript/src/shared/custom-objects/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for custom-objects
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base CustomObject schema
74
export const customObjectsSchema = z
85
.object({

typescript/src/shared/customer-group/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for customer-group
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base CustomerGroup schema
74
export const customerGroupSchema = z
85
.object({

typescript/src/shared/customer/output-schema.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import {z} from 'zod';
22

3-
// Generated schemas for customer
4-
// DO NOT EDIT - This file is auto-generated
5-
63
// Base Customer schema
74
export const customerSchema = z
85
.object({

0 commit comments

Comments
 (0)