增强异常捕获能力,减少闪退
This commit is contained in:
parent
c61938383b
commit
660ba2ad53
@ -239,7 +239,10 @@ public class Gateway
|
||||
api_key = "sk-db177155677e438f832860e7f4da6afc"
|
||||
};
|
||||
List<Message> messages = new List<Message>();
|
||||
string toolInfos = await GetToolInfos(new McpServerList());
|
||||
string toolInfos = "";
|
||||
try
|
||||
{
|
||||
toolInfos = await GetToolInfos(new McpServerList());
|
||||
messages.Add(new Message
|
||||
{
|
||||
Role = "system",
|
||||
@ -250,6 +253,11 @@ public class Gateway
|
||||
Role = "user",
|
||||
Content = message
|
||||
});
|
||||
}catch (Exception ex)
|
||||
{
|
||||
log.Error(ex);
|
||||
MessageBox.Show(ex.Message,"获取MCP列表失败");
|
||||
}
|
||||
goOn = true;
|
||||
string toolPattern = "<tool_use>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/tool_use>";
|
||||
string promptPattern = "<prompt>([\\s\\S]*?)<name>([\\s\\S]*?)<\\/name>([\\s\\S]*?)<arguments>([\\s\\S]*?)<\\/arguments>([\\s\\S]*?)<\\/prompt>";
|
||||
@ -286,12 +294,15 @@ public class Gateway
|
||||
{
|
||||
type = MessageType.END_TAG,
|
||||
content = "",
|
||||
id = (timestamp+3).ToString(),
|
||||
id = (timestamp + 3).ToString(),
|
||||
role = "assistant"
|
||||
};
|
||||
callback?.Invoke(endMessageListItem1);
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await foreach(LlmStreamChat llmStreamChat in bailian.SendChatStreamAsync(jsonContent))
|
||||
{
|
||||
if (!goOn)
|
||||
@ -418,6 +429,12 @@ public class Gateway
|
||||
log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
}catch (Exception e)
|
||||
{
|
||||
log.Error(e.Message);
|
||||
MessageBox.Show(e.Message, "请求大模型出错");
|
||||
}
|
||||
|
||||
if (messageContent != "")
|
||||
{
|
||||
messages.Add(new Message
|
||||
@ -616,7 +633,8 @@ public class Gateway
|
||||
StringBuilder toolInfos = new StringBuilder();
|
||||
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
||||
{
|
||||
log.Info($"正在列出{mcpServer.Name}中的工具");
|
||||
try
|
||||
{
|
||||
if (mcpServer is InnerMcpServer)
|
||||
{
|
||||
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
||||
@ -687,15 +705,26 @@ public class Gateway
|
||||
toolInfos.AppendLine();
|
||||
}
|
||||
}
|
||||
}catch (Exception e)
|
||||
{
|
||||
log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
List<UserPrompt> prompts = DynamicPrompt.GetAllPrompts();
|
||||
foreach (UserPrompt userPrompt in prompts)
|
||||
{
|
||||
try
|
||||
{
|
||||
XNode node = JsonConvert.DeserializeXNode(JsonConvert.SerializeObject(new PromptDefinition(){UserPrompt = userPrompt}));
|
||||
toolInfos.AppendLine(node.ToString());
|
||||
toolInfos.AppendLine();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.Error(e.Message);
|
||||
}
|
||||
}
|
||||
return toolInfos.ToString();
|
||||
}
|
||||
|
||||
|
||||
@ -35,53 +35,53 @@ public class McpServerList
|
||||
Description = "可以调用进行查询知识库,获取相关参考信息。",
|
||||
IsActive = true
|
||||
});
|
||||
//servers.Add("filesystem", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "filesystem",
|
||||
// Type = "stdio",
|
||||
// Command = "npx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "-y",
|
||||
// "@modelcontextprotocol/server-filesystem",
|
||||
// "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("fetch", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "fetch",
|
||||
// Type = "stdio",
|
||||
// Command = "uvx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "mcp-server-fetch"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("bing-search", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "bing-search",
|
||||
// Type = "stdio",
|
||||
// Command = "npx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "bing-cn-mcp"
|
||||
// }
|
||||
//});
|
||||
//servers.Add("mcp-python-interpreter", new StdioMcpServer()
|
||||
//{
|
||||
// Name = "mcp-python-interpreter",
|
||||
// Type = "stdio",
|
||||
// Command = "uvx",
|
||||
// Args = new List<string>()
|
||||
// {
|
||||
// "--native-tls",
|
||||
// "mcp-python-interpreter",
|
||||
// "--dir",
|
||||
// "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData",
|
||||
// "--python-path",
|
||||
// "C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe"
|
||||
// }
|
||||
//});
|
||||
servers.Add("filesystem", new StdioMcpServer()
|
||||
{
|
||||
Name = "filesystem",
|
||||
Type = "stdio",
|
||||
Command = "npxh",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"-y",
|
||||
"@modelcontextprotocol/server-filesystem",
|
||||
"D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData"
|
||||
}
|
||||
});
|
||||
servers.Add("fetch", new StdioMcpServer()
|
||||
{
|
||||
Name = "fetch",
|
||||
Type = "stdio",
|
||||
Command = "uvx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"mcp-server-fetch"
|
||||
}
|
||||
});
|
||||
servers.Add("bing-search", new StdioMcpServer()
|
||||
{
|
||||
Name = "bing-search",
|
||||
Type = "stdio",
|
||||
Command = "npx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"bing-cn-mcp"
|
||||
}
|
||||
});
|
||||
servers.Add("mcp-python-interpreter", new StdioMcpServer()
|
||||
{
|
||||
Name = "mcp-python-interpreter",
|
||||
Type = "stdio",
|
||||
Command = "uvx",
|
||||
Args = new List<string>()
|
||||
{
|
||||
"--native-tls",
|
||||
"mcp-python-interpreter",
|
||||
"--dir",
|
||||
"D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\TestData",
|
||||
"--python-path",
|
||||
"C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\custom\\python.exe"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public McpServer GetServer(string name)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user