fastmcp 2.10.6__py3-none-any.whl → 2.11.1__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 (61) hide show
  1. fastmcp/cli/cli.py +128 -33
  2. fastmcp/cli/install/claude_code.py +42 -1
  3. fastmcp/cli/install/claude_desktop.py +42 -1
  4. fastmcp/cli/install/cursor.py +42 -1
  5. fastmcp/cli/install/mcp_json.py +41 -0
  6. fastmcp/cli/run.py +127 -1
  7. fastmcp/client/__init__.py +2 -0
  8. fastmcp/client/auth/oauth.py +68 -99
  9. fastmcp/client/oauth_callback.py +18 -0
  10. fastmcp/client/transports.py +69 -15
  11. fastmcp/contrib/component_manager/example.py +2 -2
  12. fastmcp/experimental/server/openapi/README.md +266 -0
  13. fastmcp/experimental/server/openapi/__init__.py +38 -0
  14. fastmcp/experimental/server/openapi/components.py +348 -0
  15. fastmcp/experimental/server/openapi/routing.py +132 -0
  16. fastmcp/experimental/server/openapi/server.py +466 -0
  17. fastmcp/experimental/utilities/openapi/README.md +239 -0
  18. fastmcp/experimental/utilities/openapi/__init__.py +68 -0
  19. fastmcp/experimental/utilities/openapi/director.py +208 -0
  20. fastmcp/experimental/utilities/openapi/formatters.py +355 -0
  21. fastmcp/experimental/utilities/openapi/json_schema_converter.py +340 -0
  22. fastmcp/experimental/utilities/openapi/models.py +85 -0
  23. fastmcp/experimental/utilities/openapi/parser.py +618 -0
  24. fastmcp/experimental/utilities/openapi/schemas.py +538 -0
  25. fastmcp/mcp_config.py +125 -88
  26. fastmcp/prompts/prompt.py +11 -1
  27. fastmcp/resources/resource.py +21 -1
  28. fastmcp/resources/template.py +20 -1
  29. fastmcp/server/auth/__init__.py +18 -2
  30. fastmcp/server/auth/auth.py +225 -7
  31. fastmcp/server/auth/providers/bearer.py +25 -473
  32. fastmcp/server/auth/providers/in_memory.py +4 -2
  33. fastmcp/server/auth/providers/jwt.py +538 -0
  34. fastmcp/server/auth/providers/workos.py +151 -0
  35. fastmcp/server/auth/registry.py +52 -0
  36. fastmcp/server/context.py +107 -26
  37. fastmcp/server/dependencies.py +9 -2
  38. fastmcp/server/http.py +48 -57
  39. fastmcp/server/middleware/middleware.py +3 -23
  40. fastmcp/server/openapi.py +1 -1
  41. fastmcp/server/proxy.py +50 -11
  42. fastmcp/server/server.py +168 -59
  43. fastmcp/settings.py +73 -6
  44. fastmcp/tools/tool.py +36 -3
  45. fastmcp/tools/tool_manager.py +38 -2
  46. fastmcp/tools/tool_transform.py +112 -3
  47. fastmcp/utilities/components.py +41 -3
  48. fastmcp/utilities/json_schema.py +136 -98
  49. fastmcp/utilities/json_schema_type.py +1 -3
  50. fastmcp/utilities/mcp_config.py +28 -0
  51. fastmcp/utilities/openapi.py +243 -57
  52. fastmcp/utilities/tests.py +54 -6
  53. fastmcp/utilities/types.py +94 -11
  54. {fastmcp-2.10.6.dist-info → fastmcp-2.11.1.dist-info}/METADATA +4 -3
  55. fastmcp-2.11.1.dist-info/RECORD +108 -0
  56. fastmcp/server/auth/providers/bearer_env.py +0 -63
  57. fastmcp/utilities/cache.py +0 -26
  58. fastmcp-2.10.6.dist-info/RECORD +0 -93
  59. {fastmcp-2.10.6.dist-info → fastmcp-2.11.1.dist-info}/WHEEL +0 -0
  60. {fastmcp-2.10.6.dist-info → fastmcp-2.11.1.dist-info}/entry_points.txt +0 -0
  61. {fastmcp-2.10.6.dist-info → fastmcp-2.11.1.dist-info}/licenses/LICENSE +0 -0
