- 
                Notifications
    You must be signed in to change notification settings 
- Fork 552
Introduce acceptance helpers to ElicitResult and client capability checks on IMcpServer #666
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
      
      
            dogukandemir
  wants to merge
  6
  commits into
  modelcontextprotocol:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dogukandemir:feature/elicitation-extension-methods-to-improve-readability
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +359
        
        
          −7
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      920e53b
              
                Elicitation extension methods to improve code readability and reduce …
              
              
                dogukandemir b8cf68b
              
                Replace ElicitResult extension methods with JsonIgnore properties
              
              
                dogukandemir e9f8516
              
                Add ClientSupportsRoots and ClientSupportsSampling methods
              
              
                dogukandemir 142fcfa
              
                rename ThrowIfXXXUnsupported methods to ThrowIfClientXXXUnsupported
              
              
                dogukandemir 55d419f
              
                rename IsCancelled to IsCanceled - American English
              
              
                dogukandemir b600f30
              
                use canceled in xml comment
              
              
                dogukandemir File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            160 changes: 160 additions & 0 deletions
          
          160 
        
  tests/ModelContextProtocol.Tests/Protocol/ElicitResultTests.cs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| using System.Text.Json; | ||
| using ModelContextProtocol.Protocol; | ||
|  | ||
| namespace ModelContextProtocol.Tests.Protocol; | ||
|  | ||
| public class ElicitResultTests | ||
| { | ||
| [Theory] | ||
| [InlineData("accept")] | ||
| [InlineData("Accept")] | ||
| [InlineData("ACCEPT")] | ||
| [InlineData("AccEpt")] | ||
| public void IsAccepted_Returns_True_For_VariousAcceptedActions(string action) | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = action }; | ||
|  | ||
| // Act | ||
| var isAccepted = result.IsAccepted; | ||
|  | ||
| // Assert | ||
| Assert.True(isAccepted); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("decline")] | ||
| [InlineData("Decline")] | ||
| [InlineData("DECLINE")] | ||
| [InlineData("DecLine")] | ||
| public void IsDeclined_Returns_True_For_VariousDeclinedActions(string action) | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = action }; | ||
|  | ||
| // Act | ||
| var isDeclined = result.IsDeclined; | ||
|  | ||
| // Assert | ||
| Assert.True(isDeclined); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("cancel")] | ||
| [InlineData("Cancel")] | ||
| [InlineData("CANCEL")] | ||
| [InlineData("CanCel")] | ||
| public void IsCancelled_Returns_True_For_VariousCancelledActions(string action) | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = action }; | ||
|  | ||
| // Act | ||
| var isCancelled = result.IsCanceled; | ||
|  | ||
| // Assert | ||
| Assert.True(isCancelled); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsAccepted_Returns_False_For_DefaultAction() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult(); | ||
|  | ||
| // Act & Assert | ||
| Assert.False(result.IsAccepted); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsDeclined_Returns_False_For_DefaultAction() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult(); | ||
|  | ||
| // Act & Assert | ||
| Assert.False(result.IsDeclined); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsCancelled_Returns_True_For_DefaultAction() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult(); | ||
|  | ||
| // Act & Assert | ||
| Assert.True(result.IsCanceled); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsAccepted_Returns_False_For_Null_Action() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = null! }; | ||
|  | ||
| // Act & Assert | ||
| Assert.False(result.IsAccepted); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsDeclined_Returns_False_For_Null_Action() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = null! }; | ||
|  | ||
| // Act & Assert | ||
| Assert.False(result.IsDeclined); | ||
| } | ||
|  | ||
| [Fact] | ||
| public void IsCancelled_Returns_False_For_Null_Action() | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = null! }; | ||
|  | ||
| // Act & Assert | ||
| Assert.False(result.IsCanceled); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("accept")] | ||
| [InlineData("decline")] | ||
| [InlineData("cancel")] | ||
| [InlineData("unknown")] | ||
| public void JsonSerialization_ExcludesJsonIgnoredProperties(string action) | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = action }; | ||
|  | ||
| // Act | ||
| var json = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions); | ||
|  | ||
| // Assert | ||
| Assert.DoesNotContain("IsAccepted", json); | ||
| Assert.DoesNotContain("IsDeclined", json); | ||
| Assert.DoesNotContain("IsCanceled", json); | ||
| Assert.Contains($"\"action\":\"{action}\"", json); | ||
| } | ||
|  | ||
| [Theory] | ||
| [InlineData("accept", true, false, false)] | ||
| [InlineData("decline", false, true, false)] | ||
| [InlineData("cancel", false, false, true)] | ||
| [InlineData("unknown", false, false, false)] | ||
| public void JsonRoundTrip_PreservesActionAndComputedProperties(string action, bool isAccepted, bool isDeclined, bool isCancelled) | ||
| { | ||
| // Arrange | ||
| var result = new ElicitResult { Action = action }; | ||
|  | ||
| // Act | ||
| var json = JsonSerializer.Serialize(result, McpJsonUtilities.DefaultOptions); | ||
| var deserialized = JsonSerializer.Deserialize<ElicitResult>(json, McpJsonUtilities.DefaultOptions); | ||
|  | ||
| // Assert | ||
| Assert.NotNull(deserialized); | ||
| Assert.Equal(action, deserialized.Action); | ||
| Assert.Equal(isAccepted, deserialized.IsAccepted); | ||
| Assert.Equal(isDeclined, deserialized.IsDeclined); | ||
| Assert.Equal(isCancelled, deserialized.IsCanceled); | ||
| } | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These would be better as properties rather than as methods. Should we start thinking about using C# 14?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you're referring to extension members, right? if that's the case, I suggest we merge this PR as-is and then I can work on a follow up PR to convert all extension methods in whole solution to extension members for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub is it ok to resolve this conversation and proceed with what I suggested?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say let's remove these methods for now and reconsider once C# 14 is generally available. There's also this ongoing PR that proposes to convert
IMcpClientet al to abstract interfaces so extensions might not even be necessary.