README - replace code snippets with examples -- auth examples (#1164)
This commit is contained in:
parent
813da6a579
commit
41184ba6c8
1 changed files with 241 additions and 31 deletions
46
examples/snippets/servers/oauth_server.py
Normal file
46
examples/snippets/servers/oauth_server.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
"""
|
||||
Run from the repository root:
|
||||
uv run examples/snippets/servers/oauth_server.py
|
||||
"""
|
||||
|
||||
from pydantic import AnyHttpUrl
|
||||
|
||||
from mcp.server.auth.provider import AccessToken, TokenVerifier
|
||||
from mcp.server.auth.settings import AuthSettings
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
|
||||
class SimpleTokenVerifier(TokenVerifier):
|
||||
"""Simple token verifier for demonstration."""
|
||||
|
||||
async def verify_token(self, token: str) -> AccessToken | None:
|
||||
pass # This is where you would implement actual token validation
|
||||
|
||||
|
||||
# Create FastMCP instance as a Resource Server
|
||||
mcp = FastMCP(
|
||||
"Weather Service",
|
||||
# Token verifier for authentication
|
||||
token_verifier=SimpleTokenVerifier(),
|
||||
# Auth settings for RFC 9728 Protected Resource Metadata
|
||||
auth=AuthSettings(
|
||||
issuer_url=AnyHttpUrl("https://auth.example.com"), # Authorization Server URL
|
||||
resource_server_url=AnyHttpUrl("http://localhost:3001"), # This server's URL
|
||||
required_scopes=["user"],
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def get_weather(city: str = "London") -> dict[str, str]:
|
||||
"""Get weather data for a city"""
|
||||
return {
|
||||
"city": city,
|
||||
"temperature": "22",
|
||||
"condition": "Partly cloudy",
|
||||
"humidity": "65%",
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mcp.run(transport="streamable-http")
|
||||
Loading…
Reference in a new issue