MCP客户端库

一个开源的TypeScript库,用于连接任何LLM到任何MCP服务器,并构建具有工具访问权限的自定义代理。

  • by dforel
  • TypeScript
  • 本地部署
  • 13 星标
  • 更新于
在 GitHub 查看 dforel/easy-mcp-use

使用说明

英语|简体中文

Unified MCP Client Library for TypeScript

🌐 Easy MCP Use是一个用于连接的开源TypeScript库 任何LLM到任何MCP服务器 并构建具有工具访问权限的自定义代理,而无需使用闭源或应用程序客户端。

💡 让开发人员轻松地将任何LLM连接到具有完整TypeScript支持的web浏览、文件操作等工具。

特性

✨ 主要特点

特性 描述
🔄 易用性 只需6行TypeScript代码即可创建第一个支持MCP的代理
🤖 LLM灵活性 适用于任何支持LangChain并支持工具调用的LLM(OpenAI、Anthropic、Groq、LLama等)
🌐 HTTP支持 直接连接到在特定HTTP端口上运行的MCP服务器
⚙️ 动态服务器选择 TODO代理可以从可用池中为给定任务动态选择最合适的MCP服务器
🧩 多服务器支持 TODO在单个代理中同时使用多个MCP服务器
🛡️ 工具限制 TODO限制文件系统或网络访问等潜在危险的工具
📝 类型安全 TODO完全支持TypeScript,所有API和配置都有类型定义

快速启动

使用npm:

npm install easy-mcp-use

或者从源代码安装:

git clone https://github.com/dforel/easy-mcp-use.git
cd easy-mcp-use
npm install
npm run build

安装LangChain提供程序

通过LangChain,各种LLM提供商可以轻松使用mcp。您需要为您选择的LLM安装相应的LangChain提供程序包。例如:

# For OpenAI
npm install @langchain/openai

# For Anthropic
npm install @langchain/anthropic

# For other providers, check the [LangChain chat models documentation](https://js.langchain.com/docs/integrations/chat/)

并将您要使用的提供商的API密钥添加到您的 .env 文件。

OPENAI_API_KEY=
ANTHROPIC_API_KEY=

重要:只有具有工具调用功能的模型才能轻松使用mcp。确保您选择的模型支持函数调用或工具使用。

启动您的代理:


import { MCPClient } from 'easy-mcp-use';
import { MCPAgent, MCPAgentOptions } from 'easy-mcp-use';
import { ChatOpenAI } from '@langchain/openai';
import dotenv from 'dotenv';
dotenv.config();
 

const openAIApiKey = process.env.openRouteApiKey; 

if (!openAIApiKey) {
  throw new Error("openAIApiKey environment variable is not set");
}
console.log(`openAIApiKey: ${openAIApiKey}`);

async function main() {
    

  let config = {"mcpServers": {"http": {"url": "http://localhost:3001/sse"}}}
  // 从配置文件创建客户端
  const client = MCPClient.fromConfig( config );

  try { 
    const chat = new ChatOpenAI(
      {
        modelName: 'google/gemini-2.0-flash-exp:free', 
        streaming: true,
        openAIApiKey: openAIApiKey,
        configuration: {
          baseURL: 'https://openrouter.ai/api/v1',  
        }
      }
    );
    let options = {
      client: client,
      // verbose: true,
      maxSteps: 30, 
      llm:  chat,
    }
    let agent = new MCPAgent(options)

    let result = agent.run(
      `
      100 rmb can exchange how much doller?
      ` 
    );

     console.log( JSON.stringify(result) );
  } finally {
    // console.info('finally');
  }
}

main().catch(console.error);

您还可以从配置文件中添加服务器配置,如下所示:

const client = MCPClient.fromConfigFile(
    path.join(__dirname, 'browser_mcp.json')
);

配置文件示例(browser_mcp.json):

{
  "mcpServers": {
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"],
      "env": {
        "DISPLAY": ":1"
      }
    }
  }
}

有关其他设置、型号等信息,请查看文档。

示例用例

http服务器示例

