Add logging support
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.
This commit is contained in:
parent
830913be5a
commit
afa2fb4077
1 changed files with 434 additions and 7 deletions
|
|
@ -105,6 +105,8 @@ module JsonRpcHandler
|
|||
result = method.call(params)
|
||||
|
||||
success_response(id: id, result: result)
|
||||
rescue MCP::Server::RequestHandlerError => e
|
||||
handle_request_error(e, id, id_validation_pattern)
|
||||
rescue StandardError => e
|
||||
error_response(id: id, id_validation_pattern: id_validation_pattern, error: {
|
||||
code: ErrorCode::INTERNAL_ERROR,
|
||||
|
|
@ -114,6 +116,24 @@ module JsonRpcHandler
|
|||
end
|
||||
end
|
||||
|
||||
def handle_request_error(error, id, id_validation_pattern)
|
||||
error_type = error.respond_to?(:error_type) ? error.error_type : nil
|
||||
|
||||
code, message = case error_type
|
||||
when :invalid_request then [ErrorCode::INVALID_REQUEST, "Invalid Request"]
|
||||
when :invalid_params then [ErrorCode::INVALID_PARAMS, "Invalid params"]
|
||||
when :parse_error then [ErrorCode::PARSE_ERROR, "Parse error"]
|
||||
when :internal_error then [ErrorCode::INTERNAL_ERROR, "Internal error"]
|
||||
else [ErrorCode::INTERNAL_ERROR, "Internal error"]
|
||||
end
|
||||
|
||||
error_response(id: id, id_validation_pattern: id_validation_pattern, error: {
|
||||
code: code,
|
||||
message: message,
|
||||
data: error.message,
|
||||
})
|
||||
end
|
||||
|
||||
def valid_version?(version)
|
||||
version == Version::V2_0
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue