Skip to content

Commit 8b93b99

Browse files
committed
Register scopes automatically for PRM
1 parent dc8d056 commit 8b93b99

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

examples/example-fastmcp-mcp/src/auth0/authz.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
from mcp.server.fastmcp import Context
77

8+
from . import Auth0Mcp
89
from .errors import AuthenticationRequired, InsufficientScope
910

11+
# Collect required scopes from all decorated functions
12+
_scopes_required: set[str] = set()
1013

1114
def require_scopes(required_scopes: Iterable[str]):
1215
"""
@@ -19,6 +22,10 @@ async def my_tool(name: str, ctx: Context) -> str:
1922
return f"Hello {name}!"
2023
"""
2124
required_scopes_list = list(required_scopes)
25+
26+
# Collect scopes when decorator is applied
27+
_scopes_required.update(required_scopes_list)
28+
2229
def decorator(func):
2330
@wraps(func)
2431
async def wrapper(*args, **kwargs):
@@ -39,3 +46,9 @@ async def wrapper(*args, **kwargs):
3946
return await func(*args, **kwargs)
4047
return wrapper
4148
return decorator
49+
50+
def register_required_scopes(auth0_mcp: Auth0Mcp) -> None:
51+
"""Register all scopes that were collected from @require_scopes decorators."""
52+
if _scopes_required:
53+
auth0_mcp.register_scopes(list(_scopes_required))
54+
_scopes_required.clear()

examples/example-fastmcp-mcp/src/tools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
from mcp.server.fastmcp import Context
44

55
from .auth0 import Auth0Mcp
6-
from .auth0.authz import require_scopes
6+
from .auth0.authz import register_required_scopes, require_scopes
77

88

99
def register_tools(auth0_mcp: Auth0Mcp) -> None:
1010
"""
1111
Register all tools with the MCP server.
1212
"""
1313
mcp = auth0_mcp.mcp
14-
# Register scopes used by tools for Protected Resource Metadata
15-
auth0_mcp.register_scopes(["tool:greet", "tool:whoami"])
1614

1715
# Tool without required scopes
1816
@mcp.tool()
@@ -50,3 +48,6 @@ async def whoami(ctx: Context) -> str:
5048
"scopes": auth_info.get("scopes", []),
5149
}
5250
return json.dumps(response_data, indent=2)
51+
52+
# Register all scopes used by tools for Protected Resource Metadata
53+
register_required_scopes(auth0_mcp)

0 commit comments

Comments
 (0)