fastmcp 2.12.5__py3-none-any.whl → 2.14.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.
Files changed (133) hide show
  1. fastmcp/__init__.py +2 -23
  2. fastmcp/cli/__init__.py +0 -3
  3. fastmcp/cli/__main__.py +5 -0
  4. fastmcp/cli/cli.py +19 -33
  5. fastmcp/cli/install/claude_code.py +6 -6
  6. fastmcp/cli/install/claude_desktop.py +3 -3
  7. fastmcp/cli/install/cursor.py +18 -12
  8. fastmcp/cli/install/gemini_cli.py +3 -3
  9. fastmcp/cli/install/mcp_json.py +3 -3
  10. fastmcp/cli/install/shared.py +0 -15
  11. fastmcp/cli/run.py +13 -8
  12. fastmcp/cli/tasks.py +110 -0
  13. fastmcp/client/__init__.py +9 -9
  14. fastmcp/client/auth/oauth.py +123 -225
  15. fastmcp/client/client.py +697 -95
  16. fastmcp/client/elicitation.py +11 -5
  17. fastmcp/client/logging.py +18 -14
  18. fastmcp/client/messages.py +7 -5
  19. fastmcp/client/oauth_callback.py +85 -171
  20. fastmcp/client/roots.py +2 -1
  21. fastmcp/client/sampling.py +1 -1
  22. fastmcp/client/tasks.py +614 -0
  23. fastmcp/client/transports.py +117 -30
  24. fastmcp/contrib/component_manager/__init__.py +1 -1
  25. fastmcp/contrib/component_manager/component_manager.py +2 -2
  26. fastmcp/contrib/component_manager/component_service.py +10 -26
  27. fastmcp/contrib/mcp_mixin/README.md +32 -1
  28. fastmcp/contrib/mcp_mixin/__init__.py +2 -2
  29. fastmcp/contrib/mcp_mixin/mcp_mixin.py +14 -2
  30. fastmcp/dependencies.py +25 -0
  31. fastmcp/experimental/sampling/handlers/openai.py +3 -3
  32. fastmcp/experimental/server/openapi/__init__.py +20 -21
  33. fastmcp/experimental/utilities/openapi/__init__.py +16 -47
  34. fastmcp/mcp_config.py +3 -4
  35. fastmcp/prompts/__init__.py +1 -1
  36. fastmcp/prompts/prompt.py +54 -51
  37. fastmcp/prompts/prompt_manager.py +16 -101
  38. fastmcp/resources/__init__.py +5 -5
  39. fastmcp/resources/resource.py +43 -21
  40. fastmcp/resources/resource_manager.py +9 -168
  41. fastmcp/resources/template.py +161 -61
  42. fastmcp/resources/types.py +30 -24
  43. fastmcp/server/__init__.py +1 -1
  44. fastmcp/server/auth/__init__.py +9 -14
  45. fastmcp/server/auth/auth.py +197 -46
  46. fastmcp/server/auth/handlers/authorize.py +326 -0
  47. fastmcp/server/auth/jwt_issuer.py +236 -0
  48. fastmcp/server/auth/middleware.py +96 -0
  49. fastmcp/server/auth/oauth_proxy.py +1469 -298
  50. fastmcp/server/auth/oidc_proxy.py +91 -20
  51. fastmcp/server/auth/providers/auth0.py +40 -21
  52. fastmcp/server/auth/providers/aws.py +29 -3
  53. fastmcp/server/auth/providers/azure.py +312 -131
  54. fastmcp/server/auth/providers/debug.py +114 -0
  55. fastmcp/server/auth/providers/descope.py +86 -29
  56. fastmcp/server/auth/providers/discord.py +308 -0
  57. fastmcp/server/auth/providers/github.py +29 -8
  58. fastmcp/server/auth/providers/google.py +48 -9
  59. fastmcp/server/auth/providers/in_memory.py +29 -5
  60. fastmcp/server/auth/providers/introspection.py +281 -0
  61. fastmcp/server/auth/providers/jwt.py +48 -31
  62. fastmcp/server/auth/providers/oci.py +233 -0
  63. fastmcp/server/auth/providers/scalekit.py +238 -0
  64. fastmcp/server/auth/providers/supabase.py +188 -0
  65. fastmcp/server/auth/providers/workos.py +35 -17
  66. fastmcp/server/context.py +236 -116
  67. fastmcp/server/dependencies.py +503 -18
  68. fastmcp/server/elicitation.py +286 -48
  69. fastmcp/server/event_store.py +177 -0
  70. fastmcp/server/http.py +71 -20
  71. fastmcp/server/low_level.py +165 -2
  72. fastmcp/server/middleware/__init__.py +1 -1
  73. fastmcp/server/middleware/caching.py +476 -0
  74. fastmcp/server/middleware/error_handling.py +14 -10
  75. fastmcp/server/middleware/logging.py +50 -39
  76. fastmcp/server/middleware/middleware.py +29 -16
  77. fastmcp/server/middleware/rate_limiting.py +3 -3
  78. fastmcp/server/middleware/tool_injection.py +116 -0
  79. fastmcp/server/openapi/__init__.py +35 -0
  80. fastmcp/{experimental/server → server}/openapi/components.py +15 -10
  81. fastmcp/{experimental/server → server}/openapi/routing.py +3 -3
  82. fastmcp/{experimental/server → server}/openapi/server.py +6 -5
  83. fastmcp/server/proxy.py +72 -48
  84. fastmcp/server/server.py +1415 -733
  85. fastmcp/server/tasks/__init__.py +21 -0
  86. fastmcp/server/tasks/capabilities.py +22 -0
  87. fastmcp/server/tasks/config.py +89 -0
  88. fastmcp/server/tasks/converters.py +205 -0
  89. fastmcp/server/tasks/handlers.py +356 -0
  90. fastmcp/server/tasks/keys.py +93 -0
  91. fastmcp/server/tasks/protocol.py +355 -0
  92. fastmcp/server/tasks/subscriptions.py +205 -0
  93. fastmcp/settings.py +125 -113
  94. fastmcp/tools/__init__.py +1 -1
  95. fastmcp/tools/tool.py +138 -55
  96. fastmcp/tools/tool_manager.py +30 -112
  97. fastmcp/tools/tool_transform.py +12 -21
  98. fastmcp/utilities/cli.py +67 -28
  99. fastmcp/utilities/components.py +10 -5
  100. fastmcp/utilities/inspect.py +79 -23
  101. fastmcp/utilities/json_schema.py +4 -4
  102. fastmcp/utilities/json_schema_type.py +8 -8
  103. fastmcp/utilities/logging.py +118 -8
  104. fastmcp/utilities/mcp_config.py +1 -2
  105. fastmcp/utilities/mcp_server_config/__init__.py +3 -3
  106. fastmcp/utilities/mcp_server_config/v1/environments/base.py +1 -2
  107. fastmcp/utilities/mcp_server_config/v1/environments/uv.py +6 -6
  108. fastmcp/utilities/mcp_server_config/v1/mcp_server_config.py +5 -5
  109. fastmcp/utilities/mcp_server_config/v1/schema.json +3 -0
  110. fastmcp/utilities/mcp_server_config/v1/sources/base.py +0 -1
  111. fastmcp/{experimental/utilities → utilities}/openapi/README.md +7 -35
  112. fastmcp/utilities/openapi/__init__.py +63 -0
  113. fastmcp/{experimental/utilities → utilities}/openapi/director.py +14 -15
  114. fastmcp/{experimental/utilities → utilities}/openapi/formatters.py +5 -5
  115. fastmcp/{experimental/utilities → utilities}/openapi/json_schema_converter.py +7 -3
  116. fastmcp/{experimental/utilities → utilities}/openapi/parser.py +37 -16
  117. fastmcp/utilities/tests.py +92 -5
  118. fastmcp/utilities/types.py +86 -16
  119. fastmcp/utilities/ui.py +626 -0
  120. {fastmcp-2.12.5.dist-info → fastmcp-2.14.0.dist-info}/METADATA +24 -15
  121. fastmcp-2.14.0.dist-info/RECORD +156 -0
  122. {fastmcp-2.12.5.dist-info → fastmcp-2.14.0.dist-info}/WHEEL +1 -1
  123. fastmcp/cli/claude.py +0 -135
  124. fastmcp/server/auth/providers/bearer.py +0 -25
  125. fastmcp/server/openapi.py +0 -1083
  126. fastmcp/utilities/openapi.py +0 -1568
  127. fastmcp/utilities/storage.py +0 -204
  128. fastmcp-2.12.5.dist-info/RECORD +0 -134
  129. fastmcp/{experimental/server → server}/openapi/README.md +0 -0
  130. fastmcp/{experimental/utilities → utilities}/openapi/models.py +3 -3
  131. fastmcp/{experimental/utilities → utilities}/openapi/schemas.py +2 -2
  132. {fastmcp-2.12.5.dist-info → fastmcp-2.14.0.dist-info}/entry_points.txt +0 -0
  133. {fastmcp-2.12.5.dist-info → fastmcp-2.14.0.dist-info}/licenses/LICENSE +0 -0
