Collection Manager

Manages Anki collection lifecycle and provides thread-safe access.

Collection Manager for Anki MCP Server.

Handles opening, closing, and managing Anki collection instances. Supports multiple collections and thread-safe access.

class mousetail.server.collection_manager.CollectionManager[source]

Bases: object

Manages Anki collection lifecycle and access.

__init__()[source]
list_available_collections()[source]

List all available Anki collections on the system.

Return type:

list[dict[str, str]]

Returns:

List of dicts with ‘profile’ and ‘path’ keys

check_collection_accessible(path=None)[source]

Check if a collection is accessible (not locked by Anki app).

Parameters:

path (Optional[str]) – Path to collection file. If None, uses default.

Return type:

str

Returns:

Resolved collection path

Raises:
  • ValueError – If path is invalid or collection doesn’t exist

  • AnkiError – If collection is locked or cannot be accessed

open_collection(path=None)[source]

Open a collection and return its path identifier.

Parameters:

path (Optional[str]) – Path to collection file. If None, uses default.

Return type:

str

Returns:

Collection path (identifier)

Raises:
  • ValueError – If path is invalid or collection doesn’t exist

  • AnkiError – If collection cannot be opened

close_collection(path)[source]

Close a collection.

Parameters:

path (str) – Path to collection file

close_all()[source]

Close all open collections.

get_collection(path=None)[source]

Get a collection with thread-safe access.

Parameters:

path (Optional[str]) – Path to collection. If None, uses default or first open collection.

Yields:

Collection instance

Example

>>> manager = CollectionManager()
>>> with manager.get_collection() as col:
...     note = col.new_note(notetype)
get_collection_info(path=None)[source]

Get information about a collection.

Parameters:

path (Optional[str]) – Path to collection. If None, uses default.

Return type:

dict

Returns:

Dict with collection information

mousetail.server.collection_manager.get_manager()[source]

Get the global collection manager instance.

Return type:

CollectionManager

CollectionManager Class

class mousetail.server.collection_manager.CollectionManager[source]

Bases: object

Manages Anki collection lifecycle and access.

__init__()[source]
list_available_collections()[source]

List all available Anki collections on the system.

Return type:

list[dict[str, str]]

Returns:

List of dicts with ‘profile’ and ‘path’ keys

check_collection_accessible(path=None)[source]

Check if a collection is accessible (not locked by Anki app).

Parameters:

path (Optional[str]) – Path to collection file. If None, uses default.

Return type:

str

Returns:

Resolved collection path

Raises:
  • ValueError – If path is invalid or collection doesn’t exist

  • AnkiError – If collection is locked or cannot be accessed

open_collection(path=None)[source]

Open a collection and return its path identifier.

Parameters:

path (Optional[str]) – Path to collection file. If None, uses default.

Return type:

str

Returns:

Collection path (identifier)

Raises:
  • ValueError – If path is invalid or collection doesn’t exist

  • AnkiError – If collection cannot be opened

close_collection(path)[source]

Close a collection.

Parameters:

path (str) – Path to collection file

close_all()[source]

Close all open collections.

get_collection(path=None)[source]

Get a collection with thread-safe access.

Parameters:

path (Optional[str]) – Path to collection. If None, uses default or first open collection.

Yields:

Collection instance

Example

>>> manager = CollectionManager()
>>> with manager.get_collection() as col:
...     note = col.new_note(notetype)
get_collection_info(path=None)[source]

Get information about a collection.

Parameters:

path (Optional[str]) – Path to collection. If None, uses default.

Return type:

dict

Returns:

Dict with collection information

Global Manager Function

mousetail.server.collection_manager.get_manager()[source]

Get the global collection manager instance.

Return type:

CollectionManager