@@ -1,48 +1,235 @@
1
+ from __future__ import annotations
2
+
1
3
  from mcp.server.auth.provider import (
2
4
  AccessToken,
3
5
  AuthorizationCode,
4
6
  OAuthAuthorizationServerProvider,
5
7
  RefreshToken,
6
8
  )
9
+ from mcp.server.auth.provider import (
10
+ TokenVerifier as TokenVerifierProtocol,
11
+ )
12
+ from mcp.server.auth.routes import (
13
+ create_auth_routes,
14
+ create_protected_resource_routes,
15
+ )
7
16
  from mcp.server.auth.settings import (
8
17
  ClientRegistrationOptions,
9
18
  RevocationOptions,
10
19
  )
11
20
  from pydantic import AnyHttpUrl
21
+ from starlette.routing import Route
22
+
23
+
24
+ class AuthProvider(TokenVerifierProtocol):
25
+ """Base class for all FastMCP authentication providers.
26
+
27
+ This class provides a unified interface for all authentication providers,
28
+ whether they are simple token verifiers or full OAuth authorization servers.
29
+ All providers must be able to verify tokens and can optionally provide
30
+ custom authentication routes.
31
+ """
32
+
33
+ def __init__(self, resource_server_url: AnyHttpUrl | str | None = None):
34
+ """
35
+ Initialize the auth provider.
36
+
37
+ Args:
38
+ resource_server_url: The URL of this resource server. This is used
39
+ for RFC 8707 resource indicators, including creating the WWW-Authenticate
40
+ header.
41
+ """
42
+ if isinstance(resource_server_url, str):
43
+ resource_server_url = AnyHttpUrl(resource_server_url)
44
+ self.resource_server_url = resource_server_url
45
+
46
+ async def verify_token(self, token: str) -> AccessToken | None:
47
+ """Verify a bearer token and return access info if valid.
48
+
49
+ All auth providers must implement token verification.
50
+
51
+ Args:
52
+ token: The token string to validate
53
+
54
+ Returns:
55
+ AccessToken object if valid, None if invalid or expired
56
+ """
57
+ raise NotImplementedError("Subclasses must implement verify_token")
58
+
59
+ def get_routes(self) -> list[Route]:
60
+ """Get the routes for this authentication provider.
61
+
62
+ Each provider is responsible for creating whatever routes it needs:
63
+ - TokenVerifier: typically no routes (default implementation)
64
+ - RemoteAuthProvider: protected resource metadata routes
65
+ - OAuthProvider: full OAuth authorization server routes
66
+ - Custom providers: whatever routes they need
67
+
68
+ Returns:
69
+ List of routes for this provider
70
+ """
71
+ return []
72
+
73
+ def get_resource_metadata_url(self) -> AnyHttpUrl | None:
74
+ """Get the resource metadata URL for RFC 9728 compliance."""
75
+ if self.resource_server_url is None:
76
+ return None
77
+
78
+ # Add .well-known path for RFC 9728 compliance
79
+ resource_metadata_url = AnyHttpUrl(
80
+ str(self.resource_server_url).rstrip("/")
81
+ + "/.well-known/oauth-protected-resource"
82
+ )
83
+ return resource_metadata_url
84
+
85
+
86
+ class TokenVerifier(AuthProvider):
87
+ """Base class for token verifiers (Resource Servers).
88
+
89
+ This class provides token verification capability without OAuth server functionality.
90
+ Token verifiers typically don't provide authentication routes by default.
91
+ """
92
+
93
+ def __init__(
94
+ self,
95
+ resource_server_url: AnyHttpUrl | str | None = None,
96
+ required_scopes: list[str] | None = None,
97
+ ):
98
+ """
99
+ Initialize the token verifier.
100
+
101
+ Args:
102
+ resource_server_url: The URL of this resource server. This is used
103
+ for RFC 8707 resource indicators, including creating the WWW-Authenticate
104
+ header.
105
+ required_scopes: Scopes that are required for all requests
106
+ """
107
+ super().__init__(resource_server_url=resource_server_url)
108
+ self.required_scopes = required_scopes or []
109
+
110
+ async def verify_token(self, token: str) -> AccessToken | None:
111
+ """Verify a bearer token and return access info if valid."""
112
+ raise NotImplementedError("Subclasses must implement verify_token")
113
+
114
+
115
+ class RemoteAuthProvider(AuthProvider):
116
+ """Authentication provider for resource servers that verify tokens from known authorization servers.
117
+
118
+ This provider composes a TokenVerifier with authorization server metadata to create
119
+ standardized OAuth 2.0 Protected Resource endpoints (RFC 9728). Perfect for:
120
+ - JWT verification with known issuers
121
+ - Remote token introspection services
122
+ - Any resource server that knows where its tokens come from
123
+
124
+ Use this when you have token verification logic and want to advertise
125
+ the authorization servers that issue valid tokens.
126
+ """
127
+
128
+ def __init__(
129
+ self,
130
+ token_verifier: TokenVerifier,
131
+ authorization_servers: list[AnyHttpUrl],
132
+ resource_server_url: AnyHttpUrl | str,
133
+ ):
134
+ """Initialize the remote auth provider.
135
+
136
+ Args:
137
+ token_verifier: TokenVerifier instance for token validation
138
+ authorization_servers: List of authorization servers that issue valid tokens
139
+ resource_server_url: URL of this resource server. This is used
140
+ for RFC 8707 resource indicators, including creating the WWW-Authenticate
141
+ header.
142
+ """
143
+ super().__init__(resource_server_url=resource_server_url)
144
+ self.token_verifier = token_verifier
145
+ self.authorization_servers = authorization_servers
146
+
147
+ async def verify_token(self, token: str) -> AccessToken | None:
148
+ """Verify token using the configured token verifier."""
149
+ return await self.token_verifier.verify_token(token)
150
+
151
+ def get_routes(self) -> list[Route]:
152
+ """Get OAuth routes for this provider.
153
+
154
+ By default, returns only the standardized OAuth 2.0 Protected Resource routes.
155
+ Subclasses can override this method to add additional routes by calling
156
+ super().get_routes() and extending the returned list.
157
+ """
158
+ assert self.resource_server_url is not None
159
+
160
+ return create_protected_resource_routes(
161
+ resource_url=self.resource_server_url,
162
+ authorization_servers=self.authorization_servers,
163
+ scopes_supported=self.token_verifier.required_scopes,
164
+ )
12
165
 
