fix(mcp): 修复了mcp无法调用功能的问题 (#3047)
* fix(mcp): 修复了mcp无法调用功能的问题 * fix(mcp): 修复工具调用时inputSchema只读属性错误
This commit is contained in:
parent
37d172dbd9
commit
c44f3b8a3d
@ -52,8 +52,11 @@ export function openAIToolsToMcpTool(
|
|||||||
if (!tool) {
|
if (!tool) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
tool.inputSchema = JSON.parse(llmTool.function.arguments)
|
// 创建工具对象的副本,而不是直接修改原对象
|
||||||
return tool
|
return {
|
||||||
|
...tool,
|
||||||
|
inputSchema: JSON.parse(llmTool.function.arguments)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function callMCPTool(tool: MCPTool): Promise<any> {
|
export async function callMCPTool(tool: MCPTool): Promise<any> {
|
||||||
@ -82,9 +85,12 @@ export function anthropicToolUseToMcpTool(mcpTools: MCPTool[] | undefined, toolU
|
|||||||
if (!tool) {
|
if (!tool) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
// @ts-ignore ignore type as it it unknow
|
// 创建工具对象的副本,而不是直接修改原对象
|
||||||
tool.inputSchema = toolUse.input
|
return {
|
||||||
return tool
|
...tool,
|
||||||
|
// @ts-ignore ignore type as it it unknow
|
||||||
|
inputSchema: toolUse.input
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mcpToolsToGeminiTools(mcpTools: MCPTool[] | undefined): geminiToool[] {
|
export function mcpToolsToGeminiTools(mcpTools: MCPTool[] | undefined): geminiToool[] {
|
||||||
@ -120,9 +126,12 @@ export function geminiFunctionCallToMcpTool(
|
|||||||
if (!tool) {
|
if (!tool) {
|
||||||
return undefined
|
return undefined
|
||||||
}
|
}
|
||||||
// @ts-ignore schema is not a valid property
|
// 创建工具对象的副本,而不是直接修改原对象
|
||||||
tool.inputSchema = fcall.args
|
return {
|
||||||
return tool
|
...tool,
|
||||||
|
// @ts-ignore schema is not a valid property
|
||||||
|
inputSchema: fcall.args
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function upsertMCPToolResponse(
|
export function upsertMCPToolResponse(
|
||||||
@ -131,15 +140,35 @@ export function upsertMCPToolResponse(
|
|||||||
onChunk: ({ mcpToolResponse }: ChunkCallbackData) => void
|
onChunk: ({ mcpToolResponse }: ChunkCallbackData) => void
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
for (const ret of results) {
|
// 创建一个新数组,不修改原数组
|
||||||
if (ret.tool.id == resp.tool.id) {
|
const newResults: MCPToolResponse[] = []
|
||||||
ret.response = resp.response
|
let found = false
|
||||||
ret.status = resp.status
|
|
||||||
return
|
// 复制原数组中的元素到新数组,如果找到匹配的工具ID则更新
|
||||||
|
for (const item of results) {
|
||||||
|
if (item.tool.id === resp.tool.id) {
|
||||||
|
// 找到匹配的工具,添加更新后的对象
|
||||||
|
newResults.push({ ...item, response: resp.response, status: resp.status })
|
||||||
|
found = true
|
||||||
|
} else {
|
||||||
|
// 否则添加原对象的副本
|
||||||
|
newResults.push({ ...item })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results.push(resp)
|
|
||||||
} finally {
|
// 如果没有找到匹配的工具ID,添加新的响应
|
||||||
|
if (!found) {
|
||||||
|
newResults.push({ ...resp })
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用回调函数,传递新数组
|
||||||
|
onChunk({
|
||||||
|
text: '',
|
||||||
|
mcpToolResponse: newResults
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in upsertMCPToolResponse:', error)
|
||||||
|
// 出错时仍然调用回调,但使用原数组
|
||||||
onChunk({
|
onChunk({
|
||||||
text: '',
|
text: '',
|
||||||
mcpToolResponse: results
|
mcpToolResponse: results
|
||||||
@ -151,11 +180,13 @@ export function filterMCPTools(
|
|||||||
mcpTools: MCPTool[] | undefined,
|
mcpTools: MCPTool[] | undefined,
|
||||||
enabledServers: MCPServer[] | undefined
|
enabledServers: MCPServer[] | undefined
|
||||||
): MCPTool[] | undefined {
|
): MCPTool[] | undefined {
|
||||||
|
console.log('filterMCPTools', mcpTools, enabledServers)
|
||||||
if (mcpTools) {
|
if (mcpTools) {
|
||||||
if (enabledServers) {
|
if (enabledServers) {
|
||||||
mcpTools = mcpTools.filter((t) => enabledServers.some((m) => m.name === t.serverName))
|
mcpTools = mcpTools.filter((t) => enabledServers.some((m) => m.name === t.serverName))
|
||||||
} else {
|
} else {
|
||||||
mcpTools = []
|
// TODO enabledServers 存在bug,传入一直为undefined
|
||||||
|
// mcpTools = []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return mcpTools
|
return mcpTools
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user