|
| 1 | +/** |
| 2 | + * Dynamic server management example for mcp-use. |
| 3 | + * |
| 4 | + * This example demonstrates how to equip an MCPAgent with a tool |
| 5 | + * to dynamically add and connect to MCP servers during a run. |
| 6 | + */ |
| 7 | + |
| 8 | +import { ChatOpenAI } from '@langchain/openai' |
| 9 | +import { config } from 'dotenv' |
| 10 | +import { MCPAgent, MCPClient } from '../index.js' |
| 11 | +import { LangChainAdapter } from '../src/adapters/langchain_adapter.js' |
| 12 | +import { ServerManager } from '../src/managers/server_manager.js' |
| 13 | + |
| 14 | +// Load environment variables from .env file |
| 15 | +config() |
| 16 | + |
| 17 | +async function main() { |
| 18 | + // Create an empty MCPClient. It has no servers to start with. |
| 19 | + const client = new MCPClient() |
| 20 | + |
| 21 | + // The LLM to power the agent |
| 22 | + const llm = new ChatOpenAI({ model: 'gpt-4o', temperature: 0 }) |
| 23 | + |
| 24 | + // Create the agent, enabling the ServerManager |
| 25 | + const agent = new MCPAgent({ |
| 26 | + llm, |
| 27 | + client, |
| 28 | + maxSteps: 30, |
| 29 | + useServerManager: true, |
| 30 | + serverManagerFactory: client => new ServerManager(client, new LangChainAdapter()), |
| 31 | + autoInitialize: true, |
| 32 | + }) |
| 33 | + |
| 34 | + // Define the server configuration that the agent will be asked to add. |
| 35 | + const serverConfigA = { |
| 36 | + command: 'npx', |
| 37 | + args: ['@playwright/mcp@latest'], |
| 38 | + env: { |
| 39 | + DISPLAY: ':1', |
| 40 | + }, |
| 41 | + } |
| 42 | + const serverConfigB = { |
| 43 | + command: 'npx', |
| 44 | + args: ['-y', '@openbnb/mcp-server-airbnb', '--ignore-robots-txt'], |
| 45 | + } |
| 46 | + // We'll pass the config as a JSON string in the prompt. |
| 47 | + const serverConfigStringA = JSON.stringify(serverConfigA, null, 2) |
| 48 | + const serverConfigStringB = JSON.stringify(serverConfigB, null, 2) |
| 49 | + |
| 50 | + const query = `I need to browse the web. To do this, please add and connect to a new MCP server for Playwright. |
| 51 | + The server name is 'playwright' and its configuration is: |
| 52 | + \`\`\`json |
| 53 | + ${serverConfigStringA} |
| 54 | + \`\`\` |
| 55 | + Once the server is ready, navigate to https://github.com/mcp-use/mcp-use, give a star to the project, and then provide a concise summary of the project's README. |
| 56 | +
|
| 57 | + Then, please add and connect to a new MCP server for Airbnb. |
| 58 | + The server name is 'airbnb' and its configuration is: |
| 59 | + \`\`\`json |
| 60 | + ${serverConfigStringB} |
| 61 | + \`\`\` |
| 62 | + and give me a house in the location of the company mcp-use. |
| 63 | + ` |
| 64 | + |
| 65 | + // Run the agent. It will first use the AddMCPServerTool, then the tools from the new server. |
| 66 | + const result = await agent.run(query) |
| 67 | + |
| 68 | + console.log(`\n✅ Final Result:\n${result}`) |
| 69 | + |
| 70 | + // Clean up the session created by the agent |
| 71 | + await client.closeAllSessions() |
| 72 | +} |
| 73 | + |
| 74 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 75 | + main().catch(console.error) |
| 76 | +} |
0 commit comments