Title: feat(server): expose lifespan / on_startup / on_shutdown hooks on serve() · Issue #709 · adcontextprotocol/adcp-client-python · GitHub
Open Graph Title: feat(server): expose lifespan / on_startup / on_shutdown hooks on serve() · Issue #709 · adcontextprotocol/adcp-client-python
X Title: feat(server): expose lifespan / on_startup / on_shutdown hooks on serve() · Issue #709 · adcontextprotocol/adcp-client-python
Description: A python library to interact with adcp servers. Contribute to adcontextprotocol/adcp-client-python development by creating an account on GitHub.
Open Graph Description: A python library to interact with adcp servers. Contribute to adcontextprotocol/adcp-client-python development by creating an account on GitHub.
X Description: A python library to interact with adcp servers. Contribute to adcontextprotocol/adcp-client-python development by creating an account on GitHub.
Opengraph URL: https://github.com/adcontextprotocol/adcp-client-python
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feat(server): expose lifespan / on_startup / on_shutdown hooks on serve()","articleBody":"## Problem\n\nAdopters who need to run background work tied to the server lifecycle — schedulers, connection pools, cache warmers, message-queue consumers, telemetry exporters — have no public hook into `serve()`'s lifespan composition.\n\n`serve.py:1703` builds a private `_composed_lifespan` that chains the MCP inner app's lifespan with the A2A inner app's lifespan, mounts it on the parent Starlette, and offers no extension point for the caller.\n\n## What adopters do today\n\nSalesagent wires a 61-LOC `SchedulerLifespanMiddleware` (`core/middleware/scheduler_lifespan.py`) that intercepts ASGI `lifespan.startup` / `lifespan.shutdown` scope events, fires user-supplied coroutines, and forwards the events through. Every adopter who needs lifecycle-tied background work writes the same shape of middleware.\n\nSketch:\n\n```python\nclass SchedulerLifespanMiddleware:\n def __init__(self, app, startups, shutdowns):\n self.app = app\n self.startups = startups\n self.shutdowns = shutdowns\n\n async def __call__(self, scope, receive, send):\n if scope[\"type\"] == \"lifespan\":\n async def wrapped_receive():\n msg = await receive()\n if msg[\"type\"] == \"lifespan.startup\":\n for hook in self.startups: await hook()\n elif msg[\"type\"] == \"lifespan.shutdown\":\n for hook in self.shutdowns: await hook()\n return msg\n await self.app(scope, wrapped_receive, send)\n return\n await self.app(scope, receive, send)\n```\n\nThis works but:\n\n1. It re-implements what Starlette's `lifespan=` already provides.\n2. It's a middleware, so it's positionally fragile — adopters get the ordering wrong (e.g. wrapping it outside `SigningVerifyMiddleware` would put it in the wrong scope).\n3. It can't `await` background tasks correctly without explicit error propagation, so adopters write subtly different (and subtly broken) variants.\n\n## Proposed\n\nPick one of:\n\n### (a) Pass-through to Starlette's lifespan signature\n\n```python\nserve(\n handler,\n on_startup=[start_scheduler, warm_cache],\n on_shutdown=[stop_scheduler],\n)\n```\n\n`serve` appends these to the lifespan it already composes. Simplest adopter surface, mirrors what Starlette/FastAPI users already know.\n\n### (b) Single composable lifespan kwarg\n\n```python\n@asynccontextmanager\nasync def lifespan(app):\n await start_scheduler()\n try:\n yield\n finally:\n await stop_scheduler()\n\nserve(handler, lifespan=lifespan)\n```\n\n`serve` chains this *inside* its existing `_composed_lifespan`. Lets adopters share state via `yield`-bound resources.\n\n(a) and (b) are not mutually exclusive — Starlette supports both. The minimal-viable surface is (a).\n\n## Why this isn't just \\\"file an ASGI middleware example\\\"\n\nThe whole point of `serve()` is that it owns the lifespan composition (PR #680 made this explicit when callable `public_url` started breaking it). Forcing adopters to break that abstraction with middleware re-introduces exactly the ordering bugs PR #680 was meant to eliminate.\n\n## Acceptance\n\n- `serve(on_startup=..., on_shutdown=...)` accepted as kwargs and threaded into `_composed_lifespan`\n- (Optional follow-on) `serve(lifespan=...)` accepts an async context manager\n- Existing transport=\\\"both\\\" + callable `public_url` (now working in 5.3.0 per #680) continues to work\n- An example in `examples/` demonstrating a scheduler tied to lifespan, replacing the middleware pattern\n- Salesagent can delete `core/middleware/scheduler_lifespan.py` and pass `on_startup=[_start_schedulers], on_shutdown=[_stop_schedulers]` instead\n\n## Real-world demand\n\nFound while auditing the 5.3.0 bump in salesagent for SDK workarounds we could now rip out. This was the one workaround that couldn't be removed because the SDK exposes no extension point.","author":{"url":"https://github.com/bokelley","@type":"Person","name":"bokelley"},"datePublished":"2026-05-12T22:56:18Z","url":"https://github.com/adcontextprotocol/adcp-client-python/issues/709"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:bbb943f5-8f83-14b8-b6f7-35d6b7f624e1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 92AE:2C3566:D844A9:11F0CDC:6B00F8E6 |
| html-safe-nonce | 0b801c2efd9a7f77e6e861a838c8d66425da2cad0f58be19f6a6d64f818d1528 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MkFFOjJDMzU2NjpEODQ0QTk6MTFGMENEQzo2QjAwRjhFNiIsInZpc2l0b3JfaWQiOiIyOTE3NDMyNzQ3NTAyOTI2MDU0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d6ddee99bb2df225e0bcd6d8262c33bd388bc3cbb438cb322360dad43862851f |
| hovercard-subject-tag | repository:1090305973 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/adcontextprotocol/adcp-client-python/issues/709 |
| twitter:image | https://opengraph.githubassets.com/ad855be62f09a05c0db141a47c0256e3d9cc590ab5a26754e07365c6cd487083/adcontextprotocol/adcp-client-python |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ad855be62f09a05c0db141a47c0256e3d9cc590ab5a26754e07365c6cd487083/adcontextprotocol/adcp-client-python |
| og:image:alt | A python library to interact with adcp servers. Contribute to adcontextprotocol/adcp-client-python development by creating an account on GitHub. |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| turbo-cache-control | no-cache |
| go-import | github.com/adcontextprotocol/adcp-client-python git https://github.com/adcontextprotocol/adcp-client-python.git |
| octolytics-dimension-user_id | 221690821 |
| octolytics-dimension-user_login | adcontextprotocol |
| octolytics-dimension-repository_id | 1090305973 |
| octolytics-dimension-repository_nwo | adcontextprotocol/adcp-client-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1090305973 |
| octolytics-dimension-repository_network_root_nwo | adcontextprotocol/adcp-client-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width