Skip to content

Commit 70ac4ff

Browse files
authored
Merge pull request #198 from awaescher/improve-mcp
Improve model context protocol support
2 parents 5c574d3 + 1153b85 commit 70ac4ff

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

demo/Demos/ToolConsole.cs

+24-17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace OllamaApiConsole.Demos;
77

88
public class ToolConsole(IOllamaApiClient ollama) : OllamaConsole(ollama)
99
{
10+
public List<object> Tools { get; } = [new GetWeatherTool(), new GetLatLonAsyncTool()];
11+
1012
public override async Task Run()
1113
{
1214
AnsiConsole.Write(new Rule("Tool chat").LeftJustified());
@@ -17,11 +19,9 @@ public override async Task Run()
1719
if (!string.IsNullOrEmpty(Ollama.SelectedModel))
1820
{
1921
var keepChatting = true;
20-
var mcpServersAdded = false;
22+
var mcpAdded = false;
2123
var systemPrompt = ReadInput($"Define a system prompt [{HintTextColor}](optional)[/]");
2224

23-
var tools = await GetTools(false);
24-
2525
do
2626
{
2727
AnsiConsole.MarkupLine("");
@@ -30,7 +30,7 @@ public override async Task Run()
3030
AnsiConsole.MarkupLine("If any tool is used, the intended usage information is printed.");
3131
WriteChatInstructionHint();
3232

33-
if (!mcpServersAdded)
33+
if (!mcpAdded)
3434
{
3535
AnsiConsole.MarkupLine($"[{HintTextColor}]Enter [{AccentTextColor}]{USE_MCP_SERVER_COMMAND}[/] to use tools from MCP servers. Caution, please install following MCP servers for this demo: [/]");
3636
AnsiConsole.MarkupLine($"[{HintTextColor}]npm install -g @modelcontextprotocol/server-filesystem [/]");
@@ -62,23 +62,34 @@ public override async Task Run()
6262
if (message.Equals(USE_MCP_SERVER_COMMAND, StringComparison.OrdinalIgnoreCase))
6363
{
6464
keepChatting = true;
65-
mcpServersAdded = true;
66-
tools = await GetTools(true);
65+
66+
if (!mcpAdded)
67+
{
68+
var mcpTools = await GetMcpTools();
69+
70+
if (mcpTools.Any())
71+
{
72+
Tools.AddRange(mcpTools);
73+
mcpAdded = true;
74+
}
75+
AnsiConsole.MarkupLine($"[{HintTextColor}]{mcpTools.Count()} tool(s) added.[/]");
76+
}
77+
6778
break;
6879
}
6980

7081
if (message.Equals(LIST_TOOLS_COMMAND, StringComparison.OrdinalIgnoreCase))
7182
{
7283
keepChatting = true;
73-
ListTools(tools);
84+
ListTools(Tools);
7485
break;
7586
}
7687

7788
var currentMessageCount = chat.Messages.Count;
7889

7990
try
8091
{
81-
await foreach (var answerToken in chat.SendAsync(message, tools))
92+
await foreach (var answerToken in chat.SendAsync(message, Tools))
8293
AnsiConsole.MarkupInterpolated($"[{AiTextColor}]{answerToken}[/]");
8394
}
8495
catch (OllamaException ex)
@@ -118,7 +129,7 @@ public override async Task Run()
118129
}
119130
}
120131

121-
private static void ListTools(object[] tools)
132+
private static void ListTools(IEnumerable<object> tools)
122133
{
123134
AnsiConsole.MarkupLine("\n[purple]Available tools:[/]");
124135

@@ -131,17 +142,13 @@ private static void ListTools(object[] tools)
131142
}
132143
}
133144

134-
private static async Task<object[]> GetTools(bool withMcpServers)
145+
private static async Task<object[]> GetMcpTools()
135146
{
136-
object[] tools = [new GetWeatherTool(), new GetLatLonAsyncTool()];
137-
138-
if (withMcpServers)
139-
tools = tools.Union(await OllamaSharp.ModelContextProtocol.Tools.GetFromMcpServers(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "server_config.json"))).ToArray();
140-
141-
return tools.ToArray();
147+
// expect a config file for the demo app
148+
var config = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "server_config.json");
149+
return await OllamaSharp.ModelContextProtocol.Tools.GetFromMcpServers(config);
142150
}
143151

