Skip to content

Add logging decorator for tool invocations #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2025

Conversation

akolotov
Copy link
Collaborator

@akolotov akolotov commented Jul 17, 2025

Summary

  • implement log_tool_invocation decorator
  • log tool invocations via decorator
  • configure root logger in server.py
  • decorate all tools with @log_tool_invocation
  • add unit test for decorator
  • update documentation and rules

Closes #161


https://chatgpt.com/codex/tasks/task_b_6878330535e08323904e49c0150ebfe3

Summary by CodeRabbit

  • New Features

    • Introduced automatic logging for tool function invocations, enhancing traceability of tool usage.
  • Bug Fixes

    • None.
  • Documentation

    • Updated guidelines and examples to require logging for all MCP tool functions.
    • Expanded documentation to describe new logging capabilities.
  • Tests

    • Added tests to verify correct logging behavior for tool invocations.
  • Chores

    • Configured basic logging settings for improved log formatting and consistency.

Copy link

coderabbitai bot commented Jul 17, 2025

Walkthrough

A logging decorator, @log_tool_invocation, was introduced and applied to all MCP tool functions to log tool invocations with arguments (excluding ctx). Logging configuration was added to the server. Documentation and rule files were updated to require and describe the decorator, and a unit test was added to verify its behavior.

Changes

File(s) Change Summary
blockscout_mcp_server/tools/common.py Added the log_tool_invocation async decorator for logging tool invocations.
blockscout_mcp_server/server.py Configured standard logging with timestamp, level, and message format.
blockscout_mcp_server/tools/address_tools.py
blockscout_mcp_server/tools/block_tools.py
blockscout_mcp_server/tools/chains_tools.py
blockscout_mcp_server/tools/contract_tools.py
blockscout_mcp_server/tools/ens_tools.py
blockscout_mcp_server/tools/get_instructions.py
blockscout_mcp_server/tools/search_tools.py
blockscout_mcp_server/tools/transaction_tools.py
Applied @log_tool_invocation decorator to all MCP tool async functions.
tests/tools/test_log_tool_invocation.py Added async unit test for the decorator, verifying logging output and argument filtering.
AGENTS.md Updated documentation to describe the new logging utility and decorator in tools/common.py.
.cursor/rules/110-new-mcp-tool.mdc Updated rule to require the decorator and included code examples using it.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant ToolFunction
    participant Logger

    Client->>Server: Call MCP tool (e.g., get_address_by_ens_name)
    Server->>ToolFunction: Invoke tool (decorated with @log_tool_invocation)
    ToolFunction->>Logger: Log tool name and args (excluding ctx)
    ToolFunction-->>Server: Return result
    Server-->>Client: Return tool response
Loading

Assessment against linked issues

Objective Addressed Explanation
Create @log_tool_invocation decorator in blockscout_mcp_server/tools/common.py (#161)
Configure standard Python logger in blockscout_mcp_server/server.py (#161)
Apply @log_tool_invocation to all MCP tool functions in blockscout_mcp_server/tools/ (#161)
Add unit tests for the decorator verifying log output, argument filtering, and return value (#161)
Update AGENTS.md and .cursor/rules/110-new-mcp-tool.mdc to document/mandate the decorator (#161)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes detected. All modifications directly relate to the objectives in the linked issue, focusing on logging, decorator application, documentation, and testing.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ab094ab and 81b61d0.

📒 Files selected for processing (3)
  • blockscout_mcp_server/server.py (2 hunks)
  • blockscout_mcp_server/tools/common.py (2 hunks)
  • tests/tools/test_log_tool_invocation.py (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • blockscout_mcp_server/server.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/tools/test_log_tool_invocation.py
  • blockscout_mcp_server/tools/common.py
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@akolotov akolotov requested a review from Copilot July 17, 2025 02:35
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a @log_tool_invocation decorator to automatically log tool calls, applies it across all MCP tool functions, configures the root logger, adds a unit test for the decorator, and updates documentation and coding rules.

  • Implemented log_tool_invocation in tools.common
  • Decorated all existing tools with @log_tool_invocation and configured logging in server.py
  • Added a unit test for the decorator and updated docs/rules to mandate its use

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/tools/test_log_tool_invocation.py Added tests for the log_tool_invocation decorator
blockscout_mcp_server/tools/transaction_tools.py Imported and applied @log_tool_invocation to transaction tools
blockscout_mcp_server/tools/search_tools.py Imported and applied @log_tool_invocation to search tools
blockscout_mcp_server/tools/get_instructions.py Imported and applied @log_tool_invocation to instructions tool
blockscout_mcp_server/tools/ens_tools.py Imported and applied @log_tool_invocation to ENS tools
blockscout_mcp_server/tools/contract_tools.py Imported and applied @log_tool_invocation to contract tools
blockscout_mcp_server/tools/common.py Implemented the log_tool_invocation decorator
blockscout_mcp_server/tools/chains_tools.py Imported and applied @log_tool_invocation to chains tools
blockscout_mcp_server/tools/block_tools.py Imported and applied @log_tool_invocation to block tools
blockscout_mcp_server/tools/address_tools.py Imported and applied @log_tool_invocation to address tools
blockscout_mcp_server/server.py Configured root logger with basicConfig
AGENTS.md Updated docs to mention the new logging decorator
.cursor/rules/110-new-mcp-tool.mdc Added guideline to decorate tools with @log_tool_invocation
.cursor/AGENTS.md Updated agent guidance to require the new decorator
Comments suppressed due to low confidence (2)

.cursor/rules/110-new-mcp-tool.mdc:100

  • [nitpick] The indentation of this bullet is inconsistent with the surrounding list items; align it (e.g., 5 spaces) for consistent Markdown formatting.
   - Decorate each tool function with `@log_tool_invocation` from `tools.common`

tests/tools/test_log_tool_invocation.py:10

  • Add tests for scenarios with default or missing parameters (beyond ctx) to ensure the decorator correctly binds and logs default argument values.
async def test_log_tool_invocation_decorator(caplog: pytest.LogCaptureFixture, mock_ctx: Context) -> None:

@akolotov akolotov merged commit b1d3497 into main Jul 17, 2025
7 checks passed
@akolotov akolotov assigned akolotov and unassigned akolotov Jul 17, 2025
@akolotov akolotov deleted the codex/add-logging-for-tool-invocations branch July 17, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance Server Observability with Tool Invocation Logging
1 participant