Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if !NET9_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using HotChocolate.Adapters.Mcp.Directives;
using HotChocolate.Adapters.Mcp.Storage;
using HotChocolate.Execution.Configuration;
Expand Down Expand Up @@ -29,17 +27,24 @@ public static IRequestExecutorBuilder AddMcp(

builder.AddDirectiveType<McpToolAnnotationsDirectiveType>();

builder.ConfigureOnRequestExecutorCreatedAsync(
async (executor, cancellationToken) =>
{
var schema = executor.Schema;
var storageObserver = schema.Services.GetRequiredService<McpStorageObserver>();
await storageObserver.StartAsync(cancellationToken);
});

return builder;
}

public static IRequestExecutorBuilder AddMcpStorage<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, T> factory,
Func<IServiceProvider, bool>? skipIf = null)
where T : class, IMcpStorage
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(factory);

builder.ConfigureSchemaServices(s => s.AddSingleton<IMcpStorage, T>(factory));

return builder.AddMcpStorageWarmupTask(skipIf);
}

public static IRequestExecutorBuilder AddMcpStorage(
this IRequestExecutorBuilder builder,
IMcpStorage storage)
Expand All @@ -49,6 +54,20 @@ public static IRequestExecutorBuilder AddMcpStorage(

builder.ConfigureSchemaServices(s => s.AddSingleton(storage));

return builder.AddMcpStorageWarmupTask();
}

private static IRequestExecutorBuilder AddMcpStorageWarmupTask(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, bool>? skipIf = null)
{
builder.AddWarmupTask(async (executor, cancellationToken) =>
{
var schema = executor.Schema;
var storageObserver = schema.Services.GetRequiredService<McpStorageObserver>();
await storageObserver.StartAsync(cancellationToken);
}, skipIf);

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\AspNetCore\src\AspNetCore\HotChocolate.AspNetCore.csproj" />
<ProjectReference Include="..\..\..\Core\src\Types\HotChocolate.Types.csproj" />
<ProjectReference Include="..\Adapters.Mcp.Core\HotChocolate.Adapters.Mcp.Core.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if !NET9_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using HotChocolate.Adapters.Mcp.Storage;
using HotChocolate.Fusion.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,16 +24,24 @@ public static IFusionGatewayBuilder AddMcp(
builder.ConfigureSchemaServices(
(_, services) => services.AddMcpSchemaServices(configureServerOptions, configureServer));

builder.AddWarmupTask(async (executor, cancellationToken) =>
{
var schema = executor.Schema;
var storageObserver = schema.Services.GetRequiredService<McpStorageObserver>();
await storageObserver.StartAsync(cancellationToken);
});

return builder;
}

public static IFusionGatewayBuilder AddMcpStorage<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(
this IFusionGatewayBuilder builder,
Func<IServiceProvider, T> factory,
Func<IServiceProvider, bool>? skipIf = null)
where T : class, IMcpStorage
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(factory);

builder.ConfigureSchemaServices((_, s) => s.AddSingleton<IMcpStorage, T>(factory));

return builder.AddMcpStorageWarmupTask(skipIf);
}

public static IFusionGatewayBuilder AddMcpStorage(
this IFusionGatewayBuilder builder,
IMcpStorage storage)
Expand All @@ -45,6 +51,20 @@ public static IFusionGatewayBuilder AddMcpStorage(

builder.ConfigureSchemaServices((_, s) => s.AddSingleton(storage));

return builder.AddMcpStorageWarmupTask();
}

private static IFusionGatewayBuilder AddMcpStorageWarmupTask(
this IFusionGatewayBuilder builder,
Func<IServiceProvider, bool>? skipIf = null)
{
builder.AddWarmupTask(async (executor, cancellationToken) =>
{
var schema = executor.Schema;
var storageObserver = schema.Services.GetRequiredService<McpStorageObserver>();
await storageObserver.StartAsync(cancellationToken);
}, skipIf);

return builder;
}
}
Loading