Update to M.E.AI 9.3.0-preview.1.25161.3 (#61)
This commit is contained in:
parent
24c706b4ff
commit
fe632617dc
9 changed files with 20 additions and 142 deletions
|
|
@ -8,11 +8,12 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.AI" Version="9.1.0-preview.1.25064.3" />
|
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25161.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.25064.3" />
|
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25161.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\..\..\src\McpDotNet.Extensions.AI\McpDotNet.Extensions.AI.csproj" />
|
||||||
<ProjectReference Include="..\..\..\..\src\mcpdotnet\mcpdotnet.csproj" />
|
<ProjectReference Include="..\..\..\..\src\mcpdotnet\mcpdotnet.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
using McpDotNet.Client;
|
|
||||||
using McpDotNet.Protocol.Types;
|
|
||||||
using Microsoft.Extensions.AI;
|
|
||||||
|
|
||||||
namespace SimpleToolsConsole;
|
|
||||||
|
|
||||||
public class McpAIFunction : AIFunction
|
|
||||||
{
|
|
||||||
private readonly Tool _tool;
|
|
||||||
private readonly IMcpClient _client;
|
|
||||||
private readonly AIFunctionMetadata _metadata;
|
|
||||||
|
|
||||||
public McpAIFunction(Tool tool, IMcpClient client)
|
|
||||||
{
|
|
||||||
_tool = tool ?? throw new ArgumentNullException(nameof(tool));
|
|
||||||
_client = client ?? throw new ArgumentNullException(nameof(client));
|
|
||||||
_metadata = new AIFunctionMetadata(tool.Name)
|
|
||||||
{
|
|
||||||
Description = tool.Description,
|
|
||||||
Parameters = MapFunctionParameters(tool.InputSchema),
|
|
||||||
ReturnParameter = new AIFunctionReturnParameterMetadata()
|
|
||||||
{
|
|
||||||
ParameterType = typeof(string),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => _tool.Name;
|
|
||||||
|
|
||||||
protected async override Task<object?> InvokeCoreAsync(IEnumerable<KeyValuePair<string, object?>> arguments, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
if (arguments is null)
|
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(arguments));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert arguments to dictionary format expected by mcpdotnet
|
|
||||||
Dictionary<string, object> argDict = [];
|
|
||||||
foreach (var arg in arguments)
|
|
||||||
{
|
|
||||||
if (arg.Value is not null)
|
|
||||||
{
|
|
||||||
argDict[arg.Key] = arg.Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call the tool through mcpdotnet
|
|
||||||
var result = await _client.CallToolAsync(
|
|
||||||
_tool.Name,
|
|
||||||
argDict.Count == 0 ? [] : argDict,
|
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
// Extract the text content from the result
|
|
||||||
// For simplicity in this sample, we'll just concatenate all text content
|
|
||||||
return string.Join("\n", result.Content
|
|
||||||
.Where(c => c.Type == "text")
|
|
||||||
.Select(c => c.Text));
|
|
||||||
}
|
|
||||||
|
|
||||||
public override AIFunctionMetadata Metadata => _metadata;
|
|
||||||
|
|
||||||
private static List<AIFunctionParameterMetadata> MapFunctionParameters(JsonSchema? jsonSchema)
|
|
||||||
{
|
|
||||||
var properties = jsonSchema?.Properties;
|
|
||||||
if (properties == null)
|
|
||||||
{
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
HashSet<string> requiredProperties = [.. jsonSchema!.Required ?? []];
|
|
||||||
return properties.Select(kvp =>
|
|
||||||
new AIFunctionParameterMetadata(kvp.Key)
|
|
||||||
{
|
|
||||||
Description = kvp.Value.Description,
|
|
||||||
ParameterType = McpTypeMapper.MapJsonToDotNetType(kvp.Value.Type, requiredProperties.Contains(kvp.Key)),
|
|
||||||
IsRequired = requiredProperties.Contains(kvp.Key)
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
using McpDotNet.Client;
|
|
||||||
using McpDotNet.Protocol.Types;
|
|
||||||
using Microsoft.Extensions.AI;
|
|
||||||
|
|
||||||
namespace SimpleToolsConsole;
|
|
||||||
|
|
||||||
public static class McpToolExtensions
|
|
||||||
{
|
|
||||||
public static AITool ToAITool(this Tool tool, IMcpClient client)
|
|
||||||
=> new McpAIFunction(tool, client);
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
// Enable nullable reference types, as we need this for dynamic mapping of JSON schema to .NET types
|
|
||||||
#nullable enable
|
|
||||||
|
|
||||||
namespace SimpleToolsConsole;
|
|
||||||
|
|
||||||
public static class McpTypeMapper
|
|
||||||
{
|
|
||||||
public static Type MapJsonToDotNetType(string jsonType, bool required)
|
|
||||||
{
|
|
||||||
// For simplicity, we don't handle complex types in this sample, but use object instead
|
|
||||||
var baseType = jsonType switch
|
|
||||||
{
|
|
||||||
"string" => typeof(string),
|
|
||||||
"integer" => typeof(int),
|
|
||||||
"number" => typeof(double),
|
|
||||||
"boolean" => typeof(bool),
|
|
||||||
"array" => typeof(IEnumerable<object>),
|
|
||||||
"object" => typeof(IDictionary<string, object>),
|
|
||||||
_ => typeof(object)
|
|
||||||
};
|
|
||||||
|
|
||||||
// If it's a value type and not required, make it nullable
|
|
||||||
if (!required && baseType.IsValueType)
|
|
||||||
{
|
|
||||||
return typeof(Nullable<>).MakeGenericType(baseType);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reference types are already nullable when not required
|
|
||||||
return baseType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
using McpDotNet.Client;
|
using McpDotNet.Client;
|
||||||
using McpDotNet.Configuration;
|
using McpDotNet.Configuration;
|
||||||
|
using McpDotNet.Extensions.AI;
|
||||||
using McpDotNet.Protocol.Transport;
|
using McpDotNet.Protocol.Transport;
|
||||||
using Microsoft.Extensions.AI;
|
using Microsoft.Extensions.AI;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using OpenAI;
|
using OpenAI;
|
||||||
using SimpleToolsConsole;
|
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
|
|
@ -81,7 +81,7 @@ internal class Program
|
||||||
|
|
||||||
// Call the chat client
|
// Call the chat client
|
||||||
Console.WriteLine("Asking GPT-4o-mini to call the Echo Tool...");
|
Console.WriteLine("Asking GPT-4o-mini to call the Echo Tool...");
|
||||||
var response = chatClient.CompleteStreamingAsync(
|
var response = chatClient.GetStreamingResponseAsync(
|
||||||
messages,
|
messages,
|
||||||
new() { Tools = mappedTools });
|
new() { Tools = mappedTools });
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,12 +40,12 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\mcpdotnet\mcpdotnet.csproj" />
|
<ProjectReference Include="..\mcpdotnet\mcpdotnet.csproj" />
|
||||||
|
|
||||||
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25114.11" />
|
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.3.0-preview.1.25161.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
|
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25161.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
<PackageReference Include="System.Text.Json" Version="9.0.2" />
|
<PackageReference Include="System.Text.Json" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -38,16 +38,16 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.3" />
|
||||||
<PackageReference Include="System.Net.ServerSentEvents" Version="9.0.2" />
|
<PackageReference Include="System.Net.ServerSentEvents" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
<Compile Include="..\Common\Polyfills\**\*.cs" />
|
<Compile Include="..\Common\Polyfills\**\*.cs" />
|
||||||
<PackageReference Include="Microsoft.Bcl.Memory" Version="9.0.2" />
|
<PackageReference Include="Microsoft.Bcl.Memory" Version="9.0.3" />
|
||||||
<PackageReference Include="System.Text.Json" Version="9.0.2" />
|
<PackageReference Include="System.Text.Json" Version="9.0.3" />
|
||||||
<PackageReference Include="System.Threading.Channels" Version="9.0.2" />
|
<PackageReference Include="System.Threading.Channels" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ namespace McpDotNet.Extensions.AI.Tests;
|
||||||
[Trait("Execution", "Manual")]
|
[Trait("Execution", "Manual")]
|
||||||
public class IntegrationTests
|
public class IntegrationTests
|
||||||
{
|
{
|
||||||
private const string OpenAIKey = ""; // Provide your own key when running the test. Do not commit it.
|
private string _openAIKey = Environment.GetEnvironmentVariable("Provide your own key when running the test. Do not commit it.")!;
|
||||||
|
|
||||||
private static McpServerConfig GetEverythingServerConfig()
|
private static McpServerConfig GetEverythingServerConfig()
|
||||||
{
|
{
|
||||||
|
|
@ -51,7 +51,7 @@ public class IntegrationTests
|
||||||
var tools = await client.ListToolsAsync();
|
var tools = await client.ListToolsAsync();
|
||||||
var mappedTools = tools.Tools.Select(t => t.ToAITool(client)).ToList();
|
var mappedTools = tools.Tools.Select(t => t.ToAITool(client)).ToList();
|
||||||
|
|
||||||
IChatClient openaiClient = new OpenAIClient(OpenAIKey)
|
IChatClient openaiClient = new OpenAIClient(_openAIKey)
|
||||||
.AsChatClient("gpt-4o-mini");
|
.AsChatClient("gpt-4o-mini");
|
||||||
|
|
||||||
IChatClient chatClient = new ChatClientBuilder(openaiClient)
|
IChatClient chatClient = new ChatClientBuilder(openaiClient)
|
||||||
|
|
@ -79,8 +79,7 @@ public class IntegrationTests
|
||||||
new() { Tools = mappedTools, Temperature = 0 });
|
new() { Tools = mappedTools, Temperature = 0 });
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(4, messages.Count); // 1 system messages, 1 user message, 1 tool message, 1 response message
|
Assert.Equal("Echo: Hello MCP!", response.Text);
|
||||||
Assert.Equal("Echo: Hello MCP!", response.Message.Contents[0].ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -88,7 +87,7 @@ public class IntegrationTests
|
||||||
{
|
{
|
||||||
await using var sessionScope = await McpSessionScope.CreateAsync(GetEverythingServerConfig());
|
await using var sessionScope = await McpSessionScope.CreateAsync(GetEverythingServerConfig());
|
||||||
|
|
||||||
IChatClient openaiClient = new OpenAIClient(OpenAIKey)
|
IChatClient openaiClient = new OpenAIClient(_openAIKey)
|
||||||
.AsChatClient("gpt-4o-mini");
|
.AsChatClient("gpt-4o-mini");
|
||||||
|
|
||||||
IChatClient chatClient = new ChatClientBuilder(openaiClient)
|
IChatClient chatClient = new ChatClientBuilder(openaiClient)
|
||||||
|
|
@ -116,7 +115,6 @@ public class IntegrationTests
|
||||||
new() { Tools = sessionScope.Tools, Temperature = 0 });
|
new() { Tools = sessionScope.Tools, Temperature = 0 });
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(4, messages.Count); // 1 system messages, 1 user message, 1 tool message, 1 response message
|
Assert.Equal("Echo: Hello MCP!", response.Text);
|
||||||
Assert.Equal("Echo: Hello MCP!", response.Message.Contents[0].ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\src\McpDotNet.Extensions.AI\McpDotNet.Extensions.AI.csproj" />
|
<ProjectReference Include="..\..\src\McpDotNet.Extensions.AI\McpDotNet.Extensions.AI.csproj" />
|
||||||
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25114.11" />
|
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25161.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue