OpenAPI 模块¶
应用工厂¶
toolregistry_server.openapi.create_openapi_app ¶
create_openapi_app(route_table: RouteTable, title: str = 'ToolRegistry Server', version: str = '1.0.0', description: str = 'OpenAPI server for ToolRegistry tools', dependencies: Sequence[Any] | None = None, enable_etag: bool = True) -> FastAPI
Create a FastAPI application from a RouteTable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route_table
|
RouteTable
|
The RouteTable to expose. |
required |
title
|
str
|
API title for OpenAPI schema. |
'ToolRegistry Server'
|
version
|
str
|
API version for OpenAPI schema. |
'1.0.0'
|
description
|
str
|
API description for OpenAPI schema. |
'OpenAPI server for ToolRegistry tools'
|
dependencies
|
Sequence[Any] | None
|
Optional list of global dependencies (e.g., authentication). |
None
|
enable_etag
|
bool
|
Whether to enable ETag middleware for cache validation. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
FastAPI
|
A configured FastAPI application with: |
FastAPI
|
|
FastAPI
|
|
FastAPI
|
|
FastAPI
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If FastAPI is not installed. |
Source code in src/toolregistry_server/openapi/__init__.py
适配器¶
toolregistry_server.openapi.adapter ¶
Automatic route generation from a RouteTable.
Converts a :class:~toolregistry_server.RouteTable into a FastAPI
:class:~fastapi.APIRouter by introspecting each registered route
and dynamically creating Pydantic request models and route handlers.
add_tools_endpoint ¶
Add a /tools endpoint that returns the list of available tools.
This endpoint supports ETag-based cache validation. The response includes the current ETag in both the response body and headers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
route_table
|
RouteTable
|
The RouteTable to query for tools. |
required |
Example
Source code in src/toolregistry_server/openapi/adapter.py
route_table_to_router ¶
Convert a :class:~toolregistry_server.RouteTable into a FastAPI router.
Routes are generated for all registered tools regardless of their current enabled/disabled state. Each endpoint checks the route's enabled state at request time and returns HTTP 503 if the tool has been disabled, allowing runtime enable/disable without restarting the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route_table
|
RouteTable
|
The route table to convert. |
required |
prefix
|
str
|
URL prefix for all generated routes. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
APIRouter
|
class: |
Raises:
| Type | Description |
|---|---|
ImportError
|
If FastAPI is not installed. |
Source code in src/toolregistry_server/openapi/adapter.py
setup_dynamic_openapi ¶
Configure dynamic OpenAPI schema generation that filters out disabled tools.
This replaces FastAPI's default cached OpenAPI schema with a dynamic one
that checks tool enable/disable status on every request to /openapi.json.
Disabled tools are excluded from the schema so they do not appear in
/docs or /openapi.json, and re-enabling them makes them visible
again immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
route_table
|
RouteTable
|
The route table used for enable/disable status checks. |
required |
Source code in src/toolregistry_server/openapi/adapter.py
中间件¶
toolregistry_server.openapi.middleware ¶
ETag middleware for OpenAPI server.
This module provides middleware for ETag-based cache validation, supporting conditional requests with If-None-Match headers.
ETagMiddleware ¶
Middleware for ETag-based cache validation.
This middleware intercepts requests and checks the If-None-Match header against the current ETag from the RouteTable. If they match, it returns a 304 Not Modified response. Otherwise, it adds the current ETag to the response headers.
The middleware only applies ETag handling to GET requests on specific paths (e.g., /tools, /openapi.json) to avoid interfering with tool execution endpoints.
Example
Attributes:
| Name | Type | Description |
|---|---|---|
app |
The ASGI application to wrap. |
|
route_table |
The RouteTable instance for ETag generation. |
|
etag_paths |
Set of paths that should have ETag handling. |
Initialize the ETag middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
ASGIApp
|
The ASGI application to wrap. |
required |
route_table
|
RouteTable
|
The RouteTable instance for ETag generation. |
required |
Source code in src/toolregistry_server/openapi/middleware.py
__call__
async
¶
Process the request through the middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
Scope
|
The ASGI scope dictionary. |
required |
receive
|
Receive
|
The ASGI receive callable. |
required |
send
|
Send
|
The ASGI send callable. |
required |
Source code in src/toolregistry_server/openapi/middleware.py
add_etag_middleware ¶
Add ETag middleware to a FastAPI application.
This is a convenience function to add the ETagMiddleware to a FastAPI application with the correct configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
route_table
|
RouteTable
|
The RouteTable instance for ETag generation. |
required |