Commit 2f502bc
committed
[v1.x] Expire idle Streamable HTTP sessions by default and cap concurrent sessions
v1.x backport of #3395.
Stateful Streamable HTTP sessions get a lifecycle the server owns: a
session is forgotten and its transport terminated as soon as it ends
(client DELETE, a refused or failed opening request, idle expiry,
crash), sessions with nothing in flight expire after
session_idle_timeout (default 1800 seconds; an open GET stream or a
request being answered holds the session), and a manager admits at most
max_sessions (default 10 000) and answers 503 beyond that. A stateless
transport is terminated even when its request is cancelled. Both
settings are FastMCP(...) keyword arguments and Settings fields.
Differences from #3395:
- the per-session task drives Server.run() rather than serve_loop;
- idle expiry ends a session by discarding it (which closes the
transport's streams, as a client DELETE does) rather than by
cancelling Server.run(): on 1.x Server.run() enters the server's
lifespan per session, and its teardown should complete rather than
run under cancellation;
- the settings are FastMCP constructor arguments / Settings fields
rather than run()/app-factory keywords, and
FastMCP.streamable_http_app() takes no options;
- the manager's JSON-RPC error bodies keep this branch's
id "server-error" envelope;
- the transport also closes its per-request SSE streams when a response
ends (the 1.x share of an earlier main change), which the in-process
tests need;
- tests drive the manager through raw ASGI callables;
- documentation lives in docs/server.md.1 parent 5ebdfed commit 2f502bc
7 files changed
Lines changed: 883 additions & 208 deletions
File tree
- docs
- src/mcp/server
- fastmcp
- tests
- server
- fastmcp
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1254 | 1254 | | |
1255 | 1255 | | |
1256 | 1256 | | |
| 1257 | + | |
1257 | 1258 | | |
1258 | 1259 | | |
1259 | 1260 | | |
| |||
1426 | 1427 | | |
1427 | 1428 | | |
1428 | 1429 | | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
1429 | 1433 | | |
1430 | 1434 | | |
1431 | 1435 | | |
| |||
1536 | 1540 | | |
1537 | 1541 | | |
1538 | 1542 | | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
1539 | 1576 | | |
1540 | 1577 | | |
1541 | 1578 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
66 | 70 | | |
67 | 71 | | |
68 | 72 | | |
| |||
108 | 112 | | |
109 | 113 | | |
110 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
111 | 119 | | |
112 | 120 | | |
113 | 121 | | |
| |||
169 | 177 | | |
170 | 178 | | |
171 | 179 | | |
| 180 | + | |
| 181 | + | |
172 | 182 | | |
173 | 183 | | |
174 | 184 | | |
| |||
197 | 207 | | |
198 | 208 | | |
199 | 209 | | |
| 210 | + | |
| 211 | + | |
200 | 212 | | |
201 | 213 | | |
202 | 214 | | |
| |||
966 | 978 | | |
967 | 979 | | |
968 | 980 | | |
| 981 | + | |
| 982 | + | |
969 | 983 | | |
970 | 984 | | |
971 | 985 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
150 | 151 | | |
151 | 152 | | |
152 | 153 | | |
| 154 | + | |
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| |||
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
170 | 179 | | |
171 | 180 | | |
172 | | - | |
| 181 | + | |
| 182 | + | |
173 | 183 | | |
174 | 184 | | |
175 | 185 | | |
| 186 | + | |
| 187 | + | |
176 | 188 | | |
177 | 189 | | |
178 | 190 | | |
| |||
188 | 200 | | |
189 | 201 | | |
190 | 202 | | |
191 | | - | |
| 203 | + | |
| 204 | + | |
192 | 205 | | |
| 206 | + | |
| 207 | + | |
193 | 208 | | |
194 | 209 | | |
195 | 210 | | |
| |||
402 | 417 | | |
403 | 418 | | |
404 | 419 | | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
405 | 446 | | |
406 | 447 | | |
407 | 448 | | |
| |||
643 | 684 | | |
644 | 685 | | |
645 | 686 | | |
646 | | - | |
647 | 687 | | |
| 688 | + | |
| 689 | + | |
648 | 690 | | |
649 | 691 | | |
650 | 692 | | |
| |||
747 | 789 | | |
748 | 790 | | |
749 | 791 | | |
| 792 | + | |
| 793 | + | |
750 | 794 | | |
751 | 795 | | |
752 | | - | |
753 | 796 | | |
754 | 797 | | |
755 | 798 | | |
| |||
992 | 1035 | | |
993 | 1036 | | |
994 | 1037 | | |
| 1038 | + | |
| 1039 | + | |
995 | 1040 | | |
996 | 1041 | | |
997 | 1042 | | |
| |||
0 commit comments