How to Use LlamaIndex.TS to Orchestrate MCP Servers
In this post, we’ll demonstrate how to orchestrate Model Context Protocol (MCP) servers using llamaindex.TS in a real-world TypeScript application. We’ll use the Azure AI Travel Agents project as our base, focusing on best practices for secure, scalable,…
In this post, we’ll demonstrate how to orchestrate Model Context Protocol (MCP) servers using llamaindex.TS in a real-world TypeScript application. We’ll use the Azure AI Travel Agents project as our base, focusing on best practices for secure, scalable, and maintainable orchestration. Feel free to star the repo to get notified of the latest changes. If you are interested in an overview of the Azure AI Travel Agents project, please read our announcement blog! Why llamaindex.TS and MCP? llamaindex.TS provides a modular, composable framework for building LLM-powered applications in TypeScript. MCP enables tool interoperability and streaming, making it ideal for orchestrating multiple AI services. Project Structure The Llamaindex.TS orchestrator lives in src/api/src/orchestrator/llamaindex, with provider modules for different LLM backends and MCP clients. We currently support: Azure OpenAI Docker Models Azure AI Foundry Local Github Model Ollama Feel free to explore the codebase and suggest more providers. // filepath: src/api/src/mcp/mcp- import EventEmitter from 'node:events'; import from '@modelcontextprotocol/sdk/client/index.js'; import from '@modelcontextprotocol/sdk/client/streamable export class MCPClient extends EventEmitter ` } : , }); this.client = new Client(serverName, this.transport); } async connect() async listTools() async callTool(name: string, toolArgs: any) async close() } Best Practice: Always pass the Authorization header for secure access, as shown above. Calling an MCP Tool Manually Suppose you want to get destination recommendations from the MCP server: import from '../../mcp/mcp- const DESTINATION_SERVER_URL = process.env.MCP_DESTINATION_RECOMMENDATION_URL!; const ACCESS_TOKEN = process.env.MCP_DESTINATION_RECOMMENDATION_ACCESS_TOKEN; const mcpClient = new MCPClient('destination-recommendation', DESTINATION_SERVER_URL, ACCESS_TOKEN); await mcpClient.connect(); const tools = await mcpClient.listTools(); console.log('Available tools:', tools); const result = await mcpClient.callTool('getDestinationsByPreferences', ); console.log('Recommended destinations:', result); await mcpClient.close(); Tip: Always close the MCP client gracefully to release resources. Orchestrating LLMs and MCP Tools With Llamaindex.TS The mcp client from <> makes it easy to connect to MCP servers and retrieve tool definitions dynamically. Below is a sample from the project’s orchestrator setup, showing how to use mcp to fetch tools and create agents for each MCP server. Here is an example of what an mcpServerConfig object might look like:const mcpServerConfig = ; You can then use this config with the mcp client:import from "@llamaindex/tools"; import from "llamaindex"; // ...existing code... const mcpServerConfig = mcpToolsConfig["echo-ping"].config; const tools = await mcp(mcpServerConfig).tools(); const echoAgent = agent(); agentsList.push(echoAgent); handoffTargets.push(echoAgent); toolsList.push(...tools); // ...other code... const travelAgent = agent(); agentsList.push(travelAgent); // Create the multi-agent workflow return multiAgent(); You can repeat this pattern to compose a multi-agent workflow where each agent is powered by tools discovered at runtime from the MCP server. See project for a full example. You can then use this LLM instance to orchestrate calls to MCP tools, such as itinerary planning or destination recommendation. Security Considerations Always use access tokens and secure headers. Never hardcode secrets; use environment variables and secret managers. Join the Community: We encourage you to join our Azure AI Foundry Developer Community to share your experiences, ask questions, and get support: aka.ms/foundry/discord Join our Discord community for real-time discussions and support. aka.ms/foundry/forum - Visit our Azure AI Foundry Developer Forum to ask questions and share your knowledge. Conclusion By combining llamaindex.TS with MCP’s Streamable HTTP transport, you can orchestrate powerful, secure, and scalable AI workflows in TypeScript. The Azure AI Travel Agents project provides a robust template for building your own orchestrator. References: llamaindex.TS Documentation MCP Streamable HTTP Spec Azure AI Travel Agents Sample
Based on reporting by hackernoon.com.
In this post, we’ll demonstrate how to orchestrate Model Context Protocol (MCP) servers using llamaindex.TS in a real-world TypeScript application.
