Skip to content

Get session id sample code #564 #565

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ModelContextProtocol.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<File Path=".github/workflows/release.yml" />
</Folder>
<Folder Name="/samples/">
<Project Path="samples/GetSessionId/GetSessionId.csproj" Id="b90422b8-777b-45ce-84d8-5ca8395bd818" />
<Project Path="samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj" />
<Project Path="samples/ChatWithTools/ChatWithTools.csproj" />
<Project Path="samples/EverythingServer/EverythingServer.csproj" />
Expand Down
13 changes: 13 additions & 0 deletions samples/GetSessionId/GetSessionId.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ModelContextProtocol.AspNetCore\ModelContextProtocol.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions samples/GetSessionId/GetSessionId.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@GetSessionId_HostAddress = http://localhost:5002

GET {{GetSessionId_HostAddress}}/weatherforecast/
Accept: application/json

###
16 changes: 16 additions & 0 deletions samples/GetSessionId/GetSessionIdTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ModelContextProtocol.Server;
using System.ComponentModel;

[McpServerToolType]
public class GetSessionIdTool
{
public GetSessionIdTool()
{
}

[McpServerTool(Name = "get_session"), Description("Returns current session id")]
public async Task<string?> GetSession(IMcpServer mcpServer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample seems somewhat artificial to me -- the session id is mostly intended as a key for session state so it would be nice to have an example showcasing that.

{
return mcpServer.SessionId;
}
}
15 changes: 15 additions & 0 deletions samples/GetSessionId/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
14 changes: 14 additions & 0 deletions samples/GetSessionId/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
8 changes: 8 additions & 0 deletions samples/GetSessionId/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions samples/GetSessionId/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}