Skip to content

Commit 38bb94f

Browse files
feat: 2163 mcp handlers accept options
1 parent 3ae2111 commit 38bb94f

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

packages/mcp/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ export const generateMcp: ClientBuilder = async (verbOptions, options) => {
122122
handlerArgsTypes.push(` bodyParams: ${verbOptions.body.definition};`);
123123
}
124124

125+
handlerArgsTypes.push(` options?: RequestInit;`);
126+
125127
const handlerArgsName = `${verbOptions.operationName}Args`;
126128
const handlerArgsImplementation = handlerArgsTypes.length
127129
? `
@@ -146,6 +148,8 @@ ${handlerArgsTypes.join('\n')}
146148
if (verbOptions.body.definition) fetchParams.push(`args.bodyParams`);
147149
if (verbOptions.queryParams) fetchParams.push(`args.queryParams`);
148150

151+
fetchParams.push(`args.options`);
152+
149153
const handlerName = `${verbOptions.operationName}Handler`;
150154
const handlerImplementation = `
151155
export const ${handlerName} = async (${handlerArgsTypes.length ? `args: ${handlerArgsName}` : ''}) => {

samples/mcp/petstore/src/handlers.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ import {
4141

4242
export type findPetsByStatusArgs = {
4343
queryParams: FindPetsByStatusParams;
44+
options?: RequestInit;
4445
};
4546

4647
export const findPetsByStatusHandler = async (args: findPetsByStatusArgs) => {
47-
const res = await findPetsByStatus(args.queryParams);
48+
const res = await findPetsByStatus(args.queryParams, args.options);
4849

4950
return {
5051
content: [
@@ -63,10 +64,11 @@ export const findPetsByStatusHandler = async (args: findPetsByStatusArgs) => {
6364

6465
export type findPetsByTagsArgs = {
6566
queryParams: FindPetsByTagsParams;
67+
options?: RequestInit;
6668
};
6769

6870
export const findPetsByTagsHandler = async (args: findPetsByTagsArgs) => {
69-
const res = await findPetsByTags(args.queryParams);
71+
const res = await findPetsByTags(args.queryParams, args.options);
7072

7173
return {
7274
content: [
@@ -87,10 +89,11 @@ export type getPetByIdArgs = {
8789
pathParams: {
8890
petId: number;
8991
};
92+
options?: RequestInit;
9093
};
9194

9295
export const getPetByIdHandler = async (args: getPetByIdArgs) => {
93-
const res = await getPetById(args.pathParams.petId);
96+
const res = await getPetById(args.pathParams.petId, args.options);
9497

9598
return {
9699
content: [
@@ -112,10 +115,15 @@ export type updatePetWithFormArgs = {
112115
petId: number;
113116
};
114117
queryParams: UpdatePetWithFormParams;
118+
options?: RequestInit;
115119
};
116120

117121
export const updatePetWithFormHandler = async (args: updatePetWithFormArgs) => {
118-
const res = await updatePetWithForm(args.pathParams.petId, args.queryParams);
122+
const res = await updatePetWithForm(
123+
args.pathParams.petId,
124+
args.queryParams,
125+
args.options,
126+
);
119127

120128
return {
121129
content: [
@@ -136,10 +144,11 @@ export type deletePetArgs = {
136144
pathParams: {
137145
petId: number;
138146
};
147+
options?: RequestInit;
139148
};
140149

141150
export const deletePetHandler = async (args: deletePetArgs) => {
142-
const res = await deletePet(args.pathParams.petId);
151+
const res = await deletePet(args.pathParams.petId, args.options);
143152

144153
return {
145154
content: [
@@ -156,8 +165,12 @@ export const deletePetHandler = async (args: deletePetArgs) => {
156165
* @summary Returns pet inventories by status.
157166
*/
158167

159-
export const getInventoryHandler = async () => {
160-
const res = await getInventory();
168+
export type getInventoryArgs = {
169+
options?: RequestInit;
170+
};
171+
172+
export const getInventoryHandler = async (args: getInventoryArgs) => {
173+
const res = await getInventory(args.options);
161174

162175
return {
163176
content: [
@@ -178,10 +191,11 @@ export type getOrderByIdArgs = {
178191
pathParams: {
179192
orderId: number;
180193
};
194+
options?: RequestInit;
181195
};
182196

183197
export const getOrderByIdHandler = async (args: getOrderByIdArgs) => {
184-
const res = await getOrderById(args.pathParams.orderId);
198+
const res = await getOrderById(args.pathParams.orderId, args.options);
185199

186200
return {
187201
content: [
@@ -202,10 +216,11 @@ export type deleteOrderArgs = {
202216
pathParams: {
203217
orderId: number;
204218
};
219+
options?: RequestInit;
205220
};
206221

207222
export const deleteOrderHandler = async (args: deleteOrderArgs) => {
208-
const res = await deleteOrder(args.pathParams.orderId);
223+
const res = await deleteOrder(args.pathParams.orderId, args.options);
209224

210225
return {
211226
content: [
@@ -224,10 +239,11 @@ export const deleteOrderHandler = async (args: deleteOrderArgs) => {
224239

225240
export type loginUserArgs = {
226241
queryParams: LoginUserParams;
242+
options?: RequestInit;
227243
};
228244

229245
export const loginUserHandler = async (args: loginUserArgs) => {
230-
const res = await loginUser(args.queryParams);
246+
const res = await loginUser(args.queryParams, args.options);
231247

232248
return {
233249
content: [
@@ -244,8 +260,12 @@ export const loginUserHandler = async (args: loginUserArgs) => {
244260
* @summary Logs out current logged in user session.
245261
*/
246262

247-
export const logoutUserHandler = async () => {
248-
const res = await logoutUser();
263+
export type logoutUserArgs = {
264+
options?: RequestInit;
265+
};
266+
267+
export const logoutUserHandler = async (args: logoutUserArgs) => {
268+
const res = await logoutUser(args.options);
249269

250270
return {
251271
content: [
@@ -266,10 +286,11 @@ export type getUserByNameArgs = {
266286
pathParams: {
267287
username: string;
268288
};
289+
options?: RequestInit;
269290
};
270291

271292
export const getUserByNameHandler = async (args: getUserByNameArgs) => {
272-
const res = await getUserByName(args.pathParams.username);
293+
const res = await getUserByName(args.pathParams.username, args.options);
273294

274295
return {
275296
content: [
@@ -290,10 +311,11 @@ export type deleteUserArgs = {
290311
pathParams: {
291312
username: string;
292313
};
314+
options?: RequestInit;
293315
};
294316

295317
export const deleteUserHandler = async (args: deleteUserArgs) => {
296-
const res = await deleteUser(args.pathParams.username);
318+
const res = await deleteUser(args.pathParams.username, args.options);
297319

298320
return {
299321
content: [

0 commit comments

Comments
 (0)