Skip to content

Commit f6d0461

Browse files
committed
chore: changed overlooked agent/mcp essentials
1 parent 8283d97 commit f6d0461

12 files changed

Lines changed: 267 additions & 259 deletions

File tree

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"changelog": [
44
"@changesets/changelog-github",
55
{
6-
"repo": "commercetools/mcp-essentials"
6+
"repo": "commercetools/commerce-mcp"
77
}
88
],
99
"commit": false,

.cursor/rules/docs.mdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ globs:
44
alwaysApply: false
55
---
66

7-
# Creating a New namespace tool Function in @commercetools/agent-essentials
7+
# Creating a New namespace tool Function in @commercetools/commerce-agent
88

99
# Updating the README.md file in the project root with the newly added feature or namespaces
1010

Lines changed: 82 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,114 @@
11
---
2-
description: Refactoring a tool Function in @commercetools/agent-essentials
3-
globs:
2+
description: Refactoring a tool Function in @commercetools/commerce-agent
3+
globs:
44
alwaysApply: false
55
---
6-
# Refactoring a tool Function in @commercetools/agent-essentials
76

8-
This document outlines the steps taken to refactor a function in @commercetools/agent-essentials to connect to commercetools API.
7+
# Refactoring a tool Function in @commercetools/commerce-agent
8+
9+
This document outlines the steps taken to refactor a function in @commercetools/commerce-agent to connect to commercetools API.
910

1011
The following steps are defining implementation of an example `cart.read`. So the `(namespace)` here refers to `cart`.
1112

12-
# NOTES:
13+
# NOTES:
14+
1315
1. CRUD functions are read, update and create. do not add delete functions.
1416
2. DO NOT MODIFY `parameters.ts` or `prompts.ts` or `tools.ts`
1517
3. when creating update base functions, use get (by id or key) base functions to first fetch the entity, and use the version from the fetched entity in the update payload
1618

1719
# Refactor steps
1820

1921
1. Create a `base.functions.ts` file in the `namespace` directory. This file will export a couple of basic CRUD functions that will be used inside other files in this namespace. you can extract this base functions from current `functions.ts` file in the namespace.
20-
- IMPORTANT: base functions should be generic as possible, e.g. instead of having a separate function for query cart for a specific user and another one for user in a store, we create one that accepts a "where" cluase.
21-
- Note: sometimes the commercetools SDK has different endpoint when a parameter is present, like query in store (look to the code sample below). in that case, we check that parameter inside the base function but keep the function as simple as possible and low complexity.
22-
- GOAL: the goal of this step is maximize reusability.
23-
- example: base functions created for 'cart.read' are: readCartById, readCartByKey, queryCart, queryCarts.
24-
- file example: `typescript/src/shared/cart/base.functions.ts`
25-
- code sample:
22+
- IMPORTANT: base functions should be generic as possible, e.g. instead of having a separate function for query cart for a specific user and another one for user in a store, we create one that accepts a "where" cluase.
23+
- Note: sometimes the commercetools SDK has different endpoint when a parameter is present, like query in store (look to the code sample below). in that case, we check that parameter inside the base function but keep the function as simple as possible and low complexity.
24+
- GOAL: the goal of this step is maximize reusability.
25+
- example: base functions created for 'cart.read' are: readCartById, readCartByKey, queryCart, queryCarts.
26+
- file example: `typescript/src/shared/cart/base.functions.ts`
27+
- code sample:
2628

27-
```ts
28-
const queryCart = async (
29-
apiRoot: ApiRoot,
30-
projectKey: string,
31-
queryArgs: any,
32-
storeKey?: string
33-
) => {
34-
if (storeKey) {
35-
const carts = await apiRoot
36-
.withProjectKey({projectKey})
37-
.inStoreKeyWithStoreKeyValue({storeKey})
38-
.carts()
39-
.get({queryArgs})
40-
.execute();
41-
return carts.body;
42-
}
43-
const carts = await apiRoot
44-
.withProjectKey({projectKey})
45-
.carts()
46-
.get({queryArgs})
47-
.execute();
48-
return carts.body;
49-
};
50-
```
29+
```ts
30+
const queryCart = async (
31+
apiRoot: ApiRoot,
32+
projectKey: string,
33+
queryArgs: any,
34+
storeKey?: string
35+
) => {
36+
if (storeKey) {
37+
const carts = await apiRoot
38+
.withProjectKey({ projectKey })
39+
.inStoreKeyWithStoreKeyValue({ storeKey })
40+
.carts()
41+
.get({ queryArgs })
42+
.execute();
43+
return carts.body;
44+
}
45+
const carts = await apiRoot
46+
.withProjectKey({ projectKey })
47+
.carts()
48+
.get({ queryArgs })
49+
.execute();
50+
return carts.body;
51+
};
52+
```
5153

