@@ -24,6 +24,7 @@ public class ChatTools
2424
2525 private List < McpClient > _mcpClients = [ ] ;
2626 private Dictionary < string , McpClient > _mcpClientsByEndpoint = [ ] ;
27+ private const string _mcpToolSeparator = "_._" ;
2728
2829 /// <summary>
2930 /// Initializes a new instance of the <see cref="ChatTools"/> class.
@@ -85,13 +86,18 @@ internal void Add(BinaryData toolDefinitions, McpClient client)
8586
8687 var tools = toolsElement . EnumerateArray ( ) ;
8788 // the replacement is to deal with OpenAI's tool name regex validation.
88- var serverKey = client . ServerEndpoint . AbsoluteUri . Replace ( '/' , '_' ) . Replace ( ':' , '_' ) ;
89+ var serverKey = client . ServerEndpoint . Host + client . ServerEndpoint . Port . ToString ( ) ;
8990
9091 foreach ( var tool in tools )
9192 {
92- var name = $ "{ serverKey } __.__ { tool . GetProperty ( "name" ) . GetString ( ) ! } ";
93+ var name = $ "{ serverKey } { _mcpToolSeparator } { tool . GetProperty ( "name" ) . GetString ( ) ! } ";
9394 var description = tool . GetProperty ( "description" ) . GetString ( ) ! ;
94- var inputSchema = tool . GetProperty ( "inputSchema" ) . GetRawText ( ) ;
95+ var inputSchema = JsonSerializer . Serialize (
96+ JsonSerializer . Deserialize < JsonElement > ( tool . GetProperty ( "inputSchema" ) . GetRawText ( ) ) ,
97+ new JsonSerializerOptions
98+ {
99+ Encoder = System . Text . Encodings . Web . JavaScriptEncoder . UnsafeRelaxedJsonEscaping ,
100+ } ) ;
95101
96102 var chatTool = ChatTool . CreateFunctionTool (
97103 name ,
@@ -188,11 +194,10 @@ private async Task<string> CallMcp(ChatToolCall call)
188194 if ( _mcpMethods . TryGetValue ( call . FunctionName , out Func < string , BinaryData , Task < BinaryData > > ? method ) )
189195 {
190196 #if ! NETSTANDARD2_0
191- var actualFunctionName = call . FunctionName . Split ( "__.__" , 2 ) [ 1 ] ;
197+ var actualFunctionName = call . FunctionName . Split ( _mcpToolSeparator , 2 ) [ 1 ] ;
192198 #else
193- var separator = "__.__" ;
194- var index = call . FunctionName . IndexOf ( separator ) ;
195- var actualFunctionName = call . FunctionName . Substring ( index + separator . Length ) ;
199+ var index = call . FunctionName . IndexOf ( _mcpToolSeparator ) ;
200+ var actualFunctionName = call . FunctionName . Substring ( index + _mcpToolSeparator . Length ) ;
196201 #endif
197202 var result = await method ( actualFunctionName , call . FunctionArguments ) . ConfigureAwait ( false ) ;
198203 return result . ToString ( ) ;
0 commit comments