13
166
 
14
167
  class OAuthProvider(
15
- OAuthAuthorizationServerProvider[AuthorizationCode, RefreshToken, AccessToken]
168
+ AuthProvider,
169
+ OAuthAuthorizationServerProvider[AuthorizationCode, RefreshToken, AccessToken],
16
170
  ):
171
+ """OAuth Authorization Server provider.
172
+
173
+ This class provides full OAuth server functionality including client registration,
174
+ authorization flows, token issuance, and token verification.
175
+ """
176
+
17
177
  def __init__(
18
178
  self,
19
- issuer_url: AnyHttpUrl | str,
179
+ *,
180
+ base_url: AnyHttpUrl | str,
181
+ issuer_url: AnyHttpUrl | str | None = None,
20
182
  service_documentation_url: AnyHttpUrl | str | None = None,
21
183
  client_registration_options: ClientRegistrationOptions | None = None,
22
184
  revocation_options: RevocationOptions | None = None,
23
185
  required_scopes: list[str] | None = None,
186
+ resource_server_url: AnyHttpUrl | str | None = None,
24
187
  ):
25
188
  """
26
189
  Initialize the OAuth provider.
27
190
 
28
191
  Args:
29
- issuer_url: The URL of the OAuth issuer.
192
+ base_url: The public URL of this FastMCP server
193
+ issuer_url: The issuer URL for OAuth metadata (defaults to base_url)
30
194
  service_documentation_url: The URL of the service documentation.
31
195
  client_registration_options: The client registration options.
32
196
  revocation_options: The revocation options.
33
197
  required_scopes: Scopes that are required for all requests.
198
+ resource_server_url: The URL of this resource server (for RFC 8707 resource indicators, defaults to base_url)
34
199
  """
