Bot commited on
Commit
c27c221
·
1 Parent(s): ca02f17

fix: resolve Promise mapping bug in loadMcpTools and loadWorkFlowTools to correctly load and connect configured MCP servers

Browse files
src/app/api/chat/route.ts CHANGED
@@ -202,25 +202,13 @@ export async function POST(request: Request) {
202
 
203
  const stream = createUIMessageStream({
204
  execute: async ({ writer: dataStream }) => {
205
- const MCP_TOOLS = await safe()
206
- .map(errorIf(() => !isToolCallAllowed && "Not allowed"))
207
- .map(() =>
208
- loadMcpTools({
209
- mentions,
210
- allowedMcpServers,
211
- }),
212
- )
213
- .orElse({});
214
 
215
- const WORKFLOW_TOOLS = await safe()
216
- .map(errorIf(() => !isToolCallAllowed && "Not allowed"))
217
- .map(() =>
218
- loadWorkFlowTools({
219
- mentions,
220
- dataStream,
221
- }),
222
- )
223
- .orElse({});
224
 
225
  const APP_DEFAULT_TOOLS = await safe()
226
  .map(errorIf(() => !isToolCallAllowed && "Not allowed"))
 
202
 
203
  const stream = createUIMessageStream({
204
  execute: async ({ writer: dataStream }) => {
205
+ const MCP_TOOLS = isToolCallAllowed
206
+ ? await loadMcpTools({ mentions, allowedMcpServers })
207
+ : {};
 
 
 
 
 
 
208
 
209
+ const WORKFLOW_TOOLS = isToolCallAllowed
210
+ ? await loadWorkFlowTools({ mentions, dataStream })
211
+ : {};
 
 
 
 
 
 
212
 
213
  const APP_DEFAULT_TOOLS = await safe()
214
  .map(errorIf(() => !isToolCallAllowed && "Not allowed"))
src/app/api/chat/shared.chat.ts CHANGED
@@ -393,26 +393,28 @@ export const workflowToVercelAITools = (
393
  );
394
  };
395
 
396
- export const loadMcpTools = (opt?: {
397
  mentions?: ChatMention[];
398
  allowedMcpServers?: Record<string, AllowedMCPServer>;
399
- }) =>
400
- safe(() => mcpClientsManager.tools())
401
- .map((tools) => {
402
- if (opt?.mentions?.length) {
403
- return filterMCPToolsByMentions(tools, opt.mentions);
404
- }
405
- return filterMCPToolsByAllowedMCPServers(tools, opt?.allowedMcpServers);
406
- })
407
- .orElse({} as Record<string, VercelAIMcpTool>);
 
 
408
 
409
- export const loadWorkFlowTools = (opt: {
410
  mentions?: ChatMention[];
411
  dataStream: UIMessageStreamWriter;
412
- }) =>
413
- safe(() =>
414
- opt?.mentions?.length
415
- ? workflowRepository.selectToolByIds(
416
  opt?.mentions
417
  ?.filter((m) => m.type == "workflow")
418
  .map(
@@ -420,10 +422,12 @@ export const loadWorkFlowTools = (opt: {
420
  (v as Extract<ChatMention, { type: "workflow" }>).workflowId,
421
  ),
422
  )
423
- : [],
424
- )
425
- .map((tools) => workflowToVercelAITools(tools, opt.dataStream))
426
- .orElse({} as Record<string, VercelAIWorkflowTool>);
 
 
427
 
428
  export const loadAppDefaultTools = (opt?: {
429
  mentions?: ChatMention[];
 
393
  );
394
  };
395
 
396
+ export const loadMcpTools = async (opt?: {
397
  mentions?: ChatMention[];
398
  allowedMcpServers?: Record<string, AllowedMCPServer>;
399
+ }) => {
400
+ try {
401
+ const tools = await mcpClientsManager.tools();
402
+ if (opt?.mentions?.length) {
403
+ return filterMCPToolsByMentions(tools, opt.mentions);
404
+ }
405
+ return filterMCPToolsByAllowedMCPServers(tools, opt?.allowedMcpServers);
406
+ } catch (e) {
407
+ return {} as Record<string, VercelAIMcpTool>;
408
+ }
409
+ };
410
 
411
+ export const loadWorkFlowTools = async (opt: {
412
  mentions?: ChatMention[];
413
  dataStream: UIMessageStreamWriter;
414
+ }) => {
415
+ try {
416
+ const tools = opt?.mentions?.length
417
+ ? await workflowRepository.selectToolByIds(
418
  opt?.mentions
419
  ?.filter((m) => m.type == "workflow")
420
  .map(
 
422
  (v as Extract<ChatMention, { type: "workflow" }>).workflowId,
423
  ),
424
  )
425
+ : [];
426
+ return workflowToVercelAITools(tools, opt.dataStream);
427
+ } catch (e) {
428
+ return {} as Record<string, VercelAIWorkflowTool>;
429
+ }
430
+ };
431
 
432
  export const loadAppDefaultTools = (opt?: {
433
  mentions?: ChatMention[];