Skip to content

Commit 1c0c458

Browse files
committed
Fix OAuth example test collection
1 parent b82d854 commit 1c0c458

3 files changed

Lines changed: 68 additions & 8 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import annotations
2+
3+
import importlib
4+
import sys
5+
from collections.abc import Callable
6+
from pathlib import Path
7+
from types import ModuleType
8+
9+
import pytest
10+
11+
@pytest.fixture
12+
def load_example_module() -> Callable[[Path, str], ModuleType]:
13+
"""Import a workspace example without requiring it in the root test environment."""
14+
15+
def load(package_root: Path, module_name: str) -> ModuleType:
16+
original_path = sys.path.copy()
17+
try:
18+
sys.path.insert(0, str(package_root))
19+
return importlib.import_module(module_name)
20+
finally:
21+
sys.path[:] = original_path
22+
23+
return load

examples/clients/simple-auth-client/tests/test_oauth_resource_url.py renamed to tests/examples/simple_auth/test_oauth_resource_url.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1-
from collections.abc import AsyncIterator
1+
from __future__ import annotations
2+
3+
from collections.abc import AsyncIterator, Callable
24
from contextlib import asynccontextmanager
5+
from pathlib import Path
6+
from types import ModuleType
7+
from typing import Protocol, cast
38

49
import anyio
510
import pytest
611
from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream
12+
713
from mcp.client.auth import OAuthClientProvider
814
from mcp.shared.message import SessionMessage
915

10-
from mcp_simple_auth_client import main as client_module
11-
from mcp_simple_auth_client.main import SimpleAuthClient
16+
CLIENT_ROOT = Path(__file__).parents[3] / "examples" / "clients" / "simple-auth-client"
17+
18+
19+
class SimpleAuthClient(Protocol):
20+
def __init__(
21+
self,
22+
server_url: str,
23+
transport_type: str = "streamable-http",
24+
client_metadata_url: str | None = None,
25+
) -> None: ...
26+
27+
async def connect(self) -> None: ...
28+
29+
30+
class ClientModule(Protocol):
31+
SimpleAuthClient: type[SimpleAuthClient]
1232

1333

1434
@pytest.mark.anyio
15-
async def test_oauth_client_preserves_the_complete_connection_url(monkeypatch: pytest.MonkeyPatch) -> None:
35+
async def test_oauth_client_preserves_the_complete_connection_url(
36+
monkeypatch: pytest.MonkeyPatch,
37+
load_example_module: Callable[[Path, str], ModuleType],
38+
) -> None:
1639
"""The example passes the opaque MCP endpoint unchanged to its OAuth provider."""
40+
client_module = cast(ClientModule, load_example_module(CLIENT_ROOT, "mcp_simple_auth_client.main"))
1741
resource_url = "https://mcp.example.com/prefix/mcp?tenant=mcp"
1842
providers: list[OAuthClientProvider] = []
1943
sessions = 0
@@ -49,9 +73,9 @@ async def record_session(
4973

5074
monkeypatch.setattr(client_module, "CallbackServer", FakeCallbackServer)
5175
monkeypatch.setattr(client_module, "sse_client", fake_sse_client)
52-
monkeypatch.setattr(SimpleAuthClient, "_run_session", record_session)
76+
monkeypatch.setattr(client_module.SimpleAuthClient, "_run_session", record_session)
5377

54-
await SimpleAuthClient(resource_url, transport_type="sse").connect()
78+
await client_module.SimpleAuthClient(resource_url, transport_type="sse").connect()
5579

5680
assert sessions == 1
5781
assert [str(provider.context.server_url) for provider in providers] == [resource_url]

examples/servers/simple-auth/tests/test_resource_server_urls.py renamed to tests/examples/simple_auth/test_resource_server_urls.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
from typing import Literal
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable
4+
from pathlib import Path
5+
from types import ModuleType
6+
from typing import Literal, Protocol, cast
27

38
import pytest
9+
from click import Command
410
from click.testing import CliRunner
5-
from mcp_simple_auth import server
611

712
from mcp.server.mcpserver.server import MCPServer
813

14+
SERVER_ROOT = Path(__file__).parents[3] / "examples" / "servers" / "simple-auth"
15+
16+
17+
class ServerModule(Protocol):
18+
main: Command
19+
920

1021
@pytest.mark.parametrize(
1122
("transport", "endpoint"),
1223
[("streamable-http", "/mcp"), ("sse", "/sse")],
1324
)
1425
def test_selected_transport_uses_one_resource_path(
1526
monkeypatch: pytest.MonkeyPatch,
27+
load_example_module: Callable[[Path, str], ModuleType],
1628
transport: Literal["sse", "streamable-http"],
1729
endpoint: str,
1830
) -> None:
1931
"""The example advertises and serves the selected transport path."""
32+
server = cast(ServerModule, load_example_module(SERVER_ROOT, "mcp_simple_auth.server"))
2033
created: list[MCPServer] = []
2134
run_arguments: list[dict[str, object]] = []
2235

0 commit comments

Comments
 (0)