@answerai/answeragent-mcp
Version:
A lightweight Model Context Protocol (MCP) server for Answer AI chatflow and document store management
110 lines (109 loc) • 3.6 kB
JavaScript
import * as chatflowTools from "./chatflows.js";
import * as documentStoreTools from "./document-stores.js";
import * as toolsApiTools from "./tools-api.js";
import * as assistantTools from "./assistants.js";
import * as documentLoaderTools from "./document-loaders.js";
// Get all available tools for listing
export const getAllTools = () => {
const tools = [];
// Add Chatflow Tools
for (const tool of Object.values(chatflowTools)) {
const schema = tool.inputSchema;
tools.push({
name: tool.name,
description: tool.description,
inputSchema: {
type: "object",
properties: schema.properties,
...(schema.required ? { required: schema.required } : {}),
},
});
}
// Add Document Store Tools
for (const tool of Object.values(documentStoreTools)) {
const schema = tool.inputSchema;
tools.push({
name: tool.name,
description: tool.description,
inputSchema: {
type: "object",
properties: schema.properties,
...(schema.required ? { required: schema.required } : {}),
},
});
}
// Add Tools API Tools
for (const tool of Object.values(toolsApiTools)) {
const schema = tool.inputSchema;
tools.push({
name: tool.name,
description: tool.description,
inputSchema: {
type: "object",
properties: schema.properties,
...(schema.required ? { required: schema.required } : {}),
},
});
}
// Add Assistant Tools
for (const tool of Object.values(assistantTools)) {
const schema = tool.inputSchema;
tools.push({
name: tool.name,
description: tool.description,
inputSchema: {
type: "object",
properties: schema.properties,
...(schema.required ? { required: schema.required } : {}),
},
});
}
// Add Document Loader Tools
for (const tool of Object.values(documentLoaderTools)) {
const schema = tool.inputSchema;
tools.push({
name: tool.name,
description: tool.description,
inputSchema: {
type: "object",
properties: schema.properties,
...(schema.required ? { required: schema.required } : {}),
},
});
}
return tools;
};
// Handle tool calls
export async function handleToolCall(name, args) {
// Check chatflow tools
for (const tool of Object.values(chatflowTools)) {
if (tool.name === name) {
return await tool.handler(args);
}
}
// Check document store tools
for (const tool of Object.values(documentStoreTools)) {
if (tool.name === name) {
return await tool.handler(args);
}
}
// Check tools API tools
for (const tool of Object.values(toolsApiTools)) {
if (tool.name === name) {
return await tool.handler(args);
}
}
// Check assistant tools
for (const tool of Object.values(assistantTools)) {
if (tool.name === name) {
return await tool.handler(args);
}
}
// Check document loader tools
for (const tool of Object.values(documentLoaderTools)) {
if (tool.name === name) {
return await tool.handler(args);
}
}
throw new Error(`Unknown tool: ${name}`);
}