Server instructions added to IMcpClient (#17)

This commit is contained in:
Peder Holdgaard Pedersen 2025-01-24 20:36:05 +01:00 • committed by GitHub
commit 86f1edfe38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 58 additions and 6 deletions

View file

@ -22,7 +22,7 @@ This library aims to provide a clean, specification-compliant implementation of
- Support for stdio and SSE transports - Support for stdio and SSE transports
- Support for all MCP capabilities: Tool, Resource, Prompt, Sampling, Roots - Support for all MCP capabilities: Tool, Resource, Prompt, Sampling, Roots
- Support for the Completion utility capability - Support for the Completion utility capability
- Support for pagination and notifications - Support for server instructions, pagination and notifications
- Async/await pattern throughout - Async/await pattern throughout
- Comprehensive logging support - Comprehensive logging support
- Compatible with .NET 8.0 and later - Compatible with .NET 8.0 and later

View file

@ -69,8 +69,16 @@ internal class Program
Model = AnthropicModels.Claude35Haiku, Model = AnthropicModels.Claude35Haiku,
Stream = false, Stream = false,
Temperature = 1.0m, Temperature = 1.0m,
Tools = anthropicTools Tools = anthropicTools,
System = [new SystemMessage("You will be helping the user test MCP server tool call functionality.")]
}; };
// If the server provides instructions, add them as the system prompt
if (!string.IsNullOrEmpty(client.ServerInstructions))
{
parameters.System.Add(new SystemMessage(client.ServerInstructions));
}
var res = await antClient.Messages.GetClaudeMessageAsync(parameters); var res = await antClient.Messages.GetClaudeMessageAsync(parameters);
messages.Add(res.Message); messages.Add(res.Message);

View file

@ -8,9 +8,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="mcpdotnet" Version="0.6.0.1" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.1.0-preview.1.25064.3" /> <PackageReference Include="Microsoft.Extensions.AI" Version="9.1.0-preview.1.25064.3" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.25064.3" /> <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.1.0-preview.1.25064.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\mcpdotnet\mcpdotnet.csproj" />
</ItemGroup>
</Project> </Project>

View file

@ -3,6 +3,7 @@ using McpDotNet.Configuration;
using Microsoft.Extensions.AI; using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using OpenAI; using OpenAI;
using OpenAI.Chat;
using SimpleToolsConsole; using SimpleToolsConsole;
internal class Program internal class Program
@ -64,9 +65,22 @@ internal class Program
.UseFunctionInvocation() .UseFunctionInvocation()
.Build(); .Build();
// Create message list
IList<Microsoft.Extensions.AI.ChatMessage> messages = new List<Microsoft.Extensions.AI.ChatMessage>();
// Add a system message
messages.Add(new(ChatRole.System, "You are a helpful assistant, helping us test MCP server functionality."));
// If MCP server provides instructions, add them as an additional system message (you could also add it as a content part)
if (!string.IsNullOrEmpty(client.ServerInstructions))
{
messages.Add(new(ChatRole.System, client.ServerInstructions));
}
// Add a user message
messages.Add(new(ChatRole.User, "Please call the echo tool with the string 'Hello MCP!' and give me the response as-is."));
// 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.CompleteStreamingAsync(
"Please call the echo tool with the string 'Hello MCP!' and give me the response as-is.", messages,
new() { Tools = mappedTools }); new() { Tools = mappedTools });
await foreach (var update in response) await foreach (var update in response)

View file

@ -25,6 +25,13 @@ public interface IMcpClient : IAsyncDisposable
/// </summary> /// </summary>
Implementation? ServerInfo { get; } Implementation? ServerInfo { get; }
/// <summary>
/// Instructions describing how to use the server and its features.
/// This can be used by clients to improve the LLM's understanding of available tools, resources, etc.
/// It can be thought of like a "hint" to the model. For example, this information MAY be added to the system prompt.
/// </summary>
string? ServerInstructions { get; }
/// <summary> /// <summary>
/// Gets or sets the handler for server sampling requests. /// Gets or sets the handler for server sampling requests.
/// </summary> /// </summary>

View file

@ -37,6 +37,9 @@ internal class McpClient : IMcpClient
/// <inheritdoc/> /// <inheritdoc/>
public Implementation? ServerInfo { get; private set; } public Implementation? ServerInfo { get; private set; }
/// <inheritdoc/>
public string? ServerInstructions { get; private set; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="McpClient"/> class. /// Initializes a new instance of the <see cref="McpClient"/> class.
/// </summary> /// </summary>
@ -154,6 +157,7 @@ internal class McpClient : IMcpClient
_logger.ServerCapabilitiesReceived(_serverConfig.Id, _serverConfig.Name, JsonSerializer.Serialize(initializeResponse.Capabilities), JsonSerializer.Serialize(initializeResponse.ServerInfo)); _logger.ServerCapabilitiesReceived(_serverConfig.Id, _serverConfig.Name, JsonSerializer.Serialize(initializeResponse.Capabilities), JsonSerializer.Serialize(initializeResponse.ServerInfo));
ServerCapabilities = initializeResponse.Capabilities; ServerCapabilities = initializeResponse.Capabilities;
ServerInfo = initializeResponse.ServerInfo; ServerInfo = initializeResponse.ServerInfo;
ServerInstructions = initializeResponse.Instructions;
// Validate protocol version // Validate protocol version
if (initializeResponse.ProtocolVersion != _options.ProtocolVersion) if (initializeResponse.ProtocolVersion != _options.ProtocolVersion)

View file

@ -9,7 +9,7 @@
<PropertyGroup> <PropertyGroup>
<!-- license and package properties --> <!-- license and package properties -->
<PackageId>mcpdotnet</PackageId> <PackageId>mcpdotnet</PackageId>
<Version>0.8.0.1</Version> <Version>0.9.0.1</Version>
<Authors>PederHP</Authors> <Authors>PederHP</Authors>
<Description>.NET client library for the Model Context Protocol (MCP)</Description> <Description>.NET client library for the Model Context Protocol (MCP)</Description>
<PackageProjectUrl>https://github.com/PederHP/mcpdotnet</PackageProjectUrl> <PackageProjectUrl>https://github.com/PederHP/mcpdotnet</PackageProjectUrl>

View file

@ -27,6 +27,22 @@ public class IntegrationTests : IClassFixture<IntegrationTestFixture>
Assert.NotNull(client); Assert.NotNull(client);
} }
[Fact]
public async Task Connect_EverythingServer_ShouldProvideServerFields()
{
// Arrange
// Act
var client = await _fixture.Factory.GetClientAsync("everything");
// Assert
Assert.NotNull(client.ServerCapabilities);
Assert.NotNull(client.ServerInfo);
// Note: Comment the below assertion back when the everything server is updated to provide instructions
// Assert.NotNull(client.ServerInstructions);
}
[Fact] [Fact]
public async Task ListTools_Stdio_EverythingServer() public async Task ListTools_Stdio_EverythingServer()
{ {