How to connect to MCP server? #348
-
|
According to the docs I have installed the The docs state I have created a basic MCP server using the var builder = WebApplication.CreateBuilder(args);
// Add the MCP services: the transport to use (http) and the tools to register.
builder.Services
.AddMcpServer()
.WithHttpTransport()
.WithTools<RandomNumberTools>();
var app = builder.Build();
app.MapMcp();
app.UseHttpsRedirection();
app.Run();/// <summary>
/// Sample MCP tools for demonstration purposes.
/// These tools can be invoked by MCP clients to perform various operations.
/// </summary>
internal class RandomNumberTools
{
[McpServerTool]
[Description("Generates a random number between the specified minimum and maximum values.")]
public int GetRandomNumber(
[Description("Minimum value (inclusive)")] int min = 0,
[Description("Maximum value (exclusive)")] int max = 100)
{
return Random.Shared.Next(min, max);
}
}And this is my current client: using OllamaSharp;
using OllamaSharp.ModelContextProtocol;
using OllamaSharp.ModelContextProtocol.Server;
var uri = new Uri("http://localhost:11434");
var ollama = new OllamaApiClient(uri);
var models = await ollama.ListLocalModelsAsync();
ollama.SelectedModel = "llama3.2:latest";
var tools = await Tools.GetFromMcpServers(new McpServerConfiguration[]
{
new McpServerConfiguration()
{
Name = "Localhost MCP Server",
// How to get MCP from https://localhost:5269
}
});
foreach (var item in tools)
{
Console.WriteLine(item.Function);
}
var chat = new Chat(ollama);
while (true)
{
var message = Console.ReadLine();
await foreach (var answerToken in chat.SendAsync(message))
{
Console.Write(answerToken);
}
}Can someone help me out here? Thanks! :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
It reads MCP configs like shown here. I understand that our docs seem to be a bit vague here. But please also note that MCP is a pretty new and moving protocol. I highly recommend using the official C# sdk which can be used with OllamaSharp, too. |
Beta Was this translation helpful? Give feedback.
It reads MCP configs like shown here.
The demo console shows how to read this file here.
I understand that our docs seem to be a bit vague here. But please also note that MCP is a pretty new and moving protocol. I highly recommend using the official C# sdk which can be used with OllamaSharp, too.