api2ai 1.0.6 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +159 -54
- package/package.json +238 -20
- package/.claude/settings.local.json +0 -7
- package/bun.lock +0 -17
- package/example-petstore/.env.example +0 -14
- package/example-petstore/README.md +0 -168
- package/example-petstore/bun.lock +0 -1127
- package/example-petstore/package.json +0 -19
- package/example-petstore/src/http-client.js +0 -120
- package/example-petstore/src/index.js +0 -659
- package/example-petstore/src/tools-config.js +0 -247
- package/example.js +0 -164
- package/generate-mcp-use-server.js +0 -864
|
@@ -1,659 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* petstore-api - MCP Server
|
|
5
|
-
*
|
|
6
|
-
* Features:
|
|
7
|
-
* - 19 API tools available
|
|
8
|
-
* - Built-in Inspector at http://localhost:3000/inspector
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import 'dotenv/config';
|
|
12
|
-
import { MCPServer } from 'mcp-use/server';
|
|
13
|
-
import { text, object } from 'mcp-use/server';
|
|
14
|
-
import { z } from 'zod';
|
|
15
|
-
import { executeRequest } from './http-client.js';
|
|
16
|
-
import { toolConfigMap } from './tools-config.js';
|
|
17
|
-
|
|
18
|
-
// ============================================================================
|
|
19
|
-
// Configuration
|
|
20
|
-
// ============================================================================
|
|
21
|
-
|
|
22
|
-
const PORT = parseInt(process.env.PORT || '3000');
|
|
23
|
-
const isDev = process.env.NODE_ENV !== 'production';
|
|
24
|
-
|
|
25
|
-
// API configuration
|
|
26
|
-
const apiConfig = {
|
|
27
|
-
baseUrl: process.env.API_BASE_URL || '/api/v3',
|
|
28
|
-
headers: {},
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// Set up authentication headers
|
|
32
|
-
if (process.env.API_KEY) {
|
|
33
|
-
apiConfig.headers['Authorization'] = `Bearer ${process.env.API_KEY}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (process.env.API_AUTH_HEADER) {
|
|
37
|
-
const [key, ...valueParts] = process.env.API_AUTH_HEADER.split(':');
|
|
38
|
-
const value = valueParts.join(':'); // Handle values with colons
|
|
39
|
-
if (key && value) {
|
|
40
|
-
apiConfig.headers[key.trim()] = value.trim();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// ============================================================================
|
|
45
|
-
// Server Setup
|
|
46
|
-
// ============================================================================
|
|
47
|
-
|
|
48
|
-
const server = new MCPServer({
|
|
49
|
-
name: 'petstore-api',
|
|
50
|
-
version: '1.0.0',
|
|
51
|
-
description: 'MCP server generated from OpenAPI specification',
|
|
52
|
-
baseUrl: process.env.MCP_URL || `http://localhost:${PORT}`,
|
|
53
|
-
allowedOrigins: isDev
|
|
54
|
-
? undefined // Development: allow all origins
|
|
55
|
-
: process.env.ALLOWED_ORIGINS?.split(',').map(s => s.trim()) || [],
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// ============================================================================
|
|
59
|
-
// Tool Registrations
|
|
60
|
-
// ============================================================================
|
|
61
|
-
|
|
62
|
-
// Add a new pet to the store.
|
|
63
|
-
server.tool(
|
|
64
|
-
{
|
|
65
|
-
name: 'addPet',
|
|
66
|
-
description: 'Add a new pet to the store.',
|
|
67
|
-
schema: z.object({
|
|
68
|
-
requestBody: z.unknown().describe('Create a new pet in the store')
|
|
69
|
-
}),
|
|
70
|
-
},
|
|
71
|
-
async (params) => {
|
|
72
|
-
const toolConfig = toolConfigMap.get('addPet');
|
|
73
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
74
|
-
|
|
75
|
-
if (!result.ok) {
|
|
76
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
77
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
78
|
-
}`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Return MCP content based on response type
|
|
82
|
-
if (typeof result.data === 'string') {
|
|
83
|
-
return text(result.data);
|
|
84
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
85
|
-
return object(result.data);
|
|
86
|
-
} else {
|
|
87
|
-
return text(String(result.data));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
// Update an existing pet.
|
|
93
|
-
server.tool(
|
|
94
|
-
{
|
|
95
|
-
name: 'updatePet',
|
|
96
|
-
description: 'Update an existing pet.',
|
|
97
|
-
schema: z.object({
|
|
98
|
-
requestBody: z.unknown().describe('Update an existent pet in the store')
|
|
99
|
-
}),
|
|
100
|
-
},
|
|
101
|
-
async (params) => {
|
|
102
|
-
const toolConfig = toolConfigMap.get('updatePet');
|
|
103
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
104
|
-
|
|
105
|
-
if (!result.ok) {
|
|
106
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
107
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
108
|
-
}`);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Return MCP content based on response type
|
|
112
|
-
if (typeof result.data === 'string') {
|
|
113
|
-
return text(result.data);
|
|
114
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
115
|
-
return object(result.data);
|
|
116
|
-
} else {
|
|
117
|
-
return text(String(result.data));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
// Finds Pets by status.
|
|
123
|
-
server.tool(
|
|
124
|
-
{
|
|
125
|
-
name: 'findPetsByStatus',
|
|
126
|
-
description: 'Finds Pets by status.',
|
|
127
|
-
schema: z.object({
|
|
128
|
-
status: z.enum(['available', 'pending', 'sold']).describe('Status values that need to be considered for filter')
|
|
129
|
-
}),
|
|
130
|
-
},
|
|
131
|
-
async (params) => {
|
|
132
|
-
const toolConfig = toolConfigMap.get('findPetsByStatus');
|
|
133
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
134
|
-
|
|
135
|
-
if (!result.ok) {
|
|
136
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
137
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
138
|
-
}`);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Return MCP content based on response type
|
|
142
|
-
if (typeof result.data === 'string') {
|
|
143
|
-
return text(result.data);
|
|
144
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
145
|
-
return object(result.data);
|
|
146
|
-
} else {
|
|
147
|
-
return text(String(result.data));
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
);
|
|
151
|
-
|
|
152
|
-
// Finds Pets by tags.
|
|
153
|
-
server.tool(
|
|
154
|
-
{
|
|
155
|
-
name: 'findPetsByTags',
|
|
156
|
-
description: 'Finds Pets by tags.',
|
|
157
|
-
schema: z.object({
|
|
158
|
-
tags: z.array(z.string()).describe('Tags to filter by')
|
|
159
|
-
}),
|
|
160
|
-
},
|
|
161
|
-
async (params) => {
|
|
162
|
-
const toolConfig = toolConfigMap.get('findPetsByTags');
|
|
163
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
164
|
-
|
|
165
|
-
if (!result.ok) {
|
|
166
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
167
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
168
|
-
}`);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// Return MCP content based on response type
|
|
172
|
-
if (typeof result.data === 'string') {
|
|
173
|
-
return text(result.data);
|
|
174
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
175
|
-
return object(result.data);
|
|
176
|
-
} else {
|
|
177
|
-
return text(String(result.data));
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// Find pet by ID.
|
|
183
|
-
server.tool(
|
|
184
|
-
{
|
|
185
|
-
name: 'getPetById',
|
|
186
|
-
description: 'Find pet by ID.',
|
|
187
|
-
schema: z.object({
|
|
188
|
-
petId: z.number().int().describe('ID of pet to return')
|
|
189
|
-
}),
|
|
190
|
-
},
|
|
191
|
-
async (params) => {
|
|
192
|
-
const toolConfig = toolConfigMap.get('getPetById');
|
|
193
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
194
|
-
|
|
195
|
-
if (!result.ok) {
|
|
196
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
197
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
198
|
-
}`);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Return MCP content based on response type
|
|
202
|
-
if (typeof result.data === 'string') {
|
|
203
|
-
return text(result.data);
|
|
204
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
205
|
-
return object(result.data);
|
|
206
|
-
} else {
|
|
207
|
-
return text(String(result.data));
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
// Updates a pet in the store with form data.
|
|
213
|
-
server.tool(
|
|
214
|
-
{
|
|
215
|
-
name: 'updatePetWithForm',
|
|
216
|
-
description: 'Updates a pet in the store with form data.',
|
|
217
|
-
schema: z.object({
|
|
218
|
-
petId: z.number().int().describe('ID of pet that needs to be updated'),
|
|
219
|
-
name: z.string().optional().describe('Name of pet that needs to be updated'),
|
|
220
|
-
status: z.string().optional().describe('Status of pet that needs to be updated')
|
|
221
|
-
}),
|
|
222
|
-
},
|
|
223
|
-
async (params) => {
|
|
224
|
-
const toolConfig = toolConfigMap.get('updatePetWithForm');
|
|
225
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
226
|
-
|
|
227
|
-
if (!result.ok) {
|
|
228
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
229
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
230
|
-
}`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Return MCP content based on response type
|
|
234
|
-
if (typeof result.data === 'string') {
|
|
235
|
-
return text(result.data);
|
|
236
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
237
|
-
return object(result.data);
|
|
238
|
-
} else {
|
|
239
|
-
return text(String(result.data));
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
// Deletes a pet.
|
|
245
|
-
server.tool(
|
|
246
|
-
{
|
|
247
|
-
name: 'deletePet',
|
|
248
|
-
description: 'Deletes a pet.',
|
|
249
|
-
schema: z.object({
|
|
250
|
-
api_key: z.string().optional(),
|
|
251
|
-
petId: z.number().int().describe('Pet id to delete')
|
|
252
|
-
}),
|
|
253
|
-
},
|
|
254
|
-
async (params) => {
|
|
255
|
-
const toolConfig = toolConfigMap.get('deletePet');
|
|
256
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
257
|
-
|
|
258
|
-
if (!result.ok) {
|
|
259
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
260
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
261
|
-
}`);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// Return MCP content based on response type
|
|
265
|
-
if (typeof result.data === 'string') {
|
|
266
|
-
return text(result.data);
|
|
267
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
268
|
-
return object(result.data);
|
|
269
|
-
} else {
|
|
270
|
-
return text(String(result.data));
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
);
|
|
274
|
-
|
|
275
|
-
// Uploads an image.
|
|
276
|
-
server.tool(
|
|
277
|
-
{
|
|
278
|
-
name: 'uploadFile',
|
|
279
|
-
description: 'Uploads an image.',
|
|
280
|
-
schema: z.object({
|
|
281
|
-
petId: z.number().int().describe('ID of pet to update'),
|
|
282
|
-
additionalMetadata: z.string().optional().describe('Additional Metadata'),
|
|
283
|
-
requestBody: z.string().optional().describe('Request body')
|
|
284
|
-
}),
|
|
285
|
-
},
|
|
286
|
-
async (params) => {
|
|
287
|
-
const toolConfig = toolConfigMap.get('uploadFile');
|
|
288
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
289
|
-
|
|
290
|
-
if (!result.ok) {
|
|
291
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
292
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
293
|
-
}`);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Return MCP content based on response type
|
|
297
|
-
if (typeof result.data === 'string') {
|
|
298
|
-
return text(result.data);
|
|
299
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
300
|
-
return object(result.data);
|
|
301
|
-
} else {
|
|
302
|
-
return text(String(result.data));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
);
|
|
306
|
-
|
|
307
|
-
// Returns pet inventories by status.
|
|
308
|
-
server.tool(
|
|
309
|
-
{
|
|
310
|
-
name: 'getInventory',
|
|
311
|
-
description: 'Returns pet inventories by status.',
|
|
312
|
-
schema: z.object({}),
|
|
313
|
-
},
|
|
314
|
-
async (params) => {
|
|
315
|
-
const toolConfig = toolConfigMap.get('getInventory');
|
|
316
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
317
|
-
|
|
318
|
-
if (!result.ok) {
|
|
319
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
320
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
321
|
-
}`);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// Return MCP content based on response type
|
|
325
|
-
if (typeof result.data === 'string') {
|
|
326
|
-
return text(result.data);
|
|
327
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
328
|
-
return object(result.data);
|
|
329
|
-
} else {
|
|
330
|
-
return text(String(result.data));
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
);
|
|
334
|
-
|
|
335
|
-
// Place an order for a pet.
|
|
336
|
-
server.tool(
|
|
337
|
-
{
|
|
338
|
-
name: 'placeOrder',
|
|
339
|
-
description: 'Place an order for a pet.',
|
|
340
|
-
schema: z.object({
|
|
341
|
-
requestBody: z.unknown().optional().describe('Request body')
|
|
342
|
-
}),
|
|
343
|
-
},
|
|
344
|
-
async (params) => {
|
|
345
|
-
const toolConfig = toolConfigMap.get('placeOrder');
|
|
346
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
347
|
-
|
|
348
|
-
if (!result.ok) {
|
|
349
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
350
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
351
|
-
}`);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// Return MCP content based on response type
|
|
355
|
-
if (typeof result.data === 'string') {
|
|
356
|
-
return text(result.data);
|
|
357
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
358
|
-
return object(result.data);
|
|
359
|
-
} else {
|
|
360
|
-
return text(String(result.data));
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
);
|
|
364
|
-
|
|
365
|
-
// Find purchase order by ID.
|
|
366
|
-
server.tool(
|
|
367
|
-
{
|
|
368
|
-
name: 'getOrderById',
|
|
369
|
-
description: 'Find purchase order by ID.',
|
|
370
|
-
schema: z.object({
|
|
371
|
-
orderId: z.number().int().describe('ID of order that needs to be fetched')
|
|
372
|
-
}),
|
|
373
|
-
},
|
|
374
|
-
async (params) => {
|
|
375
|
-
const toolConfig = toolConfigMap.get('getOrderById');
|
|
376
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
377
|
-
|
|
378
|
-
if (!result.ok) {
|
|
379
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
380
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
381
|
-
}`);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// Return MCP content based on response type
|
|
385
|
-
if (typeof result.data === 'string') {
|
|
386
|
-
return text(result.data);
|
|
387
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
388
|
-
return object(result.data);
|
|
389
|
-
} else {
|
|
390
|
-
return text(String(result.data));
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
// Delete purchase order by identifier.
|
|
396
|
-
server.tool(
|
|
397
|
-
{
|
|
398
|
-
name: 'deleteOrder',
|
|
399
|
-
description: 'Delete purchase order by identifier.',
|
|
400
|
-
schema: z.object({
|
|
401
|
-
orderId: z.number().int().describe('ID of the order that needs to be deleted')
|
|
402
|
-
}),
|
|
403
|
-
},
|
|
404
|
-
async (params) => {
|
|
405
|
-
const toolConfig = toolConfigMap.get('deleteOrder');
|
|
406
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
407
|
-
|
|
408
|
-
if (!result.ok) {
|
|
409
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
410
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
411
|
-
}`);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// Return MCP content based on response type
|
|
415
|
-
if (typeof result.data === 'string') {
|
|
416
|
-
return text(result.data);
|
|
417
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
418
|
-
return object(result.data);
|
|
419
|
-
} else {
|
|
420
|
-
return text(String(result.data));
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
);
|
|
424
|
-
|
|
425
|
-
// Create user.
|
|
426
|
-
server.tool(
|
|
427
|
-
{
|
|
428
|
-
name: 'createUser',
|
|
429
|
-
description: 'Create user.',
|
|
430
|
-
schema: z.object({
|
|
431
|
-
requestBody: z.unknown().optional().describe('Created user object')
|
|
432
|
-
}),
|
|
433
|
-
},
|
|
434
|
-
async (params) => {
|
|
435
|
-
const toolConfig = toolConfigMap.get('createUser');
|
|
436
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
437
|
-
|
|
438
|
-
if (!result.ok) {
|
|
439
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
440
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
441
|
-
}`);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// Return MCP content based on response type
|
|
445
|
-
if (typeof result.data === 'string') {
|
|
446
|
-
return text(result.data);
|
|
447
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
448
|
-
return object(result.data);
|
|
449
|
-
} else {
|
|
450
|
-
return text(String(result.data));
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
);
|
|
454
|
-
|
|
455
|
-
// Creates list of users with given input array.
|
|
456
|
-
server.tool(
|
|
457
|
-
{
|
|
458
|
-
name: 'createUsersWithListInput',
|
|
459
|
-
description: 'Creates list of users with given input array.',
|
|
460
|
-
schema: z.object({
|
|
461
|
-
requestBody: z.array(z.unknown()).optional().describe('Request body')
|
|
462
|
-
}),
|
|
463
|
-
},
|
|
464
|
-
async (params) => {
|
|
465
|
-
const toolConfig = toolConfigMap.get('createUsersWithListInput');
|
|
466
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
467
|
-
|
|
468
|
-
if (!result.ok) {
|
|
469
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
470
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
471
|
-
}`);
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
// Return MCP content based on response type
|
|
475
|
-
if (typeof result.data === 'string') {
|
|
476
|
-
return text(result.data);
|
|
477
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
478
|
-
return object(result.data);
|
|
479
|
-
} else {
|
|
480
|
-
return text(String(result.data));
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
);
|
|
484
|
-
|
|
485
|
-
// Logs user into the system.
|
|
486
|
-
server.tool(
|
|
487
|
-
{
|
|
488
|
-
name: 'loginUser',
|
|
489
|
-
description: 'Logs user into the system.',
|
|
490
|
-
schema: z.object({
|
|
491
|
-
username: z.string().optional().describe('The user name for login'),
|
|
492
|
-
password: z.string().optional().describe('The password for login in clear text')
|
|
493
|
-
}),
|
|
494
|
-
},
|
|
495
|
-
async (params) => {
|
|
496
|
-
const toolConfig = toolConfigMap.get('loginUser');
|
|
497
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
498
|
-
|
|
499
|
-
if (!result.ok) {
|
|
500
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
501
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
502
|
-
}`);
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
// Return MCP content based on response type
|
|
506
|
-
if (typeof result.data === 'string') {
|
|
507
|
-
return text(result.data);
|
|
508
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
509
|
-
return object(result.data);
|
|
510
|
-
} else {
|
|
511
|
-
return text(String(result.data));
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
);
|
|
515
|
-
|
|
516
|
-
// Logs out current logged in user session.
|
|
517
|
-
server.tool(
|
|
518
|
-
{
|
|
519
|
-
name: 'logoutUser',
|
|
520
|
-
description: 'Logs out current logged in user session.',
|
|
521
|
-
schema: z.object({}),
|
|
522
|
-
},
|
|
523
|
-
async (params) => {
|
|
524
|
-
const toolConfig = toolConfigMap.get('logoutUser');
|
|
525
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
526
|
-
|
|
527
|
-
if (!result.ok) {
|
|
528
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
529
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
530
|
-
}`);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
// Return MCP content based on response type
|
|
534
|
-
if (typeof result.data === 'string') {
|
|
535
|
-
return text(result.data);
|
|
536
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
537
|
-
return object(result.data);
|
|
538
|
-
} else {
|
|
539
|
-
return text(String(result.data));
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
);
|
|
543
|
-
|
|
544
|
-
// Get user by user name.
|
|
545
|
-
server.tool(
|
|
546
|
-
{
|
|
547
|
-
name: 'getUserByName',
|
|
548
|
-
description: 'Get user by user name.',
|
|
549
|
-
schema: z.object({
|
|
550
|
-
username: z.string().describe('The name that needs to be fetched. Use user1 for testing')
|
|
551
|
-
}),
|
|
552
|
-
},
|
|
553
|
-
async (params) => {
|
|
554
|
-
const toolConfig = toolConfigMap.get('getUserByName');
|
|
555
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
556
|
-
|
|
557
|
-
if (!result.ok) {
|
|
558
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
559
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
560
|
-
}`);
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
// Return MCP content based on response type
|
|
564
|
-
if (typeof result.data === 'string') {
|
|
565
|
-
return text(result.data);
|
|
566
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
567
|
-
return object(result.data);
|
|
568
|
-
} else {
|
|
569
|
-
return text(String(result.data));
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
);
|
|
573
|
-
|
|
574
|
-
// Update user resource.
|
|
575
|
-
server.tool(
|
|
576
|
-
{
|
|
577
|
-
name: 'updateUser',
|
|
578
|
-
description: 'Update user resource.',
|
|
579
|
-
schema: z.object({
|
|
580
|
-
username: z.string().describe('name that need to be deleted'),
|
|
581
|
-
requestBody: z.unknown().optional().describe('Update an existent user in the store')
|
|
582
|
-
}),
|
|
583
|
-
},
|
|
584
|
-
async (params) => {
|
|
585
|
-
const toolConfig = toolConfigMap.get('updateUser');
|
|
586
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
587
|
-
|
|
588
|
-
if (!result.ok) {
|
|
589
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
590
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
591
|
-
}`);
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
// Return MCP content based on response type
|
|
595
|
-
if (typeof result.data === 'string') {
|
|
596
|
-
return text(result.data);
|
|
597
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
598
|
-
return object(result.data);
|
|
599
|
-
} else {
|
|
600
|
-
return text(String(result.data));
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
);
|
|
604
|
-
|
|
605
|
-
// Delete user resource.
|
|
606
|
-
server.tool(
|
|
607
|
-
{
|
|
608
|
-
name: 'deleteUser',
|
|
609
|
-
description: 'Delete user resource.',
|
|
610
|
-
schema: z.object({
|
|
611
|
-
username: z.string().describe('The name that needs to be deleted')
|
|
612
|
-
}),
|
|
613
|
-
},
|
|
614
|
-
async (params) => {
|
|
615
|
-
const toolConfig = toolConfigMap.get('deleteUser');
|
|
616
|
-
const result = await executeRequest(toolConfig, params, apiConfig);
|
|
617
|
-
|
|
618
|
-
if (!result.ok) {
|
|
619
|
-
return text(`Error: ${result.status} ${result.statusText}\n${
|
|
620
|
-
typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2)
|
|
621
|
-
}`);
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
// Return MCP content based on response type
|
|
625
|
-
if (typeof result.data === 'string') {
|
|
626
|
-
return text(result.data);
|
|
627
|
-
} else if (typeof result.data === 'object' && result.data !== null) {
|
|
628
|
-
return object(result.data);
|
|
629
|
-
} else {
|
|
630
|
-
return text(String(result.data));
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
);
|
|
634
|
-
|
|
635
|
-
// ============================================================================
|
|
636
|
-
// Start Server
|
|
637
|
-
// ============================================================================
|
|
638
|
-
|
|
639
|
-
server.listen(PORT);
|
|
640
|
-
|
|
641
|
-
console.log(`
|
|
642
|
-
🚀 petstore-api MCP Server Started
|
|
643
|
-
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
644
|
-
|
|
645
|
-
📍 Server: http://localhost:${PORT}
|
|
646
|
-
🔍 Inspector: http://localhost:${PORT}/inspector
|
|
647
|
-
📡 MCP: http://localhost:${PORT}/mcp
|
|
648
|
-
🔄 SSE: http://localhost:${PORT}/sse
|
|
649
|
-
|
|
650
|
-
🛠️ Tools Available: 19
|
|
651
|
-
• addPet
|
|
652
|
-
• updatePet
|
|
653
|
-
• findPetsByStatus
|
|
654
|
-
• findPetsByTags
|
|
655
|
-
• getPetById
|
|
656
|
-
... and 14 more
|
|
657
|
-
Environment: ${isDev ? 'Development' : 'Production'}
|
|
658
|
-
API Base: ${apiConfig.baseUrl || 'Not configured'}
|
|
659
|
-
`);
|