Skip to content

Commit

Permalink
feat: Assistants Support for MCP Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-avila committed Dec 17, 2024
1 parent 05d7892 commit 884c714
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
5 changes: 3 additions & 2 deletions api/app/clients/tools/util/handleTools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { Providers } = require('@librechat/agents');
const { Tools, Constants } = require('librechat-data-provider');
const { SerpAPI } = require('@langchain/community/tools/serpapi');
const { Calculator } = require('@langchain/community/tools/calculator');
Expand Down Expand Up @@ -152,6 +151,7 @@ const loadToolWithAuth = (userId, authFields, ToolConstructor, options = {}) =>
* @param {string} object.user
* @param {Agent} [object.agent]
* @param {string} [object.model]
* @param {EModelEndpoint} [object.endpoint]
* @param {LoadToolOptions} [object.options]
* @param {boolean} [object.useSpecs]
* @param {Array<string>} object.tools
Expand All @@ -163,6 +163,7 @@ const loadTools = async ({
user,
agent,
model,
endpoint,
useSpecs,
tools = [],
options = {},
Expand Down Expand Up @@ -264,7 +265,7 @@ const loadTools = async ({
req: options.req,
toolKey: tool,
model: agent?.model ?? model,
provider: agent?.provider ?? Providers.OPENAI,
provider: agent?.provider ?? endpoint,
});
continue;
}
Expand Down
16 changes: 12 additions & 4 deletions api/server/services/MCP.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { Constants: AgentConstants } = require('@librechat/agents');
const { Constants, convertJsonSchemaToZod } = require('librechat-data-provider');
const { tool } = require('@langchain/core/tools');
const { Constants: AgentConstants } = require('@librechat/agents');
const {
Constants,
convertJsonSchemaToZod,
isAssistantsEndpoint,
} = require('librechat-data-provider');
const { logger, getMCPManager } = require('~/config');

/**
Expand All @@ -9,7 +13,7 @@ const { logger, getMCPManager } = require('~/config');
* @param {Object} params - The parameters for loading action sets.
* @param {ServerRequest} params.req - The name of the tool.
* @param {string} params.toolKey - The toolKey for the tool.
* @param {import('@librechat/agents').Providers} params.provider - The provider for the tool.
* @param {import('@librechat/agents').Providers | EModelEndpoint} params.provider - The provider for the tool.
* @param {string} params.model - The model for the tool.
* @returns { Promise<typeof tool | { _call: (toolInput: Object | string) => unknown}> } An object with `_call` method to execute the tool input.
*/
Expand All @@ -27,7 +31,11 @@ async function createMCPTool({ req, toolKey, provider }) {
const _call = async (toolInput) => {
try {
const mcpManager = await getMCPManager();
return await mcpManager.callTool(serverName, toolName, provider, toolInput);
const result = await mcpManager.callTool(serverName, toolName, provider, toolInput);
if (isAssistantsEndpoint(provider) && Array.isArray(result)) {
return result[0];
}
return result;
} catch (error) {
logger.error(`${toolName} MCP server tool call failed`, error);
return `${toolName} MCP server tool call failed.`;
Expand Down
1 change: 1 addition & 0 deletions api/server/services/ToolService.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ async function processRequiredActions(client, requiredActions) {
model: client.req.body.model ?? 'gpt-4o-mini',
tools,
functions: true,
endpoint: client.req.body.endpoint,
options: {
processFileURL,
req: client.req,
Expand Down
6 changes: 6 additions & 0 deletions api/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,12 @@
* @memberof typedefs
*/

/**
* @exports EModelEndpoint
* @typedef {import('librechat-data-provider').EModelEndpoint} EModelEndpoint
* @memberof typedefs
*/

/**
* @exports TAttachment
* @typedef {import('librechat-data-provider').TAttachment} TAttachment
Expand Down

0 comments on commit 884c714

Please sign in to comment.