mcp-use
Version:
A utility library for integrating Model Context Protocol (MCP) with LangChain, Zod, and related tools. Provides helpers for schema conversion, event streaming, and SDK usage.
47 lines (46 loc) • 1.69 kB
JavaScript
/**
* Basic usage example for mcp-use.
*
* This example demonstrates how to use the mcp-use library with MCPClient
* to connect any LLM to MCP tools through a unified interface.
*
* Special thanks to https://github.com/microsoft/playwright-mcp for the server.
*/
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ChatOpenAI } from '@langchain/openai';
import { config } from 'dotenv';
import { MCPAgent, MCPClient } from '../index.js';
// Load environment variables from .env file
config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
const config = {
mcpServers: {
playwright: {
command: 'npx',
args: ['@playwright/mcp@latest'],
env: {
DISPLAY: ':1',
},
},
},
};
// Create MCPClient from config file
const client = new MCPClient(config);
// Create LLM
const llm = new ChatOpenAI({ model: 'gpt-4o' });
// const llm = init_chat_model({ model: "llama-3.1-8b-instant", model_provider: "groq" })
// const llm = new ChatAnthropic({ model: "claude-3-" })
// const llm = new ChatGroq({ model: "llama3-8b-8192" })
// Create agent with the client
const agent = new MCPAgent({ llm, client, maxSteps: 30 });
// Run the query
const result = await agent.run(`Navigate to https://github.com/mcp-use/mcp-use, give a star to the project and write
a summary of the project.`, 30);
console.error(`\nResult: ${result}`);
}
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}