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.
- fastmcp/cli/cli.py +128 -33
- fastmcp/cli/install/claude_code.py +42 -1
- fastmcp/cli/install/claude_desktop.py +42 -1
- fastmcp/cli/install/cursor.py +42 -1
- fastmcp/cli/install/mcp_json.py +41 -0
- fastmcp/cli/run.py +127 -1
- fastmcp/client/__init__.py +2 -0
- fastmcp/client/auth/oauth.py +68 -99
- fastmcp/client/oauth_callback.py +18 -0
- fastmcp/client/transports.py +69 -15
- fastmcp/contrib/component_manager/example.py +2 -2
- fastmcp/experimental/server/openapi/README.md +266 -0
- fastmcp/experimental/server/openapi/__init__.py +38 -0
- fastmcp/experimental/server/openapi/components.py +348 -0
- fastmcp/experimental/server/openapi/routing.py +132 -0
- fastmcp/experimental/server/openapi/server.py +466 -0
- fastmcp/experimental/utilities/openapi/README.md +239 -0
- fastmcp/experimental/utilities/openapi/__init__.py +68 -0
- fastmcp/experimental/utilities/openapi/director.py +208 -0
- fastmcp/experimental/utilities/openapi/formatters.py +355 -0
- fastmcp/experimental/utilities/openapi/json_schema_converter.py +340 -0
- fastmcp/experimental/utilities/openapi/models.py +85 -0
- fastmcp/experimental/utilities/openapi/parser.py +618 -0
- fastmcp/experimental/utilities/openapi/schemas.py +538 -0
- fastmcp/mcp_config.py +125 -88
- fastmcp/prompts/prompt.py +11 -1
- fastmcp/resources/resource.py +21 -1
- fastmcp/resources/template.py +20 -1
- fastmcp/server/auth/__init__.py +17 -2
- fastmcp/server/auth/auth.py +144 -7
- fastmcp/server/auth/providers/bearer.py +25 -473
- fastmcp/server/auth/providers/in_memory.py +4 -2
- fastmcp/server/auth/providers/jwt.py +538 -0
- fastmcp/server/auth/providers/workos.py +170 -0
- fastmcp/server/auth/registry.py +52 -0
- fastmcp/server/context.py +107 -26
- fastmcp/server/dependencies.py +9 -2
- fastmcp/server/http.py +62 -30
- fastmcp/server/middleware/middleware.py +3 -23
- fastmcp/server/openapi.py +1 -1
- fastmcp/server/proxy.py +50 -11
- fastmcp/server/server.py +168 -59
- fastmcp/settings.py +73 -6
- fastmcp/tools/tool.py +36 -3
- fastmcp/tools/tool_manager.py +38 -2
- fastmcp/tools/tool_transform.py +112 -3
- fastmcp/utilities/components.py +35 -2
- fastmcp/utilities/json_schema.py +136 -98
- fastmcp/utilities/json_schema_type.py +1 -3
- fastmcp/utilities/mcp_config.py +28 -0
- fastmcp/utilities/openapi.py +240 -50
- fastmcp/utilities/tests.py +54 -6
- fastmcp/utilities/types.py +89 -11
- {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/METADATA +4 -3
- fastmcp-2.11.0.dist-info/RECORD +108 -0
- fastmcp/server/auth/providers/bearer_env.py +0 -63
- fastmcp/utilities/cache.py +0 -26
- fastmcp-2.10.6.dist-info/RECORD +0 -93
- {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/WHEEL +0 -0
- {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/entry_points.txt +0 -0
- {fastmcp-2.10.6.dist-info → fastmcp-2.11.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Route mapping logic for OpenAPI operations."""
|
|
2
|
+
|
|
3
|
+
import enum
|
|
4
|
+
import re
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
from re import Pattern
|
|
8
|
+
from typing import TYPE_CHECKING, Literal
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from .components import (
|
|
12
|
+
OpenAPIResource,
|
|
13
|
+
OpenAPIResourceTemplate,
|
|
14
|
+
OpenAPITool,
|
|
15
|
+
)
|
|
16
|
+
# Import from our new utilities
|
|
17
|
+
from fastmcp.experimental.utilities.openapi import HttpMethod, HTTPRoute
|
|
18
|
+
from fastmcp.utilities.logging import get_logger
|
|
19
|
+
|
|
20
|
+
logger = get_logger(__name__)
|
|
21
|
+
|
|
22
|
+
# Type definitions for the mapping functions
|
|
23
|
+
RouteMapFn = Callable[[HTTPRoute, "MCPType"], "MCPType | None"]
|
|
24
|
+
ComponentFn = Callable[
|
|
25
|
+
[
|
|
26
|
+
HTTPRoute,
|
|
27
|
+
"OpenAPITool | OpenAPIResource | OpenAPIResourceTemplate",
|
|
28
|
+
],
|
|
29
|
+
None,
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class MCPType(enum.Enum):
|
|
34
|
+
"""Type of FastMCP component to create from a route.
|
|
35
|
+
|
|
36
|
+
Enum values:
|
|
37
|
+
TOOL: Convert the route to a callable Tool
|
|
38
|
+
RESOURCE: Convert the route to a Resource (typically GET endpoints)
|
|
39
|
+
RESOURCE_TEMPLATE: Convert the route to a ResourceTemplate (typically GET with path params)
|
|
40
|
+
EXCLUDE: Exclude the route from being converted to any MCP component
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
TOOL = "TOOL"
|
|
44
|
+
RESOURCE = "RESOURCE"
|
|
45
|
+
RESOURCE_TEMPLATE = "RESOURCE_TEMPLATE"
|
|
46
|
+
# PROMPT = "PROMPT"
|
|
47
|
+
EXCLUDE = "EXCLUDE"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(kw_only=True)
|
|
51
|
+
class RouteMap:
|
|
52
|
+
"""Mapping configuration for HTTP routes to FastMCP component types."""
|
|
53
|
+
|
|
54
|
+
methods: list[HttpMethod] | Literal["*"] = field(default="*")
|
|
55
|
+
pattern: Pattern[str] | str = field(default=r".*")
|
|
56
|
+
|
|
57
|
+
tags: set[str] = field(
|
|
58
|
+
default_factory=set,
|
|
59
|
+
metadata={"description": "A set of tags to match. All tags must match."},
|
|
60
|
+
)
|
|
61
|
+
mcp_type: MCPType = field(
|
|
62
|
+
metadata={"description": "The type of FastMCP component to create."},
|
|
63
|
+
)
|
|
64
|
+
mcp_tags: set[str] = field(
|
|
65
|
+
default_factory=set,
|
|
66
|
+
metadata={
|
|
67
|
+
"description": "A set of tags to apply to the generated FastMCP component."
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# Default route mapping: all routes become tools.
|
|
73
|
+
# Users can provide custom route_maps to override this behavior.
|
|
74
|
+
DEFAULT_ROUTE_MAPPINGS = [
|
|
75
|
+
RouteMap(mcp_type=MCPType.TOOL),
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _determine_route_type(
|
|
80
|
+
route: HTTPRoute,
|
|
81
|
+
mappings: list[RouteMap],
|
|
82
|
+
) -> RouteMap:
|
|
83
|
+
"""
|
|
84
|
+
Determines the FastMCP component type based on the route and mappings.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
route: HTTPRoute object
|
|
88
|
+
mappings: List of RouteMap objects in priority order
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
The RouteMap that matches the route, or a catchall "Tool" RouteMap if no match is found.
|
|
92
|
+
"""
|
|
93
|
+
# Check mappings in priority order (first match wins)
|
|
94
|
+
for route_map in mappings:
|
|
95
|
+
# Check if the HTTP method matches
|
|
96
|
+
if route_map.methods == "*" or route.method in route_map.methods:
|
|
97
|
+
# Handle both string patterns and compiled Pattern objects
|
|
98
|
+
if isinstance(route_map.pattern, Pattern):
|
|
99
|
+
pattern_matches = route_map.pattern.search(route.path)
|
|
100
|
+
else:
|
|
101
|
+
pattern_matches = re.search(route_map.pattern, route.path)
|
|
102
|
+
|
|
103
|
+
if pattern_matches:
|
|
104
|
+
# Check if tags match (if specified)
|
|
105
|
+
# If route_map.tags is empty, tags are not matched
|
|
106
|
+
# If route_map.tags is non-empty, all tags must be present in route.tags (AND condition)
|
|
107
|
+
if route_map.tags:
|
|
108
|
+
route_tags_set = set(route.tags or [])
|
|
109
|
+
if not route_map.tags.issubset(route_tags_set):
|
|
110
|
+
# Tags don't match, continue to next mapping
|
|
111
|
+
continue
|
|
112
|
+
|
|
113
|
+
# We know mcp_type is not None here due to post_init validation
|
|
114
|
+
assert route_map.mcp_type is not None
|
|
115
|
+
logger.debug(
|
|
116
|
+
f"Route {route.method} {route.path} matched mapping to {route_map.mcp_type.name}"
|
|
117
|
+
)
|
|
118
|
+
return route_map
|
|
119
|
+
|
|
120
|
+
# Default fallback
|
|
121
|
+
return RouteMap(mcp_type=MCPType.TOOL)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# Export public symbols
|
|
125
|
+
__all__ = [
|
|
126
|
+
"MCPType",
|
|
127
|
+
"RouteMap",
|
|
128
|
+
"RouteMapFn",
|
|
129
|
+
"ComponentFn",
|
|
130
|
+
"DEFAULT_ROUTE_MAPPINGS",
|
|
131
|
+
"_determine_route_type",
|
|
132
|
+
]
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
"""FastMCP server implementation for OpenAPI integration."""
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from collections import Counter
|
|
5
|
+
from typing import Any, Literal
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
from jsonschema_path import SchemaPath
|
|
9
|
+
|
|
10
|
+
# Import from our new utilities and components
|
|
11
|
+
from fastmcp.experimental.utilities.openapi import (
|
|
12
|
+
HTTPRoute,
|
|
13
|
+
extract_output_schema_from_responses,
|
|
14
|
+
format_description_with_responses,
|
|
15
|
+
parse_openapi_to_http_routes,
|
|
16
|
+
)
|
|
17
|
+
from fastmcp.experimental.utilities.openapi.director import RequestDirector
|
|
18
|
+
from fastmcp.server.server import FastMCP
|
|
19
|
+
from fastmcp.utilities.logging import get_logger
|
|
20
|
+
|
|
21
|
+
from .components import (
|
|
22
|
+
OpenAPIResource,
|
|
23
|
+
OpenAPIResourceTemplate,
|
|
24
|
+
OpenAPITool,
|
|
25
|
+
)
|
|
26
|
+
from .routing import (
|
|
27
|
+
DEFAULT_ROUTE_MAPPINGS,
|
|
28
|
+
ComponentFn,
|
|
29
|
+
MCPType,
|
|
30
|
+
RouteMap,
|
|
31
|
+
RouteMapFn,
|
|
32
|
+
_determine_route_type,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
logger = get_logger(__name__)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _slugify(text: str) -> str:
|
|
39
|
+
"""
|
|
40
|
+
Convert text to a URL-friendly slug format that only contains lowercase
|
|
41
|
+
letters, uppercase letters, numbers, and underscores.
|
|
42
|
+
"""
|
|
43
|
+
if not text:
|
|
44
|
+
return ""
|
|
45
|
+
|
|
46
|
+
# Replace spaces and common separators with underscores
|
|
47
|
+
slug = re.sub(r"[\s\-\.]+", "_", text)
|
|
48
|
+
|
|
49
|
+
# Remove non-alphanumeric characters except underscores
|
|
50
|
+
slug = re.sub(r"[^a-zA-Z0-9_]", "", slug)
|
|
51
|
+
|
|
52
|
+
# Remove multiple consecutive underscores
|
|
53
|
+
slug = re.sub(r"_+", "_", slug)
|
|
54
|
+
|
|
55
|
+
# Remove leading/trailing underscores
|
|
56
|
+
slug = slug.strip("_")
|
|
57
|
+
|
|
58
|
+
return slug
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class FastMCPOpenAPI(FastMCP):
|
|
62
|
+
"""
|
|
63
|
+
FastMCP server implementation that creates components from an OpenAPI schema.
|
|
64
|
+
|
|
65
|
+
This class parses an OpenAPI specification and creates appropriate FastMCP components
|
|
66
|
+
(Tools, Resources, ResourceTemplates) based on route mappings.
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
```python
|
|
70
|
+
from fastmcp.server.openapi import FastMCPOpenAPI, RouteMap, MCPType
|
|
71
|
+
import httpx
|
|
72
|
+
|
|
73
|
+
# Define custom route mappings
|
|
74
|
+
custom_mappings = [
|
|
75
|
+
# Map all user-related endpoints to ResourceTemplate
|
|
76
|
+
RouteMap(
|
|
77
|
+
methods=["GET", "POST", "PATCH"],
|
|
78
|
+
pattern=r".*/users/.*",
|
|
79
|
+
mcp_type=MCPType.RESOURCE_TEMPLATE
|
|
80
|
+
),
|
|
81
|
+
# Map all analytics endpoints to Tool
|
|
82
|
+
RouteMap(
|
|
83
|
+
methods=["GET"],
|
|
84
|
+
pattern=r".*/analytics/.*",
|
|
85
|
+
mcp_type=MCPType.TOOL
|
|
86
|
+
),
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
# Create server with custom mappings and route mapper
|
|
90
|
+
server = FastMCPOpenAPI(
|
|
91
|
+
openapi_spec=spec,
|
|
92
|
+
client=httpx.AsyncClient(),
|
|
93
|
+
name="API Server",
|
|
94
|
+
route_maps=custom_mappings,
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
def __init__(
|
|
100
|
+
self,
|
|
101
|
+
openapi_spec: dict[str, Any],
|
|
102
|
+
client: httpx.AsyncClient,
|
|
103
|
+
name: str | None = None,
|
|
104
|
+
route_maps: list[RouteMap] | None = None,
|
|
105
|
+
route_map_fn: RouteMapFn | None = None,
|
|
106
|
+
mcp_component_fn: ComponentFn | None = None,
|
|
107
|
+
mcp_names: dict[str, str] | None = None,
|
|
108
|
+
tags: set[str] | None = None,
|
|
109
|
+
timeout: float | None = None,
|
|
110
|
+
**settings: Any,
|
|
111
|
+
):
|
|
112
|
+
"""
|
|
113
|
+
Initialize a FastMCP server from an OpenAPI schema.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
openapi_spec: OpenAPI schema as a dictionary or file path
|
|
117
|
+
client: httpx AsyncClient for making HTTP requests
|
|
118
|
+
name: Optional name for the server
|
|
119
|
+
route_maps: Optional list of RouteMap objects defining route mappings
|
|
120
|
+
route_map_fn: Optional callable for advanced route type mapping.
|
|
121
|
+
Receives (route, mcp_type) and returns MCPType or None.
|
|
122
|
+
Called on every route, including excluded ones.
|
|
123
|
+
mcp_component_fn: Optional callable for component customization.
|
|
124
|
+
Receives (route, component) and can modify the component in-place.
|
|
125
|
+
Called on every created component.
|
|
126
|
+
mcp_names: Optional dictionary mapping operationId to desired component names.
|
|
127
|
+
If an operationId is not in the dictionary, falls back to using the
|
|
128
|
+
operationId up to the first double underscore. If no operationId exists,
|
|
129
|
+
falls back to slugified summary or path-based naming.
|
|
130
|
+
All names are truncated to 56 characters maximum.
|
|
131
|
+
tags: Optional set of tags to add to all components. Components always receive any tags
|
|
132
|
+
from the route.
|
|
133
|
+
timeout: Optional timeout (in seconds) for all requests
|
|
134
|
+
**settings: Additional settings for FastMCP
|
|
135
|
+
"""
|
|
136
|
+
super().__init__(name=name or "OpenAPI FastMCP", **settings)
|
|
137
|
+
|
|
138
|
+
self._client = client
|
|
139
|
+
self._timeout = timeout
|
|
140
|
+
self._mcp_component_fn = mcp_component_fn
|
|
141
|
+
|
|
142
|
+
# Keep track of names to detect collisions
|
|
143
|
+
self._used_names = {
|
|
144
|
+
"tool": Counter(),
|
|
145
|
+
"resource": Counter(),
|
|
146
|
+
"resource_template": Counter(),
|
|
147
|
+
"prompt": Counter(),
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
# Create openapi-core Spec and RequestDirector for stateless request building
|
|
151
|
+
try:
|
|
152
|
+
self._spec = SchemaPath.from_dict(openapi_spec) # type: ignore[arg-type]
|
|
153
|
+
self._director = RequestDirector(self._spec)
|
|
154
|
+
logger.debug(
|
|
155
|
+
"Initialized OpenAPI RequestDirector for stateless request building"
|
|
156
|
+
)
|
|
157
|
+
except Exception as e:
|
|
158
|
+
logger.error(f"Failed to initialize RequestDirector: {e}")
|
|
159
|
+
raise ValueError(f"Invalid OpenAPI specification: {e}") from e
|
|
160
|
+
|
|
161
|
+
http_routes = parse_openapi_to_http_routes(openapi_spec)
|
|
162
|
+
|
|
163
|
+
# Process routes
|
|
164
|
+
route_maps = (route_maps or []) + DEFAULT_ROUTE_MAPPINGS
|
|
165
|
+
for route in http_routes:
|
|
166
|
+
# Determine route type based on mappings or default rules
|
|
167
|
+
route_map = _determine_route_type(route, route_maps)
|
|
168
|
+
|
|
169
|
+
# TODO: remove this once RouteType is removed and mcp_type is typed as MCPType without | None
|
|
170
|
+
assert route_map.mcp_type is not None
|
|
171
|
+
route_type = route_map.mcp_type
|
|
172
|
+
|
|
173
|
+
# Call route_map_fn if provided
|
|
174
|
+
if route_map_fn is not None:
|
|
175
|
+
try:
|
|
176
|
+
result = route_map_fn(route, route_type)
|
|
177
|
+
if result is not None:
|
|
178
|
+
route_type = result
|
|
179
|
+
logger.debug(
|
|
180
|
+
f"Route {route.method} {route.path} mapping customized by route_map_fn: "
|
|
181
|
+
f"type={route_type.name}"
|
|
182
|
+
)
|
|
183
|
+
except Exception as e:
|
|
184
|
+
logger.warning(
|
|
185
|
+
f"Error in route_map_fn for {route.method} {route.path}: {e}. "
|
|
186
|
+
f"Using default values."
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
# Generate a default name from the route
|
|
190
|
+
component_name = self._generate_default_name(route, mcp_names)
|
|
191
|
+
|
|
192
|
+
route_tags = set(route.tags) | route_map.mcp_tags | (tags or set())
|
|
193
|
+
|
|
194
|
+
# Create components using simplified approach with RequestDirector
|
|
195
|
+
if route_type == MCPType.TOOL:
|
|
196
|
+
self._create_openapi_tool(route, component_name, tags=route_tags)
|
|
197
|
+
elif route_type == MCPType.RESOURCE:
|
|
198
|
+
self._create_openapi_resource(route, component_name, tags=route_tags)
|
|
199
|
+
elif route_type == MCPType.RESOURCE_TEMPLATE:
|
|
200
|
+
self._create_openapi_template(route, component_name, tags=route_tags)
|
|
201
|
+
elif route_type == MCPType.EXCLUDE:
|
|
202
|
+
logger.debug(f"Excluding route: {route.method} {route.path}")
|
|
203
|
+
|
|
204
|
+
logger.debug(f"Created FastMCP OpenAPI server with {len(http_routes)} routes")
|
|
205
|
+
|
|
206
|
+
def _generate_default_name(
|
|
207
|
+
self, route: HTTPRoute, mcp_names_map: dict[str, str] | None = None
|
|
208
|
+
) -> str:
|
|
209
|
+
"""Generate a default name from the route using the configured strategy."""
|
|
210
|
+
name = ""
|
|
211
|
+
mcp_names_map = mcp_names_map or {}
|
|
212
|
+
|
|
213
|
+
# First check if there's a custom mapping for this operationId
|
|
214
|
+
if route.operation_id:
|
|
215
|
+
if route.operation_id in mcp_names_map:
|
|
216
|
+
name = mcp_names_map[route.operation_id]
|
|
217
|
+
else:
|
|
218
|
+
# If there's a double underscore in the operationId, use the first part
|
|
219
|
+
name = route.operation_id.split("__")[0]
|
|
220
|
+
else:
|
|
221
|
+
name = route.summary or f"{route.method}_{route.path}"
|
|
222
|
+
|
|
223
|
+
name = _slugify(name)
|
|
224
|
+
|
|
225
|
+
# Truncate to 56 characters maximum
|
|
226
|
+
if len(name) > 56:
|
|
227
|
+
name = name[:56]
|
|
228
|
+
|
|
229
|
+
return name
|
|
230
|
+
|
|
231
|
+
def _get_unique_name(
|
|
232
|
+
self,
|
|
233
|
+
name: str,
|
|
234
|
+
component_type: Literal["tool", "resource", "resource_template", "prompt"],
|
|
235
|
+
) -> str:
|
|
236
|
+
"""
|
|
237
|
+
Ensure the name is unique within its component type by appending numbers if needed.
|
|
238
|
+
|
|
239
|
+
Args:
|
|
240
|
+
name: The proposed name
|
|
241
|
+
component_type: The type of component ("tools", "resources", or "templates")
|
|
242
|
+
|
|
243
|
+
Returns:
|
|
244
|
+
str: A unique name for the component
|
|
245
|
+
"""
|
|
246
|
+
# Check if the name is already used
|
|
247
|
+
self._used_names[component_type][name] += 1
|
|
248
|
+
if self._used_names[component_type][name] == 1:
|
|
249
|
+
return name
|
|
250
|
+
|
|
251
|
+
else:
|
|
252
|
+
# Create the new name
|
|
253
|
+
new_name = f"{name}_{self._used_names[component_type][name]}"
|
|
254
|
+
logger.debug(
|
|
255
|
+
f"Name collision detected: '{name}' already exists as a {component_type[:-1]}. "
|
|
256
|
+
f"Using '{new_name}' instead."
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
return new_name
|
|
260
|
+
|
|
261
|
+
def _create_openapi_tool(
|
|
262
|
+
self,
|
|
263
|
+
route: HTTPRoute,
|
|
264
|
+
name: str,
|
|
265
|
+
tags: set[str],
|
|
266
|
+
):
|
|
267
|
+
"""Creates and registers an OpenAPITool with enhanced description."""
|
|
268
|
+
# Use pre-calculated schema from route
|
|
269
|
+
combined_schema = route.flat_param_schema
|
|
270
|
+
|
|
271
|
+
# Extract output schema from OpenAPI responses
|
|
272
|
+
output_schema = extract_output_schema_from_responses(
|
|
273
|
+
route.responses, route.schema_definitions, route.openapi_version
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
# Get a unique tool name
|
|
277
|
+
tool_name = self._get_unique_name(name, "tool")
|
|
278
|
+
|
|
279
|
+
base_description = (
|
|
280
|
+
route.description
|
|
281
|
+
or route.summary
|
|
282
|
+
or f"Executes {route.method} {route.path}"
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
# Format enhanced description with parameters and request body
|
|
286
|
+
enhanced_description = format_description_with_responses(
|
|
287
|
+
base_description=base_description,
|
|
288
|
+
responses=route.responses,
|
|
289
|
+
parameters=route.parameters,
|
|
290
|
+
request_body=route.request_body,
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
tool = OpenAPITool(
|
|
294
|
+
client=self._client,
|
|
295
|
+
route=route,
|
|
296
|
+
director=self._director,
|
|
297
|
+
name=tool_name,
|
|
298
|
+
description=enhanced_description,
|
|
299
|
+
parameters=combined_schema,
|
|
300
|
+
output_schema=output_schema,
|
|
301
|
+
tags=set(route.tags or []) | tags,
|
|
302
|
+
timeout=self._timeout,
|
|
303
|
+
)
|
|
304
|
+
|
|
305
|
+
# Call component_fn if provided
|
|
306
|
+
if self._mcp_component_fn is not None:
|
|
307
|
+
try:
|
|
308
|
+
self._mcp_component_fn(route, tool)
|
|
309
|
+
logger.debug(f"Tool {tool_name} customized by component_fn")
|
|
310
|
+
except Exception as e:
|
|
311
|
+
logger.warning(
|
|
312
|
+
f"Error in component_fn for tool {tool_name}: {e}. "
|
|
313
|
+
f"Using component as-is."
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
# Use the potentially modified tool name as the registration key
|
|
317
|
+
final_tool_name = tool.name
|
|
318
|
+
|
|
319
|
+
# Register the tool by directly assigning to the tools dictionary
|
|
320
|
+
self._tool_manager._tools[final_tool_name] = tool
|
|
321
|
+
logger.debug(
|
|
322
|
+
f"Registered TOOL: {final_tool_name} ({route.method} {route.path}) with tags: {route.tags}"
|
|
323
|
+
)
|
|
324
|
+
|
|
325
|
+
def _create_openapi_resource(
|
|
326
|
+
self,
|
|
327
|
+
route: HTTPRoute,
|
|
328
|
+
name: str,
|
|
329
|
+
tags: set[str],
|
|
330
|
+
):
|
|
331
|
+
"""Creates and registers an OpenAPIResource with enhanced description."""
|
|
332
|
+
# Get a unique resource name
|
|
333
|
+
resource_name = self._get_unique_name(name, "resource")
|
|
334
|
+
|
|
335
|
+
resource_uri = f"resource://{resource_name}"
|
|
336
|
+
base_description = (
|
|
337
|
+
route.description or route.summary or f"Represents {route.path}"
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
# Format enhanced description with parameters and request body
|
|
341
|
+
enhanced_description = format_description_with_responses(
|
|
342
|
+
base_description=base_description,
|
|
343
|
+
responses=route.responses,
|
|
344
|
+
parameters=route.parameters,
|
|
345
|
+
request_body=route.request_body,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
resource = OpenAPIResource(
|
|
349
|
+
client=self._client,
|
|
350
|
+
route=route,
|
|
351
|
+
director=self._director,
|
|
352
|
+
uri=resource_uri,
|
|
353
|
+
name=resource_name,
|
|
354
|
+
description=enhanced_description,
|
|
355
|
+
tags=set(route.tags or []) | tags,
|
|
356
|
+
timeout=self._timeout,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
# Call component_fn if provided
|
|
360
|
+
if self._mcp_component_fn is not None:
|
|
361
|
+
try:
|
|
362
|
+
self._mcp_component_fn(route, resource)
|
|
363
|
+
logger.debug(f"Resource {resource_uri} customized by component_fn")
|
|
364
|
+
except Exception as e:
|
|
365
|
+
logger.warning(
|
|
366
|
+
f"Error in component_fn for resource {resource_uri}: {e}. "
|
|
367
|
+
f"Using component as-is."
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
# Use the potentially modified resource URI as the registration key
|
|
371
|
+
final_resource_uri = str(resource.uri)
|
|
372
|
+
|
|
373
|
+
# Register the resource by directly assigning to the resources dictionary
|
|
374
|
+
self._resource_manager._resources[final_resource_uri] = resource
|
|
375
|
+
logger.debug(
|
|
376
|
+
f"Registered RESOURCE: {final_resource_uri} ({route.method} {route.path}) with tags: {route.tags}"
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
def _create_openapi_template(
|
|
380
|
+
self,
|
|
381
|
+
route: HTTPRoute,
|
|
382
|
+
name: str,
|
|
383
|
+
tags: set[str],
|
|
384
|
+
):
|
|
385
|
+
"""Creates and registers an OpenAPIResourceTemplate with enhanced description."""
|
|
386
|
+
# Get a unique template name
|
|
387
|
+
template_name = self._get_unique_name(name, "resource_template")
|
|
388
|
+
|
|
389
|
+
path_params = [p.name for p in route.parameters if p.location == "path"]
|
|
390
|
+
path_params.sort() # Sort for consistent URIs
|
|
391
|
+
|
|
392
|
+
uri_template_str = f"resource://{template_name}"
|
|
393
|
+
if path_params:
|
|
394
|
+
uri_template_str += "/" + "/".join(f"{{{p}}}" for p in path_params)
|
|
395
|
+
|
|
396
|
+
base_description = (
|
|
397
|
+
route.description or route.summary or f"Template for {route.path}"
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
# Format enhanced description with parameters and request body
|
|
401
|
+
enhanced_description = format_description_with_responses(
|
|
402
|
+
base_description=base_description,
|
|
403
|
+
responses=route.responses,
|
|
404
|
+
parameters=route.parameters,
|
|
405
|
+
request_body=route.request_body,
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
template_params_schema = {
|
|
409
|
+
"type": "object",
|
|
410
|
+
"properties": {
|
|
411
|
+
p.name: {
|
|
412
|
+
**(p.schema_.copy() if isinstance(p.schema_, dict) else {}),
|
|
413
|
+
**(
|
|
414
|
+
{"description": p.description}
|
|
415
|
+
if p.description
|
|
416
|
+
and not (
|
|
417
|
+
isinstance(p.schema_, dict) and "description" in p.schema_
|
|
418
|
+
)
|
|
419
|
+
else {}
|
|
420
|
+
),
|
|
421
|
+
}
|
|
422
|
+
for p in route.parameters
|
|
423
|
+
if p.location == "path"
|
|
424
|
+
},
|
|
425
|
+
"required": [
|
|
426
|
+
p.name for p in route.parameters if p.location == "path" and p.required
|
|
427
|
+
],
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
template = OpenAPIResourceTemplate(
|
|
431
|
+
client=self._client,
|
|
432
|
+
route=route,
|
|
433
|
+
director=self._director,
|
|
434
|
+
uri_template=uri_template_str,
|
|
435
|
+
name=template_name,
|
|
436
|
+
description=enhanced_description,
|
|
437
|
+
parameters=template_params_schema,
|
|
438
|
+
tags=set(route.tags or []) | tags,
|
|
439
|
+
timeout=self._timeout,
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
# Call component_fn if provided
|
|
443
|
+
if self._mcp_component_fn is not None:
|
|
444
|
+
try:
|
|
445
|
+
self._mcp_component_fn(route, template)
|
|
446
|
+
logger.debug(f"Template {uri_template_str} customized by component_fn")
|
|
447
|
+
except Exception as e:
|
|
448
|
+
logger.warning(
|
|
449
|
+
f"Error in component_fn for template {uri_template_str}: {e}. "
|
|
450
|
+
f"Using component as-is."
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
# Use the potentially modified template URI as the registration key
|
|
454
|
+
final_template_uri = template.uri_template
|
|
455
|
+
|
|
456
|
+
# Register the template by directly assigning to the templates dictionary
|
|
457
|
+
self._resource_manager._templates[final_template_uri] = template
|
|
458
|
+
logger.debug(
|
|
459
|
+
f"Registered TEMPLATE: {final_template_uri} ({route.method} {route.path}) with tags: {route.tags}"
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
# Export public symbols
|
|
464
|
+
__all__ = [
|
|
465
|
+
"FastMCPOpenAPI",
|
|
466
|
+
]
|