fastmcp/server/openapi.py DELETED
@@ -1,1083 +0,0 @@
1
- """FastMCP server implementation for OpenAPI integration."""
2
-
3
- from __future__ import annotations
4
-
5
- import enum
6
- import json
7
- import re
8
- import warnings
9
- from collections import Counter
10
- from collections.abc import Callable
11
- from dataclasses import dataclass, field
12
- from re import Pattern
13
- from typing import TYPE_CHECKING, Any, Literal
14
-
15
- import httpx
16
- from mcp.types import ToolAnnotations
17
- from pydantic.networks import AnyUrl
18
-
19
- import fastmcp
20
- from fastmcp.exceptions import ToolError
21
- from fastmcp.resources import Resource, ResourceTemplate
22
- from fastmcp.server.dependencies import get_http_headers
23
- from fastmcp.server.server import FastMCP
24
- from fastmcp.tools.tool import Tool, ToolResult
25
- from fastmcp.utilities import openapi
26
- from fastmcp.utilities.logging import get_logger
27
- from fastmcp.utilities.openapi import (
28
- HTTPRoute,
29
- _combine_schemas,
30
- extract_output_schema_from_responses,
31
- format_array_parameter,
32
- format_deep_object_parameter,
33
- format_description_with_responses,
34
- )
35
-
36
- if TYPE_CHECKING:
37
- from fastmcp.server import Context
38
-
39
- logger = get_logger(__name__)
40
-
41
- HttpMethod = Literal["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]
42
-
43
-
44
- def _slugify(text: str) -> str:
45
- """
46
- Convert text to a URL-friendly slug format that only contains lowercase
47
- letters, uppercase letters, numbers, and underscores.
48
- """
49
- if not text:
50
- return ""
51
-
52
- # Replace spaces and common separators with underscores
53
- slug = re.sub(r"[\s\-\.]+", "_", text)
54
-
55
- # Remove non-alphanumeric characters except underscores
56
- slug = re.sub(r"[^a-zA-Z0-9_]", "", slug)
57
-
58
- # Remove multiple consecutive underscores
59
- slug = re.sub(r"_+", "_", slug)
60
-
61
- # Remove leading/trailing underscores
62
- slug = slug.strip("_")
63
-
64
- return slug
65
-
66
-
67
- # Type definitions for the mapping functions
68
- RouteMapFn = Callable[[HTTPRoute, "MCPType"], "MCPType | None"]
69
- ComponentFn = Callable[
70
- [
71
- HTTPRoute,
72
- "OpenAPITool | OpenAPIResource | OpenAPIResourceTemplate",
73
- ],
74
- None,
75
- ]
76
-
77
-
78
- class MCPType(enum.Enum):
79
- """Type of FastMCP component to create from a route.
80
-
81
- Enum values:
82
- TOOL: Convert the route to a callable Tool
83
- RESOURCE: Convert the route to a Resource (typically GET endpoints)
84
- RESOURCE_TEMPLATE: Convert the route to a ResourceTemplate (typically GET with path params)
85
- EXCLUDE: Exclude the route from being converted to any MCP component
86
- IGNORE: Deprecated, use EXCLUDE instead
87
- """
88
-
89
- TOOL = "TOOL"
90
- RESOURCE = "RESOURCE"
91
- RESOURCE_TEMPLATE = "RESOURCE_TEMPLATE"
92
- # PROMPT = "PROMPT"
93
- EXCLUDE = "EXCLUDE"
94
-
95
-
96
- # Keep RouteType as an alias to MCPType for backward compatibility
97
- class RouteType(enum.Enum):
98
- """
99
- Deprecated: Use MCPType instead.
100
-
101
- This enum is kept for backward compatibility and will be removed in a future version.
102
- """
103
-
104
- TOOL = "TOOL"
105
- RESOURCE = "RESOURCE"
106
- RESOURCE_TEMPLATE = "RESOURCE_TEMPLATE"
107
- IGNORE = "IGNORE"
108
-
109
-
110
- @dataclass(kw_only=True)
111
- class RouteMap:
112
- """Mapping configuration for HTTP routes to FastMCP component types."""
113
-
114
- methods: list[HttpMethod] | Literal["*"] = field(default="*")
115
- pattern: Pattern[str] | str = field(default=r".*")
116
- route_type: RouteType | MCPType | None = field(default=None)
117
- tags: set[str] = field(
118
- default_factory=set,
119
- metadata={"description": "A set of tags to match. All tags must match."},
120
- )
121
- mcp_type: MCPType | None = field(
122
- default=None,
123
- metadata={"description": "The type of FastMCP component to create."},
124
- )
125
- mcp_tags: set[str] = field(
126
- default_factory=set,
127
- metadata={
128
- "description": "A set of tags to apply to the generated FastMCP component."
129
- },
130
- )
131
-
132
- def __post_init__(self):
133
- """Validate and process the route map after initialization."""
134
- # Handle backward compatibility for route_type, deprecated in 2.5.0
135
- if self.mcp_type is None and self.route_type is not None:
136
- if fastmcp.settings.deprecation_warnings:
137
- warnings.warn(
138
- "The 'route_type' parameter is deprecated and will be removed in a future version. "
139
- "Use 'mcp_type' instead with the appropriate MCPType value.",
140
- DeprecationWarning,
141
- stacklevel=2,
142
- )
143
- if isinstance(self.route_type, RouteType):
144
- if fastmcp.settings.deprecation_warnings:
145
- warnings.warn(
146
- "The RouteType class is deprecated and will be removed in a future version. "
147
- "Use MCPType instead.",
148
- DeprecationWarning,
149
- stacklevel=2,
150
- )
151
- # Check for the deprecated IGNORE value
152
- if self.route_type == RouteType.IGNORE:
153
- if fastmcp.settings.deprecation_warnings:
154
- warnings.warn(
155
- "RouteType.IGNORE is deprecated and will be removed in a future version. "
156
- "Use MCPType.EXCLUDE instead.",
157
- DeprecationWarning,
158
- stacklevel=2,
159
- )
160
-
161
- # Convert from RouteType to MCPType if needed
162
- if isinstance(self.route_type, RouteType):
163
- route_type_name = self.route_type.name
164
- if route_type_name == "IGNORE":
165
- route_type_name = "EXCLUDE"
166
- self.mcp_type = getattr(MCPType, route_type_name)
167
- else:
168
- self.mcp_type = self.route_type
169
- elif self.mcp_type is None:
170
- raise ValueError("`mcp_type` must be provided")
171
-
172
- # Set route_type to match mcp_type for backward compatibility
173
- if self.route_type is None:
174
- self.route_type = self.mcp_type
175
-
176
-
177
- # Default route mapping: all routes become tools.
178
- # Users can provide custom route_maps to override this behavior.
179
- DEFAULT_ROUTE_MAPPINGS = [
180
- RouteMap(mcp_type=MCPType.TOOL),
181
- ]
182
-
183
-
184
- def _determine_route_type(
185
- route: openapi.HTTPRoute,
186
- mappings: list[RouteMap],
187
- ) -> RouteMap:
188
- """
189
- Determines the FastMCP component type based on the route and mappings.
190
-
191
- Args:
192
- route: HTTPRoute object
193
- mappings: List of RouteMap objects in priority order
194
-
195
- Returns:
196
- The RouteMap that matches the route, or a catchall "Tool" RouteMap if no match is found.
197
- """
198
- # Check mappings in priority order (first match wins)
199
- for route_map in mappings:
200
- # Check if the HTTP method matches
201
- if route_map.methods == "*" or route.method in route_map.methods:
202
- # Handle both string patterns and compiled Pattern objects
203
- if isinstance(route_map.pattern, Pattern):
204
- pattern_matches = route_map.pattern.search(route.path)
205
- else:
206
- pattern_matches = re.search(route_map.pattern, route.path)
207
-
208
- if pattern_matches:
209
- # Check if tags match (if specified)
210
- # If route_map.tags is empty, tags are not matched
211
- # If route_map.tags is non-empty, all tags must be present in route.tags (AND condition)
212
- if route_map.tags:
213
- route_tags_set = set(route.tags or [])
214
- if not route_map.tags.issubset(route_tags_set):
215
- # Tags don't match, continue to next mapping
216
- continue
217
-
218
- # We know mcp_type is not None here due to post_init validation
219
- assert route_map.mcp_type is not None
220
- logger.debug(
221
- f"Route {route.method} {route.path} matched mapping to {route_map.mcp_type.name}"
222
- )
223
- return route_map
224
-
225
- # Default fallback
226
- return RouteMap(mcp_type=MCPType.TOOL)
227
-
228
-
229
- class OpenAPITool(Tool):
230
- """Tool implementation for OpenAPI endpoints."""
231
-
232
- def __init__(
233
- self,
234
- client: httpx.AsyncClient,
235
- route: openapi.HTTPRoute,
236
- name: str,
237
- description: str,
238
- parameters: dict[str, Any],
239
- output_schema: dict[str, Any] | None = None,
240
- tags: set[str] | None = None,
241
- timeout: float | None = None,
242
- annotations: ToolAnnotations | None = None,
243
- serializer: Callable[[Any], str] | None = None,
244
- ):
245
- super().__init__(
246
- name=name,
247
- description=description,
248
- parameters=parameters,
249
- output_schema=output_schema,
250
- tags=tags or set(),
251
- annotations=annotations,
252
- serializer=serializer,
253
- )
254
- self._client = client
255
- self._route = route
256
- self._timeout = timeout
257
-
258
- def __repr__(self) -> str:
259
- """Custom representation to prevent recursion errors when printing."""
260
- return f"OpenAPITool(name={self.name!r}, method={self._route.method}, path={self._route.path})"
261
-
262
- async def run(self, arguments: dict[str, Any]) -> ToolResult:
263
- """Execute the HTTP request based on the route configuration."""
264
-
265
- # Create mapping from suffixed parameter names back to original names and locations
266
- # This handles parameter collisions where suffixes were added during schema generation
267
- param_mapping = {} # suffixed_name -> (original_name, location)
268
-
269
- # First, check if we have request body properties to detect collisions
270
- body_props = set()
271
- if self._route.request_body and self._route.request_body.content_schema:
272
- content_type = next(iter(self._route.request_body.content_schema))
273
- body_schema = self._route.request_body.content_schema[content_type]
274
- body_props = set(body_schema.get("properties", {}).keys())
275
-
276
- # Build parameter mapping for potentially suffixed parameters
277
- for param in self._route.parameters:
278
- original_name = param.name
279
- suffixed_name = f"{param.name}__{param.location}"
280
-
281
- # If parameter name collides with body property, it would have been suffixed
282
- if param.name in body_props:
283
- param_mapping[suffixed_name] = (original_name, param.location)
284
- # Also map original name for backward compatibility when no collision
285
- param_mapping[original_name] = (original_name, param.location)
286
-
287
- # Prepare URL
288
- path = self._route.path
289
-
290
- # Replace path parameters with values from arguments
291
- # Look for both original and suffixed parameter names
292
- path_params = {}
293
- for p in self._route.parameters:
294
- if p.location == "path":
295
- # Try suffixed name first, then original name
296
- suffixed_name = f"{p.name}__{p.location}"
297
- if (
298
- suffixed_name in arguments
299
- and arguments.get(suffixed_name) is not None
300
- ):
301
- path_params[p.name] = arguments[suffixed_name]
302
- elif p.name in arguments and arguments.get(p.name) is not None:
303
- path_params[p.name] = arguments[p.name]
304
-
305
- # Ensure all path parameters are provided
306
- required_path_params = {
307
- p.name
308
- for p in self._route.parameters
309
- if p.location == "path" and p.required
310
- }
311
- missing_params = required_path_params - path_params.keys()
312
- if missing_params:
313
- raise ToolError(f"Missing required path parameters: {missing_params}")
314
-
315
- for param_name, param_value in path_params.items():
316
- # Handle array path parameters with style 'simple' (comma-separated)
317
- # In OpenAPI, 'simple' is the default style for path parameters
318
- param_info = next(
319
- (p for p in self._route.parameters if p.name == param_name), None
320
- )
321
-
322
- if param_info and isinstance(param_value, list):
323
- # Check if schema indicates an array type
324
- schema = param_info.schema_
325
- is_array = schema.get("type") == "array"
326
-
327
- if is_array:
328
- # Format array values as comma-separated string
329
- # This follows the OpenAPI 'simple' style (default for path)
330
- formatted_value = format_array_parameter(
331
- param_value, param_name, is_query_parameter=False
332
- )
333
- path = path.replace(f"{{{param_name}}}", str(formatted_value))
334
- continue
335
-
336
- # Default handling for non-array parameters or non-array schemas
337
- path = path.replace(f"{{{param_name}}}", str(param_value))
338
-
339
- # Prepare query parameters - filter out None and empty strings
340
- query_params = {}
341
- for p in self._route.parameters:
342
- if p.location == "query":
343
- # Try suffixed name first, then original name
344
- suffixed_name = f"{p.name}__{p.location}"
345
- param_value = None
346
-
347
- suffixed_value = arguments.get(suffixed_name)
348
- if (
349
- suffixed_name in arguments
350
- and suffixed_value is not None
351
- and suffixed_value != ""
352
- and not (
353
- isinstance(suffixed_value, list | dict)
354
- and len(suffixed_value) == 0
355
- )
356
- ):
357
- param_value = arguments[suffixed_name]
358
- else:
359
- name_value = arguments.get(p.name)
360
- if (
361
- p.name in arguments
362
- and name_value is not None
363
- and name_value != ""
364
- and not (
365
- isinstance(name_value, list | dict) and len(name_value) == 0
366
- )
367
- ):
368
- param_value = arguments[p.name]
369
-
370
- if param_value is not None:
371
- # Handle different parameter styles and types
372
- param_style = (
373
- p.style or "form"
374
- ) # Default style for query parameters is "form"
375
- param_explode = (
376
- p.explode if p.explode is not None else True
377
- ) # Default explode for query is True
378
-
379
- # Handle deepObject style for object parameters
380
- if (
381
- param_style == "deepObject"
382
- and isinstance(param_value, dict)
383
- and len(param_value) > 0
384
- ):
385
- if param_explode:
386
- # deepObject with explode=true: object properties become separate parameters
387
- # e.g., target[id]=123&target[type]=user
388
- deep_obj_params = format_deep_object_parameter(
389
- param_value, p.name
390
- )
391
- query_params.update(deep_obj_params)
392
- else:
393
- # deepObject with explode=false is not commonly used, fallback to JSON
394
- logger.warning(
395
- f"deepObject style with explode=false for parameter '{p.name}' is not standard. "
396
- f"Using JSON serialization fallback."
397
- )
398
- query_params[p.name] = json.dumps(param_value)
399
- # Handle array parameters with form style (default)
400
- elif (
401
- isinstance(param_value, list)
402
- and p.schema_.get("type") == "array"
403
- and len(param_value) > 0
404
- ):
405
- if param_explode:
406
- # When explode=True, we pass the array directly, which HTTPX will serialize
407
- # as multiple parameters with the same name
408
- query_params[p.name] = param_value
409
- else:
410
- # Format array as comma-separated string when explode=False
411
- formatted_value = format_array_parameter(
412
- param_value, p.name, is_query_parameter=True
413
- )
414
- query_params[p.name] = formatted_value
415
- else:
416
- # Non-array, non-deepObject parameters are passed as is
417
- query_params[p.name] = param_value
418
-
419
- # Prepare headers - fix typing by ensuring all values are strings
420
- headers = {}
421
-
422
- # Start with OpenAPI-defined header parameters
423
- openapi_headers = {}
424
- for p in self._route.parameters:
425
- if p.location == "header":
426
- # Try suffixed name first, then original name
427
- suffixed_name = f"{p.name}__{p.location}"
428
- param_value = None
429
-
430
- if (
431
- suffixed_name in arguments
432
- and arguments.get(suffixed_name) is not None
433
- ):
434
- param_value = arguments[suffixed_name]
435
- elif p.name in arguments and arguments.get(p.name) is not None:
436
- param_value = arguments[p.name]
437
-
438
- if param_value is not None:
439
- openapi_headers[p.name.lower()] = str(param_value)
440
- headers.update(openapi_headers)
441
-
442
- # Add headers from the current MCP client HTTP request (these take precedence)
443
- mcp_headers = get_http_headers()
444
- headers.update(mcp_headers)
445
-
446
- # Prepare request body
447
- json_data = None
448
- if self._route.request_body and self._route.request_body.content_schema:
449
- # Extract body parameters with collision-aware logic
450
- # Exclude all parameter names that belong to path/query/header locations
451
- params_to_exclude = set()
452
-
453
- for p in self._route.parameters:
454
- if (
455
- p.name in body_props
456
- ): # This parameter had a collision, so it was suffixed
457
- params_to_exclude.add(f"{p.name}__{p.location}")
458
- else: # No collision, parameter keeps original name but should still be excluded from body
459
- params_to_exclude.add(p.name)
460
-
461
- body_params = {
462
- k: v for k, v in arguments.items() if k not in params_to_exclude
463
- }
464
-
465
- if body_params:
466
- json_data = body_params
467
-
468
- # Execute the request
469
- try:
470
- response = await self._client.request(
471
- method=self._route.method,
472
- url=path,
473
- params=query_params,
474
- headers=headers,
475
- json=json_data,
476
- timeout=self._timeout,
477
- )
478
-
479
- # Raise for 4xx/5xx responses
480
- response.raise_for_status()
481
-
482
- # Try to parse as JSON first
483
- try:
484
- result = response.json()
485
-
486
- # Handle structured content based on output schema, if any
487
- structured_output = None
488
- if self.output_schema is not None:
489
- if self.output_schema.get("x-fastmcp-wrap-result"):
490
- # Schema says wrap - always wrap in result key
491
- structured_output = {"result": result}
492
- else:
493
- structured_output = result
494
- # If no output schema, use fallback logic for backward compatibility
495
- elif not isinstance(result, dict):
496
- structured_output = {"result": result}
497
- else:
498
- structured_output = result
499
-
500
- return ToolResult(structured_content=structured_output)
501
- except json.JSONDecodeError:
502
- return ToolResult(content=response.text)
503
-
504
- except httpx.HTTPStatusError as e:
505
- # Handle HTTP errors (4xx, 5xx)
506
- error_message = (
507
- f"HTTP error {e.response.status_code}: {e.response.reason_phrase}"
508
- )
509
- try:
510
- error_data = e.response.json()
511
- error_message += f" - {error_data}"
512
- except (json.JSONDecodeError, ValueError):
513
- if e.response.text:
514
- error_message += f" - {e.response.text}"
515
-
516
- raise ValueError(error_message)
517
-
518
- except httpx.RequestError as e:
519
- # Handle request errors (connection, timeout, etc.)
520
- raise ValueError(f"Request error: {str(e)}")
521
-
522
-
523
- class OpenAPIResource(Resource):
524
- """Resource implementation for OpenAPI endpoints."""
525
-
526
- def __init__(
527
- self,
528
- client: httpx.AsyncClient,
529
- route: openapi.HTTPRoute,
530
- uri: str,
531
- name: str,
532
- description: str,
533
- mime_type: str = "application/json",
534
- tags: set[str] = set(),
535
- timeout: float | None = None,
536
- ):
537
- super().__init__(
538
- uri=AnyUrl(uri), # Convert string to AnyUrl
539
- name=name,
540
- description=description,
541
- mime_type=mime_type,
542
- tags=tags,
543
- )
544
- self._client = client
545
- self._route = route
546
- self._timeout = timeout
547
-
548
- def __repr__(self) -> str:
549
- """Custom representation to prevent recursion errors when printing."""
550
- return f"OpenAPIResource(name={self.name!r}, uri={self.uri!r}, path={self._route.path})"
551
-
552
- async def read(self) -> str | bytes:
553
- """Fetch the resource data by making an HTTP request."""
554
- try:
555
- # Extract path parameters from the URI if present
556
- path = self._route.path
557
- resource_uri = str(self.uri)
558
-
559
- # If this is a templated resource, extract path parameters from the URI
560
- if "{" in path and "}" in path:
561
- # Extract the resource ID from the URI (the last part after the last slash)
562
- parts = resource_uri.split("/")
563
-
564
- if len(parts) > 1:
565
- # Find all path parameters in the route path
566
- path_params = {}
567
-
568
- # Find the path parameter names from the route path
569
- param_matches = re.findall(r"\{([^}]+)\}", path)
570
- if param_matches:
571
- # Reverse sorting from creation order (traversal is backwards)
572
- param_matches.sort(reverse=True)
573
- # Number of sent parameters is number of parts -1 (assuming first part is resource identifier)
574
- expected_param_count = len(parts) - 1
575
- # Map parameters from the end of the URI to the parameters in the path
576
- # Last parameter in URI (parts[-1]) maps to last parameter in path, and so on
577
- for i, param_name in enumerate(param_matches):
578
- # Ensure we don't use resource identifier as parameter
579
- if i < expected_param_count:
580
- # Get values from the end of parts
581
- param_value = parts[-1 - i]
582
- path_params[param_name] = param_value
583
-
584
- # Replace path parameters with their values
585
- for param_name, param_value in path_params.items():
586
- path = path.replace(f"{{{param_name}}}", str(param_value))
587
-
588
- # Filter any query parameters - get query parameters and filter out None/empty values
589
- query_params = {}
590
- for param in self._route.parameters:
591
- if param.location == "query" and hasattr(self, f"_{param.name}"):
592
- value = getattr(self, f"_{param.name}")
593
- if value is not None and value != "":
594
- query_params[param.name] = value
595
-
596
- # Prepare headers from MCP client request if available
597
- headers = {}
598
- mcp_headers = get_http_headers()
599
- headers.update(mcp_headers)
600
-
601
- response = await self._client.request(
602
- method=self._route.method,
603
- url=path,
604
- params=query_params,
605
- headers=headers,
606
- timeout=self._timeout,
607
- )
608
-
609
- # Raise for 4xx/5xx responses
610
- response.raise_for_status()
611
-
612
- # Determine content type and return appropriate format
613
- content_type = response.headers.get("content-type", "").lower()
614
-
615
- if "application/json" in content_type:
616
- result = response.json()
617
- return json.dumps(result)
618
- elif any(ct in content_type for ct in ["text/", "application/xml"]):
619
- return response.text
620
- else:
621
- return response.content
622
-
623
- except httpx.HTTPStatusError as e:
624
- # Handle HTTP errors (4xx, 5xx)
625
- error_message = (
626
- f"HTTP error {e.response.status_code}: {e.response.reason_phrase}"
627
- )
628
- try:
629
- error_data = e.response.json()
630
- error_message += f" - {error_data}"
631
- except (json.JSONDecodeError, ValueError):
632
- if e.response.text:
633
- error_message += f" - {e.response.text}"
634
-
635
- raise ValueError(error_message)
636
-
637
- except httpx.RequestError as e:
638
- # Handle request errors (connection, timeout, etc.)
639
- raise ValueError(f"Request error: {str(e)}")
640
-
641
-
642
- class OpenAPIResourceTemplate(ResourceTemplate):
643
- """Resource template implementation for OpenAPI endpoints."""
644
-
645
- def __init__(
646
- self,
647
- client: httpx.AsyncClient,
648
- route: openapi.HTTPRoute,
649
- uri_template: str,
650
- name: str,
651
- description: str,
652
- parameters: dict[str, Any],
653
- tags: set[str] = set(),
654
- timeout: float | None = None,
655
- ):
656
- super().__init__(
657
- uri_template=uri_template,
658
- name=name,
659
- description=description,
660
- parameters=parameters,
661
- tags=tags,
662
- )
663
- self._client = client
664
- self._route = route
665
- self._timeout = timeout
666
-
667
- def __repr__(self) -> str:
668
- """Custom representation to prevent recursion errors when printing."""
669
- return f"OpenAPIResourceTemplate(name={self.name!r}, uri_template={self.uri_template!r}, path={self._route.path})"
670
-
671
- async def create_resource(
672
- self,
673
- uri: str,
674
- params: dict[str, Any],
675
- context: Context | None = None,
676
- ) -> Resource:
677
- """Create a resource with the given parameters."""
678
- # Generate a URI for this resource instance
679
- uri_parts = []
680
- for key, value in params.items():
681
- uri_parts.append(f"{key}={value}")
682
-
683
- # Create and return a resource
684
- return OpenAPIResource(
685
- client=self._client,
686
- route=self._route,
687
- uri=uri,
688
- name=f"{self.name}-{'-'.join(uri_parts)}",
689
- description=self.description or f"Resource for {self._route.path}",
690
- mime_type="application/json",
691
- tags=set(self._route.tags or []),
692
- timeout=self._timeout,
693
- )
694
-
695
-
696
- class FastMCPOpenAPI(FastMCP):
697
- """
698
- FastMCP server implementation that creates components from an OpenAPI schema.
699
-
700
- This class parses an OpenAPI specification and creates appropriate FastMCP components
701
- (Tools, Resources, ResourceTemplates) based on route mappings.
702
-
703
- Example:
704
- ```python
705
- from fastmcp.server.openapi import FastMCPOpenAPI, RouteMap, MCPType
706
- import httpx
707
-
708
- # Define custom route mappings
709
- custom_mappings = [
710
- # Map all user-related endpoints to ResourceTemplate
711
- RouteMap(
712
- methods=["GET", "POST", "PATCH"],
713
- pattern=r".*/users/.*",
714
- mcp_type=MCPType.RESOURCE_TEMPLATE
715
- ),
716
- # Map all analytics endpoints to Tool
717
- RouteMap(
718
- methods=["GET"],
719
- pattern=r".*/analytics/.*",
720
- mcp_type=MCPType.TOOL
721
- ),
722
- ]
723
-
724
- # Create server with custom mappings and route mapper
725
- server = FastMCPOpenAPI(
726
- openapi_spec=spec,
727
- client=httpx.AsyncClient(),
728
- name="API Server",
729
- route_maps=custom_mappings,
730
- )
731
- ```
732
- """
733
-
734
- def __init__(
735
- self,
736
- openapi_spec: dict[str, Any],
737
- client: httpx.AsyncClient,
738
- name: str | None = None,
739
- route_maps: list[RouteMap] | None = None,
740
- route_map_fn: RouteMapFn | None = None,
741
- mcp_component_fn: ComponentFn | None = None,
742
- mcp_names: dict[str, str] | None = None,
743
- tags: set[str] | None = None,
744
- timeout: float | None = None,
745
- **settings: Any,
746
- ):
747
- """
748
- Initialize a FastMCP server from an OpenAPI schema.
749
-
750
- Args:
751
- openapi_spec: OpenAPI schema as a dictionary or file path
752
- client: httpx AsyncClient for making HTTP requests
753
- name: Optional name for the server
754
- route_maps: Optional list of RouteMap objects defining route mappings
755
- route_map_fn: Optional callable for advanced route type mapping.
756
- Receives (route, mcp_type) and returns MCPType or None.
757
- Called on every route, including excluded ones.
758
- mcp_component_fn: Optional callable for component customization.
759
- Receives (route, component) and can modify the component in-place.
760
- Called on every created component.
761
- mcp_names: Optional dictionary mapping operationId to desired component names.
762
- If an operationId is not in the dictionary, falls back to using the
763
- operationId up to the first double underscore. If no operationId exists,
764
- falls back to slugified summary or path-based naming.
765
- All names are truncated to 56 characters maximum.
766
- tags: Optional set of tags to add to all components. Components always receive any tags
767
- from the route.
768
- timeout: Optional timeout (in seconds) for all requests
769
- **settings: Additional settings for FastMCP
770
- """
771
- super().__init__(name=name or "OpenAPI FastMCP", **settings)
772
-
773
- self._client = client
774
- self._timeout = timeout
775
- self._mcp_component_fn = mcp_component_fn
776
-
777
- # Keep track of names to detect collisions
778
- self._used_names = {
779
- "tool": Counter(),
780
- "resource": Counter(),
781
- "resource_template": Counter(),
782
- "prompt": Counter(),
783
- }
784
-
785
- http_routes = openapi.parse_openapi_to_http_routes(openapi_spec)
786
-
787
- # Process routes
788
- num_excluded = 0
789
- route_maps = (route_maps or []) + DEFAULT_ROUTE_MAPPINGS
790
- for route in http_routes:
791
- # Determine route type based on mappings or default rules
792
- route_map = _determine_route_type(route, route_maps)
793
-
794
- # TODO: remove this once RouteType is removed and mcp_type is typed as MCPType without | None
795
- assert route_map.mcp_type is not None
796
- route_type = route_map.mcp_type
797
-
798
- # Call route_map_fn if provided
799
- if route_map_fn is not None:
800
- try:
801
- result = route_map_fn(route, route_type)
802
- if result is not None:
803
- route_type = result
804
- logger.debug(
805
- f"Route {route.method} {route.path} mapping customized by route_map_fn: "
806
- f"type={route_type.name}"
807
- )
808
- except Exception as e:
809
- logger.warning(
810
- f"Error in route_map_fn for {route.method} {route.path}: {e}. "
811
- f"Using default values."
812
- )
813
-
814
- # Generate a default name from the route
815
- component_name = self._generate_default_name(route, mcp_names)
816
-
817
- route_tags = set(route.tags) | route_map.mcp_tags | (tags or set())
818
-
819
- if route_type == MCPType.TOOL:
820
- self._create_openapi_tool(route, component_name, tags=route_tags)
821
- elif route_type == MCPType.RESOURCE:
822
- self._create_openapi_resource(route, component_name, tags=route_tags)
823
- elif route_type == MCPType.RESOURCE_TEMPLATE:
824
- self._create_openapi_template(route, component_name, tags=route_tags)
825
- elif route_type == MCPType.EXCLUDE:
826
- logger.info(f"Excluding route: {route.method} {route.path}")
827
- num_excluded += 1
828
-
829
- logger.info(
830
- f"Created FastMCP OpenAPI server with {len(http_routes) - num_excluded} routes"
831
- )
832
-
833
- def _generate_default_name(
834
- self, route: openapi.HTTPRoute, mcp_names_map: dict[str, str] | None = None
835
- ) -> str:
836
- """Generate a default name from the route using the configured strategy."""
837
- name = ""
838
- mcp_names_map = mcp_names_map or {}
839
-
840
- # First check if there's a custom mapping for this operationId
841
- if route.operation_id:
842
- if route.operation_id in mcp_names_map:
843
- name = mcp_names_map[route.operation_id]
844
- else:
845
- # If there's a double underscore in the operationId, use the first part
846
- name = route.operation_id.split("__")[0]
847
- else:
848
- name = route.summary or f"{route.method}_{route.path}"
849
-
850
- name = _slugify(name)
851
-
852
- # Truncate to 56 characters maximum
853
- if len(name) > 56:
854
- name = name[:56]
855
-
856
- return name
857
-
858
- def _get_unique_name(
859
- self,
860
- name: str,
861
- component_type: Literal["tool", "resource", "resource_template", "prompt"],
862
- ) -> str:
863
- """
864
- Ensure the name is unique within its component type by appending numbers if needed.
865
-
866
- Args:
867
- name: The proposed name
868
- component_type: The type of component ("tools", "resources", or "templates")
869
-
870
- Returns:
871
- str: A unique name for the component
872
- """
873
- # Check if the name is already used
874
- self._used_names[component_type][name] += 1
875
- if self._used_names[component_type][name] == 1:
876
- return name
877
-
878
- else:
879
- # Create the new name
880
- new_name = f"{name}_{self._used_names[component_type][name]}"
881
- logger.debug(
882
- f"Name collision detected: '{name}' already exists as a {component_type[:-1]}. "
883
- f"Using '{new_name}' instead."
884
- )
885
-
886
- return new_name
887
-
888
- def _create_openapi_tool(
889
- self,
890
- route: openapi.HTTPRoute,
891
- name: str,
892
- tags: set[str],
893
- ):
894
- """Creates and registers an OpenAPITool with enhanced description."""
895
- combined_schema = _combine_schemas(route)
896
-
897
- # Extract output schema from OpenAPI responses
898
- output_schema = extract_output_schema_from_responses(
899
- route.responses, route.schema_definitions, route.openapi_version
900
- )
901
-
902
- # Get a unique tool name
903
- tool_name = self._get_unique_name(name, "tool")
904
-
905
- base_description = (
906
- route.description
907
- or route.summary
908
- or f"Executes {route.method} {route.path}"
909
- )
910
-
911
- # Format enhanced description with parameters and request body
912
- enhanced_description = format_description_with_responses(
913
- base_description=base_description,
914
- responses=route.responses,
915
- parameters=route.parameters,
916
- request_body=route.request_body,
917
- )
918
-
919
- tool = OpenAPITool(
920
- client=self._client,
921
- route=route,
922
- name=tool_name,
923
- description=enhanced_description,
924
- parameters=combined_schema,
925
- output_schema=output_schema,
926
- tags=set(route.tags or []) | tags,
927
- timeout=self._timeout,
928
- )
929
-
930
- # Call component_fn if provided
931
- if self._mcp_component_fn is not None:
932
- try:
933
- self._mcp_component_fn(route, tool)
934
- logger.debug(f"Tool {tool_name} customized by component_fn")
935
- except Exception as e:
936
- logger.warning(
937
- f"Error in component_fn for tool {tool_name}: {e}. "
938
- f"Using component as-is."
939
- )
940
-
941
- # Use the potentially modified tool name as the registration key
942
- final_tool_name = tool.name
943
-
944
- # Register the tool by directly assigning to the tools dictionary
945
- self._tool_manager._tools[final_tool_name] = tool
946
- logger.debug(
947
- f"Registered TOOL: {final_tool_name} ({route.method} {route.path}) with tags: {route.tags}"
948
- )
949
-
950
- def _create_openapi_resource(
951
- self,
952
- route: openapi.HTTPRoute,
953
- name: str,
954
- tags: set[str],
955
- ):
956
- """Creates and registers an OpenAPIResource with enhanced description."""
957
- # Get a unique resource name
958
- resource_name = self._get_unique_name(name, "resource")
959
-
960
- resource_uri = f"resource://{resource_name}"
961
- base_description = (
962
- route.description or route.summary or f"Represents {route.path}"
963
- )
964
-
965
- # Format enhanced description with parameters and request body
966
- enhanced_description = format_description_with_responses(
967
- base_description=base_description,
968
- responses=route.responses,
969
- parameters=route.parameters,
970
- request_body=route.request_body,
971
- )
972
-
973
- resource = OpenAPIResource(
974
- client=self._client,
975
- route=route,
976
- uri=resource_uri,
977
- name=resource_name,
978
- description=enhanced_description,
979
- tags=set(route.tags or []) | tags,
980
- timeout=self._timeout,
981
- )
982
-
983
- # Call component_fn if provided
984
- if self._mcp_component_fn is not None:
985
- try:
986
- self._mcp_component_fn(route, resource)
987
- logger.debug(f"Resource {resource_uri} customized by component_fn")
988
- except Exception as e:
989
- logger.warning(
990
- f"Error in component_fn for resource {resource_uri}: {e}. "
991
- f"Using component as-is."
992
- )
993
-
994
- # Use the potentially modified resource URI as the registration key
995
- final_resource_uri = str(resource.uri)
996
-
997
- # Register the resource by directly assigning to the resources dictionary
998
- self._resource_manager._resources[final_resource_uri] = resource
999
- logger.debug(
1000
- f"Registered RESOURCE: {final_resource_uri} ({route.method} {route.path}) with tags: {route.tags}"
1001
- )
1002
-
1003
- def _create_openapi_template(
1004
- self,
1005
- route: openapi.HTTPRoute,
1006
- name: str,
1007
- tags: set[str],
1008
- ):
1009
- """Creates and registers an OpenAPIResourceTemplate with enhanced description."""
1010
- # Get a unique template name
1011
- template_name = self._get_unique_name(name, "resource_template")
1012
-
1013
- path_params = [p.name for p in route.parameters if p.location == "path"]
1014
- path_params.sort() # Sort for consistent URIs
1015
-
1016
- uri_template_str = f"resource://{template_name}"
1017
- if path_params:
1018
- uri_template_str += "/" + "/".join(f"{{{p}}}" for p in path_params)
1019
-
1020
- base_description = (
1021
- route.description or route.summary or f"Template for {route.path}"
1022
- )
1023
-
1024
- # Format enhanced description with parameters and request body
1025
- enhanced_description = format_description_with_responses(
1026
- base_description=base_description,
1027
- responses=route.responses,
1028
- parameters=route.parameters,
1029
- request_body=route.request_body,
1030
- )
1031
-
1032
- template_params_schema = {
1033
- "type": "object",
1034
- "properties": {
1035
- p.name: {
1036
- **(p.schema_.copy() if isinstance(p.schema_, dict) else {}),
1037
- **(
1038
- {"description": p.description}
1039
- if p.description
1040
- and not (
1041
- isinstance(p.schema_, dict) and "description" in p.schema_
1042
- )
1043
- else {}
1044
- ),
1045
- }
1046
- for p in route.parameters
1047
- if p.location == "path"
1048
- },
1049
- "required": [
1050
- p.name for p in route.parameters if p.location == "path" and p.required
1051
- ],
1052
- }
1053
-
1054
- template = OpenAPIResourceTemplate(
1055
- client=self._client,
1056
- route=route,
1057
- uri_template=uri_template_str,
1058
- name=template_name,
1059
- description=enhanced_description,
1060
- parameters=template_params_schema,
1061
- tags=set(route.tags or []) | tags,
1062
- timeout=self._timeout,
1063
- )
1064
-
1065
- # Call component_fn if provided
1066
- if self._mcp_component_fn is not None:
1067
- try:
1068
- self._mcp_component_fn(route, template)
1069
- logger.debug(f"Template {uri_template_str} customized by component_fn")
1070
- except Exception as e:
1071
- logger.warning(
1072
- f"Error in component_fn for template {uri_template_str}: {e}. "
1073
- f"Using component as-is."
1074
- )
1075
-
1076
- # Use the potentially modified template URI as the registration key
1077
- final_template_uri = template.uri_template
1078
-
1079
- # Register the template by directly assigning to the templates dictionary
1080
- self._resource_manager._templates[final_template_uri] = template
1081
- logger.debug(
1082
- f"Registered TEMPLATE: {final_template_uri} ({route.method} {route.path}) with tags: {route.tags}"
1083
- )