Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
4805965
1
Parent(s): 7657683
Expose configurable timeout for OpenAPI
Browse files
src/fastmcp/server/openapi.py
CHANGED
|
@@ -125,6 +125,7 @@ class OpenAPITool(Tool):
|
|
| 125 |
fn_metadata: Any,
|
| 126 |
is_async: bool = True,
|
| 127 |
tags: set[str] = set(),
|
|
|
|
| 128 |
):
|
| 129 |
super().__init__(
|
| 130 |
name=name,
|
|
@@ -138,6 +139,7 @@ class OpenAPITool(Tool):
|
|
| 138 |
)
|
| 139 |
self._client = client
|
| 140 |
self._route = route
|
|
|
|
| 141 |
|
| 142 |
async def _execute_request(self, *args, **kwargs):
|
| 143 |
"""Execute the HTTP request based on the route configuration."""
|
|
@@ -206,7 +208,7 @@ class OpenAPITool(Tool):
|
|
| 206 |
params=query_params,
|
| 207 |
headers=headers,
|
| 208 |
json=json_data,
|
| 209 |
-
timeout=
|
| 210 |
)
|
| 211 |
|
| 212 |
# Raise for 4xx/5xx responses
|
|
@@ -254,6 +256,7 @@ class OpenAPIResource(Resource):
|
|
| 254 |
description: str,
|
| 255 |
mime_type: str = "application/json",
|
| 256 |
tags: set[str] = set(),
|
|
|
|
| 257 |
):
|
| 258 |
super().__init__(
|
| 259 |
uri=AnyUrl(uri), # Convert string to AnyUrl
|
|
@@ -264,6 +267,7 @@ class OpenAPIResource(Resource):
|
|
| 264 |
)
|
| 265 |
self._client = client
|
| 266 |
self._route = route
|
|
|
|
| 267 |
|
| 268 |
async def read(
|
| 269 |
self, context: Context[ServerSessionT, LifespanContextT] | None = None
|
|
@@ -301,7 +305,7 @@ class OpenAPIResource(Resource):
|
|
| 301 |
response = await self._client.request(
|
| 302 |
method=self._route.method,
|
| 303 |
url=path,
|
| 304 |
-
timeout=
|
| 305 |
)
|
| 306 |
|
| 307 |
# Raise for 4xx/5xx responses
|
|
@@ -349,6 +353,7 @@ class OpenAPIResourceTemplate(ResourceTemplate):
|
|
| 349 |
description: str,
|
| 350 |
parameters: dict[str, Any],
|
| 351 |
tags: set[str] = set(),
|
|
|
|
| 352 |
):
|
| 353 |
super().__init__(
|
| 354 |
uri_template=uri_template,
|
|
@@ -361,6 +366,7 @@ class OpenAPIResourceTemplate(ResourceTemplate):
|
|
| 361 |
)
|
| 362 |
self._client = client
|
| 363 |
self._route = route
|
|
|
|
| 364 |
|
| 365 |
async def create_resource(
|
| 366 |
self,
|
|
@@ -383,6 +389,7 @@ class OpenAPIResourceTemplate(ResourceTemplate):
|
|
| 383 |
description=self.description or f"Resource for {self._route.path}",
|
| 384 |
mime_type="application/json",
|
| 385 |
tags=set(self._route.tags or []),
|
|
|
|
| 386 |
)
|
| 387 |
|
| 388 |
|
|
@@ -430,6 +437,7 @@ class FastMCPOpenAPI(FastMCP):
|
|
| 430 |
client: httpx.AsyncClient,
|
| 431 |
name: str | None = None,
|
| 432 |
route_maps: list[RouteMap] | None = None,
|
|
|
|
| 433 |
**settings: Any,
|
| 434 |
):
|
| 435 |
"""
|
|
@@ -446,7 +454,7 @@ class FastMCPOpenAPI(FastMCP):
|
|
| 446 |
super().__init__(name=name or "OpenAPI FastMCP", **settings)
|
| 447 |
|
| 448 |
self._client = client
|
| 449 |
-
|
| 450 |
http_routes = openapi.parse_openapi_to_http_routes(openapi_spec)
|
| 451 |
|
| 452 |
# Process routes
|
|
@@ -504,6 +512,7 @@ class FastMCPOpenAPI(FastMCP):
|
|
| 504 |
fn_metadata=func_metadata(_openapi_passthrough),
|
| 505 |
is_async=True,
|
| 506 |
tags=set(route.tags or []),
|
|
|
|
| 507 |
)
|
| 508 |
# Register the tool by directly assigning to the tools dictionary
|
| 509 |
self._tool_manager._tools[tool_name] = tool
|
|
@@ -532,6 +541,7 @@ class FastMCPOpenAPI(FastMCP):
|
|
| 532 |
name=resource_name,
|
| 533 |
description=enhanced_description,
|
| 534 |
tags=set(route.tags or []),
|
|
|
|
| 535 |
)
|
| 536 |
# Register the resource by directly assigning to the resources dictionary
|
| 537 |
self._resource_manager._resources[str(resource.uri)] = resource
|
|
@@ -577,6 +587,7 @@ class FastMCPOpenAPI(FastMCP):
|
|
| 577 |
description=enhanced_description,
|
| 578 |
parameters=template_params_schema,
|
| 579 |
tags=set(route.tags or []),
|
|
|
|
| 580 |
)
|
| 581 |
# Register the template by directly assigning to the templates dictionary
|
| 582 |
self._resource_manager._templates[uri_template_str] = template
|
|
|
|
| 125 |
fn_metadata: Any,
|
| 126 |
is_async: bool = True,
|
| 127 |
tags: set[str] = set(),
|
| 128 |
+
timeout: float | None = None,
|
| 129 |
):
|
| 130 |
super().__init__(
|
| 131 |
name=name,
|
|
|
|
| 139 |
)
|
| 140 |
self._client = client
|
| 141 |
self._route = route
|
| 142 |
+
self._timeout = timeout
|
| 143 |
|
| 144 |
async def _execute_request(self, *args, **kwargs):
|
| 145 |
"""Execute the HTTP request based on the route configuration."""
|
|
|
|
| 208 |
params=query_params,
|
| 209 |
headers=headers,
|
| 210 |
json=json_data,
|
| 211 |
+
timeout=self._timeout,
|
| 212 |
)
|
| 213 |
|
| 214 |
# Raise for 4xx/5xx responses
|
|
|
|
| 256 |
description: str,
|
| 257 |
mime_type: str = "application/json",
|
| 258 |
tags: set[str] = set(),
|
| 259 |
+
timeout: float | None = None,
|
| 260 |
):
|
| 261 |
super().__init__(
|
| 262 |
uri=AnyUrl(uri), # Convert string to AnyUrl
|
|
|
|
| 267 |
)
|
| 268 |
self._client = client
|
| 269 |
self._route = route
|
| 270 |
+
self._timeout = timeout
|
| 271 |
|
| 272 |
async def read(
|
| 273 |
self, context: Context[ServerSessionT, LifespanContextT] | None = None
|
|
|
|
| 305 |
response = await self._client.request(
|
| 306 |
method=self._route.method,
|
| 307 |
url=path,
|
| 308 |
+
timeout=self._timeout,
|
| 309 |
)
|
| 310 |
|
| 311 |
# Raise for 4xx/5xx responses
|
|
|
|
| 353 |
description: str,
|
| 354 |
parameters: dict[str, Any],
|
| 355 |
tags: set[str] = set(),
|
| 356 |
+
timeout: float | None = None,
|
| 357 |
):
|
| 358 |
super().__init__(
|
| 359 |
uri_template=uri_template,
|
|
|
|
| 366 |
)
|
| 367 |
self._client = client
|
| 368 |
self._route = route
|
| 369 |
+
self._timeout = timeout
|
| 370 |
|
| 371 |
async def create_resource(
|
| 372 |
self,
|
|
|
|
| 389 |
description=self.description or f"Resource for {self._route.path}",
|
| 390 |
mime_type="application/json",
|
| 391 |
tags=set(self._route.tags or []),
|
| 392 |
+
timeout=self._timeout,
|
| 393 |
)
|
| 394 |
|
| 395 |
|
|
|
|
| 437 |
client: httpx.AsyncClient,
|
| 438 |
name: str | None = None,
|
| 439 |
route_maps: list[RouteMap] | None = None,
|
| 440 |
+
timeout: float | None = None,
|
| 441 |
**settings: Any,
|
| 442 |
):
|
| 443 |
"""
|
|
|
|
| 454 |
super().__init__(name=name or "OpenAPI FastMCP", **settings)
|
| 455 |
|
| 456 |
self._client = client
|
| 457 |
+
self._timeout = timeout
|
| 458 |
http_routes = openapi.parse_openapi_to_http_routes(openapi_spec)
|
| 459 |
|
| 460 |
# Process routes
|
|
|
|
| 512 |
fn_metadata=func_metadata(_openapi_passthrough),
|
| 513 |
is_async=True,
|
| 514 |
tags=set(route.tags or []),
|
| 515 |
+
timeout=self._timeout,
|
| 516 |
)
|
| 517 |
# Register the tool by directly assigning to the tools dictionary
|
| 518 |
self._tool_manager._tools[tool_name] = tool
|
|
|
|
| 541 |
name=resource_name,
|
| 542 |
description=enhanced_description,
|
| 543 |
tags=set(route.tags or []),
|
| 544 |
+
timeout=self._timeout,
|
| 545 |
)
|
| 546 |
# Register the resource by directly assigning to the resources dictionary
|
| 547 |
self._resource_manager._resources[str(resource.uri)] = resource
|
|
|
|
| 587 |
description=enhanced_description,
|
| 588 |
parameters=template_params_schema,
|
| 589 |
tags=set(route.tags or []),
|
| 590 |
+
timeout=self._timeout,
|
| 591 |
)
|
| 592 |
# Register the template by directly assigning to the templates dictionary
|
| 593 |
self._resource_manager._templates[uri_template_str] = template
|