@@ -120,6 +120,14 @@ class Settings(BaseSettings, Generic[LifespanResultT]):
120120 # Transport security settings (DNS rebinding protection)
121121 transport_security : TransportSecuritySettings | None = None
122122
123+ def get_resource_server_url (self ) -> str :
124+ """Construct the resource server URL from host and port settings."""
125+ scheme = "https" if self .port == 443 else "http"
126+ if self .port in (80 , 443 ):
127+ return f"{ scheme } ://{ self .host } "
128+ else :
129+ return f"{ scheme } ://{ self .host } :{ self .port } "
130+
123131
124132def lifespan_wrapper (
125133 app : FastMCP ,
@@ -743,11 +751,12 @@ async def handle_sse(scope: Scope, receive: Receive, send: Send):
743751 if self ._token_verifier :
744752 # Determine resource metadata URL
745753 resource_metadata_url = None
746- if self .settings .auth and self . settings . auth . authorization_servers :
754+ if self .settings .auth :
747755 from pydantic import AnyHttpUrl
748756
757+ resource_server_url = self .settings .get_resource_server_url ()
749758 resource_metadata_url = AnyHttpUrl (
750- str ( self . settings . auth . issuer_url ) .rstrip ("/" ) + "/.well-known/oauth-protected-resource"
759+ resource_server_url .rstrip ("/" ) + "/.well-known/oauth-protected-resource"
751760 )
752761
753762 # Auth is enabled, wrap the endpoints with RequireAuthMiddleware
@@ -785,13 +794,15 @@ async def sse_endpoint(request: Request) -> Response:
785794 )
786795 )
787796 # Add protected resource metadata endpoint if configured as RS
788- if self .settings .auth and self . settings . auth . authorization_servers :
797+ if self .settings .auth :
789798 from mcp .server .auth .routes import create_protected_resource_routes
799+ from pydantic import AnyHttpUrl
790800
801+ resource_server_url = AnyHttpUrl (self .settings .get_resource_server_url ())
791802 routes .extend (
792803 create_protected_resource_routes (
793- resource_url = self . settings . auth . issuer_url ,
794- authorization_servers = self .settings .auth .authorization_servers ,
804+ resource_url = resource_server_url ,
805+ authorization_servers = [ self .settings .auth .issuer_url ] ,
795806 scopes_supported = self .settings .auth .required_scopes ,
796807 )
797808 )
@@ -858,11 +869,12 @@ async def handle_streamable_http(scope: Scope, receive: Receive, send: Send) ->
858869 if self ._token_verifier :
859870 # Determine resource metadata URL
860871 resource_metadata_url = None
861- if self .settings .auth and self . settings . auth . authorization_servers :
872+ if self .settings .auth :
862873 from pydantic import AnyHttpUrl
863874
875+ resource_server_url = self .settings .get_resource_server_url ()
864876 resource_metadata_url = AnyHttpUrl (
865- str ( self . settings . auth . issuer_url ) .rstrip ("/" ) + "/.well-known/oauth-protected-resource"
877+ resource_server_url .rstrip ("/" ) + "/.well-known/oauth-protected-resource"
866878 )
867879
868880 routes .append (
@@ -881,14 +893,16 @@ async def handle_streamable_http(scope: Scope, receive: Receive, send: Send) ->
881893 )
882894
883895 # Add protected resource metadata endpoint if configured as RS
884- if self .settings .auth and self . settings . auth . authorization_servers :
896+ if self .settings .auth :
885897 from mcp .server .auth .handlers .metadata import ProtectedResourceMetadataHandler
886898 from mcp .server .auth .routes import cors_middleware
887899 from mcp .shared .auth import ProtectedResourceMetadata
900+ from pydantic import AnyHttpUrl
888901
902+ resource_server_url = AnyHttpUrl (self .settings .get_resource_server_url ())
889903 protected_resource_metadata = ProtectedResourceMetadata (
890- resource = self . settings . auth . issuer_url ,
891- authorization_servers = self .settings .auth .authorization_servers ,
904+ resource = resource_server_url ,
905+ authorization_servers = [ self .settings .auth .issuer_url ] ,
892906 scopes_supported = self .settings .auth .required_scopes ,
893907 )
894908 routes .append (
0 commit comments