So erfassen und speichern Sie Sitzungs -IDs, wenn Sie den ModelContextProtocol (MCP) -Server mit SSE in .NET verwenden?
Posted: 19 Aug 2025, 16:05
Ich erstelle einen MCP -Server (Modellkontextprotokoll) in .NET Core mit SSE -Kommunikation. Der Server funktioniert -Ich kann eine Sitzungs -ID in der ersten Verbindung herstellen und empfangen. /> Code: < /p>
Wenn ich http: // localhost: 5000/sse (über Postman) klickte, generiert der Server eine Sitzungs -ID wie:
/message? sessionID = b7ty0l9x9qaxis96s7NOPKQ
Problem:
Ich muss diese Sitzungs -ID erfassen, wenn der Client zum ersten Mal eine Verbindung herstellt und in meiner Datenbank speichern.>
Code: Select all
Tool.cs
using Microsoft.Extensions.AI;
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.ComponentModel;
namespace Tools;
[McpServerToolType]
public sealed class GreetingTools
{
[McpServerTool, Description("Says Hello to a user")]
public static string Echo(string username)
{
return "Hello " + username;
}
}
Program.cs
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol;
using ModelContextProtocol.Server;
var builder = WebApplication.CreateBuilder(args);
// Register MCP server and discover tools from the current assembly
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
var app = builder.Build();
// Add MCP middleware
app.MapMcp();
app.Run();
/message? sessionID = b7ty0l9x9qaxis96s7NOPKQ
Problem:
Ich muss diese Sitzungs -ID erfassen, wenn der Client zum ersten Mal eine Verbindung herstellt und in meiner Datenbank speichern.>