Skip to content

MCPServerStreamableHttp: Connecting multiple times is causing timeout error #459

@gianluca-henze-parloa

Description

@gianluca-henze-parloa
  • Have you read the docs? Agents SDK docs
    Yes
  • Have you searched for related issues? Others may have had similar requests
    Yes

Question

Hi there,
I am currently facing a problem with the MCPServerStreamableHttp, where I want to connect 7 different agents to the same MCP and filter per agent. Only creating one connection doesn't work, as the filter than overrides the filter of others. Therfore I am connecting 7 times, the problem - first time I am connecting always just one random agent Connects and I have to wait 60 seconds for the timeout. After that all agents instantly connect and work as they should the whole time. That is extremly frustratig and I would love to solve it.

import { createCheckoutAgent } from './checkout';
import { createEventAgent } from './event';
import { createFaqAgent } from './faq';
import { createGreeterAgent } from './greeter';
import { MCPServerStreamableHttp, createMCPToolStaticFilter } from '@openai/agents';
import { Logger } from '@nestjs/common';
import { createFlightsAgent } from './flights';
import { createRestaurantsAgent } from './restaurants';
import { createHotelsAgent } from './hotels';

export const createTravelScenario = async (
  companyName: string,
  conversationId: string,
  memory?: string
) => {
  const logger = new Logger('TravelScenario');

  const agentToolConfigs = {
    Greeter: {
      allowed: ['show_destinations'],
      blocked: [] as string[],
    },
    Event: {
      allowed: ['find_tickets', 'find_events', 'book_trip'],
      blocked: [] as string[],
    },
    FAQ: {
      allowed: ['travel_faq'],
      blocked: [] as string[],
    },
    Checkout: {
      allowed: ['remove_from_cart_travel', 'show_travel_cart', 'request_confirmation'],
      blocked: [] as string[],
    },
    Hotels: {
      allowed: ['find_hotels', 'book_trip', 'show_travel_cart'],
      blocked: [] as string[],
    },
    Flights: {
      allowed: ['find_flights', 'book_trip', 'show_travel_cart'],
      blocked: [] as string[],
    },
    Restaurants: {
      allowed: ['find_restaurants', 'send_email'],
      blocked: [] as string[],
    },
  };

  // Create filtered MCP servers for each agent (official toolFilter API)
  const mcpServerUrl = process.env.FRONTEND_URL || 'http://localhost:3000';
  const mcpUrl = `${mcpServerUrl}/api/mcp?conversationId=${encodeURIComponent(conversationId)}`;
  const createFilteredMcpServer = async (
    agentName: keyof typeof agentToolConfigs
  ) => {
    const server = new MCPServerStreamableHttp({
      url: mcpUrl,
      name: agentName,
      cacheToolsList: true,
      toolFilter: createMCPToolStaticFilter({
        allowed: agentToolConfigs[agentName].allowed,
        blocked: agentToolConfigs[agentName].blocked,
      }),
    });
    await server.connect();
    logger.log(`✅ MCP Server connected for ${agentName} agent`);
    return server;
  };

  const [greeterMcp, eventMcp, faqMcp, checkoutMcp, hotelsMcp, flightsMcp, restaurantsMcp] =
    await Promise.all([
      createFilteredMcpServer('Greeter'),
      createFilteredMcpServer('Event'),
      createFilteredMcpServer('FAQ'),
      createFilteredMcpServer('Checkout'),
      createFilteredMcpServer('Hotels'),
      createFilteredMcpServer('Flights'),
      createFilteredMcpServer('Restaurants'),
    ]);

  // Create agents with their respective filtered MCP servers
  const greeter = createGreeterAgent(companyName, greeterMcp, memory);
  const event = createEventAgent(companyName, eventMcp, memory);
  const faq = createFaqAgent(companyName, faqMcp, memory);
  const checkout = createCheckoutAgent(companyName, checkoutMcp, memory);
  const hotels = createHotelsAgent(companyName, hotelsMcp, memory);
  const flights = createFlightsAgent(companyName, flightsMcp, memory);
  const restaurants = createRestaurantsAgent(companyName, restaurantsMcp, memory);

  greeter.handoffs = [hotels, flights, restaurants, event, faq, checkout];
  event.handoffs = [hotels, flights, restaurants, faq, checkout];
  hotels.handoffs = [flights, restaurants, event, faq, checkout];
  flights.handoffs = [restaurants, event, faq, checkout];
  restaurants.handoffs = [event, faq, checkout];
  faq.handoffs = [event, checkout];
  checkout.handoffs = [faq, event, hotels, flights, restaurants];

  return [greeter, event, faq, checkout, hotels, flights, restaurants];
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions