π The fast, Pythonic way to build MCP servers and clients.
Add a downstream smoke for Redis credential providers (#6044)
nate nowackd4af9a81b4a1
diff --git a/.github/workflows/run-downstream-smoke.yml b/.github/workflows/run-downstream-smoke.ymlindex 3c4f8c395..22997f4e2 100644--- a/.github/workflows/run-downstream-smoke.yml+++ b/.github/workflows/run-downstream-smoke.yml@@ -13,6 +13,9 @@ env:PYDANTIC_AI_VERSION: "2.47.0"LANGCHAIN_MCP_ADAPTERS_VERSION: "0.3.2"LANGCHAIN_VERSION: "1.4.2"+ BOTOCORE_VERSION: "1.43.118"+ CACHETOOLS_VERSION: "7.2.4"+ REDIS_ENTRAID_VERSION: "1.2.2"on:push:@@ -61,6 +64,9 @@ jobs:pydantic-ai: ${{ steps.versions.outputs.pydantic-ai }}langchain-mcp-adapters: ${{ steps.versions.outputs.langchain-mcp-adapters }}langchain: ${{ steps.versions.outputs.langchain }}+ botocore: ${{ steps.versions.outputs.botocore }}+ cachetools: ${{ steps.versions.outputs.cachetools }}+ redis-entraid: ${{ steps.versions.outputs.redis-entraid }}steps:- uses: actions/checkout@v7with:@@ -104,10 +110,16 @@ jobs:pydantic_ai=$(pick pydantic-ai-slim "$PYDANTIC_AI_VERSION")adapters=$(pick langchain-mcp-adapters "$LANGCHAIN_MCP_ADAPTERS_VERSION")langchain=$(pick langchain "$LANGCHAIN_VERSION")+ botocore=$(pick botocore "$BOTOCORE_VERSION")+ cachetools=$(pick cachetools "$CACHETOOLS_VERSION")+ redis_entraid=$(pick redis-entraid "$REDIS_ENTRAID_VERSION"){echo "pydantic-ai=$pydantic_ai"echo "langchain-mcp-adapters=$adapters"echo "langchain=$langchain"+ echo "botocore=$botocore"+ echo "cachetools=$cachetools"+ echo "redis-entraid=$redis_entraid"} | tee -a "$GITHUB_OUTPUT" > versions.txt{echo "### Downstream smoke plan"@@ -265,6 +277,18 @@ jobs:with: >---with "langchain-mcp-adapters==${{ needs.plan.outputs.langchain-mcp-adapters }}"--with "langchain==${{ needs.plan.outputs.langchain }}"+ # fastmcp-tasks on the Redis credential providers deployments use:+ # AWS's ElastiCache IAM provider and redis-entraid's Entra ID one.+ - consumer: redis credential providers+ script: smoke_redis_credentials.py+ redis: true+ with: >-+ --with "fastmcp @ file://$GITHUB_WORKSPACE/$(ls dist/fastmcp-4*.whl)"+ --with "fastmcp-slim @ file://$GITHUB_WORKSPACE/$(ls dist/fastmcp_slim-*.whl)"+ --with "fastmcp-tasks @ file://$GITHUB_WORKSPACE/$(ls dist/fastmcp_tasks-*.whl)"+ --with "botocore==${{ needs.plan.outputs.botocore }}"+ --with "cachetools==${{ needs.plan.outputs.cachetools }}"+ --with "redis-entraid==${{ needs.plan.outputs.redis-entraid }}"env:FASTMCP_EXPECTED_VERSION: ${{ needs.plan.outputs.fastmcp }}steps:@@ -281,6 +305,14 @@ jobs:name: fastmcp-wheelspath: dist+ - name: Install redis-server+ if: matrix.redis+ run: |+ if ! command -v redis-server >/dev/null; then+ sudo apt-get update+ sudo apt-get install -y redis-server+ fi+# Resolved outside the project and without its config, as a user's install is.- name: Resolve ${{ matrix.consumer }} environmentrun: uv run --isolated --no-project --no-config ${{ matrix.with }} python -c passdiff --git a/tests/downstream/README.md b/tests/downstream/README.mdindex 17d8c349b..a39a33740 100644--- a/tests/downstream/README.md+++ b/tests/downstream/README.md@@ -12,6 +12,7 @@ against servers started from the checkout.| pydantic-ai smoke | `smoke_pydantic_ai.py` | client-only `fastmcp-slim[client]`, as `pydantic-ai-slim[mcp]` installs it || langchain.mcp smoke | `smoke_langchain_mcp.py`, including a `ClientGroup` of a legacy HTTP server and a modern stdio server | `fastmcp`, as `langchain[mcp]` installs it || langchain-mcp-adapters smoke | `smoke_langchain_mcp_adapters.py` | none: the adapters pin `mcp<2`, so they reach FastMCP over the wire |+| redis credential providers smoke | `smoke_redis_credentials.py`: `TasksExtension` tasks through rotating credentials from AWS's ElastiCache IAM provider (botocore) and redis-entraid's streaming Entra ID provider, against a local `redis-server` | `fastmcp`, `fastmcp-slim`, and `fastmcp-tasks` wheels |Each smoke runs its checks over stdio, streamable HTTP, SSE, and a FastMCP proxyin front of a stdio server, all with bearer auth where the transport allows it.@@ -33,9 +34,10 @@ manual run with `latest`, tests each consumer's latest release.```bashuv sync-uv build --wheel --out-dir dist . fastmcp_slim+uv build --wheel --out-dir dist . fastmcp_slim fastmcp_tasksslim="fastmcp-slim @ file://$PWD/$(ls dist/fastmcp_slim-*.whl)"full="fastmcp @ file://$PWD/$(ls dist/fastmcp-4*.whl)"+tasks="fastmcp-tasks @ file://$PWD/$(ls dist/fastmcp_tasks-*.whl)"uv run --isolated --no-project --no-config \--with 'pydantic-ai-slim[mcp]' --with "${slim/fastmcp-slim/fastmcp-slim[client]}" \@@ -48,4 +50,10 @@ uv run --isolated --no-project --no-config \uv run --isolated --no-project --no-config \--with langchain-mcp-adapters --with langchain \python tests/downstream/smoke_langchain_mcp_adapters.py++# needs redis-server on PATH+uv run --isolated --no-project --no-config \+ --with "$full" --with "$slim" --with "$tasks" \+ --with botocore --with cachetools --with redis-entraid \+ python tests/downstream/smoke_redis_credentials.py```diff --git a/tests/downstream/smoke_redis_credentials.py b/tests/downstream/smoke_redis_credentials.pynew file mode 100644index 000000000..a328430af--- /dev/null+++ b/tests/downstream/smoke_redis_credentials.py@@ -0,0 +1,267 @@+"""Run fastmcp-tasks on the rotating Redis credentials real deployments use.++Two providers, each driven through `TasksExtension(credential_provider=...)`+with concurrent task calls while its credentials rotate underneath:++- AWS ElastiCache IAM auth, with the `CredentialProvider` from AWS's+ documentation: botocore presigns a short-lived SigV4 token on demand.+- Azure Managed Redis with Entra ID, with redis-entraid's streaming+ `EntraIdCredentialsProvider` refreshing its token in the background.++A local `redis-server` stands in for both services: each token a provider+mints becomes a password of the `tasks` ACL user, as the service would accept+it. No cloud account is involved. See the README for running it locally.+"""++import asyncio+import os+import shutil+import socket+import subprocess+import tempfile+import time+from datetime import timedelta+from importlib.metadata import version+from urllib.parse import ParseResult, urlencode, urlunparse++import botocore.session+import redis+from _harness import Report+from botocore.model import ServiceId+from botocore.signers import RequestSigner+from cachetools import TTLCache, cached+from redis.asyncio import Redis+from redis.auth.idp import IdentityProviderInterface+from redis.auth.token import SimpleToken, TokenInterface+from redis.auth.token_manager import RetryPolicy, TokenManagerConfig+from redis_entraid.cred_provider import EntraIdCredentialsProvider++import fastmcp+from fastmcp import Client, FastMCP+from fastmcp_tasks import TasksExtension++USER = "tasks"+CALLERS = 8+ROUND_SECONDS = 2+++class ElastiCacheIAMProvider(redis.CredentialProvider):+ """AWS's documented provider for ElastiCache IAM auth, with a 1s cache in+ place of its 900s one so tokens rotate during the run, and the botocore+ session kept alive."""++ def __init__(self, user: str, cluster_name: str, region: str = "us-east-1"):+ self.user = user+ self.cluster_name = cluster_name+ self.region = region+ # The signer holds only a weak reference to the session's event+ # emitter, so the session has to outlive __init__.+ self.session = botocore.session.get_session()+ self.request_signer = RequestSigner(+ ServiceId("elasticache"),+ self.region,+ "elasticache",+ "v4",+ self.session.get_credentials(),+ self.session.get_component("event_emitter"),+ )++ @cached(cache=TTLCache(maxsize=128, ttl=1))+ def get_credentials(self) -> tuple[str, str]:+ query_params = {"Action": "connect", "User": self.user}+ url = urlunparse(+ ParseResult(+ scheme="https",+ netloc=self.cluster_name,+ path="/",+ query=urlencode(query_params),+ params="",+ fragment="",+ )+ )+ signed_url = self.request_signer.generate_presigned_url(+ {"method": "GET", "url": url, "body": {}, "headers": {}, "context": {}},+ operation_name="connect",+ expires_in=900,+ region_name=self.region,+ )+ return (self.user, signed_url.removeprefix("https://"))+++class AcceptedByLocalRedis(ElastiCacheIAMProvider):+ """Registers each presigned token, as ElastiCache's SigV4 check would accept it."""++ def __init__(self, admin: redis.Redis):+ super().__init__(USER, "smoke-cluster")+ self.admin = admin+ self.minted: set[str] = set()++ def get_credentials(self) -> tuple[str, str]:+ user, token = super().get_credentials()+ if token not in self.minted:+ accept(self.admin, token)+ self.minted.add(token)+ return user, token+++class LocalEntraId(IdentityProviderInterface):+ """Stands in for Entra ID: mints a 1s token and registers it with Redis."""++ def __init__(self, admin: redis.Redis):+ self.admin = admin+ self.issued = 0++ def request_token(self, force_refresh: bool = False) -> TokenInterface:+ self.issued += 1+ value = f"entra-token-{self.issued}-{time.monotonic_ns()}"+ accept(self.admin, value)+ now = time.time() * 1000+ return SimpleToken(value, now + 1000, now, {"oid": USER})+++def accept(admin: redis.Redis, password: str) -> None:+ admin.execute_command(+ "ACL", "SETUSER", USER, "on", f">{password}", "~*", "&*", "+@all"+ )+++def entra_id_provider(identity: LocalEntraId) -> EntraIdCredentialsProvider:+ return EntraIdCredentialsProvider(+ identity,+ TokenManagerConfig(+ expiration_refresh_ratio=0.5,+ lower_refresh_bound_millis=0,+ token_request_execution_timeout_in_ms=1000,+ retry_policy=RetryPolicy(max_attempts=3, delay_in_ms=10),+ ),+ )+++async def run_tasks_through_rotation(+ provider: redis.CredentialProvider, port: int, admin: Redis, rounds: int = 3+) -> None:+ """Rounds of concurrent task calls. Between rounds the server drops the+ connections after the provider's credentials have moved on, so the next+ round has to reconnect with current ones; a streaming provider also+ refreshes in the background while calls are in flight."""+ server = FastMCP("rotating-credentials")+ server.add_extension(+ TasksExtension(+ url=f"redis://127.0.0.1:{port}/0",+ name=f"smoke-{time.monotonic_ns()}",+ credential_provider=provider,+ reconnection_delay=timedelta(milliseconds=10),+ )+ )++ @server.tool(task=True)+ async def double(value: int) -> int:+ return value * 2++ async with Client(server) as client:++ async def caller(n: int) -> None:+ deadline = time.monotonic() + ROUND_SECONDS+ i = 0+ while time.monotonic() < deadline:+ result = await client.call_tool("double", {"value": n * 1000 + i})+ assert result.data == (n * 1000 + i) * 2, result+ i += 1++ for _ in range(rounds):+ await asyncio.wait_for(+ asyncio.gather(*(caller(n) for n in range(CALLERS))), 30+ )+ await asyncio.sleep(1.1)+ await admin.execute_command("CLIENT", "KILL", "USER", USER)+++def start_redis(directory: str) -> tuple[subprocess.Popen[bytes], int]:+ executable = shutil.which("redis-server")+ assert executable, "redis-server must be on PATH"+ with socket.socket() as sock:+ sock.bind(("127.0.0.1", 0))+ port = sock.getsockname()[1]+ process = subprocess.Popen(+ [executable, "--bind", "127.0.0.1", "--port", str(port)]+ + ["--save", "", "--appendonly", "no", "--dir", directory],+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ )+ return process, port+++async def main() -> None:+ # Fake keys for botocore's signer; nothing is sent to AWS.+ os.environ.setdefault("AWS_ACCESS_KEY_ID", "AKIDSMOKETEST")+ os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "smoke-test-secret")+ report = Report(+ "redis credential providers",+ {+ "fastmcp": fastmcp.__version__,+ "fastmcp-tasks": version("fastmcp-tasks"),+ "pydocket": version("pydocket"),+ "redis": version("redis"),+ "botocore": version("botocore"),+ "redis-entraid": version("redis-entraid"),+ },+ )++ async def installed_build_is_under_test() -> None:+ expected = os.environ.get("FASTMCP_EXPECTED_VERSION")+ assert expected in (None, fastmcp.__version__), (+ f"expected {expected}, got {fastmcp.__version__}"+ )++ with tempfile.TemporaryDirectory(prefix="fastmcp-smoke-redis-") as directory:+ process, port = start_redis(directory)+ try:+ sync_admin = redis.Redis(host="127.0.0.1", port=port)+ for _ in range(200):+ try:+ sync_admin.ping()+ break+ except redis.ConnectionError:+ time.sleep(0.02)+ async with Redis(host="127.0.0.1", port=port) as admin:+ with report.transport("ElastiCache IAM (botocore SigV4)"):+ await report.check(+ "installed build is under test", installed_build_is_under_test+ )+ await report.check(+ "tasks run while tokens rotate",+ lambda: run_tasks_through_rotation(+ AcceptedByLocalRedis(sync_admin), port, admin+ ),+ )++ with report.transport("Entra ID (redis-entraid, streaming)"):++ async def tasks_run_while_tokens_refresh() -> None:+ identity = LocalEntraId(sync_admin)+ await run_tasks_through_rotation(+ entra_id_provider(identity), port, admin+ )+ assert identity.issued > 3, (+ f"only {identity.issued} tokens issued; the provider "+ "never refreshed during the run"+ )++ await report.check(+ "tasks run while tokens refresh",+ tasks_run_while_tokens_refresh,+ known_gap=(+ "TasksExtension rejects streaming providers until a "+ "pydocket release includes chrisguidry/docket#579"+ ),+ )+ sync_admin.close()+ finally:+ process.terminate()+ process.wait(timeout=5)++ report.finish()+++if __name__ == "__main__":+ asyncio.run(main())