|
17 | 17 | from pydantic.fields import FieldInfo |
18 | 18 | from pydantic.json_schema import GenerateJsonSchema, JsonSchemaWarningKind |
19 | 19 | from typing_extensions import is_typeddict |
20 | | -from typing_inspection.introspection import UNKNOWN, AnnotationSource, inspect_annotation, is_union_origin |
| 20 | +from typing_inspection.introspection import UNKNOWN, ForbiddenQualifier, AnnotationSource, inspect_annotation, is_union_origin |
21 | 21 |
|
22 | 22 | from mcp.server.fastmcp.exceptions import InvalidSignature |
23 | 23 | from mcp.server.fastmcp.utilities.logging import get_logger |
@@ -262,12 +262,16 @@ def func_metadata( |
262 | 262 | if sig.return_annotation is inspect.Parameter.empty and structured_output is True: |
263 | 263 | raise InvalidSignature(f"Function {func.__name__}: return annotation required for structured output") |
264 | 264 |
|
265 | | - inspected_return_ann = inspect_annotation(sig.return_annotation, annotation_source=AnnotationSource.FUNCTION) |
| 265 | + try: |
| 266 | + inspected_return_ann = inspect_annotation(sig.return_annotation, annotation_source=AnnotationSource.FUNCTION) |
| 267 | + except ForbiddenQualifier as e: |
| 268 | + raise InvalidSignature(f"Function {func.__name__}: return annotation contains an invalid type qualifier") from e |
| 269 | + |
266 | 270 | return_type_expr = inspected_return_ann.type |
267 | | - if return_type_expr is UNKNOWN and structured_output is True: |
268 | | - # `return_type_expr` is `UNKNOWN` when a bare type qualifier is used (unlikely to happen as a return annotation |
269 | | - # because it doesn't make any sense, but technically possible). |
270 | | - raise InvalidSignature(f"Function {func.__name__}: return annotation required for structured output") |
| 271 | + |
| 272 | + # `AnnotationSource.FUNCTION` allows no type qualifier to be used, so `return_type_expr` is guaranteed to *not* be |
| 273 | + # unknown (i.e. a bare `Final`). |
| 274 | + assert return_type_expr is not UNKNOWN |
271 | 275 |
|
272 | 276 | if is_union_origin(get_origin(return_type_expr)): |
273 | 277 | args = get_args(return_type_expr) |
|
0 commit comments