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
@@ -0,0 +1,25 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using TelegramSearchBot.LLMAgent;
using TelegramSearchBot.Service.AI.LLM;
using Xunit;

namespace TelegramSearchBot.LLM.Test.Service.AI.LLM {
public class AgentProcessToolRegistrationTests {
[Fact]
public void BuildServices_RegistersResponsesApiExecutor() {
using var provider = BuildAgentServices();

Assert.NotNull(provider.GetService<OpenAIResponsesService>());
}

private static ServiceProvider BuildAgentServices() {
var method = typeof(LLMAgentProgram).GetMethod("BuildServices", BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(method);

var provider = method!.Invoke(null, new object[] { 43210 }) as ServiceProvider;
Assert.NotNull(provider);
return provider!;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using TelegramSearchBot.Model;
using TelegramSearchBot.Model.AI;
using TelegramSearchBot.Service.AI.LLM;
using Xunit;

namespace TelegramSearchBot.LLM.Test.Service.AI.LLM {
public class McpToolHelperProxyTests {
[Fact]
public async Task ExecuteRegisteredToolAsync_ProxyTool_ForwardsToolContextMetadata() {
var toolName = $"proxy_context_test_{Guid.NewGuid():N}";
Dictionary<string, string>? forwardedArguments = null;

McpToolHelper.RegisterProxyTools([
new ProxyToolDefinition {
Name = toolName,
Description = "Test proxy tool.",
Parameters = [
new ProxyToolParameter {
Name = "query",
Type = "string",
Description = "Query text.",
Required = true
}
]
}
], (_, arguments) => {
forwardedArguments = new Dictionary<string, string>(arguments, StringComparer.OrdinalIgnoreCase);
return Task.FromResult("ok");
});

var result = await McpToolHelper.ExecuteRegisteredToolAsync(
toolName,
new Dictionary<string, string> {
["query"] = "telegram search"
},
new ToolContext {
ChatId = -100123,
UserId = 456,
MessageId = 789
});

Assert.Equal("ok", result);
Assert.NotNull(forwardedArguments);
Assert.Equal("telegram search", forwardedArguments!["query"]);
Assert.Equal("-100123", forwardedArguments["__chatId"]);
Assert.Equal("456", forwardedArguments["__userId"]);
Assert.Equal("789", forwardedArguments["__messageId"]);
}
}
}
9 changes: 8 additions & 1 deletion TelegramSearchBot.LLM/Service/AI/LLM/McpToolHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,14 @@ public static async Task<object> ExecuteRegisteredToolAsync(string toolName, Dic

// Check if this is a proxy tool (routed to remote process via IPC)
if (ProxyToolRegistry.ContainsKey(toolName) && _proxyToolExecutor != null) {
var proxyResult = await _proxyToolExecutor(toolName, stringArguments);
var proxyArguments = new Dictionary<string, string>(stringArguments, StringComparer.OrdinalIgnoreCase);
if (toolContext != null) {
proxyArguments["__chatId"] = toolContext.ChatId.ToString(CultureInfo.InvariantCulture);
proxyArguments["__userId"] = toolContext.UserId.ToString(CultureInfo.InvariantCulture);
proxyArguments["__messageId"] = toolContext.MessageId.ToString(CultureInfo.InvariantCulture);
}

var proxyResult = await _proxyToolExecutor(toolName, proxyArguments);
return proxyResult;
}

Expand Down
2 changes: 1 addition & 1 deletion TelegramSearchBot.LLMAgent/LLMAgentProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static async Task RunAsync(string[] args) {
var logger = services.GetRequiredService<ILoggerFactory>().CreateLogger("LLMAgent");
McpToolHelper.EnsureInitialized(
typeof(Service.AgentToolService).Assembly,
typeof(FileToolService).Assembly,
services, logger);

// Import tool definitions from Redis and register as proxy tools
Expand Down Expand Up @@ -104,6 +103,7 @@ private static ServiceProvider BuildServices(int port) {
services.AddSingleton<IConnectionMultiplexer>(_ => ConnectionMultiplexer.Connect($"localhost:{port},abortConnect=false,connectTimeout=5000,connectRetry=5"));
services.AddScoped<IMessageExtensionService, Service.InMemoryMessageExtensionService>();
services.AddScoped<OpenAIService>();
services.AddScoped<OpenAIResponsesService>();
services.AddScoped<OllamaService>();
services.AddScoped<GeminiService>();
services.AddScoped<AnthropicService>();
Expand Down
Loading