144-
145152
public enum Unit
146153
{
147154
Celsius,

demo/OllamaApiConsole.csproj

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net9.0</TargetFramework>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -12,16 +13,15 @@
1213
</ItemGroup>
1314

1415
<ItemGroup>
15-
<ProjectReference Include="..\src\OllamaSharp.ModelContextProtocol\OllamaSharp.ModelContextProtocol.csproj" />
16-
<ProjectReference Include="..\src\OllamaSharp\OllamaSharp.csproj" />
17-
<ProjectReference Include="..\src\SourceGenerators\OllamaSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
16+
<ProjectReference Include="..\src\OllamaSharp.ModelContextProtocol\OllamaSharp.ModelContextProtocol.csproj" />
17+
<ProjectReference Include="..\src\OllamaSharp\OllamaSharp.csproj" />
18+
<ProjectReference Include="..\src\SourceGenerators\OllamaSharp.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
1819
</ItemGroup>
1920

2021
<ItemGroup>
21-
<None Update="server_config.json">
22-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23-
</None>
22+
<None Update="server_config.json">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
2425
</ItemGroup>
2526

2627
</Project>
27-

docs/tool-support.md

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ OllamaSharp will automatically match tool calls from the AI model with the provi
146146
- the tool implementation itself is not duplicated but gets executed from the generated tool. This allows easy debugging.
147147
- the tool's result value will automatically be back-propagated to the chat so that the AI model can continue working.
148148
- the entire tool invocation behavior can be modified by changing the `Chat.ToolInvoker` instance.
149+
- the project containing the tools **must generate a documentation file**, otherwise the tools' summaries are lost after compilation. Add `<GenerateDocumentationFile>true</GenerateDocumentationFile>` to the corresponding project file.
149150

150151
#### Limitations
151152

@@ -154,6 +155,8 @@ OllamaSharp will automatically match tool calls from the AI model with the provi
154155
- cannot be used with non-static instance methods <sup>**_planned_**</sup>
155156
- cannot be used with interfaces to only define the meta data without providing an implementation <sup>**_planned_**</sup>
156157
- only available for C#. Visual Basic and F# are not supported <sup>**_not planned_**</sup>
158+
159+
> The project containing the Ollama tools must generate a documentation file, see "Important details".
157160
158161
## Model context protocol servers
159162

src/OllamaSharp.ModelContextProtocol/OllamaSharp.ModelContextProtocol.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
66
<GenerateDocumentationFile>True</GenerateDocumentationFile>
77
<Title>OllamaSharp.ModelContextProtocol</Title>
8-
<Copyright>Toni Wenzel</Copyright>
9-
<Authors>Andreas Wäscher, Milkey Tan, Jerrett Davis, Toni Wenzel</Authors>
8+
<Copyright>Andreas Wäscher</Copyright>
9+
<Authors>Toni Wenzel</Authors>
1010
<PackageIcon>Ollama.png</PackageIcon>
11-
<Description>Use MCP servers as tools for Ollama</Description>
11+
<Description>Use tools from model context protocol (MCP) servers with Ollama</Description>
1212
<PackageProjectUrl>https://github.com/awaescher/OllamaSharp</PackageProjectUrl>
1313
<PackageReadmeFile>README.md</PackageReadmeFile>
1414
<RepositoryUrl>https://github.com/awaescher/OllamaSharp</RepositoryUrl>

0 commit comments

Comments
 (0)