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
3 changed files with 58 additions and 6 deletions

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)