200
+
35
201
  super().__init__()
36
- if isinstance(issuer_url, str):
37
- issuer_url = AnyHttpUrl(issuer_url)
202
+
203
+ # Convert URLs to proper types
204
+ if isinstance(base_url, str):
205
+ base_url = AnyHttpUrl(base_url)
206
+ self.base_url = base_url
207
+
208
+ if issuer_url is None:
209
+ self.issuer_url = base_url
210
+ elif isinstance(issuer_url, str):
211
+ self.issuer_url = AnyHttpUrl(issuer_url)
212
+ else:
213
+ self.issuer_url = issuer_url
214
+
215
+ # Handle our own resource_server_url and required_scopes
216
+ if resource_server_url is None:
217
+ self.resource_server_url = base_url
218
+ elif isinstance(resource_server_url, str):
219
+ self.resource_server_url = AnyHttpUrl(resource_server_url)
220
+ else:
221
+ self.resource_server_url = resource_server_url
222
+ self.required_scopes = required_scopes or []
223
+
224
+ # Initialize OAuth Authorization Server Provider
225
+ OAuthAuthorizationServerProvider.__init__(self)
226
+
38
227
  if isinstance(service_documentation_url, str):
39
228
  service_documentation_url = AnyHttpUrl(service_documentation_url)
40
229
 
41
- self.issuer_url = issuer_url
42
230
  self.service_documentation_url = service_documentation_url
43
231
  self.client_registration_options = client_registration_options
44
232
  self.revocation_options = revocation_options
45
- self.required_scopes = required_scopes
46
233
 
47
234
  async def verify_token(self, token: str) -> AccessToken | None:
48
235
  """
@@ -58,3 +245,34 @@ class OAuthProvider(
58
245
  AccessToken object if valid, None if invalid or expired
59
246
  """
60
247
  return await self.load_access_token(token)
248
+
249
+ def get_routes(self) -> list[Route]:
250
+ """Get OAuth authorization server routes and optional protected resource routes.
251
+
252
+ This method creates the full set of OAuth routes including:
253
+ - Standard OAuth authorization server routes (/.well-known/oauth-authorization-server, /authorize, /token, etc.)
254
+ - Optional protected resource routes if resource_server_url is configured
255
+
256
+ Returns:
257
+ List of OAuth routes
258
+ """
259
+
260
+ # Create standard OAuth authorization server routes
261
+ oauth_routes = create_auth_routes(
262
+ provider=self,
263
+ issuer_url=self.issuer_url,
264
+ service_documentation_url=self.service_documentation_url,
265
+ client_registration_options=self.client_registration_options,
266
+ revocation_options=self.revocation_options,
267
+ )
268
+
269
+ # Add protected resource routes if this server is also acting as a resource server
270
+ if self.resource_server_url:
271
+ protected_routes = create_protected_resource_routes(
272
+ resource_url=self.resource_server_url,
273
+ authorization_servers=[self.issuer_url],
274
+ scopes_supported=self.required_scopes,
275
+ )
276
+ oauth_routes.extend(protected_routes)
277
+
278
+ return oauth_routes