-
Notifications
You must be signed in to change notification settings - Fork 944
Expand file tree
/
Copy pathtest_openapi_conversion.py
More file actions
499 lines (389 loc) · 19.8 KB
/
test_openapi_conversion.py
File metadata and controls
499 lines (389 loc) · 19.8 KB
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
import mcp.types as types
from fastapi_mcp.openapi.convert import convert_openapi_to_mcp_tools
from fastapi_mcp.openapi.utils import (
clean_schema_for_display,
generate_example_from_schema,
get_single_param_type_from_schema,
)
def test_simple_app_conversion(simple_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=simple_fastapi_app.title,
version=simple_fastapi_app.version,
openapi_version=simple_fastapi_app.openapi_version,
description=simple_fastapi_app.description,
routes=simple_fastapi_app.routes,
)
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
assert len(tools) == 6
assert len(operation_map) == 6
expected_operations = ["list_items", "get_item", "create_item", "update_item", "delete_item", "raise_error"]
for op in expected_operations:
assert op in operation_map
for tool in tools:
assert isinstance(tool, types.Tool)
assert tool.name in expected_operations
assert tool.description is not None
assert tool.inputSchema is not None
def test_complex_app_conversion(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
expected_operations = ["list_products", "get_product", "create_order", "get_customer"]
assert len(tools) == len(expected_operations)
assert len(operation_map) == len(expected_operations)
for op in expected_operations:
assert op in operation_map
for tool in tools:
assert isinstance(tool, types.Tool)
assert tool.name in expected_operations
assert tool.description is not None
assert tool.inputSchema is not None
def test_describe_full_response_schema(simple_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=simple_fastapi_app.title,
version=simple_fastapi_app.version,
openapi_version=simple_fastapi_app.openapi_version,
description=simple_fastapi_app.description,
routes=simple_fastapi_app.routes,
)
tools_full, _ = convert_openapi_to_mcp_tools(openapi_schema, describe_full_response_schema=True)
tools_simple, _ = convert_openapi_to_mcp_tools(openapi_schema, describe_full_response_schema=False)
for i, tool in enumerate(tools_full):
assert tool.description is not None
assert tools_simple[i].description is not None
tool_desc = tool.description or ""
simple_desc = tools_simple[i].description or ""
assert len(tool_desc) >= len(simple_desc)
if tool.name == "delete_item":
continue
assert "**Output Schema:**" in tool_desc
if "**Output Schema:**" in simple_desc:
assert len(tool_desc) > len(simple_desc)
def test_describe_all_responses(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
tools_all, _ = convert_openapi_to_mcp_tools(openapi_schema, describe_all_responses=True)
tools_success, _ = convert_openapi_to_mcp_tools(openapi_schema, describe_all_responses=False)
create_order_all = next(t for t in tools_all if t.name == "create_order")
create_order_success = next(t for t in tools_success if t.name == "create_order")
assert create_order_all.description is not None
assert create_order_success.description is not None
all_desc = create_order_all.description or ""
success_desc = create_order_success.description or ""
assert "400" in all_desc
assert "404" in all_desc
assert "422" in all_desc
assert all_desc.count("400") >= success_desc.count("400")
def test_schema_utils():
schema = {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"tags": {"type": "array", "items": {"type": "string"}},
},
"required": ["id", "name"],
"additionalProperties": False,
"x-internal": "Some internal data",
}
cleaned = clean_schema_for_display(schema)
assert "required" in cleaned
assert "properties" in cleaned
assert "type" in cleaned
example = generate_example_from_schema(schema)
assert "id" in example
assert "name" in example
assert "tags" in example
assert isinstance(example["id"], int)
assert isinstance(example["name"], str)
assert isinstance(example["tags"], list)
assert get_single_param_type_from_schema({"type": "string"}) == "string"
assert get_single_param_type_from_schema({"type": "array", "items": {"type": "string"}}) == "array"
array_schema = {"type": "array", "items": {"type": "string", "enum": ["red", "green", "blue"]}}
array_example = generate_example_from_schema(array_schema)
assert isinstance(array_example, list)
assert len(array_example) > 0
assert isinstance(array_example[0], str)
def test_parameter_handling(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
list_products_tool = next(tool for tool in tools if tool.name == "list_products")
properties = list_products_tool.inputSchema["properties"]
assert "product_id" not in properties # This is from get_product, not list_products
assert "category" in properties
assert "anyOf" in properties["category"] # Nullable enum preserved as anyOf
assert "type" not in properties["category"] # No top-level type for nullable schemas
assert "description" in properties["category"]
assert "Filter by product category" in properties["category"]["description"]
assert "min_price" in properties
assert "anyOf" in properties["min_price"] # Nullable number preserved as anyOf
assert "type" not in properties["min_price"] # No top-level type for nullable schemas
assert "description" in properties["min_price"]
assert "Minimum price filter" in properties["min_price"]["description"]
if "minimum" in properties["min_price"]:
assert properties["min_price"]["minimum"] > 0 # gt=0 in Query param
assert "in_stock_only" in properties
assert properties["in_stock_only"].get("type") == "boolean"
assert properties["in_stock_only"].get("default") is False # Default value preserved
assert "page" in properties
assert properties["page"].get("type") == "integer"
assert properties["page"].get("default") == 1 # Default value preserved
if "minimum" in properties["page"]:
assert properties["page"]["minimum"] >= 1 # ge=1 in Query param
assert "size" in properties
assert properties["size"].get("type") == "integer"
if "minimum" in properties["size"] and "maximum" in properties["size"]:
assert properties["size"]["minimum"] >= 1 # ge=1 in Query param
assert properties["size"]["maximum"] <= 100 # le=100 in Query param
assert "tag" in properties
assert "anyOf" in properties["tag"] # Nullable array preserved as anyOf
required = list_products_tool.inputSchema.get("required", [])
assert "page" not in required # Has default value
assert "category" not in required # Optional parameter
assert "list_products" in operation_map
assert operation_map["list_products"]["path"] == "/products"
assert operation_map["list_products"]["method"] == "get"
get_product_tool = next(tool for tool in tools if tool.name == "get_product")
get_product_props = get_product_tool.inputSchema["properties"]
assert "product_id" in get_product_props
assert get_product_props["product_id"].get("type") == "string" # UUID converted to string
assert "description" in get_product_props["product_id"]
get_customer_tool = next(tool for tool in tools if tool.name == "get_customer")
get_customer_props = get_customer_tool.inputSchema["properties"]
assert "fields" in get_customer_props
assert get_customer_props["fields"].get("type") == "array"
if "items" in get_customer_props["fields"]:
assert get_customer_props["fields"]["items"].get("type") == "string"
def test_request_body_handling(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
create_order_route = openapi_schema["paths"]["/orders"]["post"]
original_request_body = create_order_route["requestBody"]["content"]["application/json"]["schema"]
original_properties = original_request_body.get("properties", {})
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
create_order_tool = next(tool for tool in tools if tool.name == "create_order")
properties = create_order_tool.inputSchema["properties"]
assert "customer_id" in properties
assert "items" in properties
assert "shipping_address_id" in properties
assert "payment_method" in properties
assert "notes" in properties
for param_name in ["customer_id", "items", "shipping_address_id", "payment_method", "notes"]:
if "description" in original_properties.get(param_name, {}):
assert "description" in properties[param_name]
assert properties[param_name]["description"] == original_properties[param_name]["description"]
for param_name in ["customer_id", "items", "shipping_address_id", "payment_method", "notes"]:
assert properties[param_name]["title"] == param_name
for param_name in ["customer_id", "items", "shipping_address_id", "payment_method", "notes"]:
if "default" in original_properties.get(param_name, {}):
assert "default" in properties[param_name]
assert properties[param_name]["default"] == original_properties[param_name]["default"]
required = create_order_tool.inputSchema.get("required", [])
assert "customer_id" in required
assert "items" in required
assert "shipping_address_id" in required
assert "payment_method" in required
assert "notes" not in required # Optional in OrderRequest
assert properties["items"].get("type") == "array"
if "items" in properties["items"]:
item_props = properties["items"]["items"]
assert item_props.get("type") == "object"
if "properties" in item_props:
assert "product_id" in item_props["properties"]
assert "quantity" in item_props["properties"]
assert "unit_price" in item_props["properties"]
assert "total" in item_props["properties"]
for nested_param in ["product_id", "quantity", "unit_price", "total"]:
assert "title" in item_props["properties"][nested_param]
# Check if the original nested schema had descriptions
original_item_schema = original_properties.get("items", {}).get("items", {}).get("properties", {})
if "description" in original_item_schema.get(nested_param, {}):
assert "description" in item_props["properties"][nested_param]
assert (
item_props["properties"][nested_param]["description"]
== original_item_schema[nested_param]["description"]
)
assert "create_order" in operation_map
assert operation_map["create_order"]["path"] == "/orders"
assert operation_map["create_order"]["method"] == "post"
def test_missing_type_handling(complex_fastapi_app: FastAPI):
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
# Remove the type field from the product_id schema
params = openapi_schema["paths"]["/products/{product_id}"]["get"]["parameters"]
for param in params:
if param.get("name") == "product_id" and "schema" in param:
param["schema"].pop("type", None)
break
tools, operation_map = convert_openapi_to_mcp_tools(openapi_schema)
get_product_tool = next(tool for tool in tools if tool.name == "get_product")
get_product_props = get_product_tool.inputSchema["properties"]
assert "product_id" in get_product_props
assert get_product_props["product_id"].get("type") == "string" # Default type applied
def test_body_params_descriptions_and_defaults(complex_fastapi_app: FastAPI):
"""
Test that descriptions and defaults from request body parameters
are properly transferred to the MCP tool schema properties.
"""
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
order_request_schema = openapi_schema["components"]["schemas"]["OrderRequest"]
order_request_schema["properties"]["customer_id"]["description"] = "Test customer ID description"
order_request_schema["properties"]["payment_method"]["description"] = "Test payment method description"
order_request_schema["properties"]["notes"]["default"] = "Default order notes"
item_schema = openapi_schema["components"]["schemas"]["OrderItem"]
item_schema["properties"]["product_id"]["description"] = "Test product ID description"
item_schema["properties"]["quantity"]["default"] = 1
tools, _ = convert_openapi_to_mcp_tools(openapi_schema)
create_order_tool = next(tool for tool in tools if tool.name == "create_order")
properties = create_order_tool.inputSchema["properties"]
assert "description" in properties["customer_id"]
assert properties["customer_id"]["description"] == "Test customer ID description"
assert "description" in properties["payment_method"]
assert properties["payment_method"]["description"] == "Test payment method description"
assert "default" in properties["notes"]
assert properties["notes"]["default"] == "Default order notes"
if "items" in properties:
assert properties["items"]["type"] == "array"
assert "items" in properties["items"]
item_props = properties["items"]["items"]["properties"]
assert "description" in item_props["product_id"]
assert item_props["product_id"]["description"] == "Test product ID description"
assert "default" in item_props["quantity"]
assert item_props["quantity"]["default"] == 1
def test_body_params_edge_cases(complex_fastapi_app: FastAPI):
"""
Test handling of edge cases for body parameters, such as:
- Empty or missing descriptions
- Missing type information
- Empty properties object
- Schema without properties
"""
openapi_schema = get_openapi(
title=complex_fastapi_app.title,
version=complex_fastapi_app.version,
openapi_version=complex_fastapi_app.openapi_version,
description=complex_fastapi_app.description,
routes=complex_fastapi_app.routes,
)
order_request_schema = openapi_schema["components"]["schemas"]["OrderRequest"]
if "description" in order_request_schema["properties"]["customer_id"]:
del order_request_schema["properties"]["customer_id"]["description"]
if "type" in order_request_schema["properties"]["notes"]:
del order_request_schema["properties"]["notes"]["type"]
item_schema = openapi_schema["components"]["schemas"]["OrderItem"]
if "properties" in item_schema["properties"]["total"]:
del item_schema["properties"]["total"]["properties"]
tools, _ = convert_openapi_to_mcp_tools(openapi_schema)
create_order_tool = next(tool for tool in tools if tool.name == "create_order")
properties = create_order_tool.inputSchema["properties"]
assert "customer_id" in properties
assert "title" in properties["customer_id"]
assert properties["customer_id"]["title"] == "customer_id"
assert "notes" in properties
# notes is nullable (anyOf with null), so it should have anyOf or type
assert "type" in properties["notes"] or "anyOf" in properties["notes"]
if "items" in properties:
item_props = properties["items"]["items"]["properties"]
assert "total" in item_props
def test_nullable_anyof_schema_preserved():
"""Test that anyOf with null type is preserved without injecting a type field."""
openapi_schema = {
"openapi": "3.1.0",
"info": {"title": "Test", "version": "0.1.0"},
"paths": {
"/items": {
"post": {
"operationId": "create_item",
"summary": "Create an item",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"description": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Description",
},
},
"required": ["name"],
}
}
},
"required": True,
},
"responses": {"200": {"description": "OK"}},
}
},
"/items/{item_id}": {
"get": {
"operationId": "get_item",
"summary": "Get an item",
"parameters": [
{
"name": "item_id",
"in": "path",
"required": True,
"schema": {"type": "string"},
},
{
"name": "fields",
"in": "query",
"required": False,
"schema": {
"anyOf": [{"type": "string"}, {"type": "null"}],
"title": "Fields",
},
},
],
"responses": {"200": {"description": "OK"}},
}
},
},
}
tools, _ = convert_openapi_to_mcp_tools(openapi_schema)
# Check body parameter with anyOf
create_tool = next(t for t in tools if t.name == "create_item")
desc_prop = create_tool.inputSchema["properties"]["description"]
assert "anyOf" in desc_prop
assert "type" not in desc_prop, "type field should not be injected when anyOf is present"
# Check query parameter with anyOf
get_tool = next(t for t in tools if t.name == "get_item")
fields_prop = get_tool.inputSchema["properties"]["fields"]
assert "anyOf" in fields_prop
assert "type" not in fields_prop, "type field should not be injected when anyOf is present"