-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpGatewayFactory.cs
More file actions
145 lines (128 loc) · 4.51 KB
/
Copy pathMcpGatewayFactory.cs
File metadata and controls
145 lines (128 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System.Runtime.ExceptionServices;
using ManagedCode.MCPGateway.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace ManagedCode.MCPGateway;
public sealed class McpGatewayFactory(
IServiceProvider serviceProvider,
ILoggerFactory loggerFactory
) : IMcpGatewayFactory
{
public IMcpGatewayInstance Create() => Create(new McpGatewayOptions());
public IMcpGatewayInstance Create(Action<McpGatewayOptions> configure)
{
ArgumentNullException.ThrowIfNull(configure);
var options = new McpGatewayOptions();
configure(options);
return Create(options);
}
public IMcpGatewayInstance Create(McpGatewayOptions options)
{
ArgumentNullException.ThrowIfNull(options);
var gatewayOptions = Options.Create(options);
var promptChangeHub = new McpGatewayPromptChangeHub();
var registry = new McpGatewayRegistry(gatewayOptions, promptChangeHub);
var catalogRuntime = (IMcpGatewayCatalogRuntime)registry;
var runtimeServiceProvider = new McpGatewayFactoryServiceProvider(
serviceProvider,
registry
);
var promptCatalog = new McpGatewayPromptCatalog(
registry,
runtimeServiceProvider,
loggerFactory
);
var resourceCatalog = new McpGatewayResourceCatalog(registry, loggerFactory);
var gateway = new McpGateway(
runtimeServiceProvider,
gatewayOptions,
loggerFactory.CreateLogger<McpGateway>(),
loggerFactory
);
return new McpGatewayFactoryResult(
ReleaseAsync,
gateway,
promptCatalog,
resourceCatalog,
registry,
catalogRuntime,
new McpGatewayToolSet(gateway)
);
}
private static async ValueTask ReleaseAsync(
IMcpGateway runtimeGateway,
IMcpGatewayRegistry runtimeRegistry,
IMcpGatewayCatalogRuntime runtimeCatalogRuntime
)
{
var cleanupExceptions = new List<Exception>();
await DisposeAsync(runtimeGateway, cleanupExceptions);
if (
!ReferenceEquals(runtimeCatalogRuntime, runtimeRegistry)
&& runtimeCatalogRuntime is IAsyncDisposable asyncCatalogRuntime
)
{
await DisposeAsync(asyncCatalogRuntime, cleanupExceptions);
}
if (runtimeRegistry is IAsyncDisposable asyncRegistry)
{
await DisposeAsync(asyncRegistry, cleanupExceptions);
}
ThrowIfCleanupFailed(cleanupExceptions);
}
private static async ValueTask DisposeAsync(
IAsyncDisposable disposable,
List<Exception> cleanupExceptions
)
{
try
{
await disposable.DisposeAsync();
}
catch (Exception exception)
{
cleanupExceptions.Add(exception);
}
}
private static void ThrowIfCleanupFailed(List<Exception> cleanupExceptions)
{
switch (cleanupExceptions.Count)
{
case 0:
return;
case 1:
ExceptionDispatchInfo.Capture(cleanupExceptions[0]).Throw();
break;
default:
throw new AggregateException(cleanupExceptions);
}
}
private sealed class McpGatewayFactoryServiceProvider(
IServiceProvider rootServiceProvider,
IMcpGatewayRegistry registry
) : IServiceProvider, IKeyedServiceProvider
{
public object? GetService(Type serviceType)
{
if (
serviceType == typeof(IMcpGatewayCatalogSource)
|| serviceType == typeof(IMcpGatewayRegistry)
|| serviceType == typeof(IMcpGatewayCatalogRuntime)
)
{
return registry;
}
return rootServiceProvider.GetService(serviceType);
}
public object? GetKeyedService(Type serviceType, object? serviceKey) =>
rootServiceProvider is IKeyedServiceProvider keyedServiceProvider
? keyedServiceProvider.GetKeyedService(serviceType, serviceKey)
: null;
public object GetRequiredKeyedService(Type serviceType, object? serviceKey) =>
GetKeyedService(serviceType, serviceKey)
?? throw new InvalidOperationException(
$"No keyed service '{serviceType}' is registered for key '{serviceKey}'."
);
}
}