πŸš€ Easily Connect LLMs and MCP Servers with mcp-use (TypeScript)

πŸš€ Easily Connect LLMs and MCP Servers with mcp-use (TypeScript)


Image description

Integrating Large Language Models (LLMs) with external services and tools efficiently is a common challenge developers face. Enter mcp-use, an open-source TypeScript library designed to simplify this integration, allowing you to quickly and safely connect any LangChain.js-compatible LLM with MCP servers, building powerful, flexible AI agents.



🌟 Why Choose mcp-use?

mcp-use provides a unified and simple interface to help you rapidly develop intelligent agents capable of calling various tools, freeing you from complex integration tasks and enabling you to focus on your core logic.



✨ Key Features:

  • 🚦 Extreme Simplicity: Create functional intelligent agents in just a few lines of code.
  • πŸ”§ LLM Flexibility: Compatible with any LangChain.js-supported LLM that supports tool calls.
  • 🌐 HTTP/SSE Support: Easily connect to MCP servers.
  • πŸ”„ Dynamic Server Selection: Agents dynamically choose the most suitable server in real-time.
  • 🧩 Multi-Server Support: Effortlessly use multiple MCP servers simultaneously.
  • πŸ”’ Fine-Grained Tool Permissions: Restrict sensitive or potentially dangerous tool calls.
  • πŸ› οΈ Fully Customizable: Create custom agents based on LangChain.js or implement new adapters.



⚑ Quick Start



πŸ“¦ Install Dependencies

npm install mcp-use langchain @langchain/openai dotenv
Enter fullscreen mode

Exit fullscreen mode

Create a .env file:

OPENAI_API_KEY=your_api_key
Enter fullscreen mode

Exit fullscreen mode



πŸ› οΈ Example Code

import { ChatOpenAI } from '@langchain/openai';
import { MCPAgent, MCPClient } from 'mcp-use';
import 'dotenv/config';

async function main() {
  const config = {
    mcpServers: {
      playwright: { command: 'npx', args: ['@playwright/mcp@latest'] }
    }
  };

  const client = MCPClient.fromDict(config);
  const llm = new ChatOpenAI({ modelName: 'gpt-4o' });

  const agent = new MCPAgent({ llm, client, maxSteps: 20 });
  const result = await agent.run('Find the best restaurant in Tokyo using Google Search');

  console.log('πŸ” Result:', result);
}

main().catch(console.error);
Enter fullscreen mode

Exit fullscreen mode



πŸ—‚οΈ Using Configuration Files

Manage MCP server configurations in a JSON file:

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"]
    }
  }
}
Enter fullscreen mode

Exit fullscreen mode

Load the configuration file:

import { MCPClient } from 'mcp-use';
const client = MCPClient.fromConfigFile('./mcp-config.json');
Enter fullscreen mode

Exit fullscreen mode



🌍 Advanced Multi-Server Usage

const config = {
  mcpServers: {
    airbnb: { command: 'npx', args: ['@openbnb/mcp-server-airbnb'] },
    playwright: { command: 'npx', args: ['@playwright/mcp@latest'] }
  }
};

const client = MCPClient.fromDict(config);
const agent = new MCPAgent({ llm, client, useServerManager: true });
await agent.run('Search Airbnb in Barcelona, then Google restaurants nearby');
Enter fullscreen mode

Exit fullscreen mode



πŸ” Fine-Grained Permission Control

const agent = new MCPAgent({
  llm,
  client,
  disallowedTools: ['file_system', 'network']
});
Enter fullscreen mode

Exit fullscreen mode



πŸ“– Contribute

mcp-use is fully open-source, and contributions via issues or pull requests are welcome!

If you find mcp-use helpful, consider giving the project a GitHub star. Your support motivates continuous improvement!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *