Skip to content

Http Transport to communicate with MCP Server? #329

@DrFredFi

Description

@DrFredFi

Hi there!
First of all, great work - beautiful work you guys are doing here. thx!!
My question: I've been trying to setup a mcp client using Ollama locally to communicate with an MCP server that defines some tools defined with the [McpServerTool] attribute. Everything seems to work fine until I reach this part:

await foreach (var update in (chat as IChatClient).GetStreamingResponseAsync(history
        , new() { Tools = [.. tools]

When I inspect the update var, I can see that the model correctly identifies the tool/function to call, but no actual call is made to the server (where I have some breakpoints set). As a result the Text property is empty.

Image

Maybe my approach isn't quite right. Looking through the docs I've noticed that there's no httptransport defined, so I'm guessing that might be why the model is not able to call my server.
Am I guessing correctly, or am I missing something (a lot)?

Here's my client code so far:

using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using OllamaSharp;
using System.Text;

var transport = new HttpClientTransport(new()
{
    Name = "MCP Client Sample App Ollama",
    Endpoint = new Uri("https://localhost:7279/mcp")
});

await using var mcpClient = await McpClient.CreateAsync(transport);

var tools = await mcpClient.ListToolsAsync().ConfigureAwait(false);
foreach (var tool in tools)
{
    Console.WriteLine($"{tool.Name} => {tool.Description}");
}

var ollamaUrl = new Uri("http://localhost:11434/");
var modelId = "qwen3-coder:30b";
var chat = new OllamaApiClient(ollamaUrl, modelId);

var history = new List<ChatMessage>();
while (true)
{
    Console.Write("Question: ");

    var question = Console.ReadLine();
    history.Add(new(ChatRole.User, question));

    var answer = new StringBuilder();
    await foreach (var update in (chat as IChatClient).GetStreamingResponseAsync(history
        , new() { Tools = [.. tools] }
        ))
    {
        Console.Write(update.Text);
        answer.Append(update.Text);
    }
    history.Add(new(ChatRole.Assistant, answer.ToString()));
}

thx for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions