Put MapMcp in Microsoft.AspNetCore.Builder namespace (#171)
* Still dispose McpSession when MessageProcessingTask throws non-OCE exceptions * Put MapMcp in icrosoft.AspNetCore.Builder namespace
This commit is contained in:
parent
065e3c6dbb
commit
57a526ec30
5 changed files with 21 additions and 21 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
using ModelContextProtocol.AspNetCore;
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.AddMcpServer().WithToolsFromAssembly();
|
builder.Services.AddMcpServer().WithToolsFromAssembly();
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http;
|
|
||||||
using Microsoft.AspNetCore.Routing;
|
using Microsoft.AspNetCore.Routing;
|
||||||
using Microsoft.AspNetCore.WebUtilities;
|
using Microsoft.AspNetCore.WebUtilities;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -12,7 +11,7 @@ using ModelContextProtocol.Utils.Json;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace ModelContextProtocol.AspNetCore;
|
namespace Microsoft.AspNetCore.Builder;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extension methods for <see cref="IEndpointRouteBuilder"/> to add MCP endpoints.
|
/// Extension methods for <see cref="IEndpointRouteBuilder"/> to add MCP endpoints.
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ dotnet add package ModelContextProtocol.AspNetCore --prerelease
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// Program.cs
|
// Program.cs
|
||||||
using ModelContextProtocol.AspNetCore;
|
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,25 +94,30 @@ internal abstract class McpJsonRpcEndpoint : IAsyncDisposable
|
||||||
{
|
{
|
||||||
_logger.CleaningUpEndpoint(EndpointName);
|
_logger.CleaningUpEndpoint(EndpointName);
|
||||||
|
|
||||||
if (_sessionCts is not null)
|
try
|
||||||
{
|
{
|
||||||
await _sessionCts.CancelAsync().ConfigureAwait(false);
|
if (_sessionCts is not null)
|
||||||
}
|
|
||||||
|
|
||||||
if (MessageProcessingTask is not null)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
await MessageProcessingTask.ConfigureAwait(false);
|
await _sessionCts.CancelAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
|
||||||
|
if (MessageProcessingTask is not null)
|
||||||
{
|
{
|
||||||
// Ignore cancellation
|
try
|
||||||
|
{
|
||||||
|
await MessageProcessingTask.ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
// Ignore cancellation
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
_session?.Dispose();
|
{
|
||||||
_sessionCts?.Dispose();
|
_session?.Dispose();
|
||||||
|
_sessionCts?.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
_logger.EndpointCleanedUp(EndpointName);
|
_logger.EndpointCleanedUp(EndpointName);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using ModelContextProtocol.AspNetCore;
|
using ModelContextProtocol.Protocol.Types;
|
||||||
using ModelContextProtocol.Protocol.Types;
|
|
||||||
using ModelContextProtocol.Server;
|
using ModelContextProtocol.Server;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue