This content originally appeared on DEV Community and was authored by Zane
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
Create a .env
file:
OPENAI_API_KEY=your_api_key
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);
Using Configuration Files
Manage MCP server configurations in a JSON file:
{
"mcpServers": {
"playwright": {
"command": "npx",
"args": ["@playwright/mcp@latest"]
}
}
}
Load the configuration file:
import { MCPClient } from 'mcp-use';
const client = MCPClient.fromConfigFile('./mcp-config.json');
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');
Fine-Grained Permission Control
const agent = new MCPAgent({
llm,
client,
disallowedTools: ['file_system', 'network']
});
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!
This content originally appeared on DEV Community and was authored by Zane