-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpGatewayToolSet.cs
More file actions
264 lines (228 loc) · 9.1 KB
/
McpGatewayToolSet.cs
File metadata and controls
264 lines (228 loc) · 9.1 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
using ManagedCode.MCPGateway.Abstractions;
using Microsoft.Extensions.AI;
namespace ManagedCode.MCPGateway;
public sealed class McpGatewayToolSet(IMcpGateway gateway)
{
public const string DefaultSearchToolName = "gateway_tools_search";
public const string DefaultInvokeToolName = "gateway_tool_invoke";
public const string DiscoveredToolIdPropertyName = "ManagedCode.MCPGateway.ToolId";
public const string DiscoveredToolSourceIdPropertyName = "ManagedCode.MCPGateway.SourceId";
public const string DiscoveredToolKindPropertyName = "ManagedCode.MCPGateway.Kind";
public const string SearchToolDescription = "Search the gateway catalog and return the best matching tools for a user task.";
public const string InvokeToolDescription = "Invoke a gateway tool by tool id. Search first when the correct tool is unknown.";
private const string DiscoveredToolKindValue = "gateway_discovered_tool";
private const string DiscoveredToolNameSeparator = "_";
private const string DiscoveredToolDescriptionPrefix = "Direct proxy for gateway tool ";
private const string DiscoveredToolIdLabel = " (";
private const string DiscoveredToolDescriptionSeparator = "). ";
private const string DiscoveredToolRequiredArgumentsLabel = "Required arguments: ";
private const string DiscoveredToolArgumentsHint = "Pass named inputs via 'arguments' and use 'query' for free-text tool inputs when supported.";
public IReadOnlyList<AITool> CreateTools(
string searchToolName = DefaultSearchToolName,
string invokeToolName = DefaultInvokeToolName)
{
var searchTool = AIFunctionFactory.Create(
SearchAsync,
new AIFunctionFactoryOptions
{
Name = searchToolName,
Description = SearchToolDescription
});
var invokeTool = AIFunctionFactory.Create(
InvokeAsync,
new AIFunctionFactoryOptions
{
Name = invokeToolName,
Description = InvokeToolDescription
});
return [searchTool, invokeTool];
}
public IList<AITool> AddTools(
IList<AITool> tools,
string searchToolName = DefaultSearchToolName,
string invokeToolName = DefaultInvokeToolName)
{
ArgumentNullException.ThrowIfNull(tools);
var targetTools = new List<AITool>(tools);
var toolNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var tool in targetTools)
{
toolNames.Add(tool.Name);
}
foreach (var tool in CreateTools(searchToolName, invokeToolName))
{
if (toolNames.Add(tool.Name))
{
targetTools.Add(tool);
}
}
return targetTools;
}
public IReadOnlyList<AITool> CreateDiscoveredTools(
IEnumerable<McpGatewaySearchMatch> matches,
IReadOnlyCollection<string>? reservedToolNames = null,
int? maxTools = null)
{
ArgumentNullException.ThrowIfNull(matches);
var toolLimit = maxTools.GetValueOrDefault(int.MaxValue);
if (toolLimit <= 0)
{
return [];
}
var discoveredTools = new List<AITool>();
var reservedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (reservedToolNames is not null)
{
foreach (var reservedToolName in reservedToolNames)
{
if (!string.IsNullOrWhiteSpace(reservedToolName))
{
reservedNames.Add(reservedToolName);
}
}
}
foreach (var match in matches)
{
if (discoveredTools.Count == toolLimit)
{
break;
}
var functionName = CreateDiscoveredToolName(match, reservedNames);
discoveredTools.Add(CreateDiscoveredTool(match, functionName));
}
return discoveredTools;
}
public Task<McpGatewaySearchResult> SearchAsync(
string query,
int? maxResults = null,
Dictionary<string, object?>? context = null,
string? contextSummary = null,
CancellationToken cancellationToken = default)
=> gateway.SearchAsync(
new McpGatewaySearchRequest(
Query: query,
MaxResults: maxResults,
Context: context,
ContextSummary: contextSummary),
cancellationToken);
public Task<McpGatewayInvokeResult> InvokeAsync(
string toolId,
Dictionary<string, object?>? arguments = null,
string? query = null,
Dictionary<string, object?>? context = null,
string? contextSummary = null,
CancellationToken cancellationToken = default)
=> gateway.InvokeAsync(
new McpGatewayInvokeRequest(
ToolId: toolId,
Arguments: arguments,
Query: query,
Context: context,
ContextSummary: contextSummary),
cancellationToken);
private AITool CreateDiscoveredTool(
McpGatewaySearchMatch match,
string functionName)
{
Task<McpGatewayInvokeResult> InvokeDiscoveredToolAsync(
Dictionary<string, object?>? arguments = null,
string? query = null,
Dictionary<string, object?>? context = null,
string? contextSummary = null,
CancellationToken cancellationToken = default)
=> gateway.InvokeAsync(
new McpGatewayInvokeRequest(
ToolId: match.ToolId,
Arguments: arguments,
Query: query,
Context: context,
ContextSummary: contextSummary),
cancellationToken);
return AIFunctionFactory.Create(
(Func<Dictionary<string, object?>?, string?, Dictionary<string, object?>?, string?, CancellationToken, Task<McpGatewayInvokeResult>>)InvokeDiscoveredToolAsync,
new AIFunctionFactoryOptions
{
Name = functionName,
Description = BuildDiscoveredToolDescription(match),
AdditionalProperties = new Dictionary<string, object?>
{
[DiscoveredToolIdPropertyName] = match.ToolId,
[DiscoveredToolSourceIdPropertyName] = match.SourceId,
[DiscoveredToolKindPropertyName] = DiscoveredToolKindValue
}
});
}
private static string BuildDiscoveredToolDescription(McpGatewaySearchMatch match)
{
var description = $"{DiscoveredToolDescriptionPrefix}{match.ToolName}{DiscoveredToolIdLabel}{match.ToolId}{DiscoveredToolDescriptionSeparator}{match.Description}";
if (match.RequiredArguments.Count == 0)
{
return $"{description} {DiscoveredToolArgumentsHint}";
}
return $"{description} {DiscoveredToolRequiredArgumentsLabel}{BuildRequiredArgumentList(match.RequiredArguments)}. {DiscoveredToolArgumentsHint}";
}
private static string BuildRequiredArgumentList(IReadOnlyList<string> requiredArguments)
{
if (requiredArguments.Count == 1)
{
return requiredArguments[0];
}
var builder = new System.Text.StringBuilder();
for (var index = 0; index < requiredArguments.Count; index++)
{
if (index > 0)
{
builder.Append(", ");
}
builder.Append(requiredArguments[index]);
}
return builder.ToString();
}
private static string CreateDiscoveredToolName(
McpGatewaySearchMatch match,
ISet<string> reservedNames)
{
ArgumentNullException.ThrowIfNull(match);
ArgumentNullException.ThrowIfNull(reservedNames);
var sanitizedToolName = SanitizeToolName(match.ToolName);
if (reservedNames.Add(sanitizedToolName))
{
return sanitizedToolName;
}
var sanitizedSourceId = SanitizeToolName(match.SourceId);
var compositeName = $"{sanitizedSourceId}{DiscoveredToolNameSeparator}{sanitizedToolName}";
if (reservedNames.Add(compositeName))
{
return compositeName;
}
for (var suffix = 2; ; suffix++)
{
var uniqueName = $"{compositeName}{DiscoveredToolNameSeparator}{suffix}";
if (reservedNames.Add(uniqueName))
{
return uniqueName;
}
}
}
private static string SanitizeToolName(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return "gateway_tool";
}
var builder = new System.Text.StringBuilder(value.Length);
foreach (var character in value)
{
builder.Append(char.IsLetterOrDigit(character) || character == '_' ? character : '_');
}
if (builder.Length == 0)
{
return "gateway_tool";
}
if (!char.IsLetter(builder[0]) && builder[0] != '_')
{
builder.Insert(0, "t_");
}
return builder.ToString();
}
}