rmcp-openapi 0.28.1

Library for converting OpenAPI specifications to MCP tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";

// Helper function to clean tool response text by removing headers section
function cleanToolResponseText(responseData) {
  if (responseData && responseData.content && responseData.content[0] && responseData.content[0].text) {
    let text = responseData.content[0].text;
    // Remove the Headers section from the response text
    text = text.replace(/\nHeaders:\n(?:.*\n)*?\nResponse Body:\n/, "\nResponse Body:\n");
    return {
      ...responseData,
      content: [{
        ...responseData.content[0],
        text: text
      }]
    };
  }
  return responseData;
}

const transport = new StreamableHTTPClientTransport(new URL(process.env.MCP_STREAMABLE_URL || `http://127.0.0.1:8001/mcp/`));

const client = new Client(
  {
    name: "example-client",
    version: "1.0.0"
  },
  {
    capabilities: {
      prompts: {},
      resources: {},
      tools: {}
    }
  }
);

try {
  await client.connect(transport);
  
  // Step 1: List available tools
  const tools = await client.listTools();
  console.log(JSON.stringify({
    type: "tools_list",
    data: tools
  }));
  
  // Step 2: List other MCP resources for completeness
  const resources = await client.listResources();
  console.log(JSON.stringify({
    type: "resources_list", 
    data: resources
  }));
  
  const templates = await client.listResourceTemplates();
  console.log(JSON.stringify({
    type: "resource_templates_list",
    data: templates
  }));
  
  const prompts = await client.listPrompts();
  console.log(JSON.stringify({
    type: "prompts_list",
    data: prompts
  }));
  
  // Step 3: Test MCP Tool Calls - Path Parameter Test
  try {
    const getPetResult = await client.callTool({
      name: "getPetById",
      arguments: {
        petId: 123
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { petId: 123 },
      success: true,
      data: cleanToolResponseText(getPetResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById", 
      arguments: { petId: 123 },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }
  
  // Step 4: Test MCP Tool Calls - Query Parameter Test
  try {
    const findPetsResult = await client.callTool({
      name: "findPetsByStatus",
      arguments: {
        status: ["available", "pending"]
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: ["available", "pending"] },
      success: true,
      data: cleanToolResponseText(findPetsResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: ["available", "pending"] },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }
  
  // Step 5: Test MCP Tool Calls - Request Body Test
  try {
    const addPetResult = await client.callTool({
      name: "addPet",
      arguments: {
        request_body: {
          name: "MCP Test Dog",
          category: {
            id: 1,
            name: "Dogs"
          },
          photoUrls: ["https://example.com/mcp-test-dog.jpg"],
          tags: [
            {
              id: 1,
              name: "mcp-test"
            }
          ],
          status: "available"
        }
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: {
        request_body: {
          name: "MCP Test Dog",
          status: "available"
        }
      },
      success: true,
      data: cleanToolResponseText(addPetResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: {
        request_body: {
          name: "MCP Test Dog",
          status: "available"
        }
      },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }
  
  // Step 6: Test Error Scenarios - Invalid Pet ID (404)
  try {
    const errorResult = await client.callTool({
      name: "getPetById",
      arguments: {
        petId: 999999
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { petId: 999999 },
      success: true,
      data: cleanToolResponseText(errorResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { petId: 999999 },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }
  
  // Step 7: Test Error Scenarios - Invalid Request Body (400)
  try {
    const invalidPetResult = await client.callTool({
      name: "addPet",
      arguments: {
        request_body: {
          // Missing required fields like 'name' and 'photoUrls'
          status: "invalid_status_value"
        }
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: { request_body: { status: "invalid_status_value" } },
      success: true,
      data: cleanToolResponseText(invalidPetResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: { request_body: { status: "invalid_status_value" } },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Step 8: Test Invalid Parameter Validation
  // Test with typo in parameter name (pet_id instead of petId)
  try {
    const invalidParamResult = await client.callTool({
      name: "getPetById",
      arguments: {
        pet_id: 123  // Typo: should be petId
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { pet_id: 123 },
      success: true,
      data: cleanToolResponseText(invalidParamResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { pet_id: 123 },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Test with completely unknown parameter
  try {
    const unknownParamResult = await client.callTool({
      name: "findPetsByStatus",
      arguments: {
        statuses: ["available"],  // Wrong parameter name
        limit: 10  // Extra unknown parameter
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { statuses: ["available"], limit: 10 },
      success: true,
      data: cleanToolResponseText(unknownParamResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { statuses: ["available"], limit: 10 },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Step 9: Test Type Validation Errors
  // Test passing string for integer parameter
  try {
    const typeErrorResult = await client.callTool({
      name: "getPetById",
      arguments: {
        petId: "not-a-number"  // String instead of integer
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { petId: "not-a-number" },
      success: true,
      data: cleanToolResponseText(typeErrorResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetById",
      arguments: { petId: "not-a-number" },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Test passing string instead of array
  try {
    const arrayTypeErrorResult = await client.callTool({
      name: "findPetsByStatus",
      arguments: {
        status: "available"  // String instead of array
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: "available" },
      success: true,
      data: cleanToolResponseText(arrayTypeErrorResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: "available" },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Step 10: Test Enum Validation Error
  try {
    const enumErrorResult = await client.callTool({
      name: "findPetsByStatus",
      arguments: {
        status: ["invalid_status"]  // Invalid enum value
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: ["invalid_status"] },
      success: true,
      data: cleanToolResponseText(enumErrorResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "findPetsByStatus",
      arguments: { status: ["invalid_status"] },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Step 11: Test Nested Object Validation Error
  try {
    const nestedErrorResult = await client.callTool({
      name: "addPet",
      arguments: {
        request_body: {
          name: "Test Pet",
          photoUrls: ["https://example.com/photo.jpg"],
          category: {
            id: "not-a-number",  // String instead of integer
            name: "Dogs"
          },
          status: "available"
        }
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: {
        request_body: {
          name: "Test Pet",
          category: { id: "not-a-number" },
          status: "available"
        }
      },
      success: true,
      data: cleanToolResponseText(nestedErrorResult)
    }));
  } catch (error) {
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "addPet",
      arguments: {
        request_body: {
          name: "Test Pet",
          category: { id: "not-a-number" },
          status: "available"
        }
      },
      success: false,
      error: {
        message: error.message,
        code: error.code || "unknown"
      }
    }));
  }

  // Step 12: Test Tool Not Found with Suggestions
  // Test with typo in tool name (getPetByID instead of getPetById)
  try {
    const toolNotFoundResult = await client.callTool({
      name: "getPetByID",  // Typo: wrong case
      arguments: {
        petId: 123
      }
    });
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetByID",
      arguments: { petId: 123 },
      success: true,
      data: cleanToolResponseText(toolNotFoundResult)
    }));
  } catch (error) {
    // Build error object with all available fields
    const errorObj = {
      message: error.message,
      code: error.code || "unknown"
    };
    
    // Include data field if present (contains suggestions)
    if (error.data !== undefined) {
      errorObj.data = error.data;
    }
    
    console.log(JSON.stringify({
      type: "tool_call_result",
      tool: "getPetByID",
      arguments: { petId: 123 },
      success: false,
      error: errorObj
    }));
  }

} catch (connectionError) {
  console.log(JSON.stringify({
    type: "connection_error",
    error: {
      message: connectionError.message,
      code: connectionError.code || "connection_failed"
    }
  }));
} finally {
  try {
    await client.close();
    await transport.close();
  } catch (closeError) {
    // Ignore close errors
  }
}