Skip to content

v0.3.1

Choose a tag to compare

@Mark007-R Mark007-R released this 30 Sep 00:27
· 138 commits to main since this release
e0de696

Cancelling a tool call now stops its query on the database, not just the answer.

pip install -U easy-mcp-kit

The 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 by notifications/cancelled, a closed stateless connection, a deleted session, stdio shutdown, and the tool timeout.
  • Callbacks. Register with token.on_cancel(...), or poll token.cancelled / token.reason. Callbacks run on a thread of their own, off the event loop. A failing callback is logged and audited as cancel_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 a KILL QUERY gets out before the process exits. If you drive dispatch yourself, call await 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 with ServerBusyError (-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=False or use a ThreadPoolExecutor for work that must outlive the call.
  • A TimeoutError raised 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 mount build_app() in another app, call wait_for_tool_threads from its shutdown.
  • MongoDB database_factory test doubles should accept session=. A double without start_session still works, without the kill.
  • Startup warning. The connectors warn when default_timeout is not longer than their statement timeout.

Full details are in CHANGELOG.md.

What's Changed

Full Changelog: v0.3.0...v0.3.1