Skip to content

Add logging support #103

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

dak2
Copy link
Contributor

@dak2 dak2 commented Aug 4, 2025

Motivation and Context

A server can send structured logging messages to the client. https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#logging

Logging was specified in the 2024-11-05 specification, but since it was not supported in ruby-sdk, I implemented it. https://modelcontextprotocol.io/specification/2024-11-05/server/utilities/logging

I also made it possible to output a simple notification message in the examples.

How Has This Been Tested?

  • Check existing and added test cases for this case
  • Check a sse server of examples can send logging message to the client

Breaking Changes

None

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context


module MCP
class LoggingMessageNotification
VALID_LEVELS = ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"].freeze
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename to the folliwng?

Suggested change
VALID_LEVELS = ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"].freeze
LOG_LEVELS = ["debug", "info", "notice", "warning", "error", "critical", "alert", "emergency"].freeze

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest to also use the string array directly for easier parsing:

LOG_LEVELS = %w[debug info notice warning error critical alert emergency].freeze

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right according to the Ruby style guide.
However, I don't think it needs to be fixed.

This repository depends on rubocop-shopify. It disables the Style/WordArray cop.
See: https://github.com/Shopify/ruby-style-guide/blob/0de5b278c576ad2f537f9f51f5897587a9b33841/rubocop.yml#L1405-L1407

Additionally, there don't seem to be any compelling reasons to enable it for this case in this repository.

@@ -55,6 +56,7 @@ def initialize(
@server_context = server_context
@configuration = MCP.configuration.merge(configuration)
@capabilities = capabilities || default_capabilities
@logging_message_notification = nil
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly, the Python SDK uses "info" level as the default. What do you think about doing the same?
https://github.com/modelcontextprotocol/python-sdk/blob/v1.12.3/src/mcp/server/fastmcp/server.py#L132

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think it's necessary to set a default value, but what do you think?

The log_level literal specified in the MCP spec appears to be defined in mcp/types.py, and it seems that no default value has been set.

https://github.com/modelcontextprotocol/python-sdk/blob/68e25d478b3b6a026b2d9a30b3e5f34f3b1290de/src/mcp/types.py#L905

The log_level in fastmcp/server.py#L132 appears to set the default value for uvicorn's log_level.

However, if this literal is the same as the one specified in the MCP spec, I don't think it meets the logging specifications, as levels such as emergency and notice are not defined.

Copy link
Member

Choose a reason for hiding this comment

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

That's true. There's no need to set something that's not explicitly specified in the specification.
Your current suggestion makes sense to me.

@dak2 dak2 force-pushed the support-logging branch 2 times, most recently from 6f91a19 to 754f8cd Compare August 5, 2025 01:01
@@ -0,0 +1,48 @@
# typed: true
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't the test pass even without this # typed: true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dak2 dak2 force-pushed the support-logging branch from 754f8cd to aa15a5b Compare August 5, 2025 12:26
koic
koic previously approved these changes Aug 5, 2025
@koic
Copy link
Member

koic commented Aug 9, 2025

@dak2 Oops, can you rebase with the latest master to resolve the conflicts?

A server can send structured logging messages to the client.
https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#logging

Logging was specified in the 2024-11-05 specification, but since it was not supported in ruby-sdk, I implemented it.
https://modelcontextprotocol.io/specification/2024-11-05/server/utilities/logging

I also made it possible to output a simple notification message in the examples.
@dak2
Copy link
Contributor Author

dak2 commented Aug 10, 2025

@koic
Thank you for letting me know!
The conflict has been resolved. Could you please review it again?

@koic
Copy link
Member

koic commented Aug 13, 2025

Oops, sorry if my understanding of the specification and this implementation is different. As I understand it, with logging/setLevel, only messages at the specified level or higher should be sent from the server to the client as notifications/message. In other words, if the level error is specified, messages at info or warning level should not be notified.
I believe "Only sends error level and above" in the "Message Flow" section corresponds to this.
https://modelcontextprotocol.io/specification/2025-06-18/server/utilities/logging#message-flow

It seems to me that the current PR is not implemented in this way. Could you check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants