Repository navigation
v0.3.1
Cancelling a tool call now stops its query on the database, not just the answer.
pip install -U easy-mcp-kitThe problem
When a client cancelled a call, or the server's tool timeout fired, the server stopped waiting and dropped the response. The query kept running on the database until the connector's own statement deadline, because sync tools ran in a worker thread that nothing could stop.
Cancellation reaches sync tools
- A cancel token for every call. Inside a tool,
easy_mcp.current_cancel_token()returns it. It is triggered bynotifications/cancelled, a closed stateless connection, a deleted session, stdio shutdown, and the tool timeout. - Callbacks. Register with
token.on_cancel(...), or polltoken.cancelled/token.reason. Callbacks run on a thread of their own, off the event loop. A failing callback is logged and audited ascancel_callback_failed. - At shutdown, the transports give tool threads and cancel callbacks a bounded time to finish (stdio's
shutdown_timeout, 5 s over HTTP/SSE), so aKILL QUERYgets out before the process exits. If you drivedispatchyourself, callawait server.wait_for_tool_threads(5)before exiting. - Tools that ignore the token behave as before, apart from the thread changes below.
The connectors stop the statement
| Connector | On cancel or timeout |
|---|---|
| MySQL / MariaDB | KILL QUERY at once, not only at the deadline |
| Postgres | a cancel request (cancel_safe on psycopg 3.2+, cancel on 3.1) |
| SQLite | interrupt(); the progress handler also catches a cancel just before a statement starts |
| MongoDB | each call runs in its own session, ended with killSessions (the read role is enough). It reaches the primary only, and calls the deployment refuses a session run without one, as in 0.3.0 |
| GitHub | no request is sent once a call is cancelled. A write already sent may still complete, and the audit log records it as tool_finished_after_cancel |
Live-tested against MySQL 8.4, Postgres 16 and MongoDB 7 with least-privilege accounts (SELECT-only, a plain login role, read), on Windows and on Linux with Python 3.12 and 3.14. After a cancel, the query is gone from PROCESSLIST / pg_stat_activity / $currentOp within 1.5 s. That includes a stdio client that quits mid-query. On 0.3.0 the same tests show the query still running.
Changes to know about when upgrading
- Sync tools run on a daemon thread each, capped by
max_sync_workers(default 32, shared by all clients). A call beyond the cap is refused at once withServerBusyError(-32008). 0.3.0 queued it, so retry refused calls after a short delay. - Threads a sync tool starts are daemons too, because Python threads inherit the flag. They are stopped at exit, where 0.3.0 waited for them. Pass
daemon=Falseor use aThreadPoolExecutorfor work that must outlive the call. - A
TimeoutErrorraised by a tool itself, such as a socket read, is reported as a tool failure instead of the server's-32005. server.run()shuts down with SSE clients connected. It closes open legacy SSE streams first; before, Ctrl-C hung until a second one forced the exit. If you mountbuild_app()in another app, callwait_for_tool_threadsfrom its shutdown.- MongoDB
database_factorytest doubles should acceptsession=. A double withoutstart_sessionstill works, without the kill. - Startup warning. The connectors warn when
default_timeoutis not longer than their statement timeout.
Full details are in CHANGELOG.md.
What's Changed
- Record the demo video and code snapshots for 0.3.0 by @Mark007-R in #41
- Stop the database query when a tool call is cancelled by @Mark007-R in #42
- Fix cancellation issues found in pre-release review by @Mark007-R in #43
- Release 0.3.1 by @Mark007-R in #44
Full Changelog: v0.3.0...v0.3.1