Sa post na ito, i-demonstrate kung paano i-orchester ang mga server ng Model Context Protocol (MCP) gamit ang sa isang real-world na application ng TypeScript. Kami ay gumagamit ng Ang project ay ang aming base, na nag-focus sa mga pinakamahusay na mga kasanayan para sa safe, scalable, at malaman na orchestration. Pumunta sa repo upang makipag-ugnay sa mga pinakabagong mga pagbabago. Mga pahinang tumuturo Mga Mapagkukunan ng Azure Travel Kung interesado ka sa isang review ng proyekto ng Azure AI Travel Agents, tingnan mo ang aming blog na inihayag! ang blog announcement Bakit ang llamaindex.TS at MCP? llamaindex.TS ay nagbibigay ng isang modular, composable framework para sa pagbuo ng LLM-powered application sa TypeScript. Ang MCP ay nagbibigay ng tool interoperability at streaming, na gumagawa ng ito para sa orchestrating ng maraming mga serbisyo ng AI. Structure ng Proyekto Ang Llamaindex.TS orchestrator ay nakatira sa , na may mga module ng provider para sa iba't ibang mga backend ng LLM at mga client ng MCP. Kami ngayon ay sumusuporta: src/api/src/orchestrator/llamaindex Maghanap ng OpenAI Mga Modelo ng Docker Mga pahinang tumuturo sa Azure Mga Modelo ng GitHub Ilang Maging libre upang i-explore ang codebase at . Maghanap ng higit pang mga provider I-configure ang MCP Client sa Streamable HTTP Upang makipag-ugnayan sa mga server ng MCP, nang hindi gamitin ang Llamaindex.TS, maaari naming mag-script ng isang custom implementation gamit ang para sa efficient, authenticated, at streaming komunikasyon. StreamableHTTPClientTransport // filepath: src/api/src/mcp/mcp-http-client.ts import EventEmitter from 'node:events'; import { Client } from '@modelcontextprotocol/sdk/client/index.js'; import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'; export class MCPClient extends EventEmitter { private client: Client; private transport: StreamableHTTPClientTransport; constructor(serverName: string, serverUrl: string, accessToken?: string) { this.transport = new StreamableHTTPClientTransport({ url: serverUrl, headers: accessToken ? { Authorization: `Bearer ${accessToken}` } : {}, }); this.client = new Client(serverName, this.transport); } async connect() { await this.client.initialize(); } async listTools() { return this.client.listTools(); } async callTool(name: string, toolArgs: any) { return this.client.callTool(name, toolArgs); } async close() { await this.client.closeGracefully(); } } Always pass ang ang header para sa secure access, tulad ng ipinapakita sa itaas. Best Practice: Authorization I-call ang isang MCP tool manually Kailangan mong makakuha ng mga rekomendasyon ng destinasyon mula sa MCP server: import { MCPClient } from '../../mcp/mcp-http-client'; 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', { activity: 'CULTURAL', budget: 'MODERATE', season: 'SUMMER', familyFriendly: true, }); console.log('Recommended destinations:', result); await mcpClient.close(); Always close ang MCP client gracefully upang i-release ang mga resource. Tip: Pag-orchester ng mga LLM at mga tool ng MCP na may Llamaindex.TS ang Customer sa ay madaling i-connect sa mga server ng MCP at i-recover ang mga definisyon ng tool dinamis. Ang ibaba ay isang sample mula sa proyekto ng orchestrator setup, na nagpapakita kung paano gamitin ang upang makakuha ng mga tool at lumikha ng mga agent para sa bawat server ng MCP. mcp @llamaindex/tools mcp Narito ang isang halimbawa ng isang Ang object ay maaaring maging: mcpServerConfig const mcpServerConfig = { url: "http://localhost:5007", // MCP server endpoint accessToken: process.env.MCP_ECHO_PING_ACCESS_TOKEN, // Secure token from env name: "echo-ping", // Logical name for the server }; Pagkatapos ay maaari mong gamitin ang config na ito ang client: mcp import { mcp } from "@llamaindex/tools"; import { agent, multiAgent, ToolCallLLM } from "llamaindex"; // ...existing code... const mcpServerConfig = mcpToolsConfig["echo-ping"].config; const tools = await mcp(mcpServerConfig).tools(); const echoAgent = agent({ name: "EchoAgent", systemPrompt: "Echo back the received input. Do not respond with anything else. Always call the tools.", tools, llm, verbose, }); agentsList.push(echoAgent); handoffTargets.push(echoAgent); toolsList.push(...tools); // ...other code... const travelAgent = agent({ name: "TravelAgent", systemPrompt: "Acts as a triage agent to determine the best course of action for the user's query. If you cannot handle the query, please pass it to the next agent. If you can handle the query, please do so.", tools: [...toolsList], canHandoffTo: handoffTargets .map((target) => target.getAgents().map((agent) => agent.name)) .flat(), llm, verbose, }); agentsList.push(travelAgent); // Create the multi-agent workflow return multiAgent({ agents: agentsList, rootAgent: travelAgent, verbose, }); Maaari mong i-repeat ang pattern na ito upang lumikha ng isang workflow ng multi-agent na kung saan ang bawat agent ay na-powered sa pamamagitan ng mga tool na natagpuan sa runtime mula sa server ng MCP. . isang full example Pagkatapos ay maaari mong gamitin ang instance ng LLM na ito upang i-orchestrate ang mga call sa mga tool ng MCP, tulad ng itinerary planning o destination recommendation. Security Considerations ang Always gamitin ang access tokens at safe headers. Huwag i-hardcode secrets; gamitin ang mga environment variables at secret managers. Pumunta sa komunidad: I-encourage mo na sumali sa aming Azure AI Foundry Developer Community upang i-share ang iyong mga karanasan, mag-asawa ng mga tanong, at makakuha ng suporta: aka.ms/foundry/discord Pumunta sa aming komunidad ng Discord para sa real-time na mga pag-uusap at suporta. aka.ms/foundry/forum - Bisitahin ang aming Azure AI Foundry Developer Forum upang mag-asawa ng mga tanong at ibahagi ang iyong karanasan. Konklusyon Sa pamamagitan ng paghahambing ng llamaindex.TS sa MCP's Streamable HTTP transportation, maaari mong orchestrating malakas, malakas at scalable AI workflows sa TypeScript. Ang proyekto ng Azure AI Travel Agents ay nagbibigay ng isang malakas na template para sa pagbuo ng iyong sarili na orchestrator. References: Mga pahinang tumuturo sa llamaindex.TS Mga pahinang tumuturo sa HTTP Spec Mga Mapagkukunan ng Azure Travel Agent