5254
2. Create a `customer.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.customerId` is present.
53-
- GOAL: these functions are to limit the operations to the specific customerId.
54-
- example: when querying carts, it should always inject `context.customerId` to the query. If not possible, it should check the entity after it's fetched.
55-
- file example: `typescript/src/shared/cart/customer.functions.ts`
55+
- GOAL: these functions are to limit the operations to the specific customerId.
56+
- example: when querying carts, it should always inject `context.customerId` to the query. If not possible, it should check the entity after it's fetched.
57+
- file example: `typescript/src/shared/cart/customer.functions.ts`
5658

5759
3. Create a `store.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.storeKey` is present.
58-
- GOAL: these functions are to limit the operations to the specific store.
59-
- example: Limit carts fetched to a store.
60+
- GOAL: these functions are to limit the operations to the specific store.
61+
- example: Limit carts fetched to a store.
6062

6163
4. Create a `admin.functions.ts` in the namespace directory. This file, uses `base.functions.ts`. it has CRUD functions when `context.isAdmin` is present.
62-
- GOAL: these functions doesn't have any limitations.
63-
- file example: `typescript/src/shared/cart/admin.functions.ts`
64+
- GOAL: these functions doesn't have any limitations.
65+
- file example: `typescript/src/shared/cart/admin.functions.ts`
6466

6567
5. Modify `functions.ts` to import all exported methods from customer, admin and store. create a method called `contextTo<namespace>FunctionMapping` which accepts the context and returns an object of name to method mapping.
66-
- IMPORTANT: if no context is there, empty object should return
67-
- IMPORTANT: use type `Context` from `typescript/src/types/configuration.ts`
68-
- example:
69-
```ts
70-
export const contextToCartFunctionMapping = (context?: Context) => {
71-
if (context?.customerId) {
72-
return {
73-
read_cart: customer.readCart,
74-
// create_cart: customer.createCart,
75-
// update_cart: customer.updateCart,
76-
// replicate_cart: customer.replicateCart,
77-
};
78-
}
79-
if (context?.storeKey) {
80-
return {
81-
read_cart: store.readCart,
82-
// create_cart: store.createCart,
83-
// update_cart: store.updateCart,
84-
// replicate_cart: store.replicateCart,
85-
};
86-
}
87-
if (context?.isAdmin) { // IMPORTANT
88-
return {
89-
read_cart: admin.readCart,
90-
// create_cart: admin.createAdminCart,
91-
// update_cart: admin.updateAdminCart,
92-
// replicate_cart: admin.replicateAdminCart,
93-
};
94-
}
95-
};
96-
```
68+
- IMPORTANT: if no context is there, empty object should return
69+
- IMPORTANT: use type `Context` from `typescript/src/types/configuration.ts`
70+
- example:
71+
72+
```ts
73+
export const contextToCartFunctionMapping = (context?: Context) => {
74+
if (context?.customerId) {
75+
return {
76+
read_cart: customer.readCart,
77+
// create_cart: customer.createCart,
78+
// update_cart: customer.updateCart,
79+
// replicate_cart: customer.replicateCart,
80+
};
81+
}
82+
if (context?.storeKey) {
83+
return {
84+
read_cart: store.readCart,
85+
// create_cart: store.createCart,
86+
// update_cart: store.updateCart,
87+
// replicate_cart: store.replicateCart,
88+
};
89+
}
90+
if (context?.isAdmin) {
91+
// IMPORTANT
92+
return {
93+
read_cart: admin.readCart,
94+
// create_cart: admin.createAdminCart,
95+
// update_cart: admin.updateAdminCart,
96+
// replicate_cart: admin.replicateAdminCart,
97+
};
98+
}
99+
};
100+
```
97101

98-
6. create a test file to check if correct context is loading right functions from `contextTo<namespace>FunctionMapping` and if none is provided, it should be empty
102+
6. create a test file to check if correct context is loading right functions from `contextTo<namespace>FunctionMapping` and if none is provided, it should be empty
99103
7. update current tests to match the function calls
100-
8. refactor `typescript/src/shared/functions.ts` and import `import {contextToCartFunctionMapping} from './cart/functions';` then update this method return
101-
9. confirm that this method call `apiRoot.withProjectKey()` is only being called from base functions not in `customer.functions.ts` and not in `customer` and `admin`. if usage of `apiRoot.withProjectKey` found, move the method to base and reused from base functions.
104+
8. refactor `typescript/src/shared/functions.ts` and import `import {contextToCartFunctionMapping} from './cart/functions';` then update this method return
105+
9. confirm that this method call `apiRoot.withProjectKey()` is only being called from base functions not in `customer.functions.ts` and not in `customer` and `admin`. if usage of `apiRoot.withProjectKey` found, move the method to base and reused from base functions.
106+
102107
```
103108
export const contextToFunctionMapping = (context?: Context) => {
104109
return {
105110
...contextToOrderFunctionMapping(context),
106111
...contextToCartFunctionMapping(context),
107112
};
108113
};
109-
```
114+
```

0 commit comments

Comments
 (0)