Integrate Microsoft.Extensions.AI as a foundational building block (#94)

* Integrate Microsoft.Extensions.AI as a foundational building block

As with logging abstractions coming from M.E.Logging and DI abstractions coming from M.E.DependencyInjection, M.E.AI provides the relevant AI abstractions.

- Adds AsAIFunction and GetAIFunctionsAsync methods for getting functions for tools via a client.
- Uses AIFunctionFactory to implement McpServerBuilderExtensions.WithTools.
- Adds a RequestSamplingAsync overload that accepts ChatMessages/Options.
- Adds an AsSamplingChatClient method that provides an IChatClient usable on the server to request sampling of the client.
- Adds a CreateSamplingHandler method to enable an IChatClient to be easily used to satisfy sampling requests on the client.
- Deprecate McpDotNet.Extensions.AI.

* Address PR feedback
This commit is contained in:
Stephen Toub 2025-03-17 16:34:00 -04:00 • committed by GitHub
commit 4597bbd7c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 801 additions and 927 deletions

View file

@ -39,28 +39,4 @@ public class Content
/// </summary>
[JsonPropertyName("resource")]
public ResourceContents? Resource { get; set; }
/// <summary>
/// Validates the content object.
/// </summary>
/// <returns>Whether the type of tool content has the correct data fields provided.</returns>
public bool Validate()
{
if (Type == "text")
{
return !string.IsNullOrEmpty(Text);
}
else if (Type == "image")
{
return !string.IsNullOrEmpty(Data) && !string.IsNullOrEmpty(MimeType);
}
else if (Type == "resource")
{
return Resource != null;
}
else
{
return false;
}
}
}

View file

@ -21,7 +21,7 @@ public class CreateMessageRequestParams
/// The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("maxTokens")]
public int MaxTokens { get; init; }
public int? MaxTokens { get; init; }
/// <summary>
/// Messages requested by the server to be included in the prompt.

View file

@ -10,7 +10,7 @@ public class JsonSchema
/// The type of the schema, should be "object".
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("type")]
public string Type { get; set; } = string.Empty;
public string Type { get; set; } = "object";
/// <summary>
/// Map of property names to property definitions.

View file

@ -16,5 +16,5 @@ public class JsonSchemaProperty
/// A human-readable description of the property.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("description")]
public string? Description { get; set; }
public string? Description { get; set; } = string.Empty;
}

View file

@ -1,4 +1,6 @@
namespace McpDotNet.Protocol.Types;
using System.Text.Json.Serialization;
namespace McpDotNet.Protocol.Types;
/// <summary>
/// Represents a tool that the server is capable of calling. Part of the ListToolsResponse.
@ -9,18 +11,18 @@ public class Tool
/// <summary>
/// The name of the tool.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("name")]
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
/// <summary>
/// A human-readable description of the tool.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("description")]
[JsonPropertyName("description")]
public string? Description { get; set; }
/// <summary>
/// A JSON Schema object defining the expected parameters for the tool.
/// </summary>
[System.Text.Json.Serialization.JsonPropertyName("inputSchema")]
[JsonPropertyName("inputSchema")]
public JsonSchema? InputSchema { get; set; }
}