modelcontextprotocol / modelcontextprotocol/csharp-sdk
Is there a way to run multiple stateful MCP servers?
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 4.5k
- Forks
- 814
- Avg merge
- 9d 19h
- Merged PRs (30d)
- 4
Description
As the title says, is there anyway to operate multiple stateful MCP servers without running into "Session id not found" errors?
It looks like the "session id not found" error stems from caching session ids in an in-memory dictionary in StatefulSessionManager. Is there a way in the API to, may be, swap that with Redis or use AspNetCore's default session management with a shared cache with the MCP server?
Something along the lines of (which is not working at the moment)
class Program
{
static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<SessionSettings>(
builder.Configuration.GetSection(SessionSettings.SectionName));
var sessionConfig = builder.Configuration
.GetSection(SessionSettings.SectionName)
.Get<SessionSettings>() ?? new SessionSettings();
if (sessionConfig.StorageType.Equals("Redis", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(sessionConfig.RedisConnectionString))
throw new InvalidOperationException("Redis connection string is required");
builder.Services.AddSingleton<ISessionStorage>(_ =>
new RedisSessionStorage(sessionConfig.RedisConnectionString));
}
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
var app = builder.Build();
app.UseMiddleware<SessionMiddleware>();
app.MapMcp();
app.Run("http://localhost:3000");
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with StatefulSessionManager and the ISessionStorage, SessionMiddleware, and MapMcp entry points shown in the issue. Trace how session IDs are stored and retrieved across multiple server instances, then determine what shared storage behavior is needed so multiple stateful MCP servers no longer produce "Session id not found" errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, redis
- Domain
- api, backend, distributed-systems
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100