1
1
using System . Reflection ;
2
2
using System . Text . Json ;
3
3
using System . Text . Json . Serialization ;
4
- using McpDotNet . Client ;
5
- using McpDotNet . Configuration ;
4
+ using Microsoft . Extensions . Logging ;
6
5
using Microsoft . Extensions . Logging . Abstractions ;
6
+ using ModelContextProtocol . Protocol . Transport ;
7
7
using OllamaSharp . ModelContextProtocol . Server ;
8
+ using ModelContextProtocolClient = ModelContextProtocol . Client ;
8
9
9
10
namespace OllamaSharp . ModelContextProtocol ;
10
11
@@ -68,65 +69,64 @@ public static async Task<object[]> GetFromMcpServers(McpClientOptions? clientOpt
68
69
if ( mcpServers == null || mcpServers . Length == 0 )
69
70
throw new ArgumentNullException ( nameof ( mcpServers ) ) ;
70
71
72
+ var loggerFactory = clientOptions ? . LoggerFactory ?? NullLoggerFactory . Instance ;
71
73
var options = CreateMcpClientOptions ( clientOptions ) ;
72
- var servers = ConvertServerConfigurations ( mcpServers ) ;
73
-
74
- var factory = new McpClientFactory (
75
- servers ,
76
- options ,
77
- clientOptions ? . LoggerFactory ?? NullLoggerFactory . Instance , clientOptions ? . TransportFactoryMethod , clientOptions ? . ClientFactoryMethod
78
- ) ;
79
74
80
75
var result = new List < object > ( ) ;
81
-
82
- foreach ( var server in servers )
76
+ foreach ( var server in mcpServers )
83
77
{
84
- var client = await factory . GetClientAsync ( server . Id ) ;
85
-
86
- var tools = await client . ListToolsAsync ( ) ;
78
+ var clientTransport = clientOptions ? . ClientTransportFactoryMethod != null ?
79
+ clientOptions . ClientTransportFactoryMethod ( server , loggerFactory ) :
80
+ ConvertServerConfigurations ( server , loggerFactory ) ;
87
81
88
- if ( tools != null )
82
+ var client = await ModelContextProtocolClient . McpClientFactory . CreateAsync ( clientTransport , options , loggerFactory ) ;
83
+ foreach ( var tool in await ModelContextProtocolClient . McpClientExtensions . ListToolsAsync ( client ) )
89
84
{
90
- foreach ( var tool in tools . Tools )
91
- result . Add ( new McpClientTool ( tool , client ) ) ;
85
+ result . Add ( new McpClientTool ( tool , client ) ) ;
92
86
}
93
87
}
94
88
95
89
return result . ToArray ( ) ;
96
90
}
97
91
98
- private static List < McpServerConfig > ConvertServerConfigurations ( McpServerConfiguration [ ] mcpServers )
92
+ private static IClientTransport ConvertServerConfigurations ( McpServerConfiguration server , ILoggerFactory loggerFactory )
99
93
{
100
- var result = new List < McpServerConfig > ( ) ;
101
-
102
- foreach ( var server in mcpServers )
94
+ if ( server . TransportType == McpServerTransportType . Stdio )
103
95
{
104
- var config = new McpServerConfig
96
+ var stdioOptions = new StdioClientTransportOptions
105
97
{
106
- Id = server . Name ?? Guid . NewGuid ( ) . ToString ( "n" ) ,
107
- Name = server . Name ?? Guid . NewGuid ( ) . ToString ( "n" ) ,
108
- TransportType = server . TransportType . ToString ( ) ,
109
- Arguments = ResolveVariables ( server . Arguments ) ,
110
- Location = server . Command ,
111
- TransportOptions = server . Options ?? [ ]
98
+ Command = server . Command ,
99
+ Name = server . Name
112
100
} ;
113
101
102
+ if ( server . Arguments != null )
103
+ {
104
+ stdioOptions . Arguments = ResolveVariables ( server . Arguments ) ;
105
+ }
106
+
114
107
if ( server . Environment != null )
115
108
{
109
+ stdioOptions . EnvironmentVariables = [ ] ;
116
110
foreach ( var kvp in server . Environment )
117
- config . TransportOptions [ $ "env:{ kvp . Key } "] = Environment . GetEnvironmentVariable ( GetEnvironmentVariableName ( kvp . Value ) ) ?? kvp . Value ;
111
+ {
112
+ stdioOptions . EnvironmentVariables [ kvp . Key ] = Environment . GetEnvironmentVariable ( GetEnvironmentVariableName ( kvp . Value ) ) ?? kvp . Value ;
113
+ }
118
114
}
119
115
120
- if ( config . Arguments != null )
121
- config . TransportOptions [ "arguments" ] = string . Join ( ' ' , config . Arguments ) ;
122
-
123
- if ( config . TransportOptions ? . TryGetValue ( "workingDirectory" , out var dir ) == true )
124
- config . TransportOptions [ "workingDirectory" ] = ResolveVariables ( dir ) ;
116
+ if ( server . Options ? . TryGetValue ( "workingDirectory" , out var workingDirectory ) == true )
117
+ {
118
+ stdioOptions . WorkingDirectory = workingDirectory ;
119
+ }
125
120
126
- result . Add ( config ) ;
121
+ return new StdioClientTransport ( stdioOptions , loggerFactory ) ;
127
122
}
128
123
129
- return result ;
124
+ var sseOptions = new SseClientTransportOptions
125
+ {
126
+ Endpoint = new Uri ( server . Command ) ,
127
+ Name = server . Name
128
+ } ;
129
+ return new SseClientTransport ( sseOptions , loggerFactory ) ;
130
130
}
131
131
132
132
private static string GetEnvironmentVariableName ( string name )
@@ -137,22 +137,19 @@ private static string GetEnvironmentVariableName(string name)
137
137
return name ;
138
138
}
139
139
140
- private static string [ ] ? ResolveVariables ( string [ ] ? arguments )
140
+ private static string [ ] ResolveVariables ( string [ ] arguments )
141
141
{
142
- if ( arguments == null )
143
- return null ;
144
-
145
- return arguments . Select ( a => ResolveVariables ( a ) ) . ToArray ( ) ;
142
+ return arguments . Select ( ResolveVariables ) . ToArray ( ) ;
146
143
}
147
144
148
145
private static string ResolveVariables ( string argument )
149
146
{
150
147
return Environment . ExpandEnvironmentVariables ( argument ) ;
151
148
}
152
149
153
- private static McpDotNet . Client . McpClientOptions CreateMcpClientOptions ( McpClientOptions ? clientOptions )
150
+ private static ModelContextProtocolClient . McpClientOptions CreateMcpClientOptions ( McpClientOptions ? clientOptions )
154
151
{
155
- return new McpDotNet . Client . McpClientOptions
152
+ return new ModelContextProtocolClient . McpClientOptions
156
153
{
157
154
Capabilities = clientOptions ? . Capabilities ,
158
155
InitializationTimeout = clientOptions ? . InitializationTimeout ?? TimeSpan . FromSeconds ( 60 ) ,
@@ -169,5 +166,4 @@ private sealed class McpServerConfigurationFile
169
166
[ JsonPropertyName ( "mcpServers" ) ]
170
167
public Dictionary < string , McpServerConfiguration > Servers { get ; set ; } = [ ] ;
171
168
}
172
- }
173
-
169
+ }
0 commit comments