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 all MCP capabilities: Tool, Resource, Prompt, Sampling, Roots
- Support for the Completion utility capability
- Support for pagination and notifications
- Support for server instructions, pagination and notifications
- Async/await pattern throughout
- Comprehensive logging support
- Compatible with .NET 8.0 and later

View file

@ -69,8 +69,16 @@ internal class Program
Model = AnthropicModels.Claude35Haiku,
Stream = false,
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);
messages.Add(res.Message);

View file

@ -8,9 +8,12 @@
</PropertyGroup>
<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.OpenAI" Version="9.1.0-preview.1.25064.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\mcpdotnet\mcpdotnet.csproj" />
</ItemGroup>
</Project>

View file

@ -3,6 +3,7 @@ using McpDotNet.Configuration;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Logging.Abstractions;
using OpenAI;
using OpenAI.Chat;
using SimpleToolsConsole;
internal class Program
@ -63,10 +64,23 @@ internal class Program
IChatClient chatClient = new ChatClientBuilder(openaiClient)
.UseFunctionInvocation()
.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...");
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 });
await foreach (var update in response)

View file

@ -25,6 +25,13 @@ public interface IMcpClient : IAsyncDisposable
/// </summary>
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>
/// Gets or sets the handler for server sampling requests.
/// </summary>

View file

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

View file

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

View file

@ -27,6 +27,22 @@ public class IntegrationTests : IClassFixture<IntegrationTestFixture>
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]
public async Task ListTools_Stdio_EverythingServer()
{