33import logging
44import urllib .parse
55from datetime import datetime
6- from typing import Any , Dict , Literal , Optional , Union , overload
6+ from typing import Any , Dict , Literal , Optional , Union , overload , TYPE_CHECKING
77from typing_extensions import Annotated , Doc
88
9+ if TYPE_CHECKING :
10+ from fastapi import FastAPI
11+ else :
12+ FastAPI = Any
13+
914from ..errors .exceptions import SpecInvalidError
1015from ..http .client import ApiClient
1116from ..http .schemas import DeploymentResponse , AuthConfig , UpsertDeploymentRequest
@@ -80,13 +85,27 @@ def deploy(
8085) -> DeploymentResult : ...
8186
8287
88+ @overload
89+ def deploy (
90+ * ,
91+ fastapi_app : FastAPI ,
92+ api_key : str ,
93+ base_url : Optional [str ] = None ,
94+ name : Optional [str ] = None ,
95+ auth_config : Optional [AuthConfig ] = None ,
96+ api_version : Literal ["05-2025" , "latest" ] = "latest" ,
97+ timeout : int = 30 ,
98+ ) -> DeploymentResult : ...
99+
100+
83101def deploy (
84102 * ,
85103 openapi_spec_path : Annotated [Optional [str ], Doc ("Path to an OpenAPI specification file (JSON or YAML)" )] = None ,
86104 openapi_spec_url : Annotated [Optional [str ], Doc ("URL to an OpenAPI specification" )] = None ,
87105 openapi_spec : Annotated [
88106 Optional [Union [Dict [str , Any ], OpenAPISpec ]], Doc ("OpenAPI specification as a dictionary or OpenAPISpec object" )
89107 ] = None ,
108+ fastapi_app : Annotated [Optional [FastAPI ], Doc ("FastAPI application instance" )] = None ,
90109 base_url : Annotated [
91110 Optional [str ],
92111 Doc ("Base URL of the API to proxy requests to. If not provided, will try to extract from the OpenAPI spec" ),
@@ -101,7 +120,7 @@ def deploy(
101120) -> DeploymentResult :
102121 """Deploy a Model Context Protocol (MCP) server from an OpenAPI specification.
103122
104- You must provide exactly one of: openapi_spec_path, openapi_spec_url, or openapi_spec .
123+ You must provide exactly one of: openapi_spec_path, openapi_spec_url, openapi_spec, or fastapi_app .
105124
106125 Returns:
107126 A DeploymentResult object containing details of the deployment.
@@ -116,11 +135,13 @@ def deploy(
116135 logger .info ("Deploying MCP server from OpenAPI spec" )
117136
118137 # Validate input - must have exactly one openapi_spec source
119- source_count = sum (1 for x in [openapi_spec_path , openapi_spec_url , openapi_spec ] if x is not None )
138+ source_count = sum (1 for x in [openapi_spec_path , openapi_spec_url , openapi_spec , fastapi_app ] if x is not None )
120139 if source_count == 0 :
121- raise ValueError ("One of openapi_spec_path, openapi_spec_url, or openapi_spec must be provided" )
140+ raise ValueError ("One of openapi_spec_path, openapi_spec_url, openapi_spec, or fastapi_app must be provided" )
122141 if source_count > 1 :
123- raise ValueError ("Only one of openapi_spec_path, openapi_spec_url, or openapi_spec should be provided" )
142+ raise ValueError (
143+ "Only one of openapi_spec_path, openapi_spec_url, openapi_spec, or fastapi_app should be provided"
144+ )
124145
125146 # Process OpenAPI spec from the provided source
126147 spec : Optional [OpenAPISpec ] = None
@@ -158,6 +179,9 @@ def deploy(
158179 details = {"url" : openapi_spec_url },
159180 cause = e ,
160181 )
182+ elif fastapi_app is not None :
183+ logger .info ("Loading OpenAPI spec from FastAPI app" )
184+ spec = OpenAPISpec .from_fastapi (fastapi_app )
161185 elif isinstance (openapi_spec , dict ):
162186 logger .info ("Using provided OpenAPI spec dictionary" )
163187 spec = OpenAPISpec .from_dict (openapi_spec )
0 commit comments