Skip to content

precariat365/MCP-Client-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP Client Demo

这是一个基于LangChain和MCP (Model Control Protocol) 的智能客户端示例项目,展示了如何使用LangChain与多个MCP服务进行交互,实现智能对话和工具调用功能。

功能特点

  • 支持多服务器MCP客户端配置
  • 集成LangChain的ReAct Agent功能
  • 提供工具列表和资源列表查询
  • 包含天气查询和产品知识库访问示例
  • 支持异步操作

安装配置

环境要求

  • Python >= 3.12
  • 依赖包:详见pyproject.toml

安装步骤

  1. 克隆项目并安装依赖:
git clone <repository-url>
cd mcp-client
uv pip install .
  1. 配置环境变量 创建.env文件,添加以下配置:
MODEL=<your-model-name>
BASE_URL=<your-base-url>
OPENAI_API_KEY=<your-api-key>
  1. 配置MCP服务器 在config.json中配置MCP服务器信息:
{
  "mcpServers": {
    "SmartCustomerSupportMCP": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/SmartCustomerSupportMCP",
        "run",
        "mcp-smart-customer-support"
      ]
    },
    "weather": {
      "url": "http://localhost:8000/sse",
      "transport": "sse"
    }
  }
}

使用示例

1. 查看可用工具列表

await list_all_tools()

2. 查看可用资源列表

await list_all_resources()

3. 天气查询示例

await get_weather_example()

4. 读取产品知识库示例

await read_product_knowledge_example()

5. 使用Agent进行对话

response = await run_mcp_client("北京天气")
print(response)

主要组件

  • MultiServerMCPClient: 多服务器MCP客户端类
  • initialize_client(): 客户端初始化函数
  • run_mcp_client(): ReAct Agent运行函数
  • 各种工具和资源访问示例函数

注意事项

  • 确保所有MCP服务器都已正确配置并运行
  • 环境变量中的API密钥需要妥善保管
  • 异步函数调用需要使用asyncio运行

About

一个基于LangChain和MCP (Model Control Protocol) 的客户端示例,展示了如何使用LangChain与多个MCP服务进行交互,实现智能对话和工具调用功能。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages