Create http utility

This commit is contained in:
Jeremiah Lowin 2025-05-31 19:33:50 -04:00
commit ad8182eed0
5 changed files with 22 additions and 11 deletions

View file

@ -25,9 +25,9 @@ from pydantic import AnyHttpUrl, ValidationError
from 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_port
from fastmcp.utilities.logging import get_logger
__all__ = ["OAuth"]

View file

@ -8,7 +8,6 @@ and display styled responses to users.
from __future__ import annotations
import asyncio
import socket
from dataclasses import dataclass
from starlette.applications import Starlette
@ -17,6 +16,7 @@ from starlette.responses import HTMLResponse
from starlette.routing import Route
from uvicorn import Config, Server
from fastmcp.utilities.http import find_available_port
from fastmcp.utilities.logging import get_logger
logger = 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]
@dataclass
class CallbackResponse:
code: str | None = None

View file

@ -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)

View file

@ -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]

View file

@ -11,7 +11,7 @@ import fastmcp.client.auth # Import module, not the function directly
from fastmcp.client import Client
from fastmcp.client.transports import StreamableHttpTransport
from fastmcp.server.auth.auth import ClientRegistrationOptions
from fastmcp.server.auth.in_memory_provider import InMemoryOAuthProvider
from fastmcp.server.auth.providers.in_memory import InMemory
from fastmcp.server.server import FastMCP
from 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),
),