Skip to content

Commit 2aa54c3

Browse files
committed
Fixed pre-commit errors
1 parent 0671099 commit 2aa54c3

File tree

9 files changed

+67
-71
lines changed

9 files changed

+67
-71
lines changed

examples/servers/proxy_oauth/server.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# pyright: reportMissingImports=false
2-
import os
2+
import base64
3+
import json
34
import logging
4-
from dotenv import load_dotenv # type: ignore
5-
from typing import Any, cast
6-
import base64, json, time
7-
from starlette.requests import Request # type: ignore
5+
import os
6+
import time
7+
from typing import Any
88

9-
from mcp.server.fastmcp.server import Context
10-
from mcp.server.auth.proxy.server import build_proxy_server # noqa: E402
9+
from dotenv import load_dotenv # type: ignore
1110
from mcp.server.auth.providers.transparent_proxy import ProxySettings # type: ignore
11+
from mcp.server.auth.proxy.server import build_proxy_server # noqa: E402
12+
from mcp.server.fastmcp.server import Context
13+
from starlette.requests import Request # type: ignore
1214

1315
# Load environment variables from .env if present
1416
load_dotenv()
@@ -178,7 +180,8 @@ def _b64decode(segment: str) -> bytes:
178180
}
179181

180182
if jwt_claims:
181-
# Prefer the `userid` claim used in FastMCP examples; fall back to `sub` if absent.
183+
# Prefer the `userid` claim used in FastMCP examples; fall back to `sub` if
184+
# absent.
182185
uid = jwt_claims.get("userid") or jwt_claims.get("sub")
183186
if uid is not None:
184187
response["userid"] = uid # camelCase variant used in FastMCP reference

src/mcp/client/websocket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pyright: reportMissingImports=false, reportUnknownVariableType=false, reportUnknownArgumentType=false, reportUnknownMemberType=false
12
import json
23
import logging
34
from collections.abc import AsyncGenerator

src/mcp/server/auth/providers/transparent_proxy.py

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1+
# pyright: reportUnknownMemberType=false, reportUnknownVariableType=false, reportAttributeAccessIssue=false, reportUnknownArgumentType=false, reportCallIssue=false, reportUnnecessaryIsInstance=false
12
from __future__ import annotations
23

4+
import logging
5+
import os
6+
import time
7+
import uuid
8+
from collections.abc import Mapping
9+
from typing import Any, cast
10+
from urllib.parse import urlencode
11+
12+
import httpx # type: ignore
13+
from pydantic import AnyHttpUrl, AnyUrl, Field
14+
from pydantic_settings import BaseSettings, SettingsConfigDict
15+
from starlette.responses import Response
16+
from starlette.routing import Route
17+
18+
from mcp.server.auth.handlers.token import TokenHandler
19+
from mcp.server.auth.middleware.client_auth import ClientAuthenticator
20+
from mcp.server.auth.provider import (
21+
AccessToken,
22+
AuthorizationCode,
23+
AuthorizationParams,
24+
OAuthAuthorizationServerProvider,
25+
)
26+
from mcp.server.auth.proxy.routes import create_proxy_routes
27+
from mcp.server.auth.routes import create_auth_routes
28+
from mcp.server.auth.settings import ClientRegistrationOptions
29+
from mcp.server.fastmcp.utilities.logging import redact_sensitive_data
30+
from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
31+
332
"""Transparent OAuth proxy provider for FastMCP (Anthropic SDK).
433
534
This provider mimics the behaviour of fastapi_mcp's `setup_proxies=True` and the
@@ -23,42 +52,6 @@
2352
A simple helper ``TransparentOAuthProxyProvider.from_env()`` reads these vars.
2453
"""
2554

26-
import os
27-
import time
28-
import uuid
29-
import json
30-
import urllib.parse
31-
from typing import Any, cast
32-
from collections.abc import Mapping
33-
from urllib.parse import urlencode
34-
35-
import httpx # type: ignore
36-
import logging
37-
from pydantic import AnyHttpUrl, Field, AnyUrl
38-
from pydantic_settings import BaseSettings, SettingsConfigDict
39-
from starlette.routing import Route
40-
from starlette.requests import Request
41-
from starlette.responses import Response
42-
43-
from mcp.server.auth.provider import (
44-
AccessToken,
45-
AuthorizationCode,
46-
AuthorizationParams,
47-
OAuthAuthorizationServerProvider,
48-
)
49-
from mcp.server.auth.settings import ClientRegistrationOptions
50-
from mcp.shared.auth import OAuthClientInformationFull, OAuthToken
51-
from mcp.server.auth.routes import create_auth_routes
52-
from mcp.server.auth.middleware.client_auth import ClientAuthenticator
53-
from mcp.server.auth.handlers.token import TokenHandler
54-
from starlette.requests import Request
55-
from starlette.responses import Response
56-
57-
# New: route factory for proxy endpoints
58-
from mcp.server.auth.proxy.routes import create_proxy_routes # noqa: E402
59-
60-
from mcp.server.fastmcp.utilities.logging import configure_logging, redact_sensitive_data
61-
6255
__all__ = ["TransparentOAuthProxyProvider"]
6356

6457
logger = logging.getLogger("transparent_oauth_proxy")
@@ -74,15 +67,13 @@ class ProxyTokenHandler(TokenHandler):
7467
back to the caller.
7568
"""
7669

77-
def __init__(self, provider: "TransparentOAuthProxyProvider"):
70+
def __init__(self, provider: TransparentOAuthProxyProvider):
7871
# We provide a dummy ClientAuthenticator that will accept any client –
7972
# we are not going to invoke the base-class logic anyway.
8073
super().__init__(provider=provider, client_authenticator=ClientAuthenticator(provider))
8174
self.provider = provider # keep for easy access
8275

83-
async def handle(self, request): # type: ignore[override]
84-
from starlette.responses import Response
85-
76+
async def handle(self, request) -> Response: # type: ignore[override]
8677
correlation_id = str(uuid.uuid4())[:8]
8778
start_time = time.time()
8879

@@ -157,7 +148,7 @@ class ProxySettings(BaseSettings):
157148
default_scope: str = Field("openid", alias="PROXY_DEFAULT_SCOPE")
158149

159150
@classmethod
160-
def load(cls) -> "ProxySettings":
151+
def load(cls) -> ProxySettings:
161152
"""Instantiate settings from environment variables (for backwards compatibility)."""
162153
return cls()
163154

@@ -342,7 +333,7 @@ async def revoke_token(self, token: object) -> None: # noqa: D401
342333
# ------------------------------------------------------------------
343334

344335
@classmethod
345-
def from_env(cls) -> "TransparentOAuthProxyProvider":
336+
def from_env(cls) -> TransparentOAuthProxyProvider:
346337
"""Construct provider using :class:`ProxySettings` populated from the environment."""
347338
return cls(settings=ProxySettings.load())
348339

src/mcp/server/auth/proxy/routes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
from __future__ import annotations
55

6-
import os
76
import logging
7+
import os
88
import urllib.parse
99
from typing import Any
1010

1111
import httpx # type: ignore
12-
from starlette.responses import JSONResponse, RedirectResponse, Response
1312
from starlette.requests import Request
13+
from starlette.responses import JSONResponse, RedirectResponse, Response
1414
from starlette.routing import Route
1515

1616
from mcp.server.fastmcp.utilities.logging import configure_logging

src/mcp/server/auth/proxy/server.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
from typing import TYPE_CHECKING, Literal
77

8-
from mcp.server.fastmcp import FastMCP
9-
from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions
108
from pydantic import AnyHttpUrl
9+
10+
from mcp.server.auth.settings import AuthSettings, ClientRegistrationOptions
11+
from mcp.server.fastmcp import FastMCP
1112
from mcp.server.fastmcp.utilities.logging import configure_logging
1213

1314
from ..providers.transparent_proxy import TransparentOAuthProxyProvider
@@ -29,7 +30,7 @@ def build_proxy_server( # noqa: D401,E501
2930
port: int = 8000,
3031
issuer_url: str | None = None,
3132
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"] = "DEBUG",
32-
settings: "ProxySettings | None" = None,
33+
settings: ProxySettings | None = None,
3334
) -> FastMCP:
3435
"""Return a fully-configured FastMCP instance running the proxy.
3536
@@ -39,7 +40,7 @@ def build_proxy_server( # noqa: D401,E501
3940
"""
4041

4142
# Runtime import to avoid circular dependency at module import time.
42-
from ..providers.transparent_proxy import _Settings as ProxySettings # noqa: WPS433
43+
from ..providers.transparent_proxy import _Settings as ProxySettings
4344

4445
if settings is None:
4546
settings = ProxySettings.load()

src/mcp/server/fastmcp/server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
AbstractAsyncContextManager,
1010
asynccontextmanager,
1111
)
12-
from typing import Any, Generic, Literal, cast
12+
from typing import Any, Generic, Literal
1313

1414
import anyio
1515
import pydantic_core
@@ -1132,6 +1132,7 @@ async def error(self, message: str, **extra: Any) -> None:
11321132
# ---------------------------------------------------------------------------
11331133

11341134

1135+
# pyright: reportUnknownArgumentType=false, reportUnknownParameterType=false
11351136
def _build_provider_auth_routes(provider: OAuthAuthorizationServerProvider[Any, Any, Any], auth_settings: AuthSettings):
11361137
"""Return the list of Starlette routes for the given provider.
11371138

src/mcp/server/fastmcp/utilities/logging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Logging utilities for FastMCP."""
22

33
import logging
4-
from typing import Literal
54
from collections.abc import Mapping
6-
from typing import Any
5+
from typing import Any, Literal
76

87

98
def get_logger(name: str) -> logging.Logger:

tests/test_proxy_builder.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
from __future__ import annotations
55

6-
import pytest # type: ignore
7-
import httpx # type: ignore
8-
import os
9-
106
from typing import cast
7+
8+
import httpx # type: ignore
9+
import pytest # type: ignore
1110
from pydantic import AnyHttpUrl
1211

13-
from mcp.server.auth.proxy.server import build_proxy_server
12+
from mcp.server.auth.providers.transparent_proxy import _Settings as ProxySettings
1413
from mcp.server.auth.proxy import routes as proxy_routes
15-
from mcp.server.auth.providers.transparent_proxy import _Settings as ProxySettings # noqa: WPS433
14+
from mcp.server.auth.proxy.server import build_proxy_server
1615

1716

1817
@pytest.mark.anyio

tests/test_proxy_oauth_endpoints.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import base64
99
import json
1010
import urllib.parse
11-
from typing import Any, AsyncGenerator
11+
from collections.abc import AsyncGenerator
12+
from typing import Any
1213

1314
import httpx # type: ignore
1415
import pytest # type: ignore
@@ -29,10 +30,10 @@ def proxy_server(monkeypatch):
2930
os.environ.setdefault("UPSTREAM_CLIENT_SECRET", "secret123")
3031

3132
# Deferred import so the env vars above are in effect.
32-
from examples.servers.proxy_oauth import server as proxy_server_module # noqa: WPS433 (runtime import for tests)
33+
from examples.servers.proxy_oauth import server as proxy_server_module
3334

3435
# Stub library-level fetch_upstream_metadata to avoid network I/O.
35-
from mcp.server.auth.proxy import routes as proxy_routes # noqa: WPS433
36+
from mcp.server.auth.proxy import routes as proxy_routes
3637

3738
async def _fake_metadata() -> dict[str, Any]: # noqa: D401
3839
return {
@@ -118,7 +119,7 @@ async def test_authorize_redirect(client, proxy_server):
118119
async def test_revoke_proxy(client, monkeypatch, proxy_server):
119120
original_post = httpx.AsyncClient.post
120121

121-
async def _mock_post(self, url, data=None, timeout=10, **kwargs): # noqa: D401,WPS110
122+
async def _mock_post(self, url, data=None, timeout=10, **kwargs): # noqa: D401
122123
if url.endswith("/revoke"):
123124
return httpx.Response(200, json={"revoked": True})
124125
# For the test client's own request to /revoke, delegate to original implementation
@@ -140,7 +141,7 @@ async def test_token_passthrough(client, monkeypatch, proxy_server):
140141

141142
original_post = httpx.AsyncClient.post
142143

143-
async def _mock_post(self, url, *args, **kwargs): # noqa: D401,WPS110
144+
async def _mock_post(self, url, *args, **kwargs): # noqa: D401
144145
if str(url).startswith(proxy_server.UPSTREAM_TOKEN):
145146
# Record exactly what was sent upstream
146147
captured["url"] = str(url)
@@ -198,8 +199,8 @@ async def test_user_info_tool(monkeypatch, proxy_server):
198199
)
199200
dummy_token = f"header.{payload}.signature"
200201

201-
from mcp.server.auth.provider import AccessToken # local import to avoid cycles
202202
from mcp.server.auth.middleware import auth_context
203+
from mcp.server.auth.provider import AccessToken # local import to avoid cycles
203204

204205
def _fake_get_access_token(): # noqa: D401
205206
return AccessToken(token=dummy_token, client_id="client123", scopes=["openid"], expires_at=None)

0 commit comments

Comments
 (0)