- 
                Notifications
    
You must be signed in to change notification settings  - Fork 714
 
feature Add methods to McpTransportContext #570
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
      
      
            sdelamo
  wants to merge
  10
  commits into
  modelcontextprotocol:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
sdelamo:servlet-transport-context
  
      
      
   
  
    
  
  
  
 
  
      
    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.
          
          
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      7a3b20d
              
                feature Add methods to McpTransportContext
              
              
                sdelamo 1a2b0a5
              
                Update mcp-core/src/main/java/io/modelcontextprotocol/server/McpTrans…
              
              
                sdelamo f02ec6f
              
                adding size
              
              
                sdelamo 9267558
              
                Delete mcp-spring/mcp-spring-webmvc/src/main/java/io/modelcontextprot…
              
              
                sdelamo 74c995b
              
                Update mcp-core/src/main/java/io/modelcontextprotocol/server/servlet/…
              
              
                sdelamo f0632fa
              
                Update mcp-core/src/main/java/io/modelcontextprotocol/server/servlet/…
              
              
                sdelamo df0c149
              
                add licenese
              
              
                sdelamo 45ec4b8
              
                add license
              
              
                sdelamo 24098f2
              
                implementations for McpTransportContextExtractor<ServerRequest>
              
              
                sdelamo e8cdb31
              
                remove usued imports
              
              
                sdelamo 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              
        
          
          
            42 changes: 42 additions & 0 deletions
          
          42 
        
  ...o/modelcontextprotocol/server/servlet/HttpServletRequestMcpTransportContextExtractor.java
  
  
      
      
   
        
      
      
    
  
    
      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,42 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or authors. | ||
                
      
                  sdelamo marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| */ | ||
| package io.modelcontextprotocol.server.servlet; | ||
| 
     | 
||
| import io.modelcontextprotocol.common.McpTransportContext; | ||
| import io.modelcontextprotocol.server.McpTransportContextExtractor; | ||
| import io.modelcontextprotocol.spec.ProtocolVersions; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| 
     | 
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| 
     | 
||
| /** | ||
| * {@link McpTransportContextExtractor} implementation for {@link HttpServletRequest}. | ||
| */ | ||
| public class HttpServletRequestMcpTransportContextExtractor | ||
| implements McpTransportContextExtractor<HttpServletRequest> { | ||
| 
     | 
||
| @Override | ||
| public McpTransportContext extract(HttpServletRequest request) { | ||
| return McpTransportContext.create(metadata(request)); | ||
| } | ||
| 
     | 
||
| /** | ||
| * @param request Servlet Request | ||
| * @return Extracts Map for MCP Transport Context | ||
| */ | ||
| protected Map<String, Object> metadata(HttpServletRequest request) { | ||
| Map<String, Object> metadata = new HashMap<>(); | ||
                
       | 
||
| metadata.put(io.modelcontextprotocol.spec.HttpHeaders.PROTOCOL_VERSION, | ||
| Optional.ofNullable(request.getHeader(io.modelcontextprotocol.spec.HttpHeaders.PROTOCOL_VERSION)) | ||
| .orElse(ProtocolVersions.MCP_2025_03_26)); | ||
| Optional.ofNullable(request.getHeader(io.modelcontextprotocol.spec.HttpHeaders.MCP_SESSION_ID)) | ||
| .ifPresent(v -> metadata.put(io.modelcontextprotocol.spec.HttpHeaders.MCP_SESSION_ID, v)); | ||
| Optional.ofNullable(request.getHeader(io.modelcontextprotocol.spec.HttpHeaders.LAST_EVENT_ID)) | ||
| .ifPresent(v -> metadata.put(io.modelcontextprotocol.spec.HttpHeaders.LAST_EVENT_ID, v)); | ||
| return metadata; | ||
| } | ||
| 
     | 
||
| } | ||
        
          
          
            7 changes: 7 additions & 0 deletions
          
          7 
        
  mcp-core/src/main/java/io/modelcontextprotocol/server/servlet/package-info.java
  
  
      
      
   
        
      
      
    
  
    
      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,7 @@ | ||
| /* | ||
| * Copyright 2024-2024 the original author or authors. | ||
                
      
                  sdelamo marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| */ | ||
| /** | ||
| * Classes related with servlet support. | ||
| */ | ||
| package io.modelcontextprotocol.server.servlet; | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            53 changes: 53 additions & 0 deletions
          
          53 
        
  mcp-core/src/test/java/io/modelcontextprotocol/common/DefaultMcpTransportContextTest.java
  
  
      
      
   
        
      
      
    
  
    
      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,53 @@ | ||
| package io.modelcontextprotocol.common; | ||
                
      
                  sdelamo marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| import io.modelcontextprotocol.spec.HttpHeaders; | ||
| import org.junit.jupiter.api.Test; | ||
| 
     | 
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| 
     | 
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| 
     | 
||
| class DefaultMcpTransportContextTest { | ||
| 
     | 
||
| @Test | ||
| void protocolVersionNotPresent() { | ||
| var ctx = new DefaultMcpTransportContext(Collections.emptyMap()); | ||
| assertFalse(ctx.protocolVersion().isPresent()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void sessionIdNotPresent() { | ||
| var ctx = new DefaultMcpTransportContext(Collections.emptyMap()); | ||
| assertFalse(ctx.sessionId().isPresent()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void lastEventIdNotPresent() { | ||
| var ctx = new DefaultMcpTransportContext(Collections.emptyMap()); | ||
| assertFalse(ctx.lastEventId().isPresent()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void protocolVersion_returnsProvidedValue() { | ||
| var ctx = new DefaultMcpTransportContext(Map.of(HttpHeaders.PROTOCOL_VERSION, "2025-01-01", | ||
| HttpHeaders.MCP_SESSION_ID, "session-123", HttpHeaders.LAST_EVENT_ID, "evt-456")); | ||
| assertEquals("2025-01-01", ctx.protocolVersion().orElseThrow()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void sessionId_returnsProvidedValue() { | ||
| var ctx = new DefaultMcpTransportContext(Map.of(HttpHeaders.PROTOCOL_VERSION, "2025-01-01", | ||
| HttpHeaders.MCP_SESSION_ID, "session-abc", HttpHeaders.LAST_EVENT_ID, "evt-456")); | ||
| assertEquals("session-abc", ctx.sessionId().orElseThrow()); | ||
| } | ||
| 
     | 
||
| @Test | ||
| void lastEventId_returnsProvidedValue() { | ||
| var ctx = new DefaultMcpTransportContext(Map.of(HttpHeaders.PROTOCOL_VERSION, "2025-01-01", | ||
| HttpHeaders.MCP_SESSION_ID, "session-abc", HttpHeaders.LAST_EVENT_ID, "evt-999")); | ||
| assertEquals("evt-999", ctx.lastEventId().orElseThrow()); | ||
| } | ||
| 
     | 
||
| } | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
      
      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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.