"""
Django signal receivers of the MCPServer app.
They log the MCPServer's requests and tool calls, and the outcome of each attempt to index a
repository or the blog. One of them also keeps the tool result cache correct: when a repository
is indexed at a new commit, :func:`handle_mcpserver_repository_indexed` starts a new index
generation, so that no tool answers from the old files (see
:mod:`smarter.apps.mcpserver.protocol.caching`).
The signals are defined in :mod:`smarter.apps.mcpserver.signals`. The receivers are connected
when :meth:`smarter.apps.mcpserver.apps.MCPServerConfig.ready` imports this module.
.. note::
**Experimental.** The MCPServer was designed and coded by Claude Code (Anthropic's
Claude Opus 5.5), with Lawrence McDaniel as co-author. It is experimental, and will
be documented.
"""
# pylint: disable=W0613,C0115
from django.dispatch import receiver
from smarter.apps.mcpserver.protocol.caching import invalidate_cached_tool_results
from smarter.lib import logging
from smarter.lib.django.waffle import SmarterWaffleSwitches
from .models import RepositoryIndex
from .signals import (
mcpserver_called,
mcpserver_repository_index_failed,
mcpserver_repository_indexed,
mcpserver_tool_called,
)
logger = logging.getSmarterLogger(__name__, any_switches=[SmarterWaffleSwitches.MCPSERVER_LOGGING])
[docs]
@receiver(mcpserver_called, dispatch_uid="mcpserver_called")
def handle_mcpserver_called(sender, server: str, method: str, **kwargs):
"""
Log a JSON-RPC request to the MCPServer, at DEBUG.
:param sender: The class that sent the signal.
:param server: The server's name, e.g. ``smarter-project-smarter-cli``.
:param method: The JSON-RPC method, e.g. ``tools/call``.
:param kwargs: The signal's other arguments, which are ignored.
"""
logger.debug("%s %s", server, method)
[docs]
@receiver(mcpserver_repository_indexed, dispatch_uid="mcpserver_repository_indexed")
def handle_mcpserver_repository_indexed(sender, index: RepositoryIndex, **kwargs):
"""
Make the cached tool results stale, since a repository's files changed, and log it.
Side effect: a cache write, through
:func:`~smarter.apps.mcpserver.protocol.caching.invalidate_cached_tool_results`, which starts a new
index generation.
:param sender: The function that indexed the repository.
:param index: The repository's index, at its new commit.
:param kwargs: The signal's other arguments, which are ignored.
"""
invalidate_cached_tool_results()
logger.info(
"%s indexed at %s: %s files, %s bytes",
index.repository,
index.commit_sha,
index.file_count,
index.total_bytes,
)
[docs]
@receiver(mcpserver_repository_index_failed, dispatch_uid="mcpserver_repository_index_failed")
def handle_mcpserver_repository_index_failed(sender, repository: str, error: str, **kwargs):
"""
Log a repository, or the blog, that could not be indexed, at WARNING.
The cached tool results are left alone, because the index did not change.
:param sender: The function that tried to index the repository.
:param repository: The repository's name, or the blog's.
:param error: Why it could not be indexed.
:param kwargs: The signal's other arguments, which are ignored.
"""
logger.warning(
"%s could not be indexed: %s",
repository,
error,
)