使用说明
带有LangGraph代理的简单MCP客户端
使用带有Express.js和TypeScript的LangGraph代理的多服务器MCP(模型上下文协议)客户端的轻量级实现。
特性
- 使用TypeScript的Express.js服务器
- 用于处理对话的LangGraph代理
- 用于工具集成的MCP客户端
- 流媒体响应
设置
- 克隆存储库
- 安装依赖项:
npm install - 创建一个
.env包含以下变量的文件:PORT=3000 NODE_ENV=development CORS_ORIGIN=* ANTHROPIC_API_KEY=your_anthropic_api_key - 构建并启动服务器:
npm run buildnpm start
API终点
初始化聊天
GET /api/chat/init
生成聊天
POST /api/chat
请求正文:
{
"input": "Hello, how can you help me?",
"characterId": "character_123",
"llmConfig": {
"model": "claude-3-opus-20240229",
"verbose": true
},
"chatHistory": [],
"mcpServers": [
{
"name": "weather-service",
"version": "1.0",
"url": "https://example-mcp-server.com",
"key": "optional_api_key"
}
]
}
关闭聊天
GET /api/chat/close
MCP集成
服务器连接到请求中指定的MCP服务器,并检索LangGraph代理可以使用的可用工具。
发展
使用自动重新加载运行开发服务器:
npm run dev
示例用法
// Send a message and stream the response
const response = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
input: "What's the weather in New York?",
characterId: "agent1",
chatHistory: [],
mcpServers: [
{
name: "weather-service",
version: "1.0",
url: "https://my-mcp-server.com",
},
],
}),
});
// Read the streaming response
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const text = new TextDecoder().decode(value);
const events = text.split("\n\n").filter(Boolean);
for (const event of events) {
const data = JSON.parse(event);
console.log(data);
}
}