有一个示例项目: 简单的mcp使用示例 (https://github.com/dforel/easy-mcp-use-examples)

用Playwright浏览网页

import { MCPClient } from '../src/client';
import path from 'path';
import { MCPAgent, MCPAgentOptions } from '../src/agents/mcpagent';
// import { ChatOpenAI } from 'langchain/core/language_models/chat_openai';
// import { OpenAI } from "@langchain/llms/openai";
import { ChatOpenAI } from '@langchain/openai';
import { logger } from '../src/logging';
import dotenv from 'dotenv';
dotenv.config();

const openAIApiKey = process.env.openAIApiKey; 
if (!openAIApiKey) {
  throw new Error("openAIApiKey environment variable is not set");
}
logger.info(`openAIApiKey: ${openAIApiKey}`);

async function main() {
  // 从配置文件创建客户端
  const client = await MCPClient.fromConfigFile(
    path.resolve(__dirname, './browser_mcp.json')
  );

  try { 
    const chat = new ChatOpenAI(
      {
        modelName: 'google/gemini-2.0-flash-exp:free', 
        // modelName: 'google/gemini-2.5-pro-exp-03-25:free', 
        streaming: true,
        openAIApiKey: openAIApiKey,
        configuration: {
          baseURL: 'https://openrouter.ai/api/v1',  
        }
      }
    );
    let options: MCPAgentOptions = {
      client: client,
      verbose: true,
      maxSteps: 30, 
      llm:  chat,
    }
    let agent = new MCPAgent(options)

    let result = agent.run(
      `
      open bing.com
      click input
      input easy-mcp-use
      click search
      ` 
    );

     console.log( JSON.stringify(result) );
  } finally {
    console.info('finally');
  }
}

main().catch(console.error);

此示例演示了如何连接到在特定HTTP端口上运行的MCP服务器。请确保在运行此示例之前启动MCP服务器。

多服务器支持

MCP Use TS允许使用 MCPClient这使得需要来自不同服务器的工具的复杂工作流程成为可能,例如与文件操作或3D建模相结合的网页浏览。

配置

您可以在配置文件中配置多个服务器:

{
  "mcpServers": {
    "airbnb": {
      "command": "npx",
      "args": ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"]
    },
    "playwright": {
      "command": "npx",
      "args": ["@playwright/mcp@latest"],
      "env": {
        "DISPLAY": ":1"
      }
    }
  }
}

用法

这 MCPClient 类提供了管理多个服务器连接的方法。创建时 MCPAgent,您可以提供 MCPClient 配置有多个服务器。

默认情况下,代理将可以从所有配置的服务器访问工具。如果需要为特定任务定位特定服务器,可以指定 serverName 当呼叫 agent.run() 方法。

// Example: Manually selecting a server for a specific task
const airbnbResult = await agent.run(
    'Search for Airbnb listings in Barcelona',
    { serverName: 'airbnb' } // Explicitly use the airbnb server
);

const googleResult = await agent.run(
    'Find restaurants near the first result using Google Search',
    { serverName: 'playwright' } // Explicitly use the playwright server
);

特定代理的详细描述

如果您只想查看代理的调试信息,而不启用完整的调试日志记录,则可以设置 verbose 创建MCPAgent时的参数:

// Create agent with increased verbosity
const agent = new MCPAgent({
    llm,
    client,
    verbose: true  // Only shows debug messages from the agent
});

当您只需要查看代理的步骤和决策过程,而不需要查看其他组件的所有低级调试信息时,这很有用。

路线图

  • [x] Multiple Servers at once
  • [x] Test remote connectors (http, ws)
  • [ ] ...

贡献

我们热爱贡献!对于bug或功能请求,请随时打开问题。

需求

  • Node.js 18+
  • TypeScript 5.0+
  • MCP实现(如Playwright MCP)
  • LangChain和适当的模型库(OpenAI、Anthropic等)

引用

如果您在研究或项目中使用MCP use TS,请引用:

@software{easy-mcp-use,
  author = {dforel},
  title = {Easy-MCP-Use: MCP Library for TypeScript},
  year = {2025},
  publisher = {GitHub},
  url = {https://github.com/dforel/easy-mcp-use}
}

其他

这个项目是 mcp使用

我希望你喜欢

许可证

麻省理工学院