diff --git a/McpStudy.Core/EchoTool.cs b/McpStudy.Core/EchoTool.cs new file mode 100644 index 0000000..9f892f7 --- /dev/null +++ b/McpStudy.Core/EchoTool.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; + +using ModelContextProtocol.Server; + +namespace McpStudy.Core +{ + [McpServerToolType] + public static class EchoTool + { + [McpServerTool, DisplayName("EchoServer"), Description("回传原消息给客户端!")] + public static string Echo(string message) + { + Console.WriteLine($"Echo: {message}"); + + System.IO.File.AppendAllText("Mcplog.txt", $"Echo: {message}\n"); + + return $"hello {message}"; + } + } +} diff --git a/McpStudy.Core/GlobalUsing.cs b/McpStudy.Core/GlobalUsing.cs new file mode 100644 index 0000000..e02abfc --- /dev/null +++ b/McpStudy.Core/GlobalUsing.cs @@ -0,0 +1 @@ + diff --git a/McpStudy.Core/McpStudy.Core.csproj b/McpStudy.Core/McpStudy.Core.csproj new file mode 100644 index 0000000..911d91b --- /dev/null +++ b/McpStudy.Core/McpStudy.Core.csproj @@ -0,0 +1,27 @@ + + + + net9.0 + enable + + + + + + + + + + + + + + + + + + + + + + diff --git a/McpStudy.McpClient/McpStudy.McpClient.csproj b/McpStudy.McpClient/McpStudy.McpClient.csproj new file mode 100644 index 0000000..0fd40cf --- /dev/null +++ b/McpStudy.McpClient/McpStudy.McpClient.csproj @@ -0,0 +1,14 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + diff --git a/McpStudy.McpClient/Program.cs b/McpStudy.McpClient/Program.cs new file mode 100644 index 0000000..b4f50f4 --- /dev/null +++ b/McpStudy.McpClient/Program.cs @@ -0,0 +1,10 @@ +namespace McpStudy.McpClient +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("调用MCP服务器示例"); + } + } +} diff --git a/McpStudy.McpServerSSE/Controllers/WeatherForecastController.cs b/McpStudy.McpServerSSE/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..55d54f9 --- /dev/null +++ b/McpStudy.McpServerSSE/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace McpStudy.McpServerSSE.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/McpStudy.McpServerSSE/McpStudy.McpServerSSE.csproj b/McpStudy.McpServerSSE/McpStudy.McpServerSSE.csproj new file mode 100644 index 0000000..6b1e620 --- /dev/null +++ b/McpStudy.McpServerSSE/McpStudy.McpServerSSE.csproj @@ -0,0 +1,17 @@ + + + + net9.0 + enable + enable + + + + + + + + + + + diff --git a/McpStudy.McpServerSSE/McpStudy.McpServerSSE.http b/McpStudy.McpServerSSE/McpStudy.McpServerSSE.http new file mode 100644 index 0000000..3e8086b --- /dev/null +++ b/McpStudy.McpServerSSE/McpStudy.McpServerSSE.http @@ -0,0 +1,6 @@ +@McpStudy.McpServerSSE_HostAddress = http://localhost:5027 + +GET {{McpStudy.McpServerSSE_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/McpStudy.McpServerSSE/Program.cs b/McpStudy.McpServerSSE/Program.cs new file mode 100644 index 0000000..d1f9856 --- /dev/null +++ b/McpStudy.McpServerSSE/Program.cs @@ -0,0 +1,32 @@ + +namespace McpStudy.McpServerSSE +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi + builder.Services.AddOpenApi(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.MapOpenApi(); + } + + app.UseAuthorization(); + + + app.MapControllers(); + + app.Run(); + } + } +} diff --git a/McpStudy.McpServerSSE/Properties/launchSettings.json b/McpStudy.McpServerSSE/Properties/launchSettings.json new file mode 100644 index 0000000..0b4b9f7 --- /dev/null +++ b/McpStudy.McpServerSSE/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5027", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/McpStudy.McpServerSSE/WeatherForecast.cs b/McpStudy.McpServerSSE/WeatherForecast.cs new file mode 100644 index 0000000..bd2e0b3 --- /dev/null +++ b/McpStudy.McpServerSSE/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace McpStudy.McpServerSSE +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/McpStudy.McpServerSSE/appsettings.Development.json b/McpStudy.McpServerSSE/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/McpStudy.McpServerSSE/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/McpStudy.McpServerSSE/appsettings.json b/McpStudy.McpServerSSE/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/McpStudy.McpServerSSE/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/McpStudy.McpServerStdio/McpStudy.McpServerStdio.csproj b/McpStudy.McpServerStdio/McpStudy.McpServerStdio.csproj new file mode 100644 index 0000000..0fd40cf --- /dev/null +++ b/McpStudy.McpServerStdio/McpStudy.McpServerStdio.csproj @@ -0,0 +1,14 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + diff --git a/McpStudy.McpServerStdio/Program.cs b/McpStudy.McpServerStdio/Program.cs new file mode 100644 index 0000000..3829a19 --- /dev/null +++ b/McpStudy.McpServerStdio/Program.cs @@ -0,0 +1,55 @@ +using System; +using System.ComponentModel; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; + +using ModelContextProtocol.Server; + +namespace McpStudy.McpServerStdIO +{ + internal class Program + { + static async Task Main(string[] args) + { + try + { + var builder = Host.CreateApplicationBuilder(args); + builder.Logging.AddConsole(options => + { + options.LogToStandardErrorThreshold = LogLevel.Trace; + }); + + builder.Services + .AddMcpServer() + .WithStdioServerTransport() + .WithToolsFromAssembly(); + + await builder.Build().RunAsync(); + + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + finally + { + + } + } + } +} + +[McpServerToolType] +public static class TimeTool +{ + [McpServerTool, Description("Get the current time for a city")] + public static string GetCurrentTime(string city) + { + var message = $"It is {DateTime.Now.Hour}:{DateTime.Now.Minute} in {city}."; + + File.AppendAllText("Mcplog.txt", $"{message}{System.Environment.NewLine}"); + return message; + } + +} diff --git a/McpStudy.UnitTest/McpStudy.UnitTest.csproj b/McpStudy.UnitTest/McpStudy.UnitTest.csproj new file mode 100644 index 0000000..5b9d2c3 --- /dev/null +++ b/McpStudy.UnitTest/McpStudy.UnitTest.csproj @@ -0,0 +1,25 @@ + + + + net9.0 + enable + enable + false + + + + + + + + + + + + + + + + + + diff --git a/McpStudy.UnitTest/UnitTest1.cs b/McpStudy.UnitTest/UnitTest1.cs new file mode 100644 index 0000000..5bfdb96 --- /dev/null +++ b/McpStudy.UnitTest/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace McpStudy.UnitTest +{ + public class UnitTest1 + { + [Fact] + public void Test1() + { + + } + } +} diff --git a/McpStudy.sln b/McpStudy.sln new file mode 100644 index 0000000..1256cdb --- /dev/null +++ b/McpStudy.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36221.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McpStudy.Core", "McpStudy.Core\McpStudy.Core.csproj", "{431D5D01-A1CE-46AD-862C-E03508B1EFA5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McpStudy.McpClient", "McpStudy.McpClient\McpStudy.McpClient.csproj", "{6A7B2283-2503-4024-A966-2DF0851F364B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McpStudy.UnitTest", "McpStudy.UnitTest\McpStudy.UnitTest.csproj", "{09C2F3F1-B770-4DAC-81A1-0217AD09874F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McpStudy.McpServerStdio", "McpStudy.McpServerStdio\McpStudy.McpServerStdio.csproj", "{7EDCB267-57AB-E836-D3EE-AA9ABAC8E373}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "McpStudy.McpServerSSE", "McpStudy.McpServerSSE\McpStudy.McpServerSSE.csproj", "{34A4291B-BA36-491B-914D-674CF3806314}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {431D5D01-A1CE-46AD-862C-E03508B1EFA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {431D5D01-A1CE-46AD-862C-E03508B1EFA5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {431D5D01-A1CE-46AD-862C-E03508B1EFA5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {431D5D01-A1CE-46AD-862C-E03508B1EFA5}.Release|Any CPU.Build.0 = Release|Any CPU + {6A7B2283-2503-4024-A966-2DF0851F364B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6A7B2283-2503-4024-A966-2DF0851F364B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6A7B2283-2503-4024-A966-2DF0851F364B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6A7B2283-2503-4024-A966-2DF0851F364B}.Release|Any CPU.Build.0 = Release|Any CPU + {09C2F3F1-B770-4DAC-81A1-0217AD09874F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09C2F3F1-B770-4DAC-81A1-0217AD09874F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09C2F3F1-B770-4DAC-81A1-0217AD09874F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09C2F3F1-B770-4DAC-81A1-0217AD09874F}.Release|Any CPU.Build.0 = Release|Any CPU + {7EDCB267-57AB-E836-D3EE-AA9ABAC8E373}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EDCB267-57AB-E836-D3EE-AA9ABAC8E373}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EDCB267-57AB-E836-D3EE-AA9ABAC8E373}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EDCB267-57AB-E836-D3EE-AA9ABAC8E373}.Release|Any CPU.Build.0 = Release|Any CPU + {34A4291B-BA36-491B-914D-674CF3806314}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {34A4291B-BA36-491B-914D-674CF3806314}.Debug|Any CPU.Build.0 = Debug|Any CPU + {34A4291B-BA36-491B-914D-674CF3806314}.Release|Any CPU.ActiveCfg = Release|Any CPU + {34A4291B-BA36-491B-914D-674CF3806314}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F66B4535-D87E-43A7-A8C9-91FFD8992330} + EndGlobalSection +EndGlobal