From c32c6c577d205e8b436b857dd4ebc54081bd65c9 Mon Sep 17 00:00:00 2001 From: Ludovit Scholtz Date: Fri, 27 Jun 2025 21:13:37 +0200 Subject: [PATCH 1/2] Get session id sample code #564 --- ModelContextProtocol.slnx | 1 + samples/GetSessionId/GetSessionId.csproj | 13 +++++++++++++ samples/GetSessionId/GetSessionId.http | 6 ++++++ samples/GetSessionId/GetSessionIdTool.cs | 18 ++++++++++++++++++ samples/GetSessionId/Program.cs | 15 +++++++++++++++ .../Properties/launchSettings.json | 14 ++++++++++++++ .../GetSessionId/appsettings.Development.json | 8 ++++++++ samples/GetSessionId/appsettings.json | 9 +++++++++ 8 files changed, 84 insertions(+) create mode 100644 samples/GetSessionId/GetSessionId.csproj create mode 100644 samples/GetSessionId/GetSessionId.http create mode 100644 samples/GetSessionId/GetSessionIdTool.cs create mode 100644 samples/GetSessionId/Program.cs create mode 100644 samples/GetSessionId/Properties/launchSettings.json create mode 100644 samples/GetSessionId/appsettings.Development.json create mode 100644 samples/GetSessionId/appsettings.json diff --git a/ModelContextProtocol.slnx b/ModelContextProtocol.slnx index e4fd42fe..d34453bb 100644 --- a/ModelContextProtocol.slnx +++ b/ModelContextProtocol.slnx @@ -9,6 +9,7 @@ + diff --git a/samples/GetSessionId/GetSessionId.csproj b/samples/GetSessionId/GetSessionId.csproj new file mode 100644 index 00000000..a7eae26e --- /dev/null +++ b/samples/GetSessionId/GetSessionId.csproj @@ -0,0 +1,13 @@ + + + + net9.0 + enable + enable + + + + + + + diff --git a/samples/GetSessionId/GetSessionId.http b/samples/GetSessionId/GetSessionId.http new file mode 100644 index 00000000..0fdfb514 --- /dev/null +++ b/samples/GetSessionId/GetSessionId.http @@ -0,0 +1,6 @@ +@GetSessionId_HostAddress = http://localhost:5002 + +GET {{GetSessionId_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/samples/GetSessionId/GetSessionIdTool.cs b/samples/GetSessionId/GetSessionIdTool.cs new file mode 100644 index 00000000..5f2aee32 --- /dev/null +++ b/samples/GetSessionId/GetSessionIdTool.cs @@ -0,0 +1,18 @@ +using ModelContextProtocol.Server; +using System.ComponentModel; + +[McpServerToolType] +public class GetSessionIdTool +{ + private readonly IMcpServer _mcpServer; + public GetSessionIdTool(IMcpServer mcpServer) + { + _mcpServer = mcpServer; + } + + [McpServerTool(Name = "get_session"), Description("Returns current session id")] + public async Task GetSession() + { + return _mcpServer.SessionId; + } +} diff --git a/samples/GetSessionId/Program.cs b/samples/GetSessionId/Program.cs new file mode 100644 index 00000000..0bcca2b3 --- /dev/null +++ b/samples/GetSessionId/Program.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using ModelContextProtocol.Server; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services + .AddMcpServer() + .WithHttpTransport() + .WithToolsFromAssembly(); + +var app = builder.Build(); + +app.MapMcp("/mcp"); + +app.Run(); diff --git a/samples/GetSessionId/Properties/launchSettings.json b/samples/GetSessionId/Properties/launchSettings.json new file mode 100644 index 00000000..056a519e --- /dev/null +++ b/samples/GetSessionId/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:5002", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/samples/GetSessionId/appsettings.Development.json b/samples/GetSessionId/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/samples/GetSessionId/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/samples/GetSessionId/appsettings.json b/samples/GetSessionId/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/samples/GetSessionId/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From ba5a95083997aafb13e8f2b61a447a5ef2b0e10a Mon Sep 17 00:00:00 2001 From: Ludovit Scholtz Date: Sat, 28 Jun 2025 09:30:07 +0200 Subject: [PATCH 2/2] fix sample - move IMcpServer to method instead of constructor --- samples/GetSessionId/GetSessionIdTool.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/samples/GetSessionId/GetSessionIdTool.cs b/samples/GetSessionId/GetSessionIdTool.cs index 5f2aee32..02401651 100644 --- a/samples/GetSessionId/GetSessionIdTool.cs +++ b/samples/GetSessionId/GetSessionIdTool.cs @@ -4,15 +4,13 @@ [McpServerToolType] public class GetSessionIdTool { - private readonly IMcpServer _mcpServer; - public GetSessionIdTool(IMcpServer mcpServer) + public GetSessionIdTool() { - _mcpServer = mcpServer; } [McpServerTool(Name = "get_session"), Description("Returns current session id")] - public async Task GetSession() + public async Task GetSession(IMcpServer mcpServer) { - return _mcpServer.SessionId; + return mcpServer.SessionId; } }