π The fast, Pythonic way to build MCP servers and clients.
Create http utility
Jeremiah Lowinad8182eed036
diff --git a/src/fastmcp/client/auth.py b/src/fastmcp/client/auth.pyindex 6165fdfd8..43df9d442 100644--- a/src/fastmcp/client/auth.py+++ b/src/fastmcp/client/auth.py@@ -25,9 +25,9 @@ from pydantic import AnyHttpUrl, ValidationErrorfrom fastmcp.client.oauth_callback import (create_oauth_callback_server,- find_available_port,)from fastmcp.settings import settings as fastmcp_global_settings+from fastmcp.utilities.http import find_available_portfrom fastmcp.utilities.logging import get_logger__all__ = ["OAuth"]diff --git a/src/fastmcp/client/oauth_callback.py b/src/fastmcp/client/oauth_callback.pyindex f9cecd16b..891e4cdb0 100644--- a/src/fastmcp/client/oauth_callback.py+++ b/src/fastmcp/client/oauth_callback.py@@ -8,7 +8,6 @@ and display styled responses to users.from __future__ import annotationsimport asyncio-import socketfrom dataclasses import dataclassfrom starlette.applications import Starlette@@ -17,6 +16,7 @@ from starlette.responses import HTMLResponsefrom starlette.routing import Routefrom uvicorn import Config, Server+from fastmcp.utilities.http import find_available_portfrom fastmcp.utilities.logging import get_loggerlogger = get_logger(__name__)@@ -179,13 +179,6 @@ def create_callback_html("""-def find_available_port() -> int:- """Find an available port by letting the OS assign one."""- with socket.socket() as s:- s.bind(("127.0.0.1", 0))- return s.getsockname()[1]--@dataclassclass CallbackResponse:code: str | None = Nonediff --git a/src/fastmcp/server/auth/auth.py b/src/fastmcp/server/auth/auth.pyindex b92160304..b5f07c523 100644--- a/src/fastmcp/server/auth/auth.py+++ b/src/fastmcp/server/auth/auth.py@@ -23,6 +23,16 @@ class OAuthProvider(revocation_options: RevocationOptions | None = None,required_scopes: list[str] | None = None,):+ """+ Initialize the OAuth provider.++ Args:+ issuer_url: The URL of the OAuth issuer.+ service_documentation_url: The URL of the service documentation.+ client_registration_options: The client registration options.+ revocation_options: The revocation options.+ required_scopes: Scopes that are required for all requests.+ """super().__init__()if isinstance(issuer_url, str):issuer_url = AnyHttpUrl(issuer_url)diff --git a/src/fastmcp/utilities/http.py b/src/fastmcp/utilities/http.pynew file mode 100644index 000000000..22c165735--- /dev/null+++ b/src/fastmcp/utilities/http.py@@ -0,0 +1,8 @@+import socket+++def find_available_port() -> int:+ """Find an available port by letting the OS assign one."""+ with socket.socket() as s:+ s.bind(("127.0.0.1", 0))+ return s.getsockname()[1]diff --git a/tests/client/test_oauth.py b/tests/auth/test_oauth_client.pysimilarity index 98%rename from tests/client/test_oauth.pyrename to tests/auth/test_oauth_client.pyindex 1c2488ec8..e743e25c3 100644--- a/tests/client/test_oauth.py+++ b/tests/auth/test_oauth_client.py@@ -11,7 +11,7 @@ import fastmcp.client.auth # Import module, not the function directlyfrom fastmcp.client import Clientfrom fastmcp.client.transports import StreamableHttpTransportfrom fastmcp.server.auth.auth import ClientRegistrationOptions-from fastmcp.server.auth.in_memory_provider import InMemoryOAuthProvider+from fastmcp.server.auth.providers.in_memory import InMemoryfrom fastmcp.server.server import FastMCPfrom fastmcp.utilities.tests import run_server_in_process@@ -20,7 +20,7 @@ def fastmcp_server(issuer_url: str):"""Create a FastMCP server with OAuth authentication."""server = FastMCP("TestServer",- auth=InMemoryOAuthProvider(+ auth=InMemory(issuer_url=issuer_url,client_registration_options=ClientRegistrationOptions(enabled=True),),