Claude的MCP客户端
此存储库包含一个与Claude AI模型配合使用的模型上下文协议(MCP)客户端的TypeScript实现。它允许您连接到任何兼容MCP的服务器,并使用Claude与该服务器提供的工具进行交互。
什么是MCP?
模型上下文协议(MCP)是一个开放标准,使AI模型能够使用工具并通过标准化接口访问外部数据。了解更多信息,请访问 模型上下文协议.io.
先决条件
- Node.js(v16或更高版本)
- npm或纱线
- Claude的人类学API密钥
安装
- 克隆此存储库:
git clone https://github.com/yourusername/mcp-client-typescript.git
cd mcp-client-typescript- 安装依赖项:
npm install- 创建一个
.env根目录中带有Anthropic API密钥的文件:
ANTHROPIC_API_KEY=your_api_key_here建设客户
构建TypeScript代码:
npm run build这将把TypeScript代码编译为JavaScript build 目录,并使输出文件可执行。
运行客户端
要运行客户端,您需要提供MCP服务器脚本的路径:
node build/index.js path/to/server/script.js对于Python服务器:
node build/index.js path/to/server/script.py天气服务器示例
如果您在子目录中有天气服务器示例:
# First build the weather server
cd weather
npm run build
cd ..
# Then run the client with the weather server
node build/index.js ./weather/build/index.js运作原理
- 客户端使用MCP SDK连接到指定的MCP服务器
- 它从服务器检索可用工具的列表
- 当您输入查询时,它会将查询连同工具定义一起发送给Claude
- 如果克劳德决定使用工具,客户:
- 调用MCP服务器上的工具 - 将结果发送回Claude - 返回克劳德的最终回复
项目结构
index.ts:主要客户实施tsconfig.json:TypeScript配置package.json:项目依赖关系和脚本
可用命令
npm run build:编译TypeScript代码并使输出可执行
