Feature/MCP (Model Context Protocol) (#4134)

add mcp tools
This commit is contained in:
Henry Heng
2025-03-06 13:57:18 +00:00
committed by GitHub
parent 9c22bee991
commit 713ed26971
21 changed files with 1321 additions and 105 deletions
+1
View File
@@ -167,6 +167,7 @@ export interface IUsedTool {
toolInput: object
toolOutput: string | object
sourceDocuments?: ICommonObject[]
error?: string
}
export interface IMultiAgentNode {
+15
View File
@@ -24,6 +24,7 @@ import {
} from 'langchain/agents'
import { formatLogToString } from 'langchain/agents/format_scratchpad/log'
import { IUsedTool } from './Interface'
import { getErrorMessage } from './error'
export const SOURCE_DOCUMENTS_PREFIX = '\n\n----FLOWISE_SOURCE_DOCUMENTS----\n\n'
export const ARTIFACTS_PREFIX = '\n\n----FLOWISE_ARTIFACTS----\n\n'
@@ -463,7 +464,21 @@ export class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
throw e
}
observation = await new ExceptionTool().call(observation, runManager?.getChild())
usedTools.push({
tool: tool.name,
toolInput: action.toolInput as any,
toolOutput: '',
error: getErrorMessage(e)
})
return { action, observation: observation ?? '' }
} else {
usedTools.push({
tool: tool.name,
toolInput: action.toolInput as any,
toolOutput: '',
error: getErrorMessage(e)
})
return { action, observation: getErrorMessage(e) }
}
}
if (typeof observation === 'string' && observation.includes(SOURCE_DOCUMENTS_PREFIX)) {
+25
View File
@@ -0,0 +1,25 @@
type ErrorWithMessage = {
message: string
}
const isErrorWithMessage = (error: unknown): error is ErrorWithMessage => {
return (
typeof error === 'object' && error !== null && 'message' in error && typeof (error as Record<string, unknown>).message === 'string'
)
}
const toErrorWithMessage = (maybeError: unknown): ErrorWithMessage => {
if (isErrorWithMessage(maybeError)) return maybeError
try {
return new Error(JSON.stringify(maybeError))
} catch {
// fallback in case there's an error stringifying the maybeError
// like with circular references for example.
return new Error(String(maybeError))
}
}
export const getErrorMessage = (error: unknown) => {
return toErrorWithMessage(error).message
}