- 
                Notifications
    
You must be signed in to change notification settings  - Fork 841
 
Create a project template for an MCP server #6547
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
      
      
    
      
        
          +508
        
        
          −0
        
        
          
        
      
    
  
  
     Merged
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            24 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      20cc59b
              
                Start on MCP server template
              
              
                joelverhagen 38d446e
              
                Merge branch 'main' into jver/mcp-template
              
              
                joelverhagen 5ef5c50
              
                Improve
              
              
                joelverhagen 8b06c6f
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen cd6a0e2
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 36c90b1
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 5935e56
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 4127955
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 28363b4
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 4e64775
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen a2ac345
              
                Fix type, improve readme
              
              
                joelverhagen 06d2247
              
                Update src/ProjectTemplates/Microsoft.Extensions.AI.Templates/src/Mcp…
              
              
                joelverhagen 3a6a3e8
              
                Address comments
              
              
                joelverhagen fb056fc
              
                Make package path consistent
              
              
                joelverhagen 7963103
              
                Merge remote-tracking branch 'origin/jver/mcp-template' into jver/mcp…
              
              
                joelverhagen 21e8563
              
                Merge remote-tracking branch 'origin/main' into jver/mcp-template
              
              
                joelverhagen 6e30470
              
                Add icon
              
              
                joelverhagen a98b37e
              
                Add snapshot tests for MCP server template
              
              
                joelverhagen 129faff
              
                Remove return 0
              
              
                joelverhagen 6be6521
              
                Added comment
              
              
                joelverhagen 5e9d6f0
              
                Address most of the comments
              
              
                joelverhagen eabb037
              
                Address comments
              
              
                joelverhagen e29a101
              
                Remove comment that is wrong ☠️
              
              
                joelverhagen 9b8a767
              
                Address comment
              
              
                joelverhagen 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            47 changes: 10 additions & 37 deletions
          
          47 
        
  ...jectTemplates/Microsoft.Extensions.AI.Templates/src/McpServer/McpServer-CSharp/Program.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 | 
|---|---|---|
| @@ -1,59 +1,32 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using ModelContextProtocol.Server; | ||
| using System.ComponentModel; | ||
| using McpServer_CSharp.Tools; | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| var builder = Host.CreateApplicationBuilder(args); | ||
| 
     | 
||
| if (args.Length == 0 || args[0] != "start-mcp") | ||
| // Uncomment the following lines if you want to enforce the MCP command-line arguments. | ||
| // You can use command-line arguments to support multiple modes or commands in your application. | ||
| // These would be specified in the "package_arguments" property in the .mcp/server.json to inform client tools. | ||
| /* | ||
| if (args.Length == 0 || args[0] != "mcp") | ||
| { | ||
| Console.Error.WriteLine("Error: invalid command. Use the 'start-mcp' command-line argument to start the MCP server."); | ||
| return 1; | ||
| } | ||
| 
     | 
||
| var maxNumberEnv = Environment.GetEnvironmentVariable("MAX_RANDOM_NUMBER"); | ||
| if (!int.TryParse(maxNumberEnv, out var maxNumber) || maxNumber <= 0) | ||
| { | ||
| Console.Error.WriteLine("Error: you must set the MAX_RANDOM_NUMBER environment variable to a positive integer."); | ||
| Console.Error.WriteLine("Error: invalid command. Use the 'mcp' command-line argument to start the MCP server."); | ||
| return 1; | ||
| } | ||
| */ | ||
| 
     | 
||
| builder.Logging.AddConsole(consoleLogOptions => | ||
| { | ||
| // Configure all logs to go to stderr (stdout is used for the MCP protocol messages). | ||
| consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace; | ||
| }); | ||
| 
     | 
||
| // add the MCP services, discover tools from the current assembly | ||
| // Add the MCP services: the transport to use (stdio) and the tools to register. | ||
| builder.Services | ||
| .AddMcpServer() | ||
| .WithStdioServerTransport() | ||
| .WithTools<RandomNumberTool>(); | ||
| 
     | 
||
| // add dependencies for the MCP server tools | ||
| builder.Services.AddSingleton(_ => new RandomNumberService(maxNumber)); | ||
| .WithTools<RandomNumberTools>(); | ||
| 
     | 
||
| await builder.Build().RunAsync(); | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| return 0; | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| public class RandomNumberService(int max) | ||
| { | ||
| public int Max => max; | ||
| public int GetRandomNumber() => Random.Shared.Next(1, max + 1); | ||
| } | ||
| 
     | 
||
| public class RandomNumberTool(RandomNumberService service) | ||
| { | ||
| [McpServerTool, Description("Returns the maximum random number that can be generated by the tool.")] | ||
| public int MaxRandomNumber() | ||
| { | ||
| return service.Max; | ||
| } | ||
| 
     | 
||
| [McpServerTool, Description("Returns a random number between 1 and the maximum number allowed by the tool.")] | ||
| public int GetRandomNumber() | ||
| { | ||
| return service.GetRandomNumber(); | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
        
          
  
    
      
          
            49 changes: 49 additions & 0 deletions
          
          49 
        
  ...crosoft.Extensions.AI.Templates/src/McpServer/McpServer-CSharp/Tools/RandomNumberTools.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,49 @@ | ||
| using System.ComponentModel; | ||
| using ModelContextProtocol.Server; | ||
| 
     | 
||
| namespace McpServer_CSharp.Tools; | ||
| 
     | 
||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| /// <summary> | ||
| /// Sample MCP tools for demonstration purposes. | ||
| /// These tools can be invoked by MCP clients to perform various operations. | ||
| /// </summary> | ||
| public class RandomNumberTools | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| { | ||
| private readonly int _maxNumber; | ||
| 
     | 
||
| public RandomNumberTools() | ||
| { | ||
| // Process configuration settings from the environment variables. | ||
| // These will be provided by the MCP client application, such as VS Code. | ||
| // Configuration settings could be provided via dependency injection and the IOptions pattern. | ||
| var maxNumberEnv = Environment.GetEnvironmentVariable("MAX_RANDOM_NUMBER"); | ||
| if (!int.TryParse(maxNumberEnv, out var maxNumber) || maxNumber <= 0) | ||
| { | ||
| throw new InvalidOperationException("Error: you must set the MAX_RANDOM_NUMBER environment variable to a positive integer."); | ||
| } | ||
| 
     | 
||
| _maxNumber = maxNumber; | ||
| } | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| /// <summary> | ||
| /// Returns the maximum random number that can be generated by the tool. | ||
| /// </summary> | ||
| /// <returns>The maximum random number.</returns> | ||
| [McpServerTool(Name = "get_max_random_number")] | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| [Description("Returns the maximum random number that can be generated by the tool.")] | ||
| public int GetMaxRandomNumber() | ||
| { | ||
| return _maxNumber; | ||
| } | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| /// <summary> | ||
| /// Returns a random number between 1 and the maximum number allowed by the tool (inclusive). | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| /// </summary> | ||
| /// <returns>A random number.</returns> | ||
| [McpServerTool(Name = "get_random_number")] | ||
                
      
                  joelverhagen marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| [Description("Returns a random number between 1 and the maximum number allowed by the tool.")] | ||
| public int GetRandomNumber() | ||
| { | ||
| return Random.Shared.Next(1, _maxNumber + 1); | ||
| } | ||
| } | ||
      
      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.