添加本地文件、必应搜索、网址读取等第三方MCP
This commit is contained in:
parent
23c60b9c34
commit
b06e1825df
@ -5,11 +5,18 @@
|
|||||||
## 核心程序
|
## 核心程序
|
||||||
|
|
||||||
- [x] 提示词调用完整实现:以面向对象形式取代字典形式,加入参数和描述
|
- [x] 提示词调用完整实现:以面向对象形式取代字典形式,加入参数和描述
|
||||||
- [ ] 接入本地文件系统等基础性MCP服务
|
- [x] 接入本地文件系统等基础性MCP服务
|
||||||
- [ ] Python代码执行的实现
|
- [ ] Python代码执行的实现
|
||||||
- [ ] 适配qwen3、deepseek等推理模型并迁移
|
- [x] 适配qwen3、deepseek等推理模型并迁移
|
||||||
- [x] 工具执行错误消息合并为一条
|
- [x] 工具执行错误消息合并为一条
|
||||||
|
|
||||||
## 提示词工程
|
## 提示词工程
|
||||||
|
|
||||||
|
- [ ] 将原来的单独XML规则修改为内嵌XML规则
|
||||||
|
- [ ] 系统提示词明确提示调用动态Prompt和知识库
|
||||||
|
- [ ] 错误和继续提示词预留工具消息占位符
|
||||||
|
- [ ] 错误和继续提示词明示
|
||||||
|
- [ ] 系统提示词泛化增强
|
||||||
|
- [ ] 针对qwen3适当调整
|
||||||
|
|
||||||
## 前端交互
|
## 前端交互
|
||||||
@ -259,7 +259,6 @@ public class Gateway
|
|||||||
};
|
};
|
||||||
List<Message> messages = new List<Message>();
|
List<Message> messages = new List<Message>();
|
||||||
string toolInfos = await GetToolInfos(new McpServerList());
|
string toolInfos = await GetToolInfos(new McpServerList());
|
||||||
log.Info(SystemPrompt.SysPrompt(gdbPath, toolInfos));
|
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "system",
|
Role = "system",
|
||||||
@ -311,6 +310,8 @@ public class Gateway
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
string chunk = llmStreamChat.Choices[0].Delta.Content;
|
string chunk = llmStreamChat.Choices[0].Delta.Content;
|
||||||
MessageListItem reasonMessageListItem = new ChatMessageItem()
|
MessageListItem reasonMessageListItem = new ChatMessageItem()
|
||||||
{
|
{
|
||||||
@ -390,7 +391,13 @@ public class Gateway
|
|||||||
mcpToolRequests.Add(mcpToolRequest);
|
mcpToolRequests.Add(mcpToolRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
log.Error(e.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
if (messageContent != "")
|
if (messageContent != "")
|
||||||
{
|
{
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
@ -401,6 +408,8 @@ public class Gateway
|
|||||||
}
|
}
|
||||||
/*统一处理本次请求中的MCP工具调用需求*/
|
/*统一处理本次请求中的MCP工具调用需求*/
|
||||||
foreach (McpToolRequest mcpToolRequest in mcpToolRequests)
|
foreach (McpToolRequest mcpToolRequest in mcpToolRequests)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
McpServer mcpServer = mcpToolRequest.McpServer;
|
McpServer mcpServer = mcpToolRequest.McpServer;
|
||||||
string toolName = mcpToolRequest.ToolName;
|
string toolName = mcpToolRequest.ToolName;
|
||||||
@ -409,7 +418,7 @@ public class Gateway
|
|||||||
{
|
{
|
||||||
SseMcpServer sseMcpServer = mcpServer as SseMcpServer;
|
SseMcpServer sseMcpServer = mcpServer as SseMcpServer;
|
||||||
SseMcpClient client = new SseMcpClient(sseMcpServer.BaseUrl);
|
SseMcpClient client = new SseMcpClient(sseMcpServer.BaseUrl);
|
||||||
CallToolResponse toolResponse = await client.CallToolAsync(toolName,toolParams);
|
CallToolResponse toolResponse = await client.CallToolAsync(toolName, toolParams);
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
MessageListItem toolMessageItem = new ToolMessageItem
|
||||||
{
|
{
|
||||||
toolName = toolName,
|
toolName = toolName,
|
||||||
@ -417,19 +426,22 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = toolResponse.IsError ? "fail" : "success",
|
status = toolResponse.IsError ? "fail" : "success",
|
||||||
content = JsonConvert.SerializeObject(toolResponse),
|
content = JsonConvert.SerializeObject(toolResponse),
|
||||||
id = (timestamp+1).ToString()
|
id = (timestamp + 1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolResponse)) : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
Content = toolResponse.IsError
|
||||||
|
? SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
|
: SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
});
|
});
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is StdioMcpServer)
|
}
|
||||||
|
else if (mcpServer is StdioMcpServer)
|
||||||
{
|
{
|
||||||
StdioMcpServer stdioMcpServer = mcpServer as StdioMcpServer;
|
StdioMcpServer stdioMcpServer = mcpServer as StdioMcpServer;
|
||||||
StdioMcpClient client = new StdioMcpClient(stdioMcpServer.Command, stdioMcpServer.Args);
|
StdioMcpClient client = new StdioMcpClient(stdioMcpServer.Command, stdioMcpServer.Args);
|
||||||
CallToolResponse toolResponse = await client.CallToolAsync(toolName,toolParams);
|
CallToolResponse toolResponse = await client.CallToolAsync(toolName, toolParams);
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
MessageListItem toolMessageItem = new ToolMessageItem
|
||||||
{
|
{
|
||||||
toolName = toolName,
|
toolName = toolName,
|
||||||
@ -437,18 +449,21 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = toolResponse.IsError ? "fail" : "success",
|
status = toolResponse.IsError ? "fail" : "success",
|
||||||
content = JsonConvert.SerializeObject(toolResponse),
|
content = JsonConvert.SerializeObject(toolResponse),
|
||||||
id = (timestamp+1).ToString()
|
id = (timestamp + 1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
Role = "user",
|
Role = "user",
|
||||||
Content = toolResponse.IsError ? SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolResponse)) : SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
Content = toolResponse.IsError
|
||||||
|
? SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
|
: SystemPrompt.ContinuePrompt(JsonConvert.SerializeObject(toolResponse))
|
||||||
});
|
});
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (mcpServer is InnerMcpServer)
|
}
|
||||||
|
else if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
Type type = Type.GetType("LinkToolAddin.client.tool."+(mcpServer as InnerMcpServer).Name);
|
Type type = Type.GetType("LinkToolAddin.client.tool." + (mcpServer as InnerMcpServer).Name);
|
||||||
MethodInfo method = type.GetMethod(toolName,BindingFlags.Public | BindingFlags.Static);
|
MethodInfo method = type.GetMethod(toolName, BindingFlags.Public | BindingFlags.Static);
|
||||||
var methodParams = toolParams.Values.ToArray();
|
var methodParams = toolParams.Values.ToArray();
|
||||||
object[] args = new object[methodParams.Length];
|
object[] args = new object[methodParams.Length];
|
||||||
for (int i = 0; i < methodParams.Length; i++)
|
for (int i = 0; i < methodParams.Length; i++)
|
||||||
@ -464,6 +479,7 @@ public class Gateway
|
|||||||
args[i] = methodParams[i];
|
args[i] = methodParams[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var task = method.Invoke(null, args) as Task<JsonRpcResultEntity>;
|
var task = method.Invoke(null, args) as Task<JsonRpcResultEntity>;
|
||||||
JsonRpcResultEntity innerResult = await task;
|
JsonRpcResultEntity innerResult = await task;
|
||||||
if (innerResult is JsonRpcErrorEntity)
|
if (innerResult is JsonRpcErrorEntity)
|
||||||
@ -475,7 +491,7 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = "fail",
|
status = "fail",
|
||||||
content = JsonConvert.SerializeObject(innerResult),
|
content = JsonConvert.SerializeObject(innerResult),
|
||||||
id = (timestamp+1).ToString()
|
id = (timestamp + 1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
@ -483,7 +499,8 @@ public class Gateway
|
|||||||
Content = SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolMessageItem))
|
Content = SystemPrompt.ErrorPrompt(JsonConvert.SerializeObject(toolMessageItem))
|
||||||
});
|
});
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}else if (innerResult is JsonRpcSuccessEntity)
|
}
|
||||||
|
else if (innerResult is JsonRpcSuccessEntity)
|
||||||
{
|
{
|
||||||
MessageListItem toolMessageItem = new ToolMessageItem
|
MessageListItem toolMessageItem = new ToolMessageItem
|
||||||
{
|
{
|
||||||
@ -492,7 +509,7 @@ public class Gateway
|
|||||||
type = MessageType.TOOL_MESSAGE,
|
type = MessageType.TOOL_MESSAGE,
|
||||||
status = "success",
|
status = "success",
|
||||||
content = JsonConvert.SerializeObject(innerResult),
|
content = JsonConvert.SerializeObject(innerResult),
|
||||||
id = (timestamp+1).ToString()
|
id = (timestamp + 1).ToString()
|
||||||
};
|
};
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
{
|
{
|
||||||
@ -503,8 +520,17 @@ public class Gateway
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
log.Error(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
/*统一处理本次请求中的Prompt调用需求*/
|
/*统一处理本次请求中的Prompt调用需求*/
|
||||||
foreach (PromptRequest promptRequest in promptRequests)
|
foreach (PromptRequest promptRequest in promptRequests)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
string promptContent = DynamicPrompt.GetPrompt(promptRequest.PromptName, promptRequest.PromptArgs);
|
string promptContent = DynamicPrompt.GetPrompt(promptRequest.PromptName, promptRequest.PromptArgs);
|
||||||
messages.Add(new Message
|
messages.Add(new Message
|
||||||
@ -523,6 +549,12 @@ public class Gateway
|
|||||||
};
|
};
|
||||||
callback?.Invoke(toolMessageItem);
|
callback?.Invoke(toolMessageItem);
|
||||||
}
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
log.Error(e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -531,6 +563,7 @@ public class Gateway
|
|||||||
StringBuilder toolInfos = new StringBuilder();
|
StringBuilder toolInfos = new StringBuilder();
|
||||||
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
foreach (McpServer mcpServer in mcpServerList.GetAllServers())
|
||||||
{
|
{
|
||||||
|
log.Info($"正在列出{mcpServer.Name}中的工具");
|
||||||
if (mcpServer is InnerMcpServer)
|
if (mcpServer is InnerMcpServer)
|
||||||
{
|
{
|
||||||
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
InnerMcpServer innerMcpServer = (InnerMcpServer)mcpServer;
|
||||||
|
|||||||
@ -35,6 +35,53 @@ public class McpServerList
|
|||||||
Description = "可以调用进行查询知识库,获取相关参考信息。",
|
Description = "可以调用进行查询知识库,获取相关参考信息。",
|
||||||
IsActive = true
|
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"
|
||||||
|
// }
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
public McpServer GetServer(string name)
|
public McpServer GetServer(string name)
|
||||||
|
|||||||
@ -199,8 +199,15 @@ namespace LinkToolAddin.ui.dockpane
|
|||||||
{
|
{
|
||||||
string userPrompt = PromptTestTextBox.Text;
|
string userPrompt = PromptTestTextBox.Text;
|
||||||
// model可选值:qwen3-235b-a22b,qwen-max,deepseek-r1
|
// model可选值:qwen3-235b-a22b,qwen-max,deepseek-r1
|
||||||
|
try
|
||||||
|
{
|
||||||
Gateway.SendMessageStream(userPrompt,"qwen3-235b-a22b", "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\20250420_AiDemoProject.gdb", AddReplyStream);
|
Gateway.SendMessageStream(userPrompt,"qwen3-235b-a22b", "D:\\01_Project\\20250305_LinkTool\\20250420_AiDemoProject\\20250420_AiDemoProject.gdb", AddReplyStream);
|
||||||
}
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
log.Error(exception.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void AddReplyStream(MessageListItem msg)
|
public void AddReplyStream(MessageListItem msg)
|
||||||
{
|
{
|
||||||
@ -215,6 +222,8 @@ namespace LinkToolAddin.ui.dockpane
|
|||||||
messageDict.Add(msg.id, msg);
|
messageDict.Add(msg.id, msg);
|
||||||
}
|
}
|
||||||
ReplyTextBox.Clear();
|
ReplyTextBox.Clear();
|
||||||
|
try
|
||||||
|
{
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
foreach (KeyValuePair<string,MessageListItem> pair in messageDict)
|
foreach (KeyValuePair<string,MessageListItem> pair in messageDict)
|
||||||
{
|
{
|
||||||
@ -229,6 +238,11 @@ namespace LinkToolAddin.ui.dockpane
|
|||||||
ReplyTextBox.Text = builder.ToString();
|
ReplyTextBox.Text = builder.ToString();
|
||||||
ReplyTextBox.ScrollToEnd();
|
ReplyTextBox.ScrollToEnd();
|
||||||
}
|
}
|
||||||
|
}catch (Exception exception)
|
||||||
|
{
|
||||||
|
log.Error(exception.Message);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddReply(MessageListItem msg)
|
public void AddReply(MessageListItem msg)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user