fastmcp 2.10.6__py3-none-any.whl → 2.11.0__py3-none-any.whl

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.
Files changed (61) hide show
  1. fastmcp/cli/cli.py +128 -33
  2. fastmcp/cli/install/claude_code.py +42 -1
  3. fastmcp/cli/install/claude_desktop.py +42 -1
  4. fastmcp/cli/install/cursor.py +42 -1
  5. fastmcp/cli/install/mcp_json.py +41 -0
  6. fastmcp/cli/run.py +127 -1
  7. fastmcp/client/__init__.py +2 -0
  8. fastmcp/client/auth/oauth.py +68 -99
  9. fastmcp/client/oauth_callback.py +18 -0
  10. fastmcp/client/transports.py +69 -15
  11. fastmcp/contrib/component_manager/example.py +2 -2
  12. fastmcp/experimental/server/openapi/README.md +266 -0
  13. fastmcp/experimental/server/openapi/__init__.py +38 -0
  14. fastmcp/experimental/server/openapi/components.py +348 -0
  15. fastmcp/experimental/server/openapi/routing.py +132 -0
  16. fastmcp/experimental/server/openapi/server.py +466 -0
  17. fastmcp/experimental/utilities/openapi/README.md +239 -0
  18. fastmcp/experimental/utilities/openapi/__init__.py +68 -0
  19. fastmcp/experimental/utilities/openapi/director.py +208 -0
  20. fastmcp/experimental/utilities/openapi/formatters.py +355 -0
  21. fastmcp/experimental/utilities/openapi/json_schema_converter.py +340 -0
  22. fastmcp/experimental/utilities/openapi/models.py +85 -0
  23. fastmcp/experimental/utilities/openapi/parser.py +618 -0
  24. fastmcp/experimental/utilities/openapi/schemas.py +538 -0
  25. fastmcp/mcp_config.py +125 -88
  26. fastmcp/prompts/prompt.py +11 -1
  27. fastmcp/resources/resource.py +21 -1
  28. fastmcp/resources/template.py +20 -1
  29. fastmcp/server/auth/__init__.py +17 -2
  30. fastmcp/server/auth/auth.py +144 -7
  31. fastmcp/server/auth/providers/bearer.py +25 -473
  32. fastmcp/server/auth/providers/in_memory.py +4 -2
  33. fastmcp/server/auth/providers/jwt.py +538 -0
  34. fastmcp/server/auth/providers/workos.py +170 -0
  35. fastmcp/server/auth/registry.py +52 -0
  36. fastmcp/server/context.py +107 -26
  37. fastmcp/server/dependencies.py +9 -2
  38. fastmcp/server/http.py +62 -30
  39. fastmcp/server/middleware/middleware.py +3 -23
  40. fastmcp/server/openapi.py +1 -1
  41. fastmcp/server/proxy.py +50 -11
  42. fastmcp/server/server.py +168 -59
  43. fastmcp/settings.py +73 -6
  44. fastmcp/tools/tool.py +36 -3
  45. fastmcp/tools/tool_manager.py +38 -2
  46. fastmcp/tools/tool_transform.py +112 -3
  47. fastmcp/utilities/components.py +35 -2
  48. fastmcp/utilities/json_schema.py +136 -98
  49. fastmcp/utilities/json_schema_type.py +1 -3
  50. fastmcp/utilities/mcp_config.py +28 -0
  51. fastmcp/utilities/openapi.py +240 -50
  52. fastmcp/utilities/tests.py +54 -6
  53. fastmcp/utilities/types.py +89 -11
  54. {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/METADATA +4 -3
  55. fastmcp-2.11.0.dist-info/RECORD +108 -0
  56. fastmcp/server/auth/providers/bearer_env.py +0 -63
  57. fastmcp/utilities/cache.py +0 -26
  58. fastmcp-2.10.6.dist-info/RECORD +0 -93
  59. {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/WHEEL +0 -0
  60. {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/entry_points.txt +0 -0
  61. {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,618 @@
1
+ """OpenAPI parsing logic for converting OpenAPI specs to HTTPRoute objects."""
2
+
3
+ from typing import Any, Generic, TypeVar
4
+
5
+ from openapi_pydantic import (
6
+ OpenAPI,
7
+ Operation,
8
+ Parameter,
9
+ PathItem,
10
+ Reference,
11
+ RequestBody,
12
+ Response,
13
+ Schema,
14
+ )
15
+
16
+ # Import OpenAPI 3.0 models as well
17
+ from openapi_pydantic.v3.v3_0 import OpenAPI as OpenAPI_30
18
+ from openapi_pydantic.v3.v3_0 import Operation as Operation_30
19
+ from openapi_pydantic.v3.v3_0 import Parameter as Parameter_30
20
+ from openapi_pydantic.v3.v3_0 import PathItem as PathItem_30
21
+ from openapi_pydantic.v3.v3_0 import Reference as Reference_30
22
+ from openapi_pydantic.v3.v3_0 import RequestBody as RequestBody_30
23
+ from openapi_pydantic.v3.v3_0 import Response as Response_30
24
+ from openapi_pydantic.v3.v3_0 import Schema as Schema_30
25
+ from pydantic import BaseModel, ValidationError
26
+
27
+ from fastmcp.utilities.logging import get_logger
28
+
29
+ from .models import (
30
+ HTTPRoute,
31
+ JsonSchema,
32
+ ParameterInfo,
33
+ ParameterLocation,
34
+ RequestBodyInfo,
35
+ ResponseInfo,
36
+ )
37
+ from .schemas import _combine_schemas_and_map_params, _replace_ref_with_defs
38
+
39
+ logger = get_logger(__name__)
40
+
41
+ # Type variables for generic parser
42
+ TOpenAPI = TypeVar("TOpenAPI", OpenAPI, OpenAPI_30)
43
+ TSchema = TypeVar("TSchema", Schema, Schema_30)
44
+ TReference = TypeVar("TReference", Reference, Reference_30)
45
+ TParameter = TypeVar("TParameter", Parameter, Parameter_30)
46
+ TRequestBody = TypeVar("TRequestBody", RequestBody, RequestBody_30)
47
+ TResponse = TypeVar("TResponse", Response, Response_30)
48
+ TOperation = TypeVar("TOperation", Operation, Operation_30)
49
+ TPathItem = TypeVar("TPathItem", PathItem, PathItem_30)
50
+
51
+
52
+ def parse_openapi_to_http_routes(openapi_dict: dict[str, Any]) -> list[HTTPRoute]:
53
+ """
54
+ Parses an OpenAPI schema dictionary into a list of HTTPRoute objects
55
+ using the openapi-pydantic library.
56
+
57
+ Supports both OpenAPI 3.0.x and 3.1.x versions.
58
+ """
59
+ # Check OpenAPI version to use appropriate model
60
+ openapi_version = openapi_dict.get("openapi", "")
61
+
62
+ try:
63
+ if openapi_version.startswith("3.0"):
64
+ # Use OpenAPI 3.0 models
65
+ openapi_30 = OpenAPI_30.model_validate(openapi_dict)
66
+ logger.info(
67
+ f"Successfully parsed OpenAPI 3.0 schema version: {openapi_30.openapi}"
68
+ )
69
+ parser = OpenAPIParser(
70
+ openapi_30,
71
+ Reference_30,
72
+ Schema_30,
73
+ Parameter_30,
74
+ RequestBody_30,
75
+ Response_30,
76
+ Operation_30,
77
+ PathItem_30,
78
+ openapi_version,
79
+ )
80
+ return parser.parse()
81
+ else:
82
+ # Default to OpenAPI 3.1 models
83
+ openapi_31 = OpenAPI.model_validate(openapi_dict)
84
+ logger.info(
85
+ f"Successfully parsed OpenAPI 3.1 schema version: {openapi_31.openapi}"
86
+ )
87
+ parser = OpenAPIParser(
88
+ openapi_31,
89
+ Reference,
90
+ Schema,
91
+ Parameter,
92
+ RequestBody,
93
+ Response,
94
+ Operation,
95
+ PathItem,
96
+ openapi_version,
97
+ )
98
+ return parser.parse()
99
+ except ValidationError as e:
100
+ logger.error(f"OpenAPI schema validation failed: {e}")
101
+ error_details = e.errors()
102
+ logger.error(f"Validation errors: {error_details}")
103
+ raise ValueError(f"Invalid OpenAPI schema: {error_details}") from e
104
+
105
+
106
+ class OpenAPIParser(
107
+ Generic[
108
+ TOpenAPI,
109
+ TReference,
110
+ TSchema,
111
+ TParameter,
112
+ TRequestBody,
113
+ TResponse,
114
+ TOperation,
115
+ TPathItem,
116
+ ]
117
+ ):
118
+ """Unified parser for OpenAPI schemas with generic type parameters to handle both 3.0 and 3.1."""
119
+
120
+ def __init__(
121
+ self,
122
+ openapi: TOpenAPI,
123
+ reference_cls: type[TReference],
124
+ schema_cls: type[TSchema],
125
+ parameter_cls: type[TParameter],
126
+ request_body_cls: type[TRequestBody],
127
+ response_cls: type[TResponse],
128
+ operation_cls: type[TOperation],
129
+ path_item_cls: type[TPathItem],
130
+ openapi_version: str,
131
+ ):
132
+ """Initialize the parser with the OpenAPI schema and type classes."""
133
+ self.openapi = openapi
134
+ self.reference_cls = reference_cls
135
+ self.schema_cls = schema_cls
136
+ self.parameter_cls = parameter_cls
137
+ self.request_body_cls = request_body_cls
138
+ self.response_cls = response_cls
139
+ self.operation_cls = operation_cls
140
+ self.path_item_cls = path_item_cls
141
+ self.openapi_version = openapi_version
142
+
143
+ def _convert_to_parameter_location(self, param_in: str) -> ParameterLocation:
144
+ """Convert string parameter location to our ParameterLocation type."""
145
+ if param_in in ["path", "query", "header", "cookie"]:
146
+ return param_in # type: ignore[return-value] # Safe cast since we checked values
147
+ logger.warning(f"Unknown parameter location: {param_in}, defaulting to 'query'")
148
+ return "query" # type: ignore[return-value] # Safe cast to default value
149
+
150
+ def _resolve_ref(self, item: Any) -> Any:
151
+ """Resolves a reference to its target definition."""
152
+ if isinstance(item, self.reference_cls):
153
+ ref_str = item.ref
154
+ # Ensure ref_str is a string before calling startswith()
155
+ if not isinstance(ref_str, str):
156
+ return item
157
+ try:
158
+ if not ref_str.startswith("#/"):
159
+ raise ValueError(
160
+ f"External or non-local reference not supported: {ref_str}"
161
+ )
162
+
163
+ parts = ref_str.strip("#/").split("/")
164
+ target = self.openapi
165
+
166
+ for part in parts:
167
+ if part.isdigit() and isinstance(target, list):
168
+ target = target[int(part)]
169
+ elif isinstance(target, BaseModel):
170
+ # Check class fields first, then model_extra
171
+ if part in target.__class__.model_fields:
172
+ target = getattr(target, part, None)
173
+ elif target.model_extra and part in target.model_extra:
174
+ target = target.model_extra[part]
175
+ else:
176
+ # Special handling for components
177
+ if part == "components" and hasattr(target, "components"):
178
+ target = getattr(target, "components")
179
+ elif hasattr(target, part): # Fallback check
180
+ target = getattr(target, part, None)
181
+ else:
182
+ target = None # Part not found
183
+ elif isinstance(target, dict):
184
+ target = target.get(part)
185
+ else:
186
+ raise ValueError(
187
+ f"Cannot traverse part '{part}' in reference '{ref_str}'"
188
+ )
189
+
190
+ if target is None:
191
+ raise ValueError(
192
+ f"Reference part '{part}' not found in path '{ref_str}'"
193
+ )
194
+
195
+ # Handle nested references
196
+ if isinstance(target, self.reference_cls):
197
+ return self._resolve_ref(target)
198
+
199
+ return target
200
+ except (AttributeError, KeyError, IndexError, TypeError, ValueError) as e:
201
+ raise ValueError(f"Failed to resolve reference '{ref_str}': {e}") from e
202
+
203
+ return item
204
+
205
+ def _extract_schema_as_dict(self, schema_obj: Any) -> JsonSchema:
206
+ """Resolves a schema and returns it as a dictionary."""
207
+ try:
208
+ resolved_schema = self._resolve_ref(schema_obj)
209
+
210
+ if isinstance(resolved_schema, (self.schema_cls)):
211
+ # Convert schema to dictionary
212
+ result = resolved_schema.model_dump(
213
+ mode="json", by_alias=True, exclude_none=True
214
+ )
215
+ elif isinstance(resolved_schema, dict):
216
+ result = resolved_schema
217
+ else:
218
+ logger.warning(
219
+ f"Expected Schema after resolving, got {type(resolved_schema)}. Returning empty dict."
220
+ )
221
+ result = {}
222
+
223
+ return _replace_ref_with_defs(result)
224
+ except ValueError as e:
225
+ # Re-raise ValueError for external reference errors and other validation issues
226
+ if "External or non-local reference not supported" in str(e):
227
+ raise
228
+ logger.error(f"Failed to extract schema as dict: {e}", exc_info=False)
229
+ return {}
230
+ except Exception as e:
231
+ logger.error(f"Failed to extract schema as dict: {e}", exc_info=False)
232
+ return {}
233
+
234
+ def _extract_parameters(
235
+ self,
236
+ operation_params: list[Any] | None = None,
237
+ path_item_params: list[Any] | None = None,
238
+ ) -> list[ParameterInfo]:
239
+ """Extract and resolve parameters from operation and path item."""
240
+ extracted_params: list[ParameterInfo] = []
241
+ seen_params: dict[
242
+ tuple[str, str], bool
243
+ ] = {} # Use tuple of (name, location) as key
244
+ all_params = (operation_params or []) + (path_item_params or [])
245
+
246
+ for param_or_ref in all_params:
247
+ try:
248
+ parameter = self._resolve_ref(param_or_ref)
249
+
250
+ if not isinstance(parameter, self.parameter_cls):
251
+ logger.warning(
252
+ f"Expected Parameter after resolving, got {type(parameter)}. Skipping."
253
+ )
254
+ continue
255
+
256
+ # Extract parameter info - handle both 3.0 and 3.1 parameter models
257
+ param_in = parameter.param_in # Both use param_in
258
+ # Handle enum or string parameter locations
259
+ from enum import Enum
260
+
261
+ param_in_str = (
262
+ param_in.value if isinstance(param_in, Enum) else param_in
263
+ )
264
+ param_location = self._convert_to_parameter_location(param_in_str)
265
+ param_schema_obj = parameter.param_schema # Both use param_schema
266
+
267
+ # Skip duplicate parameters (same name and location)
268
+ param_key = (parameter.name, param_in_str)
269
+ if param_key in seen_params:
270
+ continue
271
+ seen_params[param_key] = True
272
+
273
+ # Extract schema
274
+ param_schema_dict = {}
275
+ if param_schema_obj:
276
+ # Process schema object
277
+ param_schema_dict = self._extract_schema_as_dict(param_schema_obj)
278
+
279
+ # Handle default value
280
+ resolved_schema = self._resolve_ref(param_schema_obj)
281
+ if (
282
+ not isinstance(resolved_schema, self.reference_cls)
283
+ and hasattr(resolved_schema, "default")
284
+ and resolved_schema.default is not None
285
+ ):
286
+ param_schema_dict["default"] = resolved_schema.default
287
+
288
+ elif hasattr(parameter, "content") and parameter.content:
289
+ # Handle content-based parameters
290
+ first_media_type = next(iter(parameter.content.values()), None)
291
+ if (
292
+ first_media_type
293
+ and hasattr(first_media_type, "media_type_schema")
294
+ and first_media_type.media_type_schema
295
+ ):
296
+ media_schema = first_media_type.media_type_schema
297
+ param_schema_dict = self._extract_schema_as_dict(media_schema)
298
+
299
+ # Handle default value in content schema
300
+ resolved_media_schema = self._resolve_ref(media_schema)
301
+ if (
302
+ not isinstance(resolved_media_schema, self.reference_cls)
303
+ and hasattr(resolved_media_schema, "default")
304
+ and resolved_media_schema.default is not None
305
+ ):
306
+ param_schema_dict["default"] = resolved_media_schema.default
307
+
308
+ # Extract explode and style properties if present
309
+ explode = getattr(parameter, "explode", None)
310
+ style = getattr(parameter, "style", None)
311
+
312
+ # Create parameter info object
313
+ param_info = ParameterInfo(
314
+ name=parameter.name,
315
+ location=param_location,
316
+ required=parameter.required,
317
+ schema=param_schema_dict,
318
+ description=parameter.description,
319
+ explode=explode,
320
+ style=style,
321
+ )
322
+ extracted_params.append(param_info)
323
+ except Exception as e:
324
+ param_name = getattr(
325
+ param_or_ref, "name", getattr(param_or_ref, "ref", "unknown")
326
+ )
327
+ logger.error(
328
+ f"Failed to extract parameter '{param_name}': {e}", exc_info=False
329
+ )
330
+
331
+ return extracted_params
332
+
333
+ def _extract_request_body(self, request_body_or_ref: Any) -> RequestBodyInfo | None:
334
+ """Extract and resolve request body information."""
335
+ if not request_body_or_ref:
336
+ return None
337
+
338
+ try:
339
+ request_body = self._resolve_ref(request_body_or_ref)
340
+
341
+ if not isinstance(request_body, self.request_body_cls):
342
+ logger.warning(
343
+ f"Expected RequestBody after resolving, got {type(request_body)}. Returning None."
344
+ )
345
+ return None
346
+
347
+ # Create request body info
348
+ request_body_info = RequestBodyInfo(
349
+ required=request_body.required,
350
+ description=request_body.description,
351
+ )
352
+
353
+ # Extract content schemas
354
+ if hasattr(request_body, "content") and request_body.content:
355
+ for media_type_str, media_type_obj in request_body.content.items():
356
+ if (
357
+ media_type_obj
358
+ and hasattr(media_type_obj, "media_type_schema")
359
+ and media_type_obj.media_type_schema
360
+ ):
361
+ try:
362
+ schema_dict = self._extract_schema_as_dict(
363
+ media_type_obj.media_type_schema
364
+ )
365
+ request_body_info.content_schema[media_type_str] = (
366
+ schema_dict
367
+ )
368
+ except ValueError as e:
369
+ # Re-raise ValueError for external reference errors
370
+ if "External or non-local reference not supported" in str(
371
+ e
372
+ ):
373
+ raise
374
+ logger.error(
375
+ f"Failed to extract schema for media type '{media_type_str}': {e}"
376
+ )
377
+ except Exception as e:
378
+ logger.error(
379
+ f"Failed to extract schema for media type '{media_type_str}': {e}"
380
+ )
381
+
382
+ return request_body_info
383
+ except ValueError as e:
384
+ # Re-raise ValueError for external reference errors
385
+ if "External or non-local reference not supported" in str(e):
386
+ raise
387
+ ref_name = getattr(request_body_or_ref, "ref", "unknown")
388
+ logger.error(
389
+ f"Failed to extract request body '{ref_name}': {e}", exc_info=False
390
+ )
391
+ return None
392
+ except Exception as e:
393
+ ref_name = getattr(request_body_or_ref, "ref", "unknown")
394
+ logger.error(
395
+ f"Failed to extract request body '{ref_name}': {e}", exc_info=False
396
+ )
397
+ return None
398
+
399
+ def _extract_responses(
400
+ self, operation_responses: dict[str, Any] | None
401
+ ) -> dict[str, ResponseInfo]:
402
+ """Extract and resolve response information."""
403
+ extracted_responses: dict[str, ResponseInfo] = {}
404
+
405
+ if not operation_responses:
406
+ return extracted_responses
407
+
408
+ for status_code, resp_or_ref in operation_responses.items():
409
+ try:
410
+ response = self._resolve_ref(resp_or_ref)
411
+
412
+ if not isinstance(response, self.response_cls):
413
+ logger.warning(
414
+ f"Expected Response after resolving for status code {status_code}, "
415
+ f"got {type(response)}. Skipping."
416
+ )
417
+ continue
418
+
419
+ # Create response info
420
+ resp_info = ResponseInfo(description=response.description)
421
+
422
+ # Extract content schemas
423
+ if hasattr(response, "content") and response.content:
424
+ for media_type_str, media_type_obj in response.content.items():
425
+ if (
426
+ media_type_obj
427
+ and hasattr(media_type_obj, "media_type_schema")
428
+ and media_type_obj.media_type_schema
429
+ ):
430
+ try:
431
+ schema_dict = self._extract_schema_as_dict(
432
+ media_type_obj.media_type_schema
433
+ )
434
+ resp_info.content_schema[media_type_str] = schema_dict
435
+ except ValueError as e:
436
+ # Re-raise ValueError for external reference errors
437
+ if (
438
+ "External or non-local reference not supported"
439
+ in str(e)
440
+ ):
441
+ raise
442
+ logger.error(
443
+ f"Failed to extract schema for media type '{media_type_str}' "
444
+ f"in response {status_code}: {e}"
445
+ )
446
+ except Exception as e:
447
+ logger.error(
448
+ f"Failed to extract schema for media type '{media_type_str}' "
449
+ f"in response {status_code}: {e}"
450
+ )
451
+
452
+ extracted_responses[str(status_code)] = resp_info
453
+ except ValueError as e:
454
+ # Re-raise ValueError for external reference errors
455
+ if "External or non-local reference not supported" in str(e):
456
+ raise
457
+ ref_name = getattr(resp_or_ref, "ref", "unknown")
458
+ logger.error(
459
+ f"Failed to extract response for status code {status_code} "
460
+ f"from reference '{ref_name}': {e}",
461
+ exc_info=False,
462
+ )
463
+ except Exception as e:
464
+ ref_name = getattr(resp_or_ref, "ref", "unknown")
465
+ logger.error(
466
+ f"Failed to extract response for status code {status_code} "
467
+ f"from reference '{ref_name}': {e}",
468
+ exc_info=False,
469
+ )
470
+
471
+ return extracted_responses
472
+
473
+ def parse(self) -> list[HTTPRoute]:
474
+ """Parse the OpenAPI schema into HTTP routes."""
475
+ routes: list[HTTPRoute] = []
476
+
477
+ if not hasattr(self.openapi, "paths") or not self.openapi.paths:
478
+ logger.warning("OpenAPI schema has no paths defined.")
479
+ return []
480
+
481
+ # Extract component schemas
482
+ schema_definitions = {}
483
+ if hasattr(self.openapi, "components") and self.openapi.components:
484
+ components = self.openapi.components
485
+ if hasattr(components, "schemas") and components.schemas:
486
+ for name, schema in components.schemas.items():
487
+ try:
488
+ if isinstance(schema, self.reference_cls):
489
+ resolved_schema = self._resolve_ref(schema)
490
+ schema_definitions[name] = self._extract_schema_as_dict(
491
+ resolved_schema
492
+ )
493
+ else:
494
+ schema_definitions[name] = self._extract_schema_as_dict(
495
+ schema
496
+ )
497
+ except Exception as e:
498
+ logger.warning(
499
+ f"Failed to extract schema definition '{name}': {e}"
500
+ )
501
+
502
+ # Process paths and operations
503
+ for path_str, path_item_obj in self.openapi.paths.items():
504
+ if not isinstance(path_item_obj, self.path_item_cls):
505
+ logger.warning(
506
+ f"Skipping invalid path item for path '{path_str}' (type: {type(path_item_obj)})"
507
+ )
508
+ continue
509
+
510
+ path_level_params = (
511
+ path_item_obj.parameters
512
+ if hasattr(path_item_obj, "parameters")
513
+ else None
514
+ )
515
+
516
+ # Get HTTP methods from the path item class fields
517
+ http_methods = [
518
+ "get",
519
+ "put",
520
+ "post",
521
+ "delete",
522
+ "options",
523
+ "head",
524
+ "patch",
525
+ "trace",
526
+ ]
527
+ for method_lower in http_methods:
528
+ operation = getattr(path_item_obj, method_lower, None)
529
+
530
+ if operation and isinstance(operation, self.operation_cls):
531
+ # Cast method to HttpMethod - safe since we only use valid HTTP methods
532
+ method_upper = method_lower.upper()
533
+
534
+ try:
535
+ parameters = self._extract_parameters(
536
+ getattr(operation, "parameters", None), path_level_params
537
+ )
538
+
539
+ request_body_info = self._extract_request_body(
540
+ getattr(operation, "requestBody", None)
541
+ )
542
+
543
+ responses = self._extract_responses(
544
+ getattr(operation, "responses", None)
545
+ )
546
+
547
+ extensions = {}
548
+ if hasattr(operation, "model_extra") and operation.model_extra:
549
+ extensions = {
550
+ k: v
551
+ for k, v in operation.model_extra.items()
552
+ if k.startswith("x-")
553
+ }
554
+
555
+ # Create initial route without pre-calculated fields
556
+ route = HTTPRoute(
557
+ path=path_str,
558
+ method=method_upper, # type: ignore[arg-type] # Known valid HTTP method
559
+ operation_id=getattr(operation, "operationId", None),
560
+ summary=getattr(operation, "summary", None),
561
+ description=getattr(operation, "description", None),
562
+ tags=getattr(operation, "tags", []) or [],
563
+ parameters=parameters,
564
+ request_body=request_body_info,
565
+ responses=responses,
566
+ schema_definitions=schema_definitions,
567
+ extensions=extensions,
568
+ openapi_version=self.openapi_version,
569
+ )
570
+
571
+ # Pre-calculate schema and parameter mapping for performance
572
+ try:
573
+ flat_schema, param_map = _combine_schemas_and_map_params(
574
+ route
575
+ )
576
+ route.flat_param_schema = flat_schema
577
+ route.parameter_map = param_map
578
+ except Exception as schema_error:
579
+ logger.warning(
580
+ f"Failed to pre-calculate schema for route {method_upper} {path_str}: {schema_error}"
581
+ )
582
+ # Continue with empty pre-calculated fields
583
+ route.flat_param_schema = {
584
+ "type": "object",
585
+ "properties": {},
586
+ }
587
+ route.parameter_map = {}
588
+ routes.append(route)
589
+ logger.info(
590
+ f"Successfully extracted route: {method_upper} {path_str}"
591
+ )
592
+ except ValueError as op_error:
593
+ # Re-raise ValueError for external reference errors
594
+ if "External or non-local reference not supported" in str(
595
+ op_error
596
+ ):
597
+ raise
598
+ op_id = getattr(operation, "operationId", "unknown")
599
+ logger.error(
600
+ f"Failed to process operation {method_upper} {path_str} (ID: {op_id}): {op_error}",
601
+ exc_info=True,
602
+ )
603
+ except Exception as op_error:
604
+ op_id = getattr(operation, "operationId", "unknown")
605
+ logger.error(
606
+ f"Failed to process operation {method_upper} {path_str} (ID: {op_id}): {op_error}",
607
+ exc_info=True,
608
+ )
609
+
610
+ logger.info(f"Finished parsing. Extracted {len(routes)} HTTP routes.")
611
+ return routes
612
+
613
+
614
+ # Export public symbols
615
+ __all__ = [
616
+ "parse_openapi_to_http_routes",
617
+ "OpenAPIParser",
618
+ ]