d365fo-client 0.2.3__py3-none-any.whl → 0.3.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 (58) hide show
  1. d365fo_client/__init__.py +7 -1
  2. d365fo_client/auth.py +9 -21
  3. d365fo_client/cli.py +25 -13
  4. d365fo_client/client.py +8 -4
  5. d365fo_client/config.py +52 -30
  6. d365fo_client/credential_sources.py +5 -0
  7. d365fo_client/main.py +1 -1
  8. d365fo_client/mcp/__init__.py +3 -1
  9. d365fo_client/mcp/auth_server/__init__.py +5 -0
  10. d365fo_client/mcp/auth_server/auth/__init__.py +30 -0
  11. d365fo_client/mcp/auth_server/auth/auth.py +372 -0
  12. d365fo_client/mcp/auth_server/auth/oauth_proxy.py +989 -0
  13. d365fo_client/mcp/auth_server/auth/providers/__init__.py +0 -0
  14. d365fo_client/mcp/auth_server/auth/providers/azure.py +325 -0
  15. d365fo_client/mcp/auth_server/auth/providers/bearer.py +25 -0
  16. d365fo_client/mcp/auth_server/auth/providers/jwt.py +547 -0
  17. d365fo_client/mcp/auth_server/auth/redirect_validation.py +65 -0
  18. d365fo_client/mcp/auth_server/dependencies.py +136 -0
  19. d365fo_client/mcp/client_manager.py +16 -67
  20. d365fo_client/mcp/fastmcp_main.py +358 -0
  21. d365fo_client/mcp/fastmcp_server.py +598 -0
  22. d365fo_client/mcp/fastmcp_utils.py +431 -0
  23. d365fo_client/mcp/main.py +40 -13
  24. d365fo_client/mcp/mixins/__init__.py +24 -0
  25. d365fo_client/mcp/mixins/base_tools_mixin.py +55 -0
  26. d365fo_client/mcp/mixins/connection_tools_mixin.py +50 -0
  27. d365fo_client/mcp/mixins/crud_tools_mixin.py +311 -0
  28. d365fo_client/mcp/mixins/database_tools_mixin.py +685 -0
  29. d365fo_client/mcp/mixins/label_tools_mixin.py +87 -0
  30. d365fo_client/mcp/mixins/metadata_tools_mixin.py +565 -0
  31. d365fo_client/mcp/mixins/performance_tools_mixin.py +109 -0
  32. d365fo_client/mcp/mixins/profile_tools_mixin.py +713 -0
  33. d365fo_client/mcp/mixins/sync_tools_mixin.py +321 -0
  34. d365fo_client/mcp/prompts/action_execution.py +1 -1
  35. d365fo_client/mcp/prompts/sequence_analysis.py +1 -1
  36. d365fo_client/mcp/tools/crud_tools.py +3 -3
  37. d365fo_client/mcp/tools/sync_tools.py +1 -1
  38. d365fo_client/mcp/utilities/__init__.py +1 -0
  39. d365fo_client/mcp/utilities/auth.py +34 -0
  40. d365fo_client/mcp/utilities/logging.py +58 -0
  41. d365fo_client/mcp/utilities/types.py +426 -0
  42. d365fo_client/metadata_v2/sync_manager_v2.py +2 -0
  43. d365fo_client/metadata_v2/sync_session_manager.py +7 -7
  44. d365fo_client/models.py +139 -139
  45. d365fo_client/output.py +2 -2
  46. d365fo_client/profile_manager.py +62 -27
  47. d365fo_client/profiles.py +118 -113
  48. d365fo_client/settings.py +355 -0
  49. d365fo_client/sync_models.py +85 -2
  50. d365fo_client/utils.py +2 -1
  51. {d365fo_client-0.2.3.dist-info → d365fo_client-0.3.0.dist-info}/METADATA +1261 -810
  52. d365fo_client-0.3.0.dist-info/RECORD +84 -0
  53. d365fo_client-0.3.0.dist-info/entry_points.txt +4 -0
  54. d365fo_client-0.2.3.dist-info/RECORD +0 -56
  55. d365fo_client-0.2.3.dist-info/entry_points.txt +0 -3
  56. {d365fo_client-0.2.3.dist-info → d365fo_client-0.3.0.dist-info}/WHEEL +0 -0
  57. {d365fo_client-0.2.3.dist-info → d365fo_client-0.3.0.dist-info}/licenses/LICENSE +0 -0
  58. {d365fo_client-0.2.3.dist-info → d365fo_client-0.3.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,372 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Any
4
+ from urllib.parse import urljoin
5
+
6
+ from mcp.server.auth.middleware.auth_context import AuthContextMiddleware
7
+ from mcp.server.auth.middleware.bearer_auth import (
8
+ BearerAuthBackend,
9
+ RequireAuthMiddleware,
10
+ )
11
+ from mcp.server.auth.provider import (
12
+ AccessToken as _SDKAccessToken,
13
+ )
14
+ from mcp.server.auth.provider import (
15
+ AuthorizationCode,
16
+ OAuthAuthorizationServerProvider,
17
+ RefreshToken,
18
+ )
19
+ from mcp.server.auth.provider import (
20
+ TokenVerifier as TokenVerifierProtocol,
21
+ )
22
+ from mcp.server.auth.routes import (
23
+ create_auth_routes,
24
+ create_protected_resource_routes,
25
+ )
26
+ from mcp.server.auth.settings import (
27
+ ClientRegistrationOptions,
28
+ RevocationOptions,
29
+ )
30
+ from pydantic import AnyHttpUrl
31
+ from starlette.middleware import Middleware
32
+ from starlette.middleware.authentication import AuthenticationMiddleware
33
+ from starlette.routing import Route
34
+
35
+
36
+ class AccessToken(_SDKAccessToken):
37
+ """AccessToken that includes all JWT claims."""
38
+
39
+ claims: dict[str, Any] = {}
40
+
41
+
42
+ class AuthProvider(TokenVerifierProtocol):
43
+ """Base class for all FastMCP authentication providers.
44
+
45
+ This class provides a unified interface for all authentication providers,
46
+ whether they are simple token verifiers or full OAuth authorization servers.
47
+ All providers must be able to verify tokens and can optionally provide
48
+ custom authentication routes.
49
+ """
50
+
51
+ def __init__(
52
+ self,
53
+ base_url: AnyHttpUrl | str | None = None,
54
+ required_scopes: list[str] | None = None,
55
+ ):
56
+ """
57
+ Initialize the auth provider.
58
+
59
+ Args:
60
+ base_url: The base URL of this server (e.g., http://localhost:8000).
61
+ This is used for constructing .well-known endpoints and OAuth metadata.
62
+ required_scopes: List of OAuth scopes required for all requests.
63
+ """
64
+ if isinstance(base_url, str):
65
+ base_url = AnyHttpUrl(base_url)
66
+ self.base_url = base_url
67
+ self.required_scopes = required_scopes or []
68
+
69
+ async def verify_token(self, token: str) -> AccessToken | None:
70
+ """Verify a bearer token and return access info if valid.
71
+
72
+ All auth providers must implement token verification.
73
+
74
+ Args:
75
+ token: The token string to validate
76
+
77
+ Returns:
78
+ AccessToken object if valid, None if invalid or expired
79
+ """
80
+ raise NotImplementedError("Subclasses must implement verify_token")
81
+
82
+ def get_routes(
83
+ self,
84
+ mcp_path: str | None = None,
85
+ mcp_endpoint: Any | None = None,
86
+ ) -> list[Route]:
87
+ """Get the routes for this authentication provider.
88
+
89
+ Each provider is responsible for creating whatever routes it needs:
90
+ - TokenVerifier: typically no routes (default implementation)
91
+ - RemoteAuthProvider: protected resource metadata routes
92
+ - OAuthProvider: full OAuth authorization server routes
93
+ - Custom providers: whatever routes they need
94
+
95
+ Args:
96
+ mcp_path: The path where the MCP endpoint is mounted (e.g., "/mcp")
97
+ mcp_endpoint: The MCP endpoint handler to protect with auth
98
+
99
+ Returns:
100
+ List of routes for this provider, including protected MCP endpoints if provided
101
+ """
102
+
103
+ routes = []
104
+
105
+ # Add protected MCP endpoint if provided
106
+ if mcp_path and mcp_endpoint:
107
+ resource_metadata_url = self._get_resource_url(
108
+ "/.well-known/oauth-protected-resource"
109
+ )
110
+
111
+ routes.append(
112
+ Route(
113
+ mcp_path,
114
+ endpoint=RequireAuthMiddleware(
115
+ mcp_endpoint, self.required_scopes, resource_metadata_url
116
+ ),
117
+ )
118
+ )
119
+
120
+ return routes
121
+
122
+ def get_middleware(self) -> list:
123
+ """Get HTTP application-level middleware for this auth provider.
124
+
125
+ Returns:
126
+ List of Starlette Middleware instances to apply to the HTTP app
127
+ """
128
+ return [
129
+ Middleware(
130
+ AuthenticationMiddleware,
131
+ backend=BearerAuthBackend(self),
132
+ ),
133
+ Middleware(AuthContextMiddleware),
134
+ ]
135
+
136
+ def _get_resource_url(self, path: str | None = None) -> AnyHttpUrl | None:
137
+ """Get the actual resource URL being protected.
138
+
139
+ Args:
140
+ path: The path where the resource endpoint is mounted (e.g., "/mcp")
141
+
142
+ Returns:
143
+ The full URL of the protected resource
144
+ """
145
+ if self.base_url is None:
146
+ return None
147
+
148
+ if path:
149
+ return AnyHttpUrl(urljoin(str(self.base_url), path))
150
+
151
+ return self.base_url
152
+
153
+
154
+ class TokenVerifier(AuthProvider):
155
+ """Base class for token verifiers (Resource Servers).
156
+
157
+ This class provides token verification capability without OAuth server functionality.
158
+ Token verifiers typically don't provide authentication routes by default.
159
+ """
160
+
161
+ def __init__(
162
+ self,
163
+ base_url: AnyHttpUrl | str | None = None,
164
+ required_scopes: list[str] | None = None,
165
+ ):
166
+ """
167
+ Initialize the token verifier.
168
+
169
+ Args:
170
+ base_url: The base URL of this server
171
+ required_scopes: Scopes that are required for all requests
172
+ """
173
+ super().__init__(base_url=base_url, required_scopes=required_scopes)
174
+
175
+ async def verify_token(self, token: str) -> AccessToken | None:
176
+ """Verify a bearer token and return access info if valid."""
177
+ raise NotImplementedError("Subclasses must implement verify_token")
178
+
179
+
180
+ class RemoteAuthProvider(AuthProvider):
181
+ """Authentication provider for resource servers that verify tokens from known authorization servers.
182
+
183
+ This provider composes a TokenVerifier with authorization server metadata to create
184
+ standardized OAuth 2.0 Protected Resource endpoints (RFC 9728). Perfect for:
185
+ - JWT verification with known issuers
186
+ - Remote token introspection services
187
+ - Any resource server that knows where its tokens come from
188
+
189
+ Use this when you have token verification logic and want to advertise
190
+ the authorization servers that issue valid tokens.
191
+ """
192
+
193
+ base_url: AnyHttpUrl
194
+
195
+ def __init__(
196
+ self,
197
+ token_verifier: TokenVerifier,
198
+ authorization_servers: list[AnyHttpUrl],
199
+ base_url: AnyHttpUrl | str,
200
+ resource_name: str | None = None,
201
+ resource_documentation: AnyHttpUrl | None = None,
202
+ ):
203
+ """Initialize the remote auth provider.
204
+
205
+ Args:
206
+ token_verifier: TokenVerifier instance for token validation
207
+ authorization_servers: List of authorization servers that issue valid tokens
208
+ base_url: The base URL of this server
209
+ resource_name: Optional name for the protected resource
210
+ resource_documentation: Optional documentation URL for the protected resource
211
+ """
212
+ super().__init__(
213
+ base_url=base_url,
214
+ required_scopes=token_verifier.required_scopes,
215
+ )
216
+ self.token_verifier = token_verifier
217
+ self.authorization_servers = authorization_servers
218
+ self.resource_name = resource_name
219
+ self.resource_documentation = resource_documentation
220
+
221
+ async def verify_token(self, token: str) -> AccessToken | None:
222
+ """Verify token using the configured token verifier."""
223
+ return await self.token_verifier.verify_token(token)
224
+
225
+ def get_routes(
226
+ self,
227
+ mcp_path: str | None = None,
228
+ mcp_endpoint: Any | None = None,
229
+ ) -> list[Route]:
230
+ """Get OAuth routes for this provider.
231
+
232
+ Creates protected resource metadata routes and optionally wraps MCP endpoints with auth.
233
+ """
234
+ # Start with base routes (protected MCP endpoint)
235
+ routes = super().get_routes(mcp_path, mcp_endpoint)
236
+
237
+ # Get the resource URL based on the MCP path
238
+ resource_url = self._get_resource_url(mcp_path)
239
+
240
+ if resource_url:
241
+ # Add protected resource metadata routes
242
+ routes.extend(
243
+ create_protected_resource_routes(
244
+ resource_url=resource_url,
245
+ authorization_servers=self.authorization_servers,
246
+ scopes_supported=self.token_verifier.required_scopes,
247
+ resource_name=self.resource_name,
248
+ resource_documentation=self.resource_documentation,
249
+ )
250
+ )
251
+
252
+ return routes
253
+
254
+
255
+ class OAuthProvider(
256
+ AuthProvider,
257
+ OAuthAuthorizationServerProvider[AuthorizationCode, RefreshToken, AccessToken],
258
+ ):
259
+ """OAuth Authorization Server provider.
260
+
261
+ This class provides full OAuth server functionality including client registration,
262
+ authorization flows, token issuance, and token verification.
263
+ """
264
+
265
+ def __init__(
266
+ self,
267
+ *,
268
+ base_url: AnyHttpUrl | str,
269
+ issuer_url: AnyHttpUrl | str | None = None,
270
+ service_documentation_url: AnyHttpUrl | str | None = None,
271
+ client_registration_options: ClientRegistrationOptions | None = None,
272
+ revocation_options: RevocationOptions | None = None,
273
+ required_scopes: list[str] | None = None,
274
+ ):
275
+ """
276
+ Initialize the OAuth provider.
277
+
278
+ Args:
279
+ base_url: The public URL of this FastMCP server
280
+ issuer_url: The issuer URL for OAuth metadata (defaults to base_url)
281
+ service_documentation_url: The URL of the service documentation.
282
+ client_registration_options: The client registration options.
283
+ revocation_options: The revocation options.
284
+ required_scopes: Scopes that are required for all requests.
285
+ """
286
+
287
+ # Convert URLs to proper types
288
+ if isinstance(base_url, str):
289
+ base_url = AnyHttpUrl(base_url)
290
+
291
+ super().__init__(base_url=base_url, required_scopes=required_scopes)
292
+ self.base_url = base_url
293
+
294
+ if issuer_url is None:
295
+ self.issuer_url = base_url
296
+ elif isinstance(issuer_url, str):
297
+ self.issuer_url = AnyHttpUrl(issuer_url)
298
+ else:
299
+ self.issuer_url = issuer_url
300
+
301
+ # Initialize OAuth Authorization Server Provider
302
+ OAuthAuthorizationServerProvider.__init__(self)
303
+
304
+ if isinstance(service_documentation_url, str):
305
+ service_documentation_url = AnyHttpUrl(service_documentation_url)
306
+
307
+ self.service_documentation_url = service_documentation_url
308
+ self.client_registration_options = client_registration_options
309
+ self.revocation_options = revocation_options
310
+
311
+ async def verify_token(self, token: str) -> AccessToken | None:
312
+ """
313
+ Verify a bearer token and return access info if valid.
314
+
315
+ This method implements the TokenVerifier protocol by delegating
316
+ to our existing load_access_token method.
317
+
318
+ Args:
319
+ token: The token string to validate
320
+
321
+ Returns:
322
+ AccessToken object if valid, None if invalid or expired
323
+ """
324
+ return await self.load_access_token(token)
325
+
326
+ def get_routes(
327
+ self,
328
+ mcp_path: str | None = None,
329
+ mcp_endpoint: Any | None = None,
330
+ ) -> list[Route]:
331
+ """Get OAuth authorization server routes and optional protected resource routes.
332
+
333
+ This method creates the full set of OAuth routes including:
334
+ - Standard OAuth authorization server routes (/.well-known/oauth-authorization-server, /authorize, /token, etc.)
335
+ - Optional protected resource routes
336
+ - Protected MCP endpoints if provided
337
+
338
+ Returns:
339
+ List of OAuth routes
340
+ """
341
+
342
+ # Create standard OAuth authorization server routes
343
+ oauth_routes = create_auth_routes(
344
+ provider=self,
345
+ issuer_url=self.issuer_url,
346
+ service_documentation_url=self.service_documentation_url,
347
+ client_registration_options=self.client_registration_options,
348
+ revocation_options=self.revocation_options,
349
+ )
350
+
351
+ # Get the resource URL based on the MCP path
352
+ resource_url = self._get_resource_url(mcp_path)
353
+
354
+ # Add protected resource routes if this server is also acting as a resource server
355
+ if resource_url:
356
+ supported_scopes = (
357
+ self.client_registration_options.valid_scopes
358
+ if self.client_registration_options
359
+ and self.client_registration_options.valid_scopes
360
+ else self.required_scopes
361
+ )
362
+ protected_routes = create_protected_resource_routes(
363
+ resource_url=resource_url,
364
+ authorization_servers=[self.issuer_url],
365
+ scopes_supported=supported_scopes,
366
+ )
367
+ oauth_routes.extend(protected_routes)
368
+
369
+ # Add protected MCP endpoint from base class
370
+ oauth_routes.extend(super().get_routes(mcp_path, mcp_endpoint))
371
+
372
+ return